Skip to main content
GameDev.net gamedev.net
🔒 Locked

Reducing CPU load...

Started by Erzengeldeslichtes Oct 19, 2003 at 11:02 PM 2 replies 400+ views
Original Post
Erzengeldeslichtes
Erzengeldeslichtes
I have a (fairly) simple program (in C++ 6, and for windows xp) which opens a text file, chooses a filename for a picture, and then displays that picture for a certain amount of time before choosing the next one. I''m using DirectX for the displaying of it so that I could put in 3d effects for fading in/out, etc. The thing is that I built it to be in either windowed mode or fullscreen mode so that I can have it displaying on one monitor while I work on the other (So that the monitor is actually doing something when I''m not using it). The problem is that when I''m working without the program on my progs use ~20% of my processor, but when I run the program it takes 100% of my processor and my mouse starts skipping and other things start to lag. How would I reduce this processor usage? I''ve put in a check to make sure that it only displays so many frames per second but it still takes all of my processor. Anyone have any ideas? (As a workaround for me I reduce the process''s priority, but I''d rather it be something the program itself does.) I''m also toying with the Sleep() command, but I don''t want to use that because it disrupts the input stream (which is based on keystates at the time of a frame, not on windows messages)
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Puffy_Taco
Puffy_Taco
You could use WaitMessage() to essentially pause the program until a new message is sent to the queue. You''d also have to use WM_TIMER messages if you wanted the picture to change without user interaction, as WaitMessage() would sit until, for example, the mouse moved while over that window. The reason the CPU usage is going up is that you''re just running through your main loop quickly! Gotta give it a break somewhere!
deepdene
deepdene
I cam across this problem myself when I was converting some SDL to win32 style code.


bool gotMsg = false;
MSG msg;
msg.message = WM_NULL;
while (msg.message != WM_QUIT)
{
// Use PeekMessage() if the app is active, so we can use idle time to

// render the scene. Else, use GetMessage() to avoid eating CPU time.

if (m_AppRunning)
gotMsg = (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0);
else
gotMsg = (GetMessage(&msg, NULL, 0U, 0U) != 0);

if (gotMsg)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


in your message proc something like this

case WM_ACTIVATE:
active = LOWORD(wParam);
minimized = HIWORD(wParam);
if ((active == WA_ACTIVE) || (active == WA_CLICKACTIVE))
{
if (minimized) // Needed???

ShowWindow(m_AppWindow, SW_RESTORE);

m_AppRunning = true;
}
else if (active == WA_INACTIVE)
{
m_AppRunning = false;
}
break;




[edited by - deepdene on October 19, 2003 12:33:30 AM]
Erzengeldeslichtes
Erzengeldeslichtes
I already have code to know wether it's active or not, the thing is that I want it to run regardless, so that when I'm working in another application it's still displaying and changing pictures (It can be visible even if it doesn't have the focus)

I'll try implementing a WaitMessage() function with a timer message, although I'll probably have to change my input to run off Windows Messages because I think that DirectInput cancels out windows key messages (could be wrong though). (The program runs off a simplification of a game engine of mine)

[edited by - Erzengeldeslichtes on October 20, 2003 1:01:29 AM]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.