CPU Load

Started by
2 comments, last by jollyjeffers 16 years, 11 months ago
I'm using Win32 API. And when I run the application, the CPU load is loud. And I was wondering how do I get it not be so loud. Heres the loop. Oh its not loud when it's visible, but when you minimize it or put it behind another window that covers it, its loud.

if(SUCCEEDED(initD3D(hWnd)))
	{
		if(SUCCEEDED(init_graphics()))
		{
			ShowWindow(hWnd, nCmdShow);
			UpdateWindow(hWnd);
			
			MSG msg;
			ZeroMemory(&msg, sizeof(MSG));
			while(msg.message != WM_QUIT)
			{
				if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
				else
					render_frame();

				// check the 'escape' key
				//if(KEY_DOWN(VK_ESCAPE))
					//PostMessage(hWnd, WM_DESTROY, 0, 0);
			}
			
			// clean up DirectX and COM
			cleanD3D();
		}
	}


Advertisement
This might not work, but try putting a Sleep(0); after DispatchMessage. It will give up the remainder of the time slice to other processes running. Peek Message is non-blocking, so it will run it as fast as it can. You might also try it after the entire If/Else block.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Quote:Original post by Kiryn
I'm using Win32 API. And when I run the application, the CPU load is loud. And I was wondering how do I get it not be so loud. Heres the loop. Oh its not loud when it's visible, but when you minimize it or put it behind another window that covers it, its loud.

*** Source Snippet Removed ***

Translation of your code snippet:
while(game is running){   if(any messages from Windows)   {      Use CPU   }   else   {      Use CPU   }}

No matter what you do, you're using 100% of the CPU.
Catch the WM_SIZE event for when you minimize and start performing short sleep events - say 20-50ms or so. That should stop your application going into a crazily tight loop yet also allow it to respond quick enough to the restore/maximize event...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement