Frank v Hoof
Frank v Hoof

Reputation: 57

LibGDX pixmap not correctly updating pixels

I am trying to create "destructable" terrain (2D) in LibGDX using a Pixmap. However, the colors that I am setting, do not correctly show on my screen.

Full source (colors are currently for debug, should be alpha=0 in release):

public class Terrain extends Actor implements Disposable {
private Sprite mySprite;
private Texture myTexture;
private Pixmap myMap;

public Terrain(Texture texture, Vector2 position)
{
    setPosition(position.x, position.y);
    if (!texture.getTextureData().isPrepared()) {
        texture.getTextureData().prepare();
    }
  // Final product will take original image from a texture to create pixmap
  //  myMap = texture.getTextureData().consumePixmap();
    myMap = new Pixmap(400, 400, Pixmap.Format.RGBA8888);
    myMap.setBlending(Pixmap.Blending.SourceOver);
    myMap.setColor(Color.BLUE);
    myMap.fillRectangle(0, 0, 400, 400);
    myTexture = new Texture(myMap, Pixmap.Format.RGBA8888, false);
    mySprite = new Sprite(myTexture);
}

public void removePixels(List<Vector2> pixels)
{
    myMap.setColor(Color.GREEN);
    myMap.fillCircle(1,1,1);
    myMap.setColor(Color.YELLOW);
    for (Vector2 pixel:pixels) {
        myMap.fillCircle((int)pixel.x, (int)pixel.y, 1); // Works, but is Black?
        myMap.drawPixel((int)pixel.x, (int)pixel.y, 255) // Does not work
    }
    myTexture = new Texture(myMap, Pixmap.Format.RGBA8888, false);
    mySprite = new Sprite(myTexture);

    System.out.println("Pixels Removed");
}

@Override
public void draw(Batch batch, float parentAlpha) {
    batch.draw(mySprite, getX(), getY(), mySprite.getWidth(), mySprite.getHeight());
 //   batch.draw(myMap, getX(), getY(), myMap.getWidth(), myMap.getHeight());
}

@Override
public void dispose() {
    myMap.dispose();
}

For some reason the shape drawn in fillCircle is always black, while pixels drawn with drawPixel do not appear to even update (remain original color). How can I set a pixel (alpha to 0) in LibGDX? (preferably without using glsl)

Upvotes: 1

Views: 528

Answers (1)

Tenfour04
Tenfour04

Reputation: 93922

When you call drawCircle(x, y, 1), you are passing in the RGBA8888 color 1, which is equivalent I think to R=0, G=0, B=0, A=1/255. When you call .drawPixel((int)pixel.x, (int)pixel.y, 255), you are passing in the RGBA8888 color 255, which I think is R=0, G=0, B=0, A=1. In both cases the call ignores the last color you set on the pixmap. Use the versions of these methods that don't take a color parameter.

I see some other errors. You are leaking a texture each time you call myTexture = new Texture(...) if you don't dispose the old one. But really you should just draw the updated pixmap to your texture which is likely faster. myTexture.draw(myMap, 0, 0); Also, there is no reason to use a Sprite object if you aren't using sprite.draw. If you draw a sprite with batch.draw, you might as well be using a TextureRegion.

Upvotes: 1

Related Questions