繁体   English   中英

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

[英]Low framerate on simple libgdx example in desktop application

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

截至目前,我仅显示512x512的徽标。 该应用程序中没有发生其他任何事情,但是当我在桌面模式下运行该应用程序时,我得到的FPS为15-16。 删除图像时,黑屏速度为60fps。 对于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的好东西,所以这个问题对我来说似乎莫名其妙。 如此低的帧频我在做什么错?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM