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.
Pixmap tile = new Pixmap(Gdx.files.getFileHandle(
"someFile.png", FileType.Internal));
FileHandle handle = Gdx.files.getFileHandle("someFont.fnt",
FileType.Internal);
BitmapFont font = new BitmapFont(handle);
BitmapFontData data = font.getData();
Pixmap fontPixmap = new Pixmap(Gdx.files.internal(data.imagePaths[0]));
Glyph glyph = data.getGlyph(getLetterToDraw().charAt(0));
tile.drawPixmap(fontPixmap, (TILE_WIDTH - glyph.width) / 2, (TILE_HEIGHT - glyph.height) / 2,
glyph.srcX, glyph.srcY, glyph.width, glyph.height);
texture = new Texture(tile);
source
share