DirectX n00b having CPU usage problem.

Posted By: MrCode

DirectX n00b having CPU usage problem. - 09/01/08 17:41

Hi all.

I've been experimenting with DirectX and Win32 quite recently (haven't even started getting into rendering 3D with it yet), and already I have a problem. I can't seem to figure out why, but every time I start my program (it just displays the standard blue background on a 640x480 window), my CPU usage skyrockets to 100%. And, when I close the window, the process doesn't end. I'm guessing the CPU usage thing is a common problem, as I've seen other DirectX programs that do the same thing (not 3DGS, of course, wink).

EDIT: In case anyone is interested, I'm working from this DirectX Tutorial: DirectXTutorial.com
Posted By: MrCode

Re: DirectX n00b having CPU usage problem. - 09/04/08 22:27

Sorry for the bump, but I really do need help with this. I can't figure it out on my own (the tutorial mentions nothing about 100% CPU usage).
Posted By: Ambassador

Re: DirectX n00b having CPU usage problem. - 09/05/08 14:43

It aint anything dangerous, I think all apps that have a while loop with no sleep func in it use the cpu at 100%. If you add a sleep of, say 1 millisecond, the cpu usage should drop. Not sure though...

something like this on windows:

while(TRUE)
{
blah blah... your code 'n stuff
Sleep(1.0);
}
Posted By: mk_1

Re: DirectX n00b having CPU usage problem. - 09/05/08 18:06

Concerning your app doesn't stop: closing the dx window doesn't mean closing the app. You need a message listener and wait for a closing message (see win32 msg handling)
Posted By: MrCode

Re: DirectX n00b having CPU usage problem. - 09/07/08 17:05

Ok, the CPU usage thing seems to have been mostly solved (I added a Sleep(1) and my CPU usage has been halved). As for the handling the close message, I have this code:

Code:
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	switch(msg)
	{
		case WM_CLOSE:
			DestroyWindow(hwnd);
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			break;
	}
	return DefWindowProc(hwnd,msg,wparam,lparam);
}


I know basic Win32 (I'm up to doing GDI stuff), I'm just new to DirectX. I don't know why my messages aren't being handled correctly. Usually when I'm writing a normal Win32 program, I don't really even need the WM_CLOSE/DestroyWindow() handler. I can just use WM_DESTROY.

EDIT: ok, CPU usage thing must have been a strange coincidence, because it's still at 100%. Well, Sleep(10) makes it lower, but I'm afraid that might make the rendering FPS (for when I start doing more with 3D) drop quite a bit.
Posted By: MrCode

Re: DirectX n00b having CPU usage problem. - 09/08/08 05:15

Ok, I've got the program-not-closing thing solved (I was just missing an if statement in my message loop). And apparently when my CPU usage still went up even after adding Sleep(1), that was the coincidence. Sorry if I seemed a little too presumptuous about that. I should be fine now. Thx for the suggestions, guys.
© 2024 lite-C Forums