How to clone BitmapFont object?

Anything libgdx related goes here!

How to clone BitmapFont object?

Postby vatuma » Sat Sep 03, 2011 1:16 pm

When I load assets in usual way I can create several objects from a single bitmap font file:
Code: Select all
redFont = new BitmapFont(fontFileHandle, false);
redFont.setColor(redColor);

greenFont = new BitmapFont(fontFileHandle, false);
greenFont.setColor(greenColor);

But how to do something like this when loading assets via AssetManager?
So if I create redFont:
Code: Select all
redFont = assetManager.get(fontFileName, BitmapFont.class);
redFont.setColor(redColor);

then how can I create greenFont?
Do I understand right that I have to use another font file for this?
Or can I somehow clone redFont?
vatuma
 
Posts: 14
Joined: Sat Mar 19, 2011 11:37 pm

Re: How to clone BitmapFont object?

Postby NateS » Sat Sep 03, 2011 10:23 pm

It isn't efficient to load the same font only to change the color. Better to use one font and set the color before drawing with it. That said, you could use 2 font files. This makes sense if you have two fonts that are actually different, eg use Hiero to have different colored gradients. You can also probably do this... (untested)

Code: Select all
BitmapFont redFont = new BitmapFont(greenFont.getData(), greenFont.getRegion(), greenFont.usesIntegerPositions());
redFont.setColor(1, 0, 0, 1);
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: How to clone BitmapFont object?

Postby vatuma » Sun Sep 04, 2011 5:57 am

Thank you, Nates! That works
vatuma
 
Posts: 14
Joined: Sat Mar 19, 2011 11:37 pm

Re: How to clone BitmapFont object?

Postby Betalord » Sun Apr 22, 2018 6:16 pm

I have another problem: I need to clone font completely (including font data). This is because I need an alternative version of the font where markup language is enabled. If I copy font as indicated above, font data object is just copied over (by reference) and hence enabling the markup language would enable it for the original font as well.
I need a markup language version font because I can't have the original font use it since user could type something like "[]" in an edit box and crash the application this way. So I need a non-markup version of the font for most widgets, and markup enabled version for certain widgets (like labels etc.).

Any idea how to completely clone the font?
Betalord
 
Posts: 11
Joined: Wed Oct 26, 2016 1:01 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 1 guest