Question

I have a BitmapFont to display the player score. The player is moving in a constant rate. For the BitmapFont I use a second OrtographicCamera and SpriteBatch so I don't need to recalculate the fonts position.

My problem is: as soon as I include the BitmapFont the framerate goes down and the whole game becomes laggy. What am I doing wrong?

public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    rbg.render(delta);

    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();    
        drawBlocks();
        drawBob();  
    spriteBatch.end();

    scoreBatch.setProjectionMatrix(scoreCam.combined);
    scoreBatch.begin();
        drawScore(this.score);
    scoreBatch.end();

    if (debug)
        drawDebug();
}

    private void drawBob() {
    bobFrame = bob.isFacingLeft() ? bobIdleLeft : bobIdleRight;
    if(bob.getState().equals(State.WALKING)) {
        bobFrame = bob.isFacingLeft() ? walkLeftAnimation.getKeyFrame(bob.getStateTime(), true) : walkRightAnimation.getKeyFrame(bob.getStateTime(), true);
    } else if (bob.getState().equals(State.JUMPING)) {
        if (bob.getVelocity().y > 0) {
            bobFrame = bob.isFacingLeft() ? bobJumpLeft : bobJumpRight;
        } else {
            bobFrame = bob.isFacingLeft() ? bobFallLeft : bobFallRight;
        }
    }
    spriteBatch.draw(bobFrame, bob.getPosition().x * ppuX, bob.getPosition().y * ppuY, Bob.WIDTH * ppuX, Bob.HEIGHT * ppuY);

    cam.position.set(cam.position.x + 24f, Gdx.graphics.getHeight() / 2f, 0);
    cam.update(); } 
private void drawBlocks() {
    for (Block block : world.getDrawableBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
    for (Block block : world.getBottomBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockBottomTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
    for (Block block : world.getTopBlocks((int)CAMERA_WIDTH, (int)CAMERA_HEIGHT)) {
        spriteBatch.draw(blockTopTexture, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);
    }
}
public void drawScore(String score) {
    BitmapFont bf = new BitmapFont(Gdx.files.internal("data/droidserif.fnt"), Gdx.files.internal("data/droidserif.png"),false);
    bf.setUseIntegerPositions(false);
    bf.setScale(2);
    bf.draw(scoreBatch, this.score, 20,50); 
}
Was it helpful?

Solution

You are creating the font every frame and that is why it is slow:

BitmapFont bf = new BitmapFont(Gdx.files.internal("data/droidserif.fnt"), Gdx.files.internal("data/droidserif.png"),false);

==> create a field in your Game class BitmapFont bf and initialize it in your create method + remove the font creation from the drawScore method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top