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

System slows down when program runs

Started by TempusElf Nov 3, 2002 at 10:33 AM 9 replies 1.2k views
Original Post
TempusElf
TempusElf
I''ve been experimenting with GDI for a while and now I''m trying to make a simple game that updates and renders even when there are no messages in the queue. Here is my main loop:
  
while (boolGameOn)
    {
        if (PeekMessage (&messages, NULL, 0, 0, PM_REMOVE))
        {
        	if (messages.message==WM_QUIT)
        	{ break;}
        TranslateMessage(&messages);
        DispatchMessage(&messages);
        }
        else
        {
            //This is where I update and render

        }
    }
  
So it runs and renders all fast but now my problem is that it seems to slow the rest of my system down a lot. For example, if I go to move the window, it waits a few seconds then moves. Or if I hit the ''X'' it waits a second and then closes. When my window moves, the programs underneath seem to have trouble repainting themselves. In VB, I would just through a DoEvents statement in there somewhere but I have no idea how to solve this problem in C++.
fireking
fireking
sleep(5) - try values ranging from 1 to 10, goodluck finding a number that works the same on all pc''s =\

--Fireking

Owner/Leader
Genetics 3rd Dimension Development
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Fonz
Fonz
hey,

Sorry, you want to speed your program up?

How do sleep or loops speed your system up?

Hmm.. my suggestion is to try some benchmarking to see whats going on inside your code.
(Not that i have any idea hwat the GDI is ..)

hope I was some help,

cheers,
Fonz
aggregate
aggregate
You're eating up too much CPU with that loop. PeekMessage() alone can be evil. You need to know when to use GetMessage() as well, and when to give up time slices (Sleep(0) hack/trick/feature?).

[edited by - aggregate on November 3, 2002 12:25:51 AM]
xg0blin
xg0blin
umm...guys, this is the windows main loop, he goes out of the loop and the program exits. This is like standard stuff here.


1. He takes the else statement out, then the window just processes events, and doesn''t do the game part, because you have to go into a perpetual loop to do the message processing. Once you leave the program is over, so taking else out would just draw a blank window and call WndProc when a message is needed.That''s not what he wants to happen here. If no message is in the queue he wants the game to play.


2. If you use GetMessage() then the same thing applies. When GetMessage is called it sits there until a message is processed, thereby always being true cause it will just sit there. So you put GetMessage instead of PeekMessage you have no game loop.


3.You must have edited or something, cause I don''t see a Sleep call anywhere and everyone is saying it is in there, but if it''s only set at 10 milliseconds then that shouldn''t show too much.


your problem probably lies elsewhere I''m guessing. If windows aren''t repainting, you might check your WNDCLASSEX definition to see how you have that set up.
Z01
Z01
Some very odd suggestions so far. =/

I have one suggestion that may or may not help. Right now you are only retrieving one message per game cycle from the message queue. If your game cycle is taking a long time, there may be lots of messages stacking up into the queue. Try putting PeekMessages in a while loop to make sure you get all the messages from the queue:

  
while( boolGameOn ) {
while( PeekMessage(...) ) {
// handle message

}
// do game stuff - render, AI, colldet, etc.

}


The issue with programs underneath having problems repainting themselves probably has nothing to do with this, its more likely that they''re being swapped to disk and you have to wait for them to swap back so they can redraw their window contents.

Unless you are using a system with cooperative threading or you are changing the priority of your main thread, using sleep() shouldn''t affect anything.
Advanced Bug
Advanced Bug
I also use PeekMessage() function in my game loop (and I even use a while loop that reads all messages before updating the game) and there are no problems with system slowing down or other windows repainting, but I remember I had those problems, when there was a SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); call at the beginning of my program.
TempusElf
TempusElf
Thanks for all the suggestions! it really helped me think about the problem... I''m going to try a few things later today... I have a lot of confidence in Z01''s suggestion... I''m starting to think that my main problem is that I''m relying on BitBlt to draw the whole window (600x400 pixels) and FillRect to clear an offscreen bitmap... maybe I should look for alternative methods or even start using a different graphics library

Topic Locked

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

Sign in to reply to this topic.