1

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 );
}
Zippy
  • 3,826
  • 5
  • 43
  • 96

1 Answers1

0

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;

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Thanks @Geobits, I implemented this and it does work (Confirmed rendering and game updates happen the same amount of times per second) but it causes 'jerkiness' of my sprites - do you have any idea what may be causing this or how to avoid it? Thanks again – Zippy Feb 24 '13 at 02:19