游戏主循环逻辑

发布于 2024-12-11 05:04:11 字数 167 浏览 0 评论 0原文

我正在使用 allegro 5 用 c++ 编写一个游戏。Allegro 5 的事件堆叠在事件队列中(例如鼠标单击或计时器在 1/FSP 时间后滴答)。所以我的问题是我的游戏的主循环的逻辑应该如何,或者因为它是基于事件的我可以在没有主循环的情况下实现它?

有什么想法真正的游戏是如何做到的吗?链接会很好。

I'm writing a game in c++ using allegro 5. Allegro 5 has events which are stacked in an event queue(like mouse clicked or timer ticked after 1/FSP time). So my question is how should be the logic of the main loop of my game, or since it's event based I can implement it without the main loop??

Any ideas how real games do it? Links will be good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陪你到最终 2024-12-18 05:04:11

我没有使用 Allegro 的经验,但是当使用 SFML 并使用 OpenGL 渲染游戏时,我自己会轮询事件队列作为主循环的一部分。类似于下面的伪代码(但更抽象):

while(game_on)
{
   auto events = poll_occured_events();
   for_each(events, do_somewithng_with_event);

   render_game();
}

到目前为止似乎工作正常......我猜想在 Allegro 中也可能有类似的东西。事件驱动的游戏很棘手,因为您需要不断更新游戏。

您可以(可能)将主循环放在另一个线程中,然后在事件线程和游戏线程之间进行同步......

I have no experience with Allegro, but when using SFML and rendering a game with OpenGL, I myself poll the event queue as part of my main loop. Something like the below pseudo-code (but more abstracted):

while(game_on)
{
   auto events = poll_occured_events();
   for_each(events, do_somewithng_with_event);

   render_game();
}

Seems to work fine so far... I'd guess something similar is possible in Allegro. Event driven games are tricky, since you need to continually update the game.

You could (possibly) have the main loop in another thread and then synchronize between event thread and game thread...

终止放荡 2024-12-18 05:04:11

我也没有任何使用 Allegro 的经验,但逻辑是一样的。

(所谓的)真实游戏也有游戏循环,但不同之处在于它们使用并行工作但在不同时间间隔内的线程。例如,有不同的线程用于物理计算、AI、游戏玩法、声音、渲染……因为用户事件通常关注游戏事件在它之前被收集(正如 Max 所建议的)并消耗到下一帧(实际上有些在下一帧中收集它)例如 5 帧)。

由于帧可能会变得太长,因此游戏会收集来自操作系统的所有事件,因此这些输入称为缓冲输入。还有另一种方法,称为无缓冲输入,它不能离散工作,而是在游戏循环期间在查询它的实例中对其进行测试。

如果用户输入非常重要并且您根本不想丢失任何输入,那么您可以使用缓冲的,否则不使用缓冲的。然而,无缓冲可能会很棘手,尤其是在调试期间。

这里有一些链接
书籍摘录游戏引擎
IOS 上的游戏循环

I don't have any experiences with Allegro too but logic would be the same.

(so called) Real games also have game loops but the diference is they use threads which are working parallel but within different time intervals. For instance there are different threads for physic calculations, AI, gameplay, sound, rendering... as user events are usually concers gameplay events are getting collected before it (as Max suggests) and consumed until the next frame (actually some collects it in for instance 5 frames).

As a frame might get too long, all the events coming from OS are getting collected by the game for that reason these inputs are called buffered input. There is also one another method which is called unbuffered input which doesn't work discrete but instead you test it during gameloop at the very instances it is queried.

If the user input is very important and you dont want to loose any inputs at all then you can use buffered otherwise unbuffered. However unbuffered might be tricky especially during debug.

here are some links
book excerpt game engine
Game Loops on IOS

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