使用 SDL/C 处理事件的最佳方法是什么?

发布于 2024-10-01 19:22:48 字数 140 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

单身狗的梦 2024-10-08 19:22:48

不要打扰另一个线程。有什么意义?

你的主线程做什么?我想象这样的事情:

  1. Update Logic
  2. Render
  3. Goto 1

如果您在更新周期之后(或期间)收到输入,那么您必须等到下一个更新周期才能看到效果。渲染时也是如此。您不妨在更新周期之前检查输入并全部以单线程方式完成。

  1. 输入
  2. 更新逻辑
  3. 渲染
  4. 转到 1

多线程在这里没有任何好处,只会增加复杂性。

如需更多阅读,请查看Christer Ericson 关于输入延迟的博客文章(他是技术总监)制作《战神》的团队)。

Don't bother with another thread. What's the point?

What does your main thread do? I imagine something like this:

  1. Update Logic
  2. Render
  3. Goto 1

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.

  1. Input
  2. Update Logic
  3. Render
  4. Goto 1

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

软的没边 2024-10-08 19:22:48

我想在不中断主线程的情况下处理按键事件。

SDL 本质上并不是一个中断或事件驱动的框架。通过调用 SDL_WaitEventSDL_PollEvent 从事件队列中读取事件来发生 IO。这必须发生在“”线程中,即调用 SDL_SetVideoMode 的线程中。

这并不是说您不能使用多个线程,并且这样做有充分的理由,例如,如果不必依赖 SDL 事件循环,它可以简化网络通信。如果您希望模拟在单独的线程中进行,那么它可以通过同步共享对象来回传递信息。特别是,您始终可以从任何线程安全地将事件放入 SDL 事件队列中。

And I want to handle key press events without interrupting the main thread.

SDL is not inherently an interrupt or event driven framework. IO occurs by reading events off of the event queue by calling SDL_WaitEvent or SDL_PollEvent. This must occur in the "main" thread, the one that called SDL_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.

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