[SDL] custom BMP font engine
sdl at elmerproductions.com
Thu Jan 30 12:30:01 PST 2003
At 2003-01-30 01:16, you wrote:
>Last night I threw this together as a custom BMP font displayer that I
>could use in a game I'm working on. The BMP file has already been loaded
>onto a surface "pFont12g". The BMP was 12px high and 10px wide per
>letter. The image starts with a blank space ASCII 32 and goes up to ASCII
>126. The function scans each character of the string submitted to be
>displayed and sets the x pos of the Src Rect based on the ASCII value. I
>also check for '\n' and effectively create a newline. It works really
>well, but I'm wondering if there is a faster way to do this? I'm also a
>little shaky with pointers, so I created a duplicate of the message and
>manipulate that rather than accidentally frying the original (is this
>needed or just my own fear?).
What I did in my game is something similar. However, I added an array of
character widths, so effectively I created a proportional font (depending
on the way you use the font it may be an improvement or not).
To get one, you can (for example) scan each vertical line of pixels for
each character, to see the vertical line that contains the last pixels,
then that is the width of that character. Something like this (very quickly
written pseudo-code, but you get the idea):
if (pixel_in_char(ch,x,y) != blank)
Note that you might want to take special care of the space to make sure it
doesn't end up as zero-length. In addition I tweaked my code to display all
digits at the same width. (Actually, I have prepared the character widths
and just read them from a datafile).
> const char * msg = message;
> for(int i=0;i<strlen(message);i++)
You can change the 'for' to:
it is more efficient than checking strlen every loop, and easier than
storing the strlen() value beforehand... The rest looks pretty decent.
More information about the SDL