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

前端 未结 4 2012
清酒与你
清酒与你 2021-01-03 11:36

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

相关标签:
4条回答
  • 2021-01-03 11:55

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

    0 讨论(0)
  • 2021-01-03 12:03
    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 12:05

    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); 
    }
    
    0 讨论(0)
  • 2021-01-03 12:09

    Use might use this

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

    change the value of 30 to FPS you want.

    0 讨论(0)
提交回复
热议问题