使用 libgdx 在运行时生成带有文本的纹理

Generating textures at runtime with text using libgdx(使用 libgdx 在运行时生成带有文本的纹理)
本文介绍了使用 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 在运行时生成带有文本的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)