[OpenGL & MFC] Processor usage goes HIGH!

Started by
5 comments, last by Vortez 14 years, 11 months ago
Hi programming gurus! I've tried to make an application similar to CEGUI LayoutEditor with C# and Axiom Framework but Axiom team told me that their support for CEGUI# is pretty outdated and not really implemented. They will try to get it back when they'll release another stable version. So, I decided to do something more challenging and go for C++, OpenGL and MFC, and than CEGUI. I found some old, really old tutorial (http://steinsoft.net/index.php?site=Programming/Tutorials/opengl_dialog) that shows how to use MFC to make a window with Windows' controls and OpenGL rendering window but the performance is not really good. When I tried to run this app, the processor usage goes to 50%-55% (2 cores CPU, probably 100% on 1 core CPUs). I'd like to know why it runs so badly and how to improve the performance (if possible). I've read some forums where people had similar problems and they said it's MFC's fault. If that is true than what other way can I use to make my application? Maybe Win32 API could be better? Or something where I could create controls easily VC++. Oh, and the most IMPORTANT thing. I've had problems with VS2005 recently. Basically, when I tried to "build and run" (F5) my test applications, IDE came up with a window telling me that there is something wrong with debugging and if I want to continue anyway (it's not about building, it's after the app was compiled and about to run). There was a tick box too (Don't show this window again, or something similar), I did tick it. Now, when I try to debug an application it just starts but quits IMMEDIATELY. I'm sure that's because of that debugging thingy I was asked in that window. It happens with every projects so it's not a project setting, it must be some tick box in the IDE itself but I can't find any that would mean the same what that pop up window told me about (I can't really remember what it said exactly, but sure something about debugging and debug database). EDIT: Here is the window I was talking about: http://img190.imageshack.us/img190/5150/errormsg.jpg It came up again but now I think it wasn't the issue. [Edited by - 50p on May 18, 2009 10:49:22 AM]
Advertisement
If no one else is using your CPU, and if your program isn't forced to slow down, it's going to run as fast as it can (while using as much CPU as it can too). That's totally normal. You're probably getting eleven billionty frames per second, so I really don't think the performance is bad. You just aren't telling it to sleep a little.

As far as that little message box thing... it's kind of important to know what it said. Maybe someone here can guess it from your description but I can't.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Ok, thanks.

When I run Fraps to see fps, I get 60fps. That's normal because I get 60fps on most next-gen game (CoD4, CoD5, Assasins Creed, etc.) and the old ones too (Counter Strike, CoD2, BF2, etc.). I think it's due to some graphic card limitations but I don't care if I have 60 or more fps because human eye can't capture more than ~30fps.

Before I run the program I get 0-3% of CPU usage and there are many applications running in the background (Xfire, MSN, Firefox, mIRC). I also asked my friend to run it on his PC and his percentage was 24%-25% (because he has Quad (4 cores)). I don't really like this and I'd like to find some other way which wouldn't use 100% CPU because CPU's temperature goes up too.

EDIT:
OK, I tried to use Sleep(100) and Sleep(50) in OnPaint event. It did drop down FPS (to 10 and 20) but also CPU usage (to 2-4 and 4-8). So I think I'm on the good way. Thanks for the sleep trick, I will remember that.
60Hz is probably your refresh-rate. If you are using windows you should read about the message system.

Not perfect, but still a good read

You should only render if there are no messages from what I remember.

But I didn't work with Windows for some time so I might be wrong ;)
Limit your FPS, you're drawing a bajillion frames and you probably have vsync on.

Find some way to time it (like timeGetTime, with a low resolution set)
if((currentTick - lastTick) < (1000 / desiredFps))...



PS the human eye doesn't see frames but rather a constant stream. And yes, it is very easy and possible for the human eye to see things that have only been shown for mere milliseconds.
Quote:Original post by clashie
Limit your FPS, you're drawing a bajillion frames and you probably have vsync on.

Find some way to time it (like timeGetTime, with a low resolution set)
if((currentTick - lastTick) < (1000 / desiredFps))...



PS the human eye doesn't see frames but rather a constant stream. And yes, it is very easy and possible for the human eye to see things that have only been shown for mere milliseconds.


Thanks for reply but I already found a way, I mean because with MFC an OpenGL control (rendering window) uses OnPaint event. So I added Sleep(25) in OnPaint event. It dropped FPS down to 40 and CPU usage is about normal. I'm happy with the results and now, I'm trying to use CEGUI with it but I get some unhandled exceptions and can't solve that problem. Maybe you guys could help me. Here is the topic I made on CEGUI's forum http://www.cegui.org.uk/phpBB2/viewtopic.php?f=10&t=4118
Here's a good way to start, a basic opengl program with no glut or mfc, pure win32: Link

You shouln't have to mess with the OnPaint event imo, you're problem must be because you didn't use your messages pump the "good" way... You have to use PeekMessage instead of GetMessage, that must be why you're mfc program is so slow, bu him only guessing here.

In general, it look like this:

void GameLoop(){	MSG msg;	static bool done = false;	// Application loop.	while(!done){		if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)){			// If a quit message is received then stop rendering and quit the app.			if(msg.message == WM_QUIT)				done = true;			TranslateMessage(&msg);  // Translate any messages.			DispatchMessage(&msg);   // Dispatch any messages.		} else {                      // Update engine timer			UpdateScene(EngineTimer.g_ElapsedTime);  // Control the game flow     		RenderScene();                           // Render a frame.		}	}}


This way, you're app will only draw a frame when there is no messages to process.

This topic is closed to new replies.

Advertisement