drawcode / oolongengine

Automatically exported from code.google.com/p/oolongengine
0 stars 0 forks source link

FPS Counter Fix #30

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Observing the FPS counter over time.

What is the expected output? What do you see instead?
The current getFPS calculation in Timer.mm uses the average number of frames 
over the length of the time of the program to deduce an FPS. If the program 
pauses or goes into the background then the total frames drawn will be less 
than 60 per second relative to when the application was launched, in addition 
any rapid or arbitrary "frame crash" will not be detected properly and the FPS 
timer will eventually become so bloated no accurate measurement is made. 

What version of the product are you using? On what operating system?
XCode 4.0.2 with iOS 4

Please provide any additional information below.
Below is a rewritten function that can be put in place of the existing FPS 
counter to deliver the current FPS and not an FPS-application-average it needs 
to be called from the rendering function like the old one but requires no 
parameters to work. I welcome any improvements on its design.

int GetAproxFps()
{
    //Do all the timing
    static int intTotalFramesTicked = 0, intPreviousFPS = 0;
    static CFTimeInterval cftLastFrameTime = 0;
    static CFTimeInterval cftThisFrameTime = 0;
    static CFTimeInterval cftDeltaTime = 0;

    int intAproxFPS;

    //Increment the total frames ticked.
    intTotalFramesTicked++;

    //Get the current time for comparison.
    cftThisFrameTime = CFAbsoluteTimeGetCurrent();

    //Set the last frame to current if no last frame exists
    if(cftLastFrameTime == 0)
    {
        cftLastFrameTime = CFAbsoluteTimeGetCurrent();
    }
    else
    {
        //Determine the time passed between function calls.
        cftDeltaTime += cftThisFrameTime - cftLastFrameTime;

        //If we have waited a second.
        if (cftDeltaTime >= 1)
        {
            //Reset everything necessary.
            cftDeltaTime = 0;
            intAproxFPS = intTotalFramesTicked;
            intTotalFramesTicked = 0;
            intPreviousFPS = intAproxFPS;
        }
        else
        {
            intAproxFPS = intPreviousFPS;
        }
    }

    //Set this frames time to the last frames time so time accumulates.
    cftLastFrameTime = cftThisFrameTime;

    return intAproxFPS;
}

Apologise if that does not display correctly.

Thank you for your lovely engine, EnlightenedOne

Original issue reported on code.google.com by dominics...@hotmail.com on 1 Jul 2011 at 12:17