Domanda

Here is the gameloop code that I have implemented into my Android App.

It is working great, but the problem I'm having is that although it's capping logic updates at 30 - the actual rendering runs flat out which obviously isn't great for battery life.

I can't work out how to also limit/cap the rendering to a fixed amount (say 30 or so FPS).

Specifically the rendering should only render when the logic has been updated instead of rendering redundant frames (i.e., when no logic update has occurred).

EDIT: I've just added the interpolation part of the code - could this be causing the problem when I cap the frames as recommended by @Geobits?

int TICKS_PER_SECOND = 30;
int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
int MAX_FRAMESKIP = 10;

long next_game_tick = GetTickCount();
int loops;

bool game_is_running = true;
while( game_is_running ) {

    loops = 0;
    while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
        update_game();

        next_game_tick += SKIP_TICKS;
        loops++;
    }
    interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
                    / float( SKIP_TICKS );
    display_game( interpolation );
}
È stato utile?

Soluzione

There are a few ways to cap framerate, but if all you want to do is:

Specifically the rendering should only render when the logic has been updated instead of rendering redundant frames (i.e., when no logic update has occurred).

Then you could just set a simple boolean flag. Set it to true in update_game(), and check it in onDraw(). If it's false, just return without drawing/clearing the screen. If true, do your drawing and set it to false;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top