Libgdx Pixmap drawCircle现在绘制

罗尼

我正在尝试在屏幕上画一个空心圆。我有一个implementsScreen,这是它的render方法:

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

但是由于某种原因,我会出现红屏

阿比舍克·雅利安(Abhishek Aryan)

Pixmap将其数据存储在本机堆内存中,请勿在inrender()方法中创建不要在render方法中也创建纹理

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

当不再需要像素图或纹理时,必须调用dispose(),否则会导致内存泄漏

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章