【发布时间】:2021-05-17 08:34:43
【问题描述】:
我正在开发一个简单的 Win 游戏。这是我的 2 个类似的游戏循环实现(在 C++ 中):
方法#1:
while (Msg.message != WM_QUIT) {
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else {
// Do update, rendering and all the real game loop stuff
}
}
方法#2:
while (Msg.message != WM_QUIT) {
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
// Do update, rendering and all the real game loop stuff
}}
我的问题是哪个更好,我应该选择哪个?是像#1 方法那样在 else 闭包中进行更新和渲染内容,还是像 #2 方法那样仅在 while 循环块中进行更新和渲染?
【问题讨论】:
-
读取消息的目的是什么 - 处理消息,直到它存在于队列中
-
第二个循环总是会增加延迟。消息到达的频率通常(远)高于呈现频率。虽然您可能会看到硬件光标移动,但它只是在稍后的时间,您的代码会观察输入。这会带来相当滞后的体验。
标签: c++ windows winapi visual-c++