# LibGdx drawing weird behaviour

I am finding strange behaviour while rendering TextureRegions in my game, only when pausing it. I am making a game for Android, in Java with LibGdx.

When I comment out the line "drawLevelPaused()" everything seems to work fine, both running and paused. When it's not commented, everything works fine until I pause the screen, then it draws those two rectangles, but maybe ships are not shown, and if I comment out drawShips() and drawTarget() (just trying) maybe one of the planets disappears, or if I change the order, other things disappear and those that disappeared before now are rendered again. I can't find the way to fix this behaviour

I beg your help, and I hope it's my mistake and not a LibGdx issue. I use OpenGL ES 2.0, stated in AndroidManifest.xml, if it is of any help. Thank you in advance.

My Screen render method(game loop) is as follows:

@Override
public void render(float delta)
{
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

controller.update(delta);
renderer.render();
}


When world state is PAUSED controller.update does nothing at all, there is a switch in it. And renderer.render() is as follows:

public void render()
{
int worldState=this.world.getWorldState();

updateCamera();
spriteBatch.begin();
drawPlanets();
drawTarget();
drawShips();

if(worldState==World.PAUSED)
{
drawLevelPaused();
}
else if(worldState==World.LEVEL_WON)
{
drawLevelWin();
}
spriteBatch.end();
}


And those methods are:

private void updateCamera()
{
this.offset=world.getCameraOffset();
}

private void drawPlanets()
{
for(Planet planet : this.world.getPlanets())
{
this.spriteBatch.draw(this.textures.getTexture(planet.getTexture()), (planet.getPosition().x - this.offset[0]) * ppuX, (planet.getPosition().y - this.offset[1]) * ppuY);
}
}

private void drawTarget()
{
Target target=this.world.getTarget();
this.spriteBatch.draw(this.textures.getTexture(target.getTexture()), (target.getPosition().x - this.offset[0]) * ppuX, (target.getPosition().y - this.offset[1]) * ppuY);
}

private void drawShips()
{
for(Ship ship : this.world.getShips())
{
this.spriteBatch.draw(this.textures.getTexture(ship.getTexture()), (ship.getPosition().x - this.offset[0]) * ppuX, (ship.getPosition().y - this.offset[1]) * ppuY, ship.getBounds().width*ppuX/2, ship.getBounds().height*ppuY/2, ship.getBounds().width*ppuX, ship.getBounds().height*ppuY, 1.0f, 1.0f, ship.getAngle()-90.0f);
}

if(this.world.getStillShipVisibility())
{
Ship ship=this.world.getStillShip();
Arrow arrow=this.world.getArrow();
this.spriteBatch.draw(this.textures.getTexture(ship.getTexture()), (ship.getPosition().x - this.offset[0]) * ppuX, (ship.getPosition().y - this.offset[1]) * ppuY, ship.getBounds().width*ppuX/2, ship.getBounds().height*ppuY/2, ship.getBounds().width*ppuX, ship.getBounds().height*ppuY, 1f, 1f, ship.getAngle() - 90f);
this.spriteBatch.draw(this.textures.getTexture(arrow.getTexture()), (ship.getCenter().x - this.offset[0] - arrow.getBounds().width/2) * ppuX, (ship.getCenter().y - this.offset[1]) * ppuY, arrow.getBounds().width*ppuX/2, 0, arrow.getBounds().width*ppuX, arrow.getBounds().height*ppuY, 1f, arrow.getRate(), ship.getAngle() - 90f);

}
}

private void drawLevelPaused()
{
this.shapeRenderer.begin(ShapeType.FilledRectangle);
this.shapeRenderer.setColor(0f, 0f, 0f, 0.8f);

this.shapeRenderer.filledRect(0, 0, this.width/this.ppuX, PAUSE_MARGIN_HEIGHT/this.ppuY);
this.shapeRenderer.filledRect(0, (this.height-PAUSE_MARGIN_HEIGHT)/this.ppuY, this.width/this.ppuX, PAUSE_MARGIN_HEIGHT/this.ppuY);

this.shapeRenderer.end();

for(Button button : this.world.getPauseButtons())
{
this.spriteBatch.draw(this.textures.getTexture(button.getTexture()), (button.getPosition().x - this.offset[0]) * this.ppuX, (button.getPosition().y - this.offset[1]) * this.ppuY);
}

}

• Try comment out the shape rendering routine in drawLevelPaused().. do you still have the problem? Sep 3 '12 at 9:10
• No! If I comment out from this.shapeRenderer.begin(...) to this.shapeRenderer.end() (both included) all planets, target, ships, arrow and buttons are shown as they should! What does that mean? Sep 3 '12 at 11:21
• I have never used ShapeRenderer, even peeking at it's source. But probably, if you move shape rendering routines to outside spritebatch's begin and end, that will solve the problem (just intuition). If you still have the problem, maybe you could just make a texture for the shape. So you will not use the shape renderer. Sep 5 '12 at 3:13
• Yes, in the ShapeRenderer source code it says that it is used for debugging purpose, so I will just make a black rectangle texture and use only SpriteBatch, thank you! Sep 5 '12 at 17:43
• @eckyputrady Greetings from the future! Your intuition was 100% correct when you said "if you move shape rendering routines to outside spritebatch's begin and end, that will solve the problem (just intuition)" -- it has solved the problem I was having. Thank you! Mar 31 '17 at 21:31

Although that's a little bit late, I'll still try to answer.

"The standard use-pattern looks as follows:"

camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);

shapeRenderer.begin(ShapeType.Line);
shapeRenderer.color(1, 1, 0, 1);
shapeRenderer.line(x, y, x2, y2);
shapeRenderer.rect(x, y, width, height);
shapeRenderer.end();


Don't forget to call setProjectionMatrix before drawing a ShapeRenderer instance, otherwise you will get unexpected results.

You can get a projection matrix from SpriteBatch.getProjectionMatrix()

• Appreciate it! I'll choose it as valid though I don't have anymore that code and I cannot test if it would solve it. Thank you anyway. Jun 23 '13 at 19:52
• I had the same problem just now, and it was fixed by following @eckyputrady's excellent comment made to the original question: "if you move shape rendering routines to outside spritebatch's begin and end, that will solve the problem (just intuition)" Mar 31 '17 at 21:33

I know it's an old post but I ran into this problem today and none of these answers solved it. The correct answer is that you can't use ShapeRenderer between SpriteBatch().begin() and end().

OP's problem would be solved by ending the batch before drawLevelPaused():

public void render()
{
int worldState=this.world.getWorldState();

updateCamera();
spriteBatch.begin();
drawPlanets();
drawTarget();
drawShips();
spriteBatch.end();

if(worldState==World.PAUSED)
{
drawLevelPaused();
}
else if(worldState==World.LEVEL_WON)
{
drawLevelWin();
}
}