0

I've just begun using LibGDX and I'm encountering a problem using batch: I have a render method in my HomeScreen that renders pretty much everything on the screen, including some Buttons. In my class Button, I render the buttons and afterwards, I use a batch to draw a text. The problem is that the text is drawn behind the rectangle of the button (even if the batch begins after the renderer of the rectangle) don't know if it's clear so here's some code:

In the Screen class:

    public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    viewport.apply();
    vector3.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    viewport.unproject(vector3);
    renderer.setProjectionMatrix(camera.combined);
    renderer.setAutoShapeType(true);
    renderer.begin(ShapeRenderer.ShapeType.Filled);
    homeStars.render(renderer);
    for (Button button: buttons)
    {
        button.render(renderer);
    }
    renderer.setColor(1,1,1,1);
    //realCursor = cursorToWorldPosition(Gdx.input.getX(),Gdx.input.getY());
    //renderer.circle((vector3.x+1)*Const.WORLD_WIDTH/2,(vector3.y+1)*Const.WORLD_HEIGHT/2,5);
    renderer.circle(vector3.x,vector3.y,5);
    renderer.set(ShapeRenderer.ShapeType.Line);
    for(HomeStar star : HomeStars.stars)
    {
        if(vector3.dst(star.position)<Const.MAX_DIST_MOUSE_STAR){
            renderer.line(vector3.x, vector3.y,star.position.x,star.position.y,Color.WHITE, Color.BLUE);
        }
    }
}

And in the Button class:

    public void render(ShapeRenderer renderer){
    if (mouseOn() == true){
        renderer.rect(position.x-width/2,position.y-height/2,width, height, Color.BLACK, Color.VIOLET, Color.BLACK, Color.VIOLET);
    }
    else if (mouseOn() == false){
        renderer.rect(position.x-width/2,position.y-height/2,width, height, Color.BLACK, Color.GRAY, Color.BLACK, Color.GRAY);
    }
    batch.begin();
    font.draw(batch, "yo", position.x, position.y);
    batch.end();
}

Any help would be much appreciated, thank you!

Vellyxenya
  • 79
  • 8

1 Answers1

1

You can't nest SpriteBatch and ShapeRenderer. Make sure to call renderer.end() before calling batch.begin() and then call renderer.begin() after calling batch.end():

renderer.end();
batch.begin();
font.draw(batch, "yo", position.x, position.y);
batch.end();
renderer.begin();

Since you are learning, that should do it for now. Note however that this will get quickly very inefficient, because it defeats the purpose of batching. You should avoid constantly switching between ShapeRenderer and SpriteBatch. SpriteBatch is perfect to render rectangles, so you might want to work to using only SpriteBatch for your buttons (or use scene2 for UI).

Xoppa
  • 7,983
  • 1
  • 23
  • 34
  • Thank you very much for your answer! By the way, in which case is Shaperenderer more appropriate than SpriteBatch? – Vellyxenya Oct 03 '16 at 17:16
  • For quick debugging or when you don't need a texture (like a font or such) at all. – Xoppa Oct 03 '16 at 19:01