Game loop design - your considerations

I had been using this run method as game loop.


public void run(){
    // Initialize the resources
    Map.initMap();
    initResources();
    // Game loop initialization
    int SKIP_STEPS = 1000/Global.STEPS_FOR_SECOND;
    // The time
    long gameTime = getCurrentTime();
    int loops;
    // FPS counter
    int frames = 0;
    long lastFPSCount = getCurrentTime();
    // UPD counter
    int updates = 0;
    long lastUPDCount = getCurrentTime();
    // The current time
    long now;
    // Benchmarking
    long updStartTime = 0;
    long updEndTime = 0;
    long updTime = 0;
    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            // calculate update count
            updates++;
            if (now - lastUPDCount > 1000){
                lastUPDCount = getCurrentTime();
                Global.ACTUAL_STEPS_FOR_SECOND = updates;
                Global.UPDATE_RATE = (int)((float)((float)Global.ACTUAL_STEPS_FOR_SECOND/(float)Global.STEPS_FOR_SECOND)*100);
                updates = 0;
            }
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
        interpolation = (now + SKIP_STEPS - gameTime)/SKIP_STEPS;
        displayGame();
        // FPS counter
        frames++;
        if (now - lastFPSCount > 1000) {
            lastFPSCount = now;
            Global.FRAMES_PER_SECOND = frames;
            frames = 0;
        }
    }
}

But the output of the bench marking is


59
8
2
2
1
1
2
1
0

I couldn’t understand why 0’s appear as elapsed time for updates. Also the STEPS_PER_SECOND is 50. Thanks.

If the updateGame(SKIP_STEPS) doesnt do anything, it’s normal right?

That method isn’t empty, it moves the objects and checks collisions. The objects are nearly 100

After narrowing down your code


    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
    }

I suspect the 2nd while loop is not executed on later. The ‘loops<Global.MAX_FRAMESKIP’ looks pretty “stable”. Try to debug the ‘now’ and ‘gameTime’.

I tried printing them out every loop. Sometimes they’re becoming equal. This other question I’ve asked helped me.

Thanks for the help

[quote]First, you should avoid calling getCurrentTime() multiple times per loop. You will potentially be getting different values at different times.
[/quote]
I just know about this, but yes it seems true ;D