java - 将文本绘制到像素图 (LibGDX)
<p><p>我正在尝试在我的游戏中制作“分享分数”按钮。作为分数共享的一部分,我想做的一部分是创建一个带有游戏 Logo 和用户分数的小图形。然后,该图形将通过用户选择的任何平台共享。但是,我坚持生成此图形。现在我有一个带有 Logo 的基本图形,但我需要一种使用 libGDX 在该图形上绘制文本(即在其上绘制用户得分)的方法。</p>
<p>换句话说,有没有办法将文本写入像素图以执行此操作?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以使用 <code>FrameBuffer</code> 对象来满足您的要求,然后以这种方式使用 <code>Gdx.gl.glReadPixels(...)</code> 从帧缓冲区中读取像素 block :</p>
<pre><code>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);
}
</code></pre></p>
<p style="font-size: 20px;">关于java - 将文本绘制到像素图 (LibGDX),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44767681/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44767681/
</a>
</p>
页:
[1]