![]() |
Multimedia »
GDI+ »
General
Intermediate
Smooth graphical fonts for your games or DirectX apps!By Matthew HazlettSmooth graphical fonts for your games or DirectX apps! |
VB, VC6, VC7, VC7.1.NET 1.0, Win2K, WinXP, Win2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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 :-)

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.
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.
LetterSpacing, SpaceSize, LeftPadding and RightPadding
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 and RightPadding 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 and Recolor
These are the colors you want your final image to be. You need to set the Recolor flag if you wish to force a new Foreground color on your image.
object.Background = color.transparent
object.Recolor = true
object.Foreground = redpicturebox1.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.
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()
Dim fntFromBmp As cGraphicFont = _ New cGraphicFont(new bitmap("font.bmp"), color.black) Rendered.Image = fntFromBmp.getString("Hello world") fntFromBmp.Dispose()
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.
For his article on ColorMatrix basics.
For his article on Hi-Res Timers.
That's about it for now, hope you enjoy this!
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 4 Jan 2004 Editor: Smitha Vijayan |
Copyright 2004 by Matthew Hazlett Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |