如何在应用程序启动后更改LwjglApplicationConfiguration

时间:2016-04-20 12:09:08

标签: java libgdx

我正在通过进化计算对应用程序进行编码,如果我以60fps渲染每次迭代需要花费大量时间,因此我以这种方式更改了DesktopLauncher:

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.height = 800;    
        config.width = 800;     
        config.vSyncEnabled = false; // Setting to false disables vertical sync
        //config.foregroundFPS = 60; // Setting to 60 fps throttling
        config.foregroundFPS = 0; // Setting to 0 disables foreground fps throttling
        config.backgroundFPS = 0; // Setting to 0 disables background fps throttling
        new LwjglApplication(new MyApp(), config);
    }
} 

现在让我们说吧;每次迭代完成后,我希望我的应用程序限制为60 fps(或启用V-Sync),如何访问/修改配置?

public class RandomScreenClass {
  ...
  @Override
    public void render(float deltaTime) {
    if(..)
    {
     //modify the fps / turn on v-sync
    }

1 个答案:

答案 0 :(得分:2)

我找不到一个"幻想"改变LwjglConfig的方法,我找不到一个吸气剂,但这有效:

  1. 在核心项目中创建一个包含两个方法的接口,比如说setForegroundFPS和setBackgroundFPS:

    SELECT SUBSTRING(description, 
                     CHARINDEX('BuildNumber', description) + 11, --The position after BuildNumber: (a)
                     CHARINDEX('][SubBuild', description) - 3 - CHARINDEX('BuildNumber', description) + 11)) --The distance from (a) to the square brackets before SubBuild
    
  2. 将Callback添加为游戏类的成员,并创建一个合适的构造函数来接收它

    public interface Callback {
       void setForegroundFPS(int foregroundFPS);
       void setBackgroundFPS(int backgroundFPS);
    }
    
  3. 在您的桌面项目中,实现回调,如此

    public YourGame(Callback callback) {
       this.callback = callback;
       ...
    }
    
  4. 现在只需调用callback.setBackgroundFPS()或callback.setForegroundFPS()
  5. 请注意,我忽略了vSync,因为它已在:

    中提供
        public static void main(String[] arg) {
            final LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
            new LwjglApplication(new YourGame(new Callback() {
                @Override
                public void setForegroundFPS(int foregroundFPS) {
                    config.foregroundFPS = foregroundFPS;
                }
    
                @Override
                public void setBackgroundFPS(int backgroundFPS) {
                    config.backgroundFPS = backgroundFPS;
                }
            }), config);
        }