Fonts and BufferedImage

Started by bittramp, March 15, 2011, 02:27:40

Previous topic - Next topic

bittramp

Hello,
I am currently using a BufferedImage to paint a font on and then creating a texture.  My problem is that the only byte sized BufferedImage type is BufferedImage.TYPE_3BYTE_BGR, there seems to be no RGB. It made for nice flow to convert the BufferedImage raster to byte [] with one call, and casting wont work if I use BufferedImage.TYPE_INT_RGB.  Is there a way to work with BufferedImage in RGB mode and do this conversion to byte [] without having to get each pixel?

The source of my project http://breadmilkbeercigarettes.com/bmbc/shelves/users/bbb/src/java/SpinningTextureCube.php if you want to see the full thing.
It creates a new 6 textures every frame with the current time and uses them for cube sides.

Thanks,
BBB

CodeBunny

If you just want something to work without needing to do it yourself, I'd recommend lobotomizing the Slick Font classes. They're pretty useful.

Even if you do want to do everything yourself, to understand what's going on, I'd still recommend looking at the source code. One particularly nice feature is that they can handle font large enough to spill over onto multiple textures, which is really good if you don't want to constrict your font selection.

On another note, BufferedImage can support multiple color formats. It should be possible to use built-in functionality to adjust it, and then use that to create the texture.

bittramp

Thanks.  I was trying to see how feasible it was to generate textures over and over again.  Like for displaying a constantly updating score or character stats.  Unless I missed it somewhere, it seems strange for a gaming library to have no methods for displaying a score.

CodeBunny

Well, there are really two ways to manage fonts in games:

1: Cache the rendered text you want to display in some manner, which makes drawing it fast (e.g., using render to texture to pre-draw your text onto a quad (my personal choice), or using display lists). Fast, clean, and easy if it doesn't change, but if you need to alter the contents you need to destroy the old cache, set up a new one, render to it, and display it. This makes sense if you want to have a dialogue box with static paragraphs of text for the reader's perusal, but not for, say, displaying text as the user enters it.

2: Use a bitmapped font and draw every character of text every update. This has the advantage of not taking any time at all if you want to continuously change what's being rendered, but if you render a lot of text it'll take a while to draw. This is ideal for a score counter: you only have about 15 characters, most likely, so it's fast regardless.

Obviously, if you want to use 1 you need to figure out 2 - after all, you need to figure out how to render the characters in the first place.