Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:39:06+00:00 2026-05-17T15:39:06+00:00

I’m trying to compile this example and play around with it a bit. I’ve

  • 0

I’m trying to compile this example and play around with it a bit. I’ve already corrected the main error the people were having with this example where they would call sdl_gl_setattribute before SDL_Init was called but I’m still getting a segfault right after the first SDL_GL_SetAttribute call. I’ve ran sdl with opengl apps before on my computer and I’m certain it’s been working with my video card.

From this code does anybody know why it would segfault? Or, does this code work on anybody else’s computer? If it makes any difference I’m on ubuntu 10.04 using freeglut3 for opengl stuff.

//compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut

#include <stdio.h>
#include "SDL.h"
#include <GL/gl.h>

int event_thread(void* nothing);

int main(int argc, char** argv) {
    float theta = 0.0f;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);

    //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    //go through and get the values to see if everything was set
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));

    //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
    SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
    if(screen == NULL) {
        printf("video error: %s\n", SDL_GetError());
    }

    //print video card memory
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory (in megabytes): %d\n", info->video_mem);

    //set opengl params for drawing in 3d space
    glViewport(0, 0, 600, 300);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glMatrixMode(GL_MODELVIEW);

    //start up the event thread
    int done = 0;
    SDL_Thread* evt_thrd;
    evt_thrd = SDL_CreateThread(event_thread, (void*)&done);

    for(;!done;) {
        //clear and move to 0, 0, 0
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glTranslatef(0.0f, 0.0f, 0.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);

        //draw the triangle
        glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(0.0f, 1.0f);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex2f(0.87f, -0.5f);
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex2f(-0.87f, -0.5f);
        glEnd();

        theta += 0.5f;
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

int event_thread(void* nothing) {
    int* done = (int*)nothing;
    SDL_Event event;

    while(1) {
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT || 
            (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                *done = 1;
                return 0;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:39:06+00:00Added an answer on May 17, 2026 at 3:39 pm

    Try this version:

    //compile with cc triangle.c -o triangle `sdl-config --libs --cflags` -lglut
    
    #include <stdio.h>
    #include "SDL.h"
    #include "SDL_opengl.h"
    
    int event_thread(void* nothing);
    
    int main(int argc, char** argv) {
        float theta = 0.0f;
    
        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
    
        //first set buffer stuff, then doublebuf (if wanted), then SDL_SetVideoMode()
        if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
        if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
        if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
        if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
        if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }
    
        //pass sdl_resizable if it's an opengl application that is windowed and not fullscreened
        SDL_Surface* screen = SDL_SetVideoMode(600, 300, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_RESIZABLE);
        if(screen == NULL) {
            printf("video error: %s\n", SDL_GetError());
        }
    
        //go through and get the values to see if everything was set
        int red, green, blue, doublebuf;
        SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
        SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
        SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
        SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
        printf("red size, green size, blue size: <%d, %d, %d>\ndouble buffered? %s\n", red, green, blue, (doublebuf == 1 ? "yes" : "no"));
    
        //print video card memory
        const SDL_VideoInfo* info = SDL_GetVideoInfo();
        printf("video card memory (in megabytes): %d\n", info->video_mem);
    
        //set opengl params for drawing in 3d space
        glViewport(0, 0, 600, 300);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClearDepth(1.0);
        glDepthFunc(GL_LESS);
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_SMOOTH);
        glMatrixMode(GL_PROJECTION);
        glMatrixMode(GL_MODELVIEW);
    
        //start up the event thread
        int done = 0;
        SDL_Thread* evt_thrd;
        evt_thrd = SDL_CreateThread(event_thread, (void*)&done);
    
        for(;!done;) {
            //clear and move to 0, 0, 0
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glTranslatef(0.0f, 0.0f, 0.0f);
            glRotatef(theta, 0.0f, 0.0f, 1.0f);
    
            //draw the triangle
            glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2f(0.0f, 1.0f);
            glColor3f(0.0f, 1.0f, 0.0f);
            glVertex2f(0.87f, -0.5f);
            glColor3f(0.0f, 0.0f, 1.0f);
            glVertex2f(-0.87f, -0.5f);
            glEnd();
    
            theta += 0.5f;
            SDL_GL_SwapBuffers();
        }
    
        SDL_Quit();
        return 0;
    }
    
    int event_thread(void* nothing) {
        int* done = (int*)nothing;
        SDL_Event event;
    
        while(1) {
            while(SDL_PollEvent(&event)) {
                if(event.type == SDL_QUIT || 
                (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
                    *done = 1;
                    return 0;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.