java - 桌面应用程序中简单 libgdx 示例的低帧率

标签 java android libgdx frame-rate

我刚刚开始学习 libGDX,并遵循 http://steigert.blogspot.in/2012/02/2-libgdx-tutorial-game-screens.html 中的示例(libGdx 帮助页面上的第二个链接)。

到目前为止,我只显示 512x512 的 Logo 。应用程序中没有发生任何其他事情,但当我在桌面模式下运行应用程序时,我得到的 FPS 为 15-16。当我删除图像时,空白屏幕的帧率为 60 fps。对于 Android,情况更糟,我在 Galaxy SL - GT-i9003 中得到 3-4 fps(《Temple Run》以设备上的可玩速度运行)。

我的笔记本电脑玩《魔兽世界》时没有任何高质量卡顿,因此令人费解的是,这么小的应用程序只能达到 15fps。

public class SplashScreen extends AbstractScreen {
    private Texture splashTexture;
    private TextureRegion splashTextureRegion;

    public SplashScreen(MyGDXGame game){
        super(game);
    }

    @Override
    public void show()
    {
        super.show();

        // load the splash image and create the texture region
        splashTexture = new Texture("splash.png");

        // we set the linear texture filter to improve the stretching
        splashTexture.setFilter( TextureFilter.Linear, TextureFilter.Linear );

        // in the image atlas, our splash image begins at (0,0) at the
        // upper-left corner and has a dimension of 512x301
        splashTextureRegion = new TextureRegion( splashTexture, 0, 0, 512, 382 );
    }

    @Override
    public void render(float delta ) {
        super.render( delta );

        // we use the SpriteBatch to draw 2D textures (it is defined in our base
        // class: AbstractScreen)
        batch.begin();

        // we tell the batch to draw the region starting at (0,0) of the
        // lower-left corner with the size of the screen
        batch.draw( splashTextureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );

        // the end method does the drawing
        batch.end();
    }

    @Override
    public void dispose()
    {
        super.dispose();
        splashTexture.dispose();
    }

}

这是 AbstractScreen 类的相关部分:

    public class AbstractScreen implements Screen {


    protected final MyGDXGame game;
    protected final BitmapFont font;
    protected final SpriteBatch batch;

    public AbstractScreen(MyGDXGame game ){
        this.game = game;
        this.font = new BitmapFont();
        this.batch = new SpriteBatch();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
    }
...
}

桌面应用程序:

public class Main {
public static void main(String[] args) {
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();

    cfg.title = "First Game";
    cfg.useGL20 = false;
    cfg.width = 800;
    cfg.height = 480;

    new LwjglApplication(new MyGDXGame(), cfg);
}

MyGDXGame如下:

public class MyGDXGame extends Game {
private FPSLogger fpsLogger;

public SplashScreen getSplashScreen() {
    return new SplashScreen( this );
}


@Override
public void create() {
    fpsLogger = new FPSLogger();
}

@Override
public void dispose() {
    super.dispose();
}

@Override
public void render() {
    super.render();
    setScreen(getSplashScreen());
    fpsLogger.log();
}

@Override
public void resize(int width, int height) {
    super.resize(width, height);
}

@Override
public void pause() {
    super.pause();
}

@Override
public void resume() {
    super.resume();
}

我读过很多关于 libGdx 的好东西,所以这个问题对我来说似乎很困惑。我做错了什么以获得如此低的帧速率?

最佳答案

我认为 render() 方法中的 setScreen(getSplashScreen()); 可能是问题所在。将其移至 MyGDXGamecreate() 方法。

现在,您正在每一帧中切换屏幕,并每次都重新创建一个 SpriteBatch ,这是一个非常重的对象(请参阅我对您的问题的评论)。

关于java - 桌面应用程序中简单 libgdx 示例的低帧率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782970/

相关文章:

java - 使用 Boolean.FALSE/.TRUE 初始化 boolean 值 - 为什么?

android - 支持 Android Camera Api 和 Camera2 Api 的问题

android - 为什么在此代码中不调用 OnComplete? (RxAndroid)

java - Libgdx - 文本字段,显示键盘输入字段

unit-testing - 找不到Gradle命令行 Assets 中的Libgdx测试

java - 在 IntelliJ Java 应用程序 SLF4j 记录器错误中运行 Tomcat

java - 在javascript下使用表达式语言?

java - 尝试在 Java 中重新分配和输出新的设定值

android - 如何通过intent直接打开Youtube App

java - 基于 Box2D 网格的碰撞