7
votes

I'm trying to make a game where you build a spaceship from parts, and fly it around and such.

I would like to create the ship from a series of components (from a TextureAtlas for instance). I'd like to draw the ship from it's component textures, and then save the drawn ship as one large Texture. (So i don't have to draw 50 component textures, just one ship texture. What would be the best way to go about this?

I've been trying to do so using a FrameBuffer. I've been able to draw the components to a Texture, and draw the texture to the screen, but no matter what I try the Texture ends up with a solid background the size of the frame buffer. It's like the clear command can't clear with transparency. Here's the drawing code I have at the moment. The ship is just a Sprite which i save the FrameBuffer texture to.

public void render(){

    if (ship == null){
        int screenwidth = Gdx.graphics.getWidth();
        int screenheight = Gdx.graphics.getHeight();

        SpriteBatch fb = new SpriteBatch();

        FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);

        fbo.begin();

        fb.enableBlending();
        Gdx.gl.glBlendFuncSeparate(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);

        Gdx.gl.glClearColor(1, 0, 1, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        fb.begin();

        atlas.createSprite("0").draw(fb);

        fb.end();

        fbo.end();

        ship = new Sprite(fbo.getColorBufferTexture());
        ship.setPosition(0, -screenheight);
    }

    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();

    batch.enableBlending();
    batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
    ship.draw(batch);
    batch.end();
}
1
I think you are looking for PixMap but i never used it myself, look for a tutorial of it!Robert P

1 Answers

15
votes

The problem here lies in this line:

 FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);

specifically with Format.RGB888. This line is saying that your FrameBuffer should be Red (8 bits) followed by Green (8 bits) followed by Blue (8 bits). Notice however, that this format doesn't have any bits for Alpha (transparency). In order to get transparency out of your frame buffer, you probably instead want to use the Format.RGBA8888, which includes an additional 8 bits for Alpha.

Hope this helps.