Hey,
This is how my game loop looks like:
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if( !Render() ) //In Render() function there are all game drawings and calculations
{
MessageBox(NULL, "Failed to render", "Error", MB_OK | MB_ICONEXCLAMATION);
PostQuitMessage(0);
}
}
}
I'm not actually sure what happens in PeekMessage(...) function. When the game window is active and has focus, everything seems to work fine. PeekMessage function seems to distribute CPU to my game in accordance with its needs. More CPU when there's collision, less when nothing happens at all. I experienced however huge growth of CPU when I minimize my window. I probably have dual core processor and thats why it's taking 50% CPU in that time.
When I erase this block of code from my game loop:
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
it takes 50% of CPU all the time -> that's why I assumed that PeekMessage distributes somehow CPU. Simple infinite loop like this:
while(1)
{}
will also take 50% of CPU.
Why is that happening and how can I prevent this growth of CPU without sleep()? I would be very grateful for help!