2

我正在尝试在我的游戏中制作“分享分数”按钮。作为分数共享的一部分,我想做的一部分是创建一个带有游戏徽标和用户分数的小图形。然后,该图形将通过用户选择的任何平台共享。但是,我坚持生成此图形。现在我有一个带有徽标的基本图形,但我需要一种使用 libGDX 在该图形上绘制文本(即在其上绘制用户分数)的方法。

换句话说,有没有办法将文本写入像素图来做到这一点?

谢谢

4

1 回答 1

4

您可以使用FrameBuffer对象来满足您的要求,然后使用以下方式从帧缓冲区中读取像素块Gdx.gl.glReadPixels(...)

FrameBuffer frameBuffer;

SpriteBatch spriteBatch;
BitmapFont font;

TextureRegion bufferTextureRegion;
Texture texture;
OrthographicCamera cam;

@Override
public void create() {

    cam=new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    cam.setToOrtho(false);

    spriteBatch=new SpriteBatch();
    texture=new Texture("badlogic.jpg");
    font=new BitmapFont();

    int w=texture.getWidth();
    int h=texture.getHeight();

    frameBuffer=new FrameBuffer(Pixmap.Format.RGBA8888,Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),false) ;
    frameBuffer.begin();

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

    spriteBatch.begin();
    spriteBatch.draw(texture,0,0);
    font.draw(spriteBatch,"Score :100",100,100);
    spriteBatch.end();

    //bufferTextureRegion =new TextureRegion(frameBuffer.getColorBufferTexture(),0,0,frameBuffer.getWidth(),frameBuffer.getHeight());
    //bufferTextureRegion.flip(false,true);

    ByteBuffer buf;
    Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGB888);
    buf = pixmap.getPixels();
    Gdx.gl.glReadPixels(0, 0, w, h, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, buf);

    frameBuffer.end();

    PixmapIO.writePNG(Gdx.files.external("output.png"), pixmap);
}
于 2017-06-27T00:01:04.123 回答