【问题标题】:Low framerate on simple libgdx example in desktop application桌面应用程序中简单 libgdx 示例的低帧率
【发布时间】:2013-11-15 23:19:15
【问题描述】:

我刚开始学习 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 的好东西,所以这个问题让我感到莫名其妙。帧率这么低,我做错了什么?

【问题讨论】:

  • 到目前为止,代码看起来还不错。我不会为每个屏幕使用一个 SpriteBatch,但它不能限制帧速率。请告诉我们MyGDXGame。 Amd 尝试启用 GL20 并设置您的配置的foregroundFPS。你用的是哪个 libgdx 和 java 版本?
  • 感谢您对 SpriteBatch 的评论。我稍后会添加 MyGDX。我正在使用 LibGDX v0.9.9 和 Java 1.7。 GL20在Desktop上没有改变帧率,手机不支持GL2.0。我在台式机和手机上运行 MetalGun,帧速率分别为 60 和 50。
  • v0.9.9 的 LwjglApplicationConfiguration 中没有设置foregroundFPS。我尝试禁用 vSYnc 并使用 CPUSynch,但我的 FPS 在桌面模式下似乎仍被限制为 16。 Android 没有任何变化,因为这些设置不会影响它。
  • 没有前景FPS?两天前我切换到0.9.9,仍然有这个设置。没有GL 2?然后它是那些 0.8% 的恐龙手机之一 :)

标签: java android libgdx frame-rate


【解决方案1】:

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

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

【讨论】:

  • 谢谢。每次渲染屏幕时我都没有捕捉到对象重新创建,这太愚蠢了。我移动了代码'setScreen(getSplashScreen());'到 onCreate,现在我在桌面模式和设备上都以 60 FPS 的速度运行。
猜你喜欢
  • 2011-07-30
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多