1
\$\begingroup\$

i'm drawing text above monster head and it's fine. But when there is to many monsters and their name gets render every second cause fps go down. When i disable the font drawing, then the fps is fine. Any idéa how to tackle this perfomence issue?

Thanks.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Make sure you don't change or recreate text objects every frame. \$\endgroup\$
    – Vaillancourt
    Aug 11, 2016 at 17:47
  • \$\begingroup\$ I do not recreate, just redraw them. If that make sense. \$\endgroup\$
    – Printer
    Aug 11, 2016 at 18:03

1 Answer 1

3
\$\begingroup\$

Every time you draw something actually you are sending data from CPU to GPU. its the slow part of drawing somethings.

So if you have unique names for each monster only thing you can do is drawing texts via opengl but i dont know if its possible or not in libgdx.

If you are using same text for all monsters so its quite possible.

  1. Create pixmap
  2. Load font texture and data
  3. Write your string to pixmap
  4. Convert pixmap to texture
  5. Create sprite from texture
  6. Change position of sprite for each monster position.
  7. So when you drawing font you wont send data to GPU just going to change position of texture already exists on GPU.

Here is code for almost of them:

// 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
//You can use method for draw string to pixmap char by char. 
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
sprite = new Sprite(new Texture(tile));
\$\endgroup\$
4
  • \$\begingroup\$ You can also use this way for drawing a lot of objects with a same texture. I drew 15k objects on phone and 100k+ objects on desktop with a 50+ fps. \$\endgroup\$ Aug 11, 2016 at 18:55
  • \$\begingroup\$ Thank you for your answer. But since i have unique names for my monsters. How can i change the text? \$\endgroup\$
    – Printer
    Aug 12, 2016 at 8:15
  • \$\begingroup\$ i researched for it but seems impossible to me. because you creating in libgdx you must send it to GPU. however you can still try the way that i explain. it would increase fps if same name showing for a long time like 10 seconds. \$\endgroup\$ Aug 12, 2016 at 10:23
  • \$\begingroup\$ actually im sure it ll increase fps. but if you still want to increase you can use lower size font and scale it. \$\endgroup\$ Aug 12, 2016 at 10:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .