1
votes

So this is pretty weird... I have an abstract BaseScreen. This screen initializes a SpriteBatch on show(). The init() calls the one of the BaseScreen's abstract methods. This draw issue is from using the 'gameView' viewport which is an ExtendViewport

SpriteBatch batch;
@Override
public void show(){
    batch = new SpriteBatch();
    gameView = new ExtendViewport(SV.V_WIDTH, SV.V_HEIGHT);
    hudView = new ScreenViewport();
    init();
}

This is what I do in the render function:

@Override
public void render(float delta){
    /* 
        Just in case render is being called more than usual 60 times, 
        I want to call update only 60 times per second. (SV.STEP = 1/60f)
     */
    accum += delta;
    while(accum >= SV.STEP){
        accum -= SV.STEP;
        update();
    }

    /* Clear Screen */
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    /* DRAW WORLD */
    batch.setProjectionMatrix(gameView.getCamera().combined);
    gameView.apply(true);
    batch.begin();
    draw(batch, SV.BatchType.World);
    batch.end();

    /* DRAW UI
    batch.setProjectionMatrix(hudView.getCamera().combined);
    hudView.apply(true);
    batch.begin();
    draw(batch, SV.BatchType.UI);
    batch.end();*/
}

I have two viewports, one for the game world and one for hud. I use them to set the projection matrix of the batch.

The draw(batch, SV.BatchType.World) command ultimately takes the code to here:

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
}

Now this piece of code draws this: 2 Draws

Notice how it doesn't draw the text.

Here's the result with batch.draw only - (font.draw is omitted) 1 Draw

Now the issue becomes 'what type of .draw was called last' and it ignores all of them - For example:

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
}

Will draw

3 consecutive fonts

and this

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
    batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
    batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
}

Will draw

2 batch draw at end

Here is an example with 6 draws, but you have to have a 'throwaway' last .draw

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
    batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
    batch.draw(heart, 10,10, h_width*2, h_height*2);
}

6 draws

Here's one last thing... the last batch draw... if you try to draw it at 0, 0 -> this happens position 0 0 draw

Seeking any kind of helpful feedback and/or reason why this may be happening. I'm a newbie with LibGdx and would not shun your feedback on how I decided to use this framework.

Thanks a bunch!

1
To print a window press Alt+PrintScreen or just use a screenshot tool like snipping tool. That way it won't leave ugly cuts like thatphuclv

1 Answers

1
votes

So I narrowed down what is causing it: it doesn't seem that the batch will be bugged when you are

1) Instantiating the batch on a BASE class 2) calling batch.start on the BASE class render method 3) calling one of the abstract methods in the BASE class after batch.start 4) The batch will be bugged.

What I did to solve this problem was to move the batch code into the derived class, and everything works fine.

I'm still opened to answers as to why this bug happens.