Roni
Roni

Reputation: 137

Libgdx Pixmap drawCircle now drawing

I'm trying to draw a hollow circle on the screen. I got a class the implements Screen and this is it's render method:

@Override
public void render(float delta) {
    update(delta);
    Gdx.gl.glClearColor(1,0,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myGame.batch.setProjectionMatrix(gamecam.combined);
    myGame.batch.begin();
    Pixmap pixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.BLACK);
    pixmap.drawCircle(100, 100, 15);
    Texture texture = new Texture(pixmap);
    myGame.batch.draw(texture, 0, 0);
    myGame.batch.end();
}

But for some reason I get a red screen

Upvotes: 0

Views: 1412

Answers (1)

AAryan
AAryan

Reputation: 20140

A Pixmap stores its data in native heap memory, don't create in render() method. Don't create Texture too in render method.

private Pixmap pixmap;
private Texture texture;

@Override
public void show() {

  float w = Gdx.graphics.getWidth();
  float h = Gdx.graphics.getHeight();

  gamecam = new OrthographicCamera(30, 30 * (h / w));
  gamecam.position.set(gamecam.viewportWidth / 2f, gamecam.viewportHeight / 2f, 0);
  gamecam.update();

  pixmap= getPixmapCircle(10, Color.WHITE,false);
  texture=new Texture(pixmap);
  texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

}

@Override
public void render() {

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

    myGame.batch.setProjectionMatrix(gamecam.combined);
    myGame.batch.begin();
    myGame.batch.draw(texture,100,100);
    myGame.batch.end();
}

public static Pixmap getPixmapCircle(int radius, Color color, boolean isFilled) {
   Pixmap pixmap=new Pixmap(2*radius+1, 2*radius+1, Pixmap.Format.RGBA8888);
   pixmap.setColor(color);
   if(isFilled)
      pixmap.fillCircle(radius, radius, radius);
   else
      pixmap.drawCircle(radius, radius, radius);
   pixmap.drawLine(radius, radius, 2*radius, radius);
   Pixmap.setFilter(Pixmap.Filter.NearestNeighbour);
   return pixmap;
}

@Override
public void dispose() {
    pixmap.dispose();
    texture.dispose();
}

It is mandatory to call dispose() when the pixmap or texture is no longer needed, otherwise memory leaks will result

Upvotes: 2

Related Questions