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

时间:2013-11-05 06:16:48

标签: java android libgdx frame-rate

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

截至目前,我只显示512x512的徽标。应用程序中没有其他任何事情发生,但是当我在桌面模式下运行应用程序时,我的FPS为15-16。当我删除图像时,我得到60fps的空白屏幕。对于Android更糟糕的是,我在Galaxy SL中获得3-4 fps - GT-i9003(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的好东西,所以这个问题对我来说似乎很困惑。如果得到如此低的帧速率,我做错了什么?

1 个答案:

答案 0 :(得分:3)

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

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