SDL_PollEvent 与 SDL_WaitEvent [英] SDL_PollEvent vs SDL_WaitEvent

查看:60
本文介绍了SDL_PollEvent 与 SDL_WaitEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在阅读这篇文章,其中包含SDL 中多线程编程的提示和建议" - https://vilimpoc.org/research/portmonitorg/sdl-tips-and-tricks.html

So I was reading this article which contains 'Tips and Advice for Multithreaded Programming in SDL' - https://vilimpoc.org/research/portmonitorg/sdl-tips-and-tricks.html

它谈到 SDL_PollEvent 效率低下,因为它会导致 CPU 使用率过高,因此建议改用 SDL_WaitEvent.

It talks about SDL_PollEvent being inefficient as it can cause excessive CPU usage and so recommends using SDL_WaitEvent instead.

它显示了两个循环的示例,但我看不出这将如何与游戏循环配合使用.SDL_WaitEvent 是否应该仅用于不需要不断更新的事物,即如果您有一个游戏正在运行,您将在每一帧执行游戏逻辑.

It shows an example of both loops but I can't see how this would work with a game loop. Is it the case that SDL_WaitEvent should only be used by things which don't require constant updates ie if you had a game running you would perform game logic each frame.

我认为它唯一可以用于绘制程序之类的程序,其中只需要对用户输入进行操作.

The only things I can think it could be used for are programs like a paint program where there is only action required on user input.

我认为我应该继续使用 SDL_PollEvent 进行通用游戏编程是否正确?

Am I correct in thinking I should continue to use SDL_PollEvent for generic game programming?

推荐答案

如果您的游戏仅根据用户输入更新/重绘,那么您可以使用 SDL_WaitEvent.但是,即使没有用户输入,大多数游戏也会进行动画/物理.所以我认为 SDL_PollEvent 最适合大多数游戏.

If your game only updates/repaints on user input, then you could use SDL_WaitEvent. However, most games have animation/physics going on even when there is no user input. So I think SDL_PollEvent would be best for most games.

SDL_WaitEvent 可能有用的一种情况是,如果您将它放在一个线程中,而将动画/逻辑放在另一个线程中.这样即使 SDL_WaitEvent 等待很长时间,您的游戏也会继续绘制/更新.(这可能实际上不起作用.请参阅下面 Henrik 的评论)

One case in which SDL_WaitEvent might be useful is if you have it in one thread and your animation/logic on another thread. That way even if SDL_WaitEvent waits for a long time, your game will continue painting/updating. ( This may not actually work. See Henrik's comment below)

对于如文章所述使用 100% CPU 的 SDL_PollEvent,当您检测到您的游戏运行速度超过所需的每秒帧数时,您可以通过在循环中添加睡眠来缓解这种情况.

As for SDL_PollEvent using 100% CPU as the article indicated, you could mitigate that by adding a sleep in your loop when you detect that your game is running more than the required frames-per-second.

这篇关于SDL_PollEvent 与 SDL_WaitEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆