使用 libgdx 在运行时生成带有文本的纹理 [英] Generating textures at runtime with text using libgdx

查看:27
本文介绍了使用 libgdx 在运行时生成带有文本的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个电话文字游戏.昨天我决定使用 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:

  1. 从背景位图创建一个新的可变位图.
  2. 在新位图上画出字母.
  3. 应用其他磁贴特定效果.
  4. 为每一帧绘制新的位图

使用 libgdx 实现我想要的最佳方式是什么?

What's the best way for me to achieve what I want using libgdx?

  1. 我应该采用类似的方法吗?如果是这样,怎么做?我尝试使用像素图,但不知道如何绘制字体.
  2. 我是否应该使用 A-Z 中所有必需的图块创建一个 spritesheet 并直接使用它?调整平铺背景的设计时有点乏味.
  3. 我应该做一些完全不同的事情吗?

回答

显然,最终代码不应该每次都从文件中加载,因为这会对性能造成巨大影响,但这里是为尝试获得相同结果的其他人提供的工作示例代码.

Answer

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 在运行时生成带有文本的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆