2
\$\begingroup\$

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

}
\$\endgroup\$
5
  • \$\begingroup\$ Try comment out the shape rendering routine in drawLevelPaused().. do you still have the problem? \$\endgroup\$ Sep 3, 2012 at 9:10
  • \$\begingroup\$ 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? \$\endgroup\$
    – Ryckes
    Sep 3, 2012 at 11:21
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Sep 5, 2012 at 3:13
  • \$\begingroup\$ 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! \$\endgroup\$
    – Ryckes
    Sep 5, 2012 at 17:43
  • \$\begingroup\$ @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! \$\endgroup\$ Mar 31, 2017 at 21:31

2 Answers 2

3
\$\begingroup\$

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

From the ShapeRenderer javadoc

"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.circle(x, y, radius);
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()

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Ryckes
    Jun 23, 2013 at 19:52
  • \$\begingroup\$ 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)" \$\endgroup\$ Mar 31, 2017 at 21:33
1
\$\begingroup\$

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .