Sign in to follow this  

Main loops for games/3d rendering

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I'm trying to do a toolset of editors and viewers (for textures, meshes, maps, etc), under Win32 and using OpenGL. I've tried using NeHe's code as a base, but when it comes to run and see the results, the process sucks 50% of my CPU (an entire core, while the other stays idle). I think the problem comes from the main loop, because it calls the rendering function again, again and again. Using other ways to refresh the scene (like ONLY on window move/resize/etc) lets the CPU rest, but then you have a pretty still scene. So, my question is, what is the best way to do a main loop for a game-like app? Even using the WM_PAINT message to redraw the scene keeps the CPU at full usage. I've tried using both these loops, and they both work bad:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

while (1)
{
	while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if (!GetMessage (&msg, NULL, 0, 0))
			ExitAndClear();
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}
	// Do everything here
}

Share this post


Link to post
Share on other sites
define "work bad". any application written without Sleep statements of non-zero duration that give the CPU back to other apps will take 100% of the CPU. there's nothing wrong with that. any fullscreen game will and should take 100% of the CPU. I like to put Sleep(0) statements where I can so that other threads run more happily when making a windowed game.

100% CPU usage isn't a bad thing. it just means you are getting maximum performance which is good.

like you say, when you yield CPU time your performance degrades. that's how it goes. you're either updating all the time or not. how frequently you update affects your framerate.

-me

Share this post


Link to post
Share on other sites
Oh, okay :D

I thought it was, well, not bad, just not good. Other old games (say HL, Q2, etc) just suck 20-25% of CPU when running, and I was almost scared when I heard my laptop's fan go full speed everytime I ran my apps.

Full CPU speed, then!

Share this post


Link to post
Share on other sites
Quote:
Original post by r2d2rigo
I thought it was, well, not bad, just not good. Other old games (say HL, Q2, etc) just suck 20-25% of CPU when running, and I was almost scared when I heard my laptop's fan go full speed everytime I ran my apps.


ah, but you can't see CPU usage when that game is running fullscreen, can you? [smile] (unless you have dual monitors). There's also going to be some blocking when you get the GPU to render which will result in less CPU usage.

If you're just developing for yourself, ignore it. If you want to release something, make it play nice when windowed or minimized. the only way I know to do this is add sleep statements with non-zero arguments [ Sleep(10) ] or whatever, or not have your main loop get executed at all when minimized.

-me

Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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