How to limit fps in Android without causing problems? Libgdx

Hi guys.
Im having a bit of a problem.
I currently adapted my game to run at 30 fps…
But, i just noticed that android config, dont have the same desktop method to limit fps…

What should i do? I saw someone talking about Thread.sleep, but its libgdx and i dont have access to all threads?

I thought you can just do Thread.sleep anytime you want :stuck_out_tongue:

@Andre Lopes

First you must get your application context.

Use: getApplicationContext()

Doesnt libgdx have several threads running ? I was unsure if i just make it sleep in the current thread or in the others as well…

Ok, i will do that.

if Fps > 30

Sleep?

I’m not really keen @ Libgdx, just suggesting! :slight_smile:

Heres what im thinking :

if(delta <= (1f/30f))
{
int sleepingTime = (1f/30f) - (delta);
sleep(sleepingTime);

}

Also, why you need to limit fps? You should use delta value for all your movement calculations and let Android handle your frame rate.
If device have low fps your objects need to move faster and if device have high fps your objects need to move slower.
Do not control your movements with fixed frame rate.

   if (delta < (1f / 30f)) {
                float sleepTime = (1f / 30f) - delta;
                try {
                    Thread.sleep((long) sleepTime);
                } catch (InterruptedException ex) {
                    LogTool.logError(ex.getLocalizedMessage());
                    ex.printStackTrace();

                }

            }

Also, its because of this article :
http://www.learn-cocos2d.com/2013/10/game-engine-multiply-delta-time-or-not/

Um, have you never played a game that limits it’s FPS to your monitor refresh rate? I bet you have, because it’s efficient and many games do it.

Well guys, the code wont work because i cant cast float to long with the required precision.
Amateur mistake haha.

Anyway, is there any other way i can do that in android or i will have to work on delta?

Gdx.graphics.getDeltaTime returns time in seconds. So to convert to millis you could multiply by 1000.

As for limiting the framerate to 30 fps, you could do:


//stores how much time has passed since the last frame.
private float timeSpent;

public void render() {
    timeSpent += Gdx.graphics.getDeltaTime();
    if(timeSpent > 1) {
        //The following loop will try to catch up if you're not at 30 fps.
        //This code will reset the amount of time it needs to spend catching up if there's too
        //much to do (maybe because the device can't keep up).
        timeSpent = 1 / 30F;
    }
    while (timeSpent >= 1 / 30F) {
        //Run your code
        timeSpent -= 1 / 30F;
    }
}

You can use Non-continuous rendering, and then you can just use any ol’ game loop, calling requestRendering() each loop around. Note however that input events will also trigger the rendering, so you should still use the graphics.getDeltaTime() in your calculations to make sure stuff won’t go wack when input is handled.

Also make sure the game loop is not in the render thread!

Its not smooth as when libgdx does it for me.

Well, i have another question guys lol

If i multiply everything for delta… What about Animations?

Animation receives that as a parameter…
float frameDuration

Should i do anything about that?