gpt4 book ai didi

java - Libgdx Pixmap drawCircle 正在绘制

转载 作者:行者123 更新时间:2023-12-02 03:02:02 27 4
gpt4 key购买 nike

我正在尝试在屏幕上画一个空心圆。我有一个 implements Screen 类,这是它的 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();
}

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

最佳答案

Pixmap 将其数据存储在 native 堆内存中,不要在 render() 方法中创建。不要在 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(),否则会导致内存泄漏

关于java - Libgdx Pixmap drawCircle 正在绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42312842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com