使用 SDL/C 处理事件的最佳方法是什么?
我正在使用 SDL 作为我的游戏项目的视图部分。我想在不中断主线程的情况下处理按键事件。因此,我决定在另一个视图线程中运行无限循环来捕获任何事件并通知主线程。但是,我不确定这是否是最好的,因为这可能会导致工作负载并降低系统性能?有没有更好的方法来做这种事情? 谢谢。
I am using SDL for the view parts of my game project. And I want to handle key press events without interrupting the main thread. So I decided to run an infinite loop in another view thread to catch any events and inform the main thread. However, I am not sure that this is the best since this may cause a workload and decrease the system performace? Is there any better way to do this kind of things?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要打扰另一个线程。有什么意义?
你的主线程做什么?我想象这样的事情:
如果您在更新周期之后(或期间)收到输入,那么您必须等到下一个更新周期才能看到效果。渲染时也是如此。您不妨在更新周期之前检查输入并全部以单线程方式完成。
多线程在这里没有任何好处,只会增加复杂性。
如需更多阅读,请查看Christer Ericson 关于输入延迟的博客文章(他是技术总监)制作《战神》的团队)。
Don't bother with another thread. What's the point?
What does your main thread do? I imagine something like this:
If you receive input after (or during) the update cycle then you have to wait till the next update cycle before you'll see the effects. The same is true during rendering. You might as well just check for input before the update cycle and do it all singlethreaded.
Multithreading gains nothing here and just increases complexity.
For some added reading, check out Christer Ericson's blog post about input latency (he's the director of technology for the team that makes God of War).
SDL 本质上并不是一个中断或事件驱动的框架。通过调用
SDL_WaitEvent
或SDL_PollEvent
从事件队列中读取事件来发生 IO。这必须发生在“主”线程中,即调用SDL_SetVideoMode
的线程中。这并不是说您不能使用多个线程,并且这样做有充分的理由,例如,如果不必依赖 SDL 事件循环,它可以简化网络通信。如果您希望模拟在单独的线程中进行,那么它可以通过同步共享对象来回传递信息。特别是,您始终可以从任何线程安全地将事件放入 SDL 事件队列中。
SDL is not inherently an interrupt or event driven framework. IO occurs by reading events off of the event queue by calling
SDL_WaitEvent
orSDL_PollEvent
. This must occur in the "main" thread, the one that calledSDL_SetVideoMode
.That's not to say you cannot use multiple threads, and there's good justification for doing so, for instance, it can simplify network communication if it doesn't have to rely on the SDL event loop. If you want the simulation to occur in a separate thread, then it can pass information back and forth through synchronized shared objects. In particular, you can always put events into the SDL event queue safely from any thread.