Smooth graphical fonts for your games or DirectX apps!






3.55/5 (9 votes)
Jan 5, 2004
3 min read

73960

809
Smooth graphical fonts for your games or DirectX apps!
Introduction
This is another major and final update to my graphicFonts
class. New in this version are shadowing, color adjustments, padding, spacing, user interface and more...
First off, you need to be able to make font files. You can get the app I have been using here.
Important: Stay away from converting font files to GIFs. During GIF compression, the colors in the image are shifted to achieve compression and as a result, they will not work properly.
Just use PNG :-)
Class Updates
- Shadowing
- Adjustable Foreground Colors
- Optimized Code
- Adjustable Shadow Offsets
- Adjustable Render Size
- Adjustable Left and Right Padding
- Adjustable space size
- More comments and Cleaner code
Interface Updates
- Hi-Res Timer for performance tuning
- New User Interface
- Separate preview window
- Ability to save rendered fonts
Sample Bitmap file
Coding Challenge
If you can shave time off the rendering process, you will be forever known as the code guru. I have the rendering time pretty good but I think it could be better. If you think you can improve performance, update the class with your ideas and email it to me. I will update this article and give you credit at the end. So are you a coding guru?
Render "abcdefghijklmnopqrstuvwxyz" with font1.png, my times are between .6 - .552.
Times may vary by system.
Class Constructor
Dim fntFromBmp As cGraphicFont = New cGraphicFont(fontFile, bgColor.Color)
This initializes the cGraphicFont
object and passes the active form, bitmap image of the fontFile
and the background color of the font file.
Updated: Got rid of the need to reference the parent form.
Class Properties
LetterSpacing
,SpaceSize
,LeftPadding
andRightPadding
This specifies spacing.
LetterSpacing
is the space between letters,SpaceSize
is the number of pixels to use if your string has a space in it,LeftPadding
andRightPadding
adjust the spacing on ether side of the output bitmap.object.LetterSpacing = 5 object.SpaceSize = 15 object.LeftPadding = 15 object.RightPadding = 15
CellsAcross
This specifies the number of horizontal cells in your image file.
CellsDown
This specifies the number of vertical cells in your image file.
object.CellsAcross = 16 object.CellsDown = 16
Offset
This is the cell number in the image you want to start reading from. Suppose your file has 2 different fonts in it. With this method, you can control the position (Cell) in the bitmap where you want to start.
Updated: Renamed this property from "FontNumber"
object.Offset = 128
FirstLetter
FirstLetter
is the letter your font bitmap starts with. Most font bitmaps start with a space.object.FirstLetter = " "
renderSize
This is the size to ether grow or shrink the resulting image. (E.g.
bitmap.width * txtSize, bitmap.height * txtSize
).So a value of 1 will render the image in the normal size. A value of 2 will double the resulting image size. Conversely, a value of .5 will shrink the resulting bitmap.
Updated: Renamed property from txtSize.
object.txtSize = 1 ' Normal Size object.txtSize = 2 ' Double Size object.txtSize = 3 ' Triple Size object.txtSize = .5 ' Half size object.txtSize = .25 ' Quorter size
Shadow
&ShadowOffset
With this property, you can cast a shadow on your rendered text.
object.Shadow = true object.ShadowOffset = 2
Background
,Foreground
andRecolor
These are the colors you want your final image to be. You need to set the
Recolor
flag if you wish to force a newForeground
color on your image.object.Background = color.transparent object.Recolor = true object.Foreground = red
Class Methods
picturebox1.image = fntFromBmp.getString(RenderText.Text)
This returns a Bitmap
object with your string. The first parameter is the text you want and the next parameter is the background color you want.
Updated: Don't specify the color anymore.
picturebox1.image = fntFromBmp.getCell(celNumber)
This returns a FORMATTED cell as a bitmap.
Updated: Now returns a formatted cell.
Example
Dim fntFromBmp As cGraphicFont = _
New cGraphicFont(new bitmap("font.bmp"), color.black)
fntFromBMP.Shadow=true
fontFromBMP.ShadowOffset = 2
fntFromBmp.LetterSpacing = 5
fntFromBmp.CellsAcross = 16
fntFromBmp.CellsDown = 16
fntFromBmp.FontNumber = 0
fntFromBmp.FirstLetter = " "
fntFromBmp.RenderSize = 1
fntFromBmp.Background = color.Navy
fntFromBmp.foreground = color.red
fntFromBmp.Recolor = true
Rendered.Image = fntFromBmp.getString("Hello world")
fntFromBmp.Dispose()
Example #2
Dim fntFromBmp As cGraphicFont = _ New cGraphicFont(new bitmap("font.bmp"), color.black) Rendered.Image = fntFromBmp.getString("Hello world") fntFromBmp.Dispose()
Tweaking
The private variable ScanLines
in the class controls how many points are used to scan the bitmap. Setting it lower will speed things up but, if you set it to low, you will cause an error in the class.
Credits
- Michael Combs
For his article on ColorMatrix basics.
- Alastair Dallas
For his article on Hi-Res Timers.
That's about it for now, hope you enjoy this!