0
\$\begingroup\$

For my Android app this is the gameloop:

public void run(){  
    while(true){
        if(frames == 0)
            cTime = System.currentTimeMillis();

        if(System.currentTimeMillis() >= lastUpdate + 5){
            if(gameState == PLAYING)
                updatePhisic();

            updateSprites();            
            this.postInvalidate();
            lastUpdate = System.currentTimeMillis();
        }

        /*try{
            Thread.sleep(sleeptime);
        }catch(InterruptedException e){}*/

        if(System.currentTimeMillis() >= cTime + 1000){
            frameRate = frames;
            frames = 0;
        }
        else
            frames++;
    }
}

Please consider this should work for multiple devices.

Testing in some devices:

  • My phone: 180 FPS
  • A Smaller phone: 200 FPS
  • A Tablet: 120 FPS

I'm trying to put it at 60 FPS but the player moves so slow, I don't know how to calculate the Delta for player. I mean that at 180 FPS in my phone works fine, that is the speed that I want. But at 120 FPS in a tablet freezes a bit.

I think that painting the screen 60 times in a second would be enough.

And this freezes sometimes:

if(System.currentTimeMillis() >= lastUpdate + 5)

This does not:

try{
    Thread.sleep(sleeptime);
}catch(InterruptedException e){}

Why?

\$\endgroup\$
3
  • \$\begingroup\$ I am not sure you should try to control the FPS that way. Just use a delta in the update methods and let the device go at its own peace \$\endgroup\$ Commented Jan 27, 2017 at 15:20
  • 1
    \$\begingroup\$ @realUser404 That's going to kill the battery in no time :P \$\endgroup\$
    – Vaillancourt
    Commented Jan 27, 2017 at 17:23
  • \$\begingroup\$ Any suggestions? \$\endgroup\$
    – txuy
    Commented Jan 27, 2017 at 17:34

1 Answer 1

1
\$\begingroup\$

There are two things that stand out here. First, having 4 calls to System.currentTimeMillis() is at best confusing and could be the source of bugs. If lastUpdate is used outside of this function, it's going to represent the idle time between frames, not the time since the last frame. Try locking in the current time at the top of your loop instead:

while(true) {
    long currTimeMs = System.currentTimeMillis();

    if(frames == 0)
            cTime = currTimeMs;

    if(currTimeMs >= lastUpdate + 5){
        ...
        lastUpdate = currTimeMs;
    }


    if(currTimeMs >= cTime + 1000){
        ...
    }
    ...
}

Along the same lines, you should have a fixed delta and pass that around:

if(currTimeMs >= lastUpdate + 5){
    deltaMs = currTimeMs - lastUpdate;
    ...
    // Make use of deltaMs
    ...
    lastUpdate = currTimeMs;
}

This gives the rest of the code a consistent view of time.

Second, you're not counting your frame rate correctly, so the numbers you mentioned are likely misleading. frames++ happens outside your update block, meaning that you increment the frame count even if you didn't render a frame.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .