-
unconstant framerate
Hi all,
I'm making yet another game, this time one that constantly refreshes the screen.. the only problem is, it runs faster at times and slower at other times (as in, during the *game*). I've tried a couple of methods, but both kind of suck:
a) Check the time, store it in a variable, then at the end of the loop check the time and sleep for x minus the time elapsed during the body of the loop.
-Works ok, but for some reason sometimes freezes the game when I minimize or move the window. I'm suspecting it's because when a variable gets high enough, it loops back into negatives.. then it ends up sleeping for x - (-348723) millisecs (?)
b) Set a timer, and when the time is up, call a gameMain() function, which resets the timer, etc.
-Doesn't freeze, but is very slow and inconsistant, thus defeating the purpose. I'm thinking it's because WM_TIMER messages have really low priority.
Does anybody have any better way (as in low performance cost, doesn't screw up) of evening out the framerate?
-
Couldnt you use the first method and stop the loop when your game loses the focus?
-
nono, I don't think that's the problem... the problem is sometimes more resources are being used and stuff, so the game slows down, resulting in your ship sometimes going really fast and sometimes at a crawl.
-
Using GDI is not the best method for games.........Your windows thread is at the mercy of the rest of the system and how busy it is.....sometimes you can be down to a crawl speed...sometimes you are doing ok...
Remember this is why timing your app from WM_TIMER is not reliable........it might try send the message at the correct interval, but if your thread is constantly losing its timeslice due to lots of activity you will not get the message at the proper interval...and once you get the message it is cued with all the other messages to the window...and this might have a slight distortion effect too..
Best bet - invest in some time with a graphics library......for example, Direct X allows you to use hardware blitters to send your image to screen faster than the GDI...also memory can be held on the graphics card freeing up your system memory fo other parts of the game......best of all you can set your window to run exclusively.....this stops other windows using up the CPU time and allows your window a lot of extra CPU time that it wouldnt have when runnign in normal mode....
-
Use something like:
Code:
DWORD x = some_value;
DWORD start, time_elapsed;
start = GetTickCount();
.
//loop
.
time_elasped = GetTickCount() - start;
if (time_elapsed < x)
Sleep(x-time_elapsed);
-