Code Hackery #2: Characters on a Texture

Alright so I said every week, but I really meant week-ish. To be honest, I’ve had some trouble actually finding some nice code to put for this post, so I’m going to more explain something I recently did with the Wurm client. I cannot post the actual code for NDA reasons, but I’ll give some examples.

This week is a bit of easy code I whipped up for the Wurm client when I was working on replacing the old, notoriously slow text rendering code. I originally had the FontTexture object drawing all of the ASCII characters to a texture, and believed that would be enough (and in terms of performance, it would’ve been), but this also meant that any other characters that were previously supported – cyrillic, greek, etc – would either crash everything, or just show nothing.
 
In Wurm there are plenty of different people playing, plenty of different languages, and most of those languages need more than the basic ASCII characters, so leaving it as is wasn’t really an option.

 

The problem with drawing all of the Unicode characters to the texture though is limits on texture size. The Wurm client tries to be very friendly to old set-ups, so there is an option in there for max texture sizes of 256×256. Obviously this cannot handle all 65536 characters of unicode, so I had to pick and choose what characters would be drawn on the texture, and also prioritize them in case the texture was still too small to hold them all – leaving the old text rendering system in place to handle any extra characters that cannot fit on the texture but are later needed.

The way I got around this (without hard-coding each individual int value of the characters I needed) was to make a 2D integer array to house the blocks of characters I would want to try and load onto the texture.

 

private static final int[][] CHARACTERS_TO_LOAD = new int[][] { { ASCII_START, ASCII_END }, { CYRILLIC_START, CYRILLIC_END }, 
                             { GREEK_START, GREEK_END }, { LATIN_EX_START, LATIN_EX_END } };

 

When the method to load the characters onto the texture image runs, it loops through that array, grabbing out each int, casting it to a char, and draws it onto the texture for later use. If the character is going to be rendered off the end of the texture, the method returns, and leaves out the rest of the characters.
 
Doing this means that all of the ASCII characters will always be on the texture, allowing for faster text rendering times in the client for the main part. When a special character is needed, it will check to see if the character is on the texture first, and if not fall back to the old text rendering system (which is a lot slower, but will not be used much since most of the text rendering will be ASCII.

Tagged , , , , , . Bookmark the permalink.

Leave a Reply