SDL:两个事件循环?
看看这里的这段代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
keyEvent
这个名称具有误导性。SDL_WaitEvent
将等待任何类型的事件,包括 QUIT。The name
keyEvent
is misleading.SDL_WaitEvent
will wait for any sort of event, including QUIT.最小的更改:
您可以在设置 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;
afterQUIT = true;
.Better changes:
Refactor your code similar to many examples available on the web (at sourceforge, or at molly rocket, or just google it).