user2510952
user2510952

Reputation: 193

Libgdx Rendering Textures vs Sprites

I am trying to build my first game with Libgdx and Box2d. The game has a similar concept as Flappy Bird. My issue is rendering the pipes.

I have tried drawing rectangles and then drawing new sprite which I can size down to different pipe sizes every time the render method is called. the issue with doing so is that I cant dispose of the texture once the rectangle leaves the screen because it will make all the other rectangles that are still visible lose their texture. if I don't dispose of the texture once it leaves the screen it will make the game very slow after 20 seconds.

The other option is to use about 10 different textures for different pipe sizes but still there is the issue of disposing of the textures.

I would appreciate any suggestions how to efficiently render different pipe sizes. I have attached my render code below

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

    world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);

    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    //background.setPosition(camera.position.x-camera.viewportWidth/2, camera.position.y-14);
    //background.draw(batch);
    batch.end();

    //bg1.update();
    //bg2.update();

    updateAnimation();

    if((TimeUtils.nanoTime()/10) - (lastDropTime/10) > 300000000) 
        createPipes();

       batch.begin();

       for(Rectangle raindrop: raindrops) {
              pipe_top.setSize(4, raindrop.height);
          pipe_top.setPosition(raindrop.x,  raindrop.y);
          pipe_top.draw(batch);

          pipe_bottom.setSize(4, raindrop.height);
          pipe_bottom.setPosition(raindrop.x, camera.position.y + (camera.viewportHeight/2-pipe_bottom.getHeight()));
          pipe_bottom.draw(batch);

       }
       batch.end();

       if(pipe.getX() < 0){
           pipe.getTexture().dispose();
       }

       Iterator<Rectangle> iter = raindrops.iterator();
          while(iter.hasNext()) {
             Rectangle raindrop = iter.next();
             raindrop.x -= 4 * Gdx.graphics.getDeltaTime();

             if(raindrop.x  < -35) iter.remove();

          }

    debug.render(world, camera.combined);
}

Upvotes: 4

Views: 4246

Answers (1)

Aurel
Aurel

Reputation: 479

You have to maintain the render() method as fast as possible. Loading resources is pretty slow, so you should create your textures and sprites in the create() method.

Also, you don't need as many sprites as raindrops, you can reuse the same sprite, change its position and size and draw it multples times.

Hope this helps.

Upvotes: 2

Related Questions