我围绕一个称为 GLWindow 的类封装了 Windows API 的 CreateWindow,我使用 OpenGL 向其渲染图像。
我花了一些时间,我什至封装了消息队列,所以基本上我的 main
函数看起来是这样的:
int main()
{
GLWindow win;
win.create("OpenGL Window", false, 1600, 800, -1, -1, true);
win.hideCursor();
win.moveMouseToCenter();
init(); //initialize some opengl stuff
bool quit = false;
WindowEvent ev;
while (!quit) {
if (win.pollEvent(ev))
{
if (ev.type == ev.WindowClosed || (ev.type == ev.KeyboardKeyPressed && ev.keyboardKey.key == Keyboard::Key::Escape))
quit = true;
else
handleEvent(ev);
}
updateCamera(&win);
render(&win);
}
return 0;
}
render
函数显然花费了最多的时间,它导致窗口过程函数被消息淹没,最终在 WM_KEYDOWN
和 WM_MOUSEMOVE
消息,因此相机更新也被延迟。 (在 updateCamera()
内)
知道我能做什么吗?
最佳答案
问题是,您的代码在渲染帧之间处理单个消息。相反,它应该耗尽整个消息队列。这就像将消息轮询表达式的 if
替换为 while
一样简单。如果遇到终止条件,立即break
可能也是一个好主意。
while (!quit) {
while (win.pollEvent(ev))
{
if (ev.type == ev.WindowClosed || (ev.type == ev.KeyboardKeyPressed && ev.keyboardKey.key == Keyboard::Key::Escape))
{
quit = true;
break;
}
else
handleEvent(ev);
}
updateCamera(&win);
render(&win);
}
关于c++ - 使用 OpenGL 渲染时 Win32 消息队列被淹没,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249319/