簡體   English   中英

將文字繪制到Pixmap(LibGDX)上

[英]Drawing text onto a Pixmap (LibGDX)

我正在嘗試在游戲中創建一個“共享分數”按鈕。 作為分數共享的一部分,我要做的一部分工作是創建一個帶有游戲徽標和用戶分數的小圖形。 然后,該圖形將通過用戶選擇的任何平台共享。 但是,我仍然無法生成此圖形。 現在,我有一個帶有徽標的基本圖形,但是我需要一種使用libGDX在該圖形上繪制文本(即在其上繪制用戶分數)的方法。

換句話說,有沒有一種方法可以將文本寫到Pixmap上呢?

謝謝

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM