Creating runtime textures with text using libgdx

I am working on a phone game. Yesterday, I decided to switch to OpenGL using libgdx to try to improve graphics performance and battery usage + to target more platforms.

As I drew a picture of a letter tile on a 2D canvas, each lettering tile would create a bitmap image for itself. I'd:

  • Create a new mutable bitmap from the background bitmap.
  • Draw a letter on the new bitmap.
  • Apply other tile specific effects.
  • Draw a new bitmap for each frame

What is the best way for me to achieve what I want to use libgdx?

  • Should I use a similar approach? If so, how? I tried using pixmaps but couldn't figure out how to draw a font.
  • Should I create a spritesheet with all the necessary fragments from AZ and just use this? A little tiring when I customize the tile background design.
  • Should I do something completely different?

Answer

Obviously, the last code should not be loaded from files every time, since this is a massive performance hit, but here is a working code example for everyone 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);
+3
source share
1 answer

: BitmapFontData BitmapFontData.getImagePath() Pixmap, glyphs. BitmapFontData.getGlyph Pixmap, Pixmap.drawPixmap(), , . : libgdx 691: BitmapFont Pixmap/Texture


BitmapFontData Pixmap :

Pixmap ( FileHandle)

Pixmap .

: PixmapAPI

Gdx.files.internal(pathfromBitmapFontData) , pixmap Glyps .

Pixmap p = new Pixmap(Gdx.files.internal(myBitmapFontData.getImagePath()))

, ! , .


Pixmap p = new Pixmap(myFont.getData().getFontFile());

+3
source

All Articles