Limit FPS in Libgdx game with Thread.sleep() doesn't work

南楼画角 提交于 2019-11-29 23:45:13

问题


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...


回答1:


Use might use this

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

change the value of 30 to FPS you want.




回答2:


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); 
}



回答3:


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;
    }
}



回答4:


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).



来源:https://stackoverflow.com/questions/21204258/limit-fps-in-libgdx-game-with-thread-sleep-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!