Search code examples
javaandroidlibgdxframe-rate

Low framerate on simple libgdx example in desktop application


I just started learning libGDX, and was following the example from http://steigert.blogspot.in/2012/02/2-libgdx-tutorial-game-screens.html (the second link on libGdx help page).

As of now, I just display a logo of 512x512. There is nothing else happening in the application but when I run the application in desktop mode,I get a FPS of 15-16. When I remove the image, I get 60fps for the blank screen. For android its even worse, I get 3-4 fps in Galaxy SL - GT-i9003 (Temple Run runs on playable speed on the device).

My laptop plays World of Warcraft without any hiccups in high quality so its baffling that such a small app would only achieve 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();
    }

}

Here is the relevant section of AbstractScreen class:

    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 );
    }
...
}

And the desktop app:

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 is as follows:

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();
}

I have read so many good things about libGdx, so this issue seems baffling to me. What am I doing wrong to get such a low frame rate?


Solution

  • I think setScreen(getSplashScreen()); in your render() method might be the problem. Move it to the create() method of MyGDXGame.

    Right now you are switching the screen in every single frame and recreate a SpriteBatch every single time which is a very heavy object (see my comment to your question).