开发者

SDL: Two Event Loops?

Take a look at this piece of code here:

void game::startLoop()
{
 while(QUIT == false)
 {
  getRoomUpdate();
  applySurface(-15, 280, zombie_lefthand, buffer);
  applySurface(455, 280, zombie_righthand, buffer);

  SDL_Flip(buffer);

  while(SDL_PollEvent(&gameEvent))
  {
   if(gameEvent.type == SDL_QUIT)
   {
    QUIT = true;
   }
  }

  while(SDL_WaitEvent(&keyEvent))
  {
   switch(keyEvent.type)
   {
    case SDL_KEYDOWN:
    switch(keyEvent.key.keysym.sym)
    {
     //blahkeypress
    }
   }
  }
 }
}

I'm trying to figure out how to allow SDL_QUIT to work while we're waiting for a keypress. Is there a way to do this or do you guys have a b开发者_开发百科etter idea?

I'm a bit of a newbie so please be specific. :D


The name keyEvent is misleading. SDL_WaitEvent will wait for any sort of event, including QUIT.

SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
    case SDL_QUIT:
        quit = true;
        break;
    /* cases for keyboard events, etc. */
}


Minimal changes:

You could add if (QUIT) break; after the inner while loop that sets QUIT.

Or, you could move the outer while loop to a separate function and add a return; after QUIT = true;.

Better changes:

Refactor your code similar to many examples available on the web (at sourceforge, or at molly rocket, or just google it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜