How to render an emoji with libgdx

柔情痞子 提交于 2019-12-22 10:49:57

问题


I have loaded a font with emoji support and I am trying to render a string with emoji's with libgdx. However, it renders the text, but not the emojis.

Code

Load Font

FileHandle fontFile = Gdx.files.internal("path/to/file.ttf");
FreeTypeFontGenerator g = new FreeTypeFontGenerator(fontFile);
FreeTypeFontGenerator.FreeTypeFontParameter p = new FreeTypeFontGenerator.FreeTypeFontParameter();
// Some config here with p
BitmapFont emojiFont= g.generateFont(p);

Render Font

public static void renderFont(SpriteBatch sb, BitmapFont font, String msg, float x, float y, Color c) {
  font.setColor(c);
  font.draw(sb, msg, x, y);
}

String str = "emoji ❤ \uD83D\uDC49 test \uD83D\uDC49 \uD83D\uDC4D test \uD83D\uDE03"
renderFont(sb, emojiFont, str, x, y, new Color(-597249));

Output

emoji test test

Questions

  1. Why is libgdx not rendering the emojis?
  2. What do I need to change to render the emojis?

回答1:


FreeTypeFontGenerator creates a BitmapFont from your TTF file. Most likely your created font does not contain the emojis.

You don't show the interesting part of your code: the parameters you set. Add the emojis you want to use to the parameters:

p.characters = "characters you want to use";


来源:https://stackoverflow.com/questions/58635857/how-to-render-an-emoji-with-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!