Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

}
share|improve this question
    
Try comment out the shape rendering routine in drawLevelPaused().. do you still have the problem? –  eckyputrady 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? –  Ryckes 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. –  eckyputrady 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! –  Ryckes Sep 5 '12 at 17:43
add comment

2 Answers 2

up vote 3 down vote accepted

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()

share|improve this answer
    
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. –  Ryckes Jun 23 '13 at 19:52
add comment

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();
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.