在Pixmap上绘制文本(LibGDX)

时间:2017-06-26 19:54:49

标签: java android ios libgdx pixmap

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

换句话说,有没有办法在Pixmap上写文字才能做到这一点?

由于

1 个答案:

答案 0 :(得分:3)

您可以根据需要使用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);
}