为什么我的计时器停止计时?
我正在创建一个绘图应用程序,当它获得 WM_SCROLL 或 WM_MOUSEMOVE 时,它会渲染 OpenGL。问题是,有很多鼠标移动,我只需要它每秒渲染最多 60 帧。所以我在我的引擎类中创建了一个名为 CanRender 的布尔值。所以在我的 render() 过程中我这样做: if(!CanRender) { 返回; } 可以渲染=假;
基本上它会阻止它渲染超过 60 FPS。
我在 WM_CREATE 中创建计时器。
当我得到 WM_TIMER 时,我将 CanRender 设置为 true。
我让它发出蜂鸣声,这样我就知道计时器正在运行。 一旦我开始滚动或移动鼠标,蜂鸣声就会停止,并且我不再看到渲染。为什么它会停止我的计时器?此外,当我最小化计时器再次启动然后重新最大化时,它会再次停止。
感谢
留言泵:
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
创作:
case WM_CREATE:
//Set Window Title
SetWindowText(hWnd,engineGL.current.caption.c_str());
SetTimer(hWnd, // handle to main window
120, // timer identifier
17, // 60 fps interval
(TIMERPROC) NULL); // no timer callback
I'm creating a drawing application that renders OpenGL when it gets a WM_SCROLL or WM_MOUSEMOVE. The thing is that there are a lot of mouse moves and I only need it to render a maximum of 60 frames per second. So I created a bool in my engine class called CanRender. so in my render() proc I do:
if(!CanRender)
{
return;
}
CanRender = false;
Basically it prevents it from rendering more than 60 FPS.
I create the timer in WM_CREATE.
when I get a WM_TIMER I set CanRender to true.
I made it beep so I know the timer is running.
As soon as I start scroling or moving the mouse, the beeping stops and I no longer see rendering. Why would it stop my timer? Also when I minimize the timer starts again then remaximize, it stops again.
Thanks
Message Pump:
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
creation:
case WM_CREATE:
//Set Window Title
SetWindowText(hWnd,engineGL.current.caption.c_str());
SetTimer(hWnd, // handle to main window
120, // timer identifier
17, // 60 fps interval
(TIMERPROC) NULL); // no timer callback
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么搞得这么复杂?
Windows应用程序中的绘图通常仅在WM_PAINT消息中完成并由RedrawWindow函数触发。您可以在 WM_SCROLL 和 WM_MOUSEMOVE 中调用 RedrawWindow。如果您的应用程序无法跟上绘图的速度,对 RedrawWindow(WM_PAINT 消息)的多次调用将会崩溃。
此外,如果您将 OpenGL 设置为与显示器垂直回扫同步,您将不会超过特定的刷新率。
至于你的问题...我猜有很多 WM_SCROLL 和 WM_MOUSEMOVE 消息。这些不能被折叠。因此,如果您在其中进行绘图(这需要时间),则会阻塞消息队列并且无法处理 WM_TIMER 消息。因此,您不会听到蜂鸣声。
Why making it so complicated?
Drawing in windows application is usually done only in WM_PAINT message and triggered by RedrawWindow function. You can call RedrawWindow within WM_SCROLL and WM_MOUSEMOVE. Multiple calls to RedrawWindow (WM_PAINT messages) will be collapsed if your application can't keep up with drawing.
Also if you set OpenGL to synchronize with your monitors vertical retrace you will not exceed certain refresh rate.
As to your question... I guess there're many WM_SCROLL and WM_MOUSEMOVE messages. And those cannot be collapsed. So if you do your drawing inside them (which takes time), you block your message queue and WM_TIMER messages cannot be handled. Thus, you don't hear beeps.