轮询后 SDL_Event.type 始终为空
我有一个通用函数,应该处理 SDL 事件队列中的任何事件。到目前为止,该函数如下所示:
int eventhandler(void* args){
cout << "Eventhandler started.\n";
while (!quit){
while (SDL_PollEvent(&event)){
cout << "Got event to handle: " << event.type << "\n";
switch (event.type){
SDL_KEYDOWN:
keyDownHandler(event.key.keysym.sym);
break;
default:
break;
}
}
}
}
但是,当我测试该函数时,我收到了一大堆事件,但它们似乎都没有类型。它甚至不打印 0 或任何东西——什么也不打印。按任意键时的输出如下所示:
Got event to handle:
没有其他内容。任何教程和 SDL 文档都说我应该处理这样的事件,但它不起作用。还有其他人有这个问题或解决方案吗?
顺便说一句,事件处理程序在 SDL_Thread 中运行,但我不认为这是问题所在。
I have a general function that is supposed to handle any event in the SDL event queue. So far, the function looks like this:
int eventhandler(void* args){
cout << "Eventhandler started.\n";
while (!quit){
while (SDL_PollEvent(&event)){
cout << "Got event to handle: " << event.type << "\n";
switch (event.type){
SDL_KEYDOWN:
keyDownHandler(event.key.keysym.sym);
break;
default:
break;
}
}
}
}
However, when I test the function, I get a whole bunch of events but none of them seem to have a type. It doesn't even print 0 or anything — just nothing. The output when pressing any key looks like this:
Got event to handle:
And nothing else. Any tutorial and the SDL docs say that I should handle events like this, but it isn't working. Anybody else have this problem or a solution?
By the way, the eventhandler runs in an SDL_Thread, but I don't think that's the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么也没发生是由于
SDL_KEYDOWN
前面缺少case
造成的。如果缺少
case
,编译器会看到一个跳转标签,您可以将其用于例如goto SDL_KEYDOWN;
,这会导致default
标签成为唯一的标签在switch
语句中。我不明白为什么
event.type
没有得到输出,除非您在某处设置一些流标志。event.type
是一个Uint8
,SDL 只是从整型类型进行类型定义,因此应该像处理它一样。与任何整数类型一样,它也不能是“空”,但它的输出可以是。That nothing happens is a result of the missing
case
in front ofSDL_KEYDOWN
.With
case
missing the compiler sees a jump label which you would use for e.g.goto SDL_KEYDOWN;
, which results in thedefault
label being the only label in theswitch
statement.I don't see why
event.type
doesn't get output though unless you set some stream-flags somewhere.event.type
is anUint8
which SDL just typedefs from integral types, so it should be handled like one. Like any integral type it also can't be "empty", but the output for it can be.