frame per second algorithm

hi. i was trying to achieve a better way of game looping. so i have a game loop algrithm. but i dont think its quite good. i want it to be better. and maybe u people may wanna discuss to make it better.

thx in advance for your contribution

here is my algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
bool quit = false;
while(quit == false)
{
    while(blah blah events)//here we hold events
    {
        if(the user want to quits)
        {
            quit = true;
        }
        //some inputs and what they do
        ...
        ...
        ...
        ...
        //here we update the game according to inputs
        //also AI decisions and acts is in it
        updateGame();
        //*here we update the screen according to updateGame results*
        //*but we also need a limitation to this anti 
        //computer-source function. couse it will use
        //everything if we dont limit it*
        if(getTime() - someValue > aSec / fps)
        {
            if(getTime() - someValue < aSec / (fps/2))
            {
                someValue = getTime();//*here we update someValue with the time 
                                      //our screenRender function works so we
                                      //can use it over and over again*
                                      //*also we use this func before 
                                      //screenRender func because screenRender func
                                      //could take a long time

                screenRender();       //*updateScreen or displayScreen func
                                      //whatever you name it*
            }
            else
            {
                //shit. either your computer or my game sucks
            }
        }
    }
}
Last edited on
Does your game logic have to be at a fixed rate? IE, your 'updateGame' function... does calling that more/less frequently affect the speed of the game?

If no:
Then you don't really need to regulate the framerate. Just let it draw as fast as it wants. You can scale it back if you want to conserve battery power on laptops by throwing in a sleep() if you detect the framerate is running too high.

Or better yet... VSync and match the user's refresh rate. Smooth updates an you don't have to worry about sleeping because that's done for you when you display.


If yes:
Regulating logic updates based on a fixed framerate can be tricky. Or at least... to do it 'right' can be tricky. I've had to do this in emulators in the past... where the emulated system runs at 60 FPS, so that's what you have to run/display at or else the games run too fast/slow.

I made a frame limiter a while ago and posted it on another forum. Forgive the use of poor Hungarian notation... I was young:

http://forums.nesdev.com/viewtopic.php?p=57398#p57398

I'm happy to answer questions about it, but most of that post covers it.

The trick is to "skip frames" when the user is running too slow. This means you run the logic, but don't do a draw. Results in slightly choppier graphics, but the game still plays normally apart from that.
@Disch
well. first of all. ty for your detailed and informative answer.

and i never thought about battery power issues. so the part you said
The trick is to "skip frames" when the user is running too slow
. well i freaked out. i need to work on it to achieve a good one but i will work hard.

also the link is quite useful and pretty confusing it will take time for me to understand all since it has just been less than a month since i started working on something out of the standard c++ library.(i chosed sdl for first)

thx again
Topic archived. No new replies allowed.