Friday, September 24, 2010

My Game Loop

In my last post, I said I would follow up with a listing of my game loop showing the use of the new metrics calculation class.  Here it is!


Note:  The metrics calculator object is called "timer" in this code.

// Loop until the program is over
while (!programOver)
{
    // Record the starting time of the loop
    timer.recordStartTime();

    // Update the screen data
    screenManager.advance();

    // Draw the updated screen
    screenManager.render(MHDisplayModeChooser.getGraphics2D());
            
    // Record the ending time of the loop
    timer.recordEndTime();

    // Calculate how long it took to run this loop and
    // use that value to see how long we should wait
    // (or "sleep") before starting the next iteration
    timer.sleep();

    // Separate UPS from FPS to maintain a  better frame rate.
    while (timer.shouldUpdate())
        screenManager.advance();
} // while (!programOver) . . .

No comments:

Post a Comment