[SDL] resizing SDL opengl window

Benjamin Deutsch ben at fictiongroup.de
Tue Jan 25 10:30:15 PST 2005


Hi,

two comments on your code (and questions to the SDL list in general):

> 	    switch (event.type) {
> 	      case SDL_VIDEORESIZE:
> 		screen = SDL_SetVideoMode(event.resize.w,
> 			event.resize.h, 16, SDL_OPENGL | SDL_RESIZABLE);
> 		reshape(); 

I've never tinkered with resizable windows, but is SDL_SetVideoMode 
really necessary? From what I've read on this list occasionally, this 
will destroy and recreate your OpenGL context, causing you to, for 
example, lose all your textures.

If your application is pure OpenGL (no on-screen SDL blitting), 
shouldn't it be enough to call glViewport?

> 	  //handle keys 
> 	  keys = SDL_GetKeyState(0);
> 	  if (keys[SDLK_ESCAPE]) { 
> 	    done = true;
> 	  } else { 
> 	    if (keys[SDLK_SPACE])  leftFirst = !leftFirst;

I'm not sure how efficient this last part is. You're already handling 
SDL events in your event loop, why not catch keypresses there? This way, 
you can also catch keys that are pressed and released between tests 
(which you would currently miss if they were really, really fast). And 
I'm not sure how much work goes into SDL_GetKeyState...

Also, the way you've set it up now, you are doing "busy waiting", which 
is generally a big no-no:
   while (!done) {
     if (key_pressed()) done = 1;
   }
This will just keep cycling at close to 100% CPU, performing the test 
way faster than a human can change the key state.

Here's my suggestion (in pseudo-code):

while (!done) {
   while (events) {
     handle resize:
       reshape
     handle keypress:
       if (space) swap_triangle()
       else if (escape) done = 1
   }
   redraw()
   if (time_passed < 10 ms) sleep (10 ms - time_passed)
}

This handles events as they appear, redraws in the main thread, and 
sleeps if there's nothing else to do.

Bye,
   Benjamin




More information about the SDL mailing list