Help with SDL event polling

Started by
8 comments, last by King of Men 18 years ago
I am at a very early stage of teaching myself SDL. I've gotten as far as setting up a window. I started with just a blank window that would close as soon as you pressed a key, thus :

  SDL_Surface *screen = NULL;
  if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return 1;
  screen = SDL_SetVideoMode(680, 420, 32, SDL_OPENGL);
  if (screen == NULL) return 1;

  SDL_Event event;
  while (true) {
    if (SDL_PollEvent(&event)) {
      if (event.type == SDL_KEYDOWN) break;
    }
  }
  SDL_Quit();
  return 0;
which worked nicely. You start the program, you get a window, you press a button, it closes. Splendid! Next I thought I'd draw a square in my window. So I included OpenGL, and added a draw-square part to my loop, thusly :

  // SDL setup as before

  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  glViewport(0, 0, 640, 480);
  glColor3f(1.0, 1.0, 1.0);
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);


  SDL_Event event;
  while (true) {
    if (SDL_PollEvent(&event)) {
      if (event.type == SDL_KEYDOWN) break;
    }
   
    glBegin(GL_POLYGON); {
      glVertex2f(-0.5, -0.5);
      glVertex2f(-0.5, 0.5);
      glVertex2f(0.5, 0.5);
      glVertex2f(0.5, -0.5);
    } glEnd();
  }

  SDL_Quit();
  return 0;
The problem is that this code hangs completely! (It doesn't draw any squares, either, but first things first.) I can't even Alt-Tab out of the window; I have to log on from another computer and kill the process. Now, it seems to me that since I'm polling every turn of the loop, my program is catching that Alt-Tab before it can get to the OS. But what I can't figure out is why it doesn't exit the while loop. Now, I've tested this and I'm fairly sure it's the OpenGL code that's the problem. If I comment out the glBegin to glEnd, the program works as before, even with OpenGL setup in place. Does anyone have a clue to what I'm doing wrong? I'm using SDL 1.2.9 and gcc 3.2.3.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Advertisement
For one thing, you aren't calling SDL_GL_SwapBuffers, so the black window makes sense. I'm not sure it'll help much though, because OpenGL isn't quite setup in your program (there is no viewport, for example).

Hope this helps.
Quote:Original post by Anonymous Poster
(there is no viewport, for example).


Whoops, there is. Sorry. ;-)
Hmm, ok, that makes sense for the black window - I'd rate you up if I knew who you were. :) It doesn't seem to explain the freezing, though. (I don't know if I dare test this very much, I've already had to borrow my coworker's machine twice to log in to mine...) I'm a bit reluctant to do anything unless there is a good chance of avoiding the freeze. :)
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Well, ok, I commented out the event polling and added the swapbuffer, and now I do get a nice white rectangle in the middle of my window, even if I do have to use non-elegant means to kill the program. So, does anyone have an idea on what is wrong with the event poll?
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
I'm not exactly sure, but shouldn't you also "| SDL_SWSURFACE" or "| SDL_HWSURFACE" in "SetVideoMode"?

I'm no expert. And I have never initialized the window WITHOUT one of those two. And I doubt that it's the problem. But I gave it a shot. LoL. =) Also, I noticed that the window dimensions (680, 420) specified in "SetVideoMode" are different from those in the call to "glViewport". I seriously doubt that's it, but I tried. LoL.

Sorry if this doesn't help, but I figured I could at least give it a shot. Good luck. =)
:==-_ Why don't the voices just leave me alone?! _-==:
Okay, nevermind. I guess I was too late. =P
:==-_ Why don't the voices just leave me alone?! _-==:
Quote:Original post by King of Men
Well, ok, I commented out the event polling and added the swapbuffer, and now I do get a nice white rectangle in the middle of my window, even if I do have to use non-elegant means to kill the program. So, does anyone have an idea on what is wrong with the event poll?


Could you try adding a delay in your loop and see if that helps? I seem to recall experiencing the same problem a while ago. It went away when the rendering became "heavier". I don't know why it happens though. :-(


Hope this helps.
Usually you want to process the entire event queue per frame.
..
while( SDL_PollEvent( &event )){
if (event.type == SDL_KEYDOWN) break;
}
..
Good Luck
0xa0000000
Ah, that was it - process the entire event queue. Thank you. :)
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

This topic is closed to new replies.

Advertisement