Question

I am developing a little game for android with libgdx and want to limit the fps to 30 to save battery. The problem is that it doesn't work. The fps just drops from 60 to 56.

Here is the part of the code: (it's at the end of the render part)

System.out.print("\nFPS: " + Gdx.graphics.getFramesPerSecond() + "\n");

    if(Gdx.graphics.getDeltaTime() < 1f/30f)
    {
        System.out.print("DeltaTime: " + Gdx.graphics.getDeltaTime() + " s\n");
        float sleep = (1f/30f-Gdx.graphics.getDeltaTime())*1000;
        System.out.print("sleep: " + sleep + " ms\n");
        try
        {
            Thread.sleep((long) sleep);
        }
        catch (InterruptedException e)
        {
            System.out.print("Error...");
            e.printStackTrace();
        }
    }

and here is the output:

FPS: 56
DeltaTime: 0.014401722 s
sleep: 18.931612 ms

FPS: 56
DeltaTime: 0.023999143 s
sleep: 9.334191 ms

FPS: 56
DeltaTime: 0.010117603 s
sleep: 23.215733 ms

thanks in advance...

Était-ce utile?

La solution 4

Use might use this

Thread.sleep((long)(1000/30-Gdx.graphics.getDeltaTime()));

change the value of 30 to FPS you want.

Autres conseils

I have found a solution on the Internet and modified it a bit. It works perfect! Just add this function to your class:

private long diff, start = System.currentTimeMillis();

public void sleep(int fps) {
    if(fps>0){
      diff = System.currentTimeMillis() - start;
      long targetDelay = 1000/fps;
      if (diff < targetDelay) {
        try{
            Thread.sleep(targetDelay - diff);
          } catch (InterruptedException e) {}
        }   
      start = System.currentTimeMillis();
    }
}

And then in your render function:

int fps = 30; //limit to 30 fps
//int fps = 0;//without any limits
@Override
public void render() {
   //draw something you need
   sleep(fps); 
}
import com.badlogic.gdx.utils.TimeUtils;

public class FPSLimiter {

    private long previousTime = TimeUtils.nanoTime();
    private long currentTime = TimeUtils.nanoTime();
    private long deltaTime = 0;
    private float fps;

    public FPSLimiter(float fps) {
        this.fps = fps;
    }

    public void delay() {
        currentTime = TimeUtils.nanoTime();
        deltaTime += currentTime - previousTime;
        while (deltaTime < 1000000000 / fps) {
            previousTime = currentTime;
            long diff = (long) (1000000000 / fps - deltaTime);
            if (diff / 1000000 > 1) {
                try {
                    Thread.currentThread().sleep(diff / 1000000 - 1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            currentTime = TimeUtils.nanoTime();
            deltaTime += currentTime - previousTime;
            previousTime = currentTime;
        }
        deltaTime -= 1000000000 / fps;
    }
}

You might look into Libgdx's "non-continuous rendering" as an alternative way of reducing the frame rate on Android. As it stands your app's UI thread will be stuck sleeping and so your app may be a bit less responsive (though sleeping for a 30th of a second isn't really that long).

Depending on your game the non-continuous rendering support might let you reduce the frame rate even further (any time there is no change to the graphics and no input expected, you do not need to draw new frames).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top