我正在开发一个电话文字游戏.昨天我决定使用 libgdx 切换到 OpenGL,以尝试提高图形性能和电池使用率 + 以针对更多平台.
I'm working on a phone word game. Yesterday I decided to switch to OpenGL using libgdx to try and improve graphics performance and battery usage + to target more platforms.
在 2D 画布上绘制字母拼贴的方式是每个字母拼贴都会为自己创建一个位图.我会:
The way letter tile drawing was working on a 2D canvas was each letter tile would create a bitmap for itself. I would:
使用 libgdx 实现我想要的最佳方式是什么?
What's the best way for me to achieve what I want using libgdx?
显然,最终代码不应该每次都从文件中加载,因为这会对性能造成巨大影响,但这里是为尝试获得相同结果的其他人提供的工作示例代码.
Obviously the final code shouldn't load from the files every time, as this is a massive performance hit, but here's working sample code for anyone else who is trying to achieve the same result.
// load the background into a pixmap
Pixmap tile = new Pixmap(Gdx.files.getFileHandle(
"someFile.png", FileType.Internal));
// load the font
FileHandle handle = Gdx.files.getFileHandle("someFont.fnt",
FileType.Internal);
BitmapFont font = new BitmapFont(handle);
// get the glypth info
BitmapFontData data = font.getData();
Pixmap fontPixmap = new Pixmap(Gdx.files.internal(data.imagePaths[0]));
Glyph glyph = data.getGlyph(getLetterToDraw().charAt(0));
// draw the character onto our base pixmap
tile.drawPixmap(fontPixmap, (TILE_WIDTH - glyph.width) / 2, (TILE_HEIGHT - glyph.height) / 2,
glyph.srcX, glyph.srcY, glyph.width, glyph.height);
// save this as a new texture
texture = new Texture(tile);
引用:你可以创建一个 BitmapFontData
实例并使用 BitmapFontData.getImagePath()
加载一个 Pixmap
包含 glyphs
.然后,您可以使用 BitmapFontData.getGlyph
来获取 Pixmap
中特定字形的位置,您可以将其与 Pixmap.drawPixmap()
一起使用做你想做的事.来源:libgdx 第 691 期:将 BitmapFont 绘制到 Pixmap/Texture
Quote: You can create a BitmapFontData
instance and use BitmapFontData.getImagePath()
to load a Pixmap
containing the glyphs
. You can then use BitmapFontData.getGlyph
to get the location of a specific glyph in the Pixmap
, which you can use with Pixmap.drawPixmap()
to do what you want to do. Source: libgdx Issue 691: Draw BitmapFont to Pixmap/Texture
您获得 BitmapFontData
的路径并使用该路径的 Filehandle 创建一个新的 Pixmap:
You get the path to the BitmapFontData
and create a new Pixmap with the Filehandle to the path with this:
像素图(文件句柄文件)
Pixmap(FileHandle file)
从给定文件创建一个新的像素图实例.
Creates a new Pixmap instance from the given file.
来源:PixmapAPI
使用 Gdx.files.internal(pathfromBitmapFontData)
作为文件句柄,你就得到了包含所有 Glyps 的像素图.
Use Gdx.files.internal(pathfromBitmapFontData)
as filehandle and you got the pixmap with all Glyps inside.
Pixmap p = new Pixmap(Gdx.files.internal(myBitmapFontData.getImagePath()))
试试它是否是内部的!可能不是我不知道的内部文件
Pixmap p = new Pixmap(myFont.getData().getFontFile());
这也应该有效
这篇关于使用 libgdx 在运行时生成带有文本的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!