OGeek|极客世界-中国程序员成长平台

标题: java - 将文本绘制到像素图 (LibGDX) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:35
标题: java - 将文本绘制到像素图 (LibGDX)

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

换句话说,有没有办法将文本写入像素图以执行此操作?

谢谢



Best Answer-推荐答案


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

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

关于java - 将文本绘制到像素图 (LibGDX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767681/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4