1
\$\begingroup\$

I'm using Glut with OpenGL for drawing. Glut is very simple and straightforward but I have to give up Glut and move on toward SDL2. I'm reading SDL for Game Development, good book. I need to speed up a bit, so I'm looking for a list of comparisons between Glut and SDL2. At least with common similarities between the two. For example, in Glut we need for drawing Display() to be passed to glutDisplayFunc(). My understanding so far for SDL2 is that SDL_Renderer is like Display() in Glut. Is this correct? Also, what do exactly SDL_Surface and SDL_RenderPresent mean?

\$\endgroup\$
2
\$\begingroup\$

I made the same transition recently. I'm not sure if what I'm doing is "SDL Best Practices" but it does work. It doesn't use and SDL_Renderer or SDL_Surface...

Here's a segment of my code. Much of it is pasted from forgotten internet pages. I think I left in everything relevant, after snipping out some of my app-specific goop. I hope it helps. (And feedback from anyone who knows better is definitely welcome here!)

As for the actual question, a comparison of glut to sdl, for me it was just:

GLUT:

  • install callbacks for events like draw, keyboard mouse
  • 1 special window by default

SDL2:

  • your own main event loop, and switch on event type yourself
  • explicit window creation & control

Example code:

SDL_Window *mainwindow; /* Our window handle */
SDL_GLContext maincontext; /* Our opengl context handle */

if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
    sdldie("Unable to initialize SDL"); /* Or die on error */

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

// next line as per: http://stackoverflow.com/questions/11961116/opengl-3-x-context-creation-using-sdl2-on-osx-macbook-air-2012
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

/* Turn on double buffering with a 24bit Z buffer.
 * You may need to change this to 16 or 32 for your system */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);

/* Create our window centered */
mainwindow = SDL_CreateWindow("The Window Name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              1600, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

maincontext = SDL_GL_CreateContext(mainwindow);

/* This makes our buffer swap syncronized with the monitor's vertical refresh */
SDL_GL_SetSwapInterval(1);

/* Clear our buffer with a red background */
glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );

/* Swap our back buffer to the front */
SDL_GL_SwapWindow(mainwindow);

SDL_Delay(200);

while (!quit)
{
  draw(); // does glDrawXxx stuff here.
  SDL_GL_SwapWindow(mainwindow);

  while (SDL_PollEvent(&e)){
    switch(e.type)
    {
      case SDL_QUIT:
        quit = true;
        break;

        case SDL_KEYDOWN:
         /* et cetera */
          break;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.