Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My Gameloop (code below), limits the game updates to 60 ticks per second. However, it renders as fast as the device on which it is being run can handle.

I'm using a fixed time step in my game and I want to be able to limit my rendering to a specified amount (in my particular case, that would also be 60). However, I need to keep rendering calls uncoupled from game update calls.

I tried lumping the render calls in with the update calls but the problem with this is that they aren't uncoupled. This could potentially lead to issues if the device can't handle rendering 60 fps. Lets say it drops to 40fps, then the game updates will also drop to 40 ticks per second. Everything in my game is based on the game running at 60 ticks per second. So everything will run at the wrong speed. (using a variable time step isn't an option, my whole game is build around a fixed time step) So basically, if the device drops to 40 rendering calls per second, it should continue running game updates at 60 ticks per second so everything runs OK.

I am quietly confident that (virtually) every device on which my app runs will easily be able to handle 60 ticks (game updates) per second. I've tested this on a very old handset and it copes with 60 ticks no problem.

I would rather limit the rendering because as things stand it's rendering redundant frames and this must have a negative effect on battery life.

Would be grateful for suggestions on how I can limit the frames renders while keeping rendering and game updates separate.

Code

long nextGameTick = System.currentTimeMillis();
//loop counter
int loops;
//This is the amount of frames that we allow our app to skip before logic updating is affected (generally shouldn't happen)
final int maxFrameskip = 5;                             
//Game updates per second
final int ticksPerSecond = 60;
//Amount of time each update should take        
final int skipTicks = (1000 / ticksPerSecond);

    public void onDrawFrame(GL10 gl) {


    //Set/Re-set loop back to 0 to start counting again
    loops=0;

    while(System.currentTimeMillis() > nextGameTick && loops < maxFrameskip){

        SceneManager.getInstance().getCurrentScene().updateLogic();
        nextGameTick+=skipTicks;
        timeCorrection += (1000d/ticksPerSecond) % 1;
        nextGameTick+=timeCorrection;
        timeCorrection %=1;
        loops++;
        tics++;

    }

    render();   

}

Please note I've already read Fix your timestep! and the similar Android Game Loops but these articles don't mention how to limit the rendering in a loop like the one I have adapted - just the game updates.

Any help would be appreciated

share|improve this question
    
If you were using interpolation as mentioned in those articles, then it would not be rendering redundant frames. –  jhoffman0x May 19 at 0:21
    
Thanks @jhoffman0x, appreciate the comment, I'm looking at interpolation right now actually, although I must admit it's a bit confusing. So each frame would always have a different position? Lets say I have a sprite moving (by chance) at 1 physical pixel per second, at 60 FPS it would move 60 pixels per second. Now on an old Galaxy Ace, my game is running flat out at 60 ticks per second but rendering at 90 Frames Per second. Surely there would still be redundant frames in there? Bigger issue, is battery drain. Isn't it good practice to limit the render calls per second? Thanks again! –  user22241 May 19 at 0:45
    
Interpolation works best when transformations are represented by floating point numbers. If you're using a graphics framework that only lets you set integer pixel positions then yes you would have redundant frames. –  jhoffman0x May 19 at 1:13
    
Of course if something was moving at more than 1 pixel per update, and you were rendering faster than you were updating, then interpolation would still make it appear to move more smoothly. Anyways, sorry for getting off on this tangent without addressing your actual question... –  jhoffman0x May 19 at 1:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.