Sign in to follow this  
Kiryn

CPU Load

Recommended Posts

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();
		}
	}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this