Advertisement

FPS control

Started by March 14, 2000 06:41 PM
2 comments, last by PsYcHoPrOg 24 years, 9 months ago
Hello. I have been looking at a lot of FPS code and either didn''t understand them, or saw that they looked really slow and inefficient. So if anyone has some efficiant sample code for frame rate control, please post it up. It would be greatly appreciated. -Thanks "Remember, I'm the monkey, and you're the cheese grater. So no messing around." -Grand Theft Auto, London
D:
Here''s what I have:

Start of the main loop:

if(frameRateOn)
if(timeNeeded)
{
// if we need the elapsed time, then get it
timeCount = timeGetTime();
frameCount = 0;
timeNeeded = false;
}

------------------
end of main loop:
// count another frame, and if needed update the frame rate
frameCount++;
if((timeGetTime() - timeCount) >= 1000)
{
frameRate = frameCount;
timeNeeded = true;
}


Sorry about the tabs. I hope that helps! (And I hope I didn''t leave anything out...)

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
Advertisement
OK here''s the idea. I''m working on this problem right now. Here''s some basic rough idea code.

//Startup:
long CountTime = timeGetTime();//or whatever in milliseconds
int fps = 30;//# of frames/second
//Somewhere in the main loop

do{}while(timeGetTime() - CountTime < (1000/fps));
CountTime = timeGetTime();

That''s it!
Just start up the CountTime, and every frame wait until the difference between the real time and the one stored from the beginning of the last frame.

Hope it works

-Ben Dilts

Psychoprog,

The process goes as follows (imagine this is your main rendering loop):

while (render)
{
(1) get a time measurement, which is a representation of the current time (this could be with GetTickCount() or some other higher-precision timer)

(Do all your drawing - this is the bulk of your code)

(2) get another time measurement and subtract the previous
in order to get the elapsed time (in milliseconds).

(3) This is the key. Divide the total time it took to render the frame (elapsed time) into a second. (see: 1000ms). This will tell you how many frames will draw during that whole second.
}

If you look at the code samples people have provided they
should all follow this basic process.

If you want to limit the frame rate I guess you could either put a short loop (pause) in your rendering loop to slow things down or else write your program so that the number of frames displayed is time bound eg. (display a new frame only every 90ms instead of every 14ms).

Paulcoz.

Edited by - paulcoz on 3/16/00 6:59:49 PM

This topic is closed to new replies.

Advertisement