LibGdx too much FPS. How to limit?

荒凉一梦 提交于 2021-02-05 04:58:13

问题


I'm doing a game and I have a problem on some (strange) low end android devices (Galaxy S mini, Galaxy ACE ), with actually bad hardware. The FPS is on both devices 85+, and on other devices (HTC Desire, Sony Xperia Arc S, Samsung Galaxy S, HTC Wildfire ) the FPS is "normal" (around 60FPS). Also 60 FPS is showing on computer too. Obviously 85+ FPS is too much, because the game experience is different and it may cause unfair advantage to those players who are playing on 85FPS+.

I want to limit the FPS to MAX 60.

I've already searched this forum and I've found the exact question I'm asking right now. Limit FPS in Libgdx game with Thread.sleep() doesn't work

The (accepted) solution from user @daniel was this:

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

But the thing is that doesn't work for me. If I use that code (30 replaced with 60 ), I get 30-33 FPS or if I use just the Daniel's code I get around 15-17 FPS. Why is that , and why is not working for me ? How to limit FPS on all devices to MAX 60 ? I tested this code on computer, and on devices also.

Any help HIGHLY appriciated. Thank you.


回答1:


In your launcher class;

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.foregroundFPS = 60;
initialize(new MyGdxGame(), config);

I don't know if this is a new feature or an old one. Use this before trying to sleep the thread.

I highly recommend not limiting the frames by sleeping the thread, just to the keep the motion at the same pace for all the devices running fast or slow. Use something like this to move things on the screen.

x+= speed*Gdx.graphics.getDeltaTime();

Good luck on your adventure with libGDX, it seems that you are just started. Because when I was just started I was asking the same questions :)


EDIT: Here is a tip:

If you have collision in your game, and if the user sleeps the game by dragging the window etc. Gdx.graphics.getDeltaTime() function will return an unexpectedly high value. For example 2 seconds instead of 16 milliseconds (60fps) you were expecting. And in that case your character will jump, through walls and collision detection will fail. So here is what you can do:

  1. You can override that function and either take average of the last 10 frames and return that value (it is a little flawed in some cases but it is better in some other cases).
  2. You can return 30 milliseconds if the value is more than 30 seconds. So the character doesn't jump, go through walls... Even if the player sleeps it for a century.
  3. Combine both. (best) Don't count values more than 30 milliseconds when taking average.

I wrote a page about this subject, it is at my website in tutorials section. Check it out. nightlybuilddigital.ga




回答2:


You don't want to do that.

You need to make your game for any phone.

If you have a code like

player.x+=10f;

Your player will move faster on better devices.. You need to move your player by the delta time value.

player.x+=Gdx.graphics.getDeltaTime()*300; // this would be ~5f if your device is running with 60 fps



回答3:


As other people have said, if you code your game right, a higher fps shouldn't make a difference. take a look at this article here for a better way to do it. It wont translate directly to libgdx, but the concepts in there are all correct




回答4:


Why would you even pause the thread, when you can simply keep track of delta time and omit drawing/updating when it's too soon? You can limit the FPS programmatically by skipping rendering and logic updates when it's too soon.

Try something like this:

private static final float MIN_FRAME_LENGTH = 1f/60f;
private float timeSinceLastRender;

/** @param delta time since the last render call. */
public void render(float delta) {
      timeSinceLastRender += delta;
      if (timeSinceLastRender >= MIN_FRAME_LENGTH) {
           // Do the actual rendering, pass timeSinceLastRender as delta time.
           timeSinceLastRender = 0f;
      }
}



回答5:


Try this c:

 if (dt < (1f / max_frames)) {

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


来源:https://stackoverflow.com/questions/23901398/libgdx-too-much-fps-how-to-limit

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