SDL:两个事件循环?

发布于 2024-10-08 10:51:48 字数 609 浏览 0 评论 0原文

看看这里的这段代码:

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
    }
   }
  }
 }
}

我试图找出如何让 SDL_QUIT 在我们等待按键时工作。有没有办法做到这一点,或者你们有更好的主意吗?

我是个新手,所以请具体一点。 :D

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 better idea?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-10-15 10:51:48

keyEvent 这个名称具有误导性。 SDL_WaitEvent 将等待任何类型的事件,包括 QUIT。

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

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. */
}
凤舞天涯 2024-10-15 10:51:48

最小的更改:

您可以在设置 QUIT 的内部 while 循环之后添加 if (QUIT) break;

或者,您可以将外部 while 循环移至单独的函数,并在 QUIT = true; 之后添加 return;

更好的改变:

重构你的代码,类似于网络上的许多可用示例(在sourceforge,或在莫莉火箭,或者只是谷歌它)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文