This tutorial is the second part in a two part series on how to use DirectFont, a utility created by myself, and available on the Downloads page. DirectFont is broken into two distinct processes...creating the font collections and then using them in your DirectX application. This tutorial will cover the second half of DirectFont...Using Collections in your DirectX Apps.
Contents
There are a couple things that need to be set up on your computer in order to start using DirectFont.
| Method 1 | Method 2 | ||
|
Dim moDirectFont As New DirectFont |
| Method | Parameter | Parameter Desc. | Method Desc. | ||
| Initialize As Boolean | poSurface As DirectDrawSurface7 | The DirectDraw surface to draw on. This is most likely the back buffer, but it can be the primary surface. |
|
||
| [poDirectDraw as DirectDraw7] | Optional. The instace of DirectDraw to use to do all the operations. If this is Nothing, the DLL creates it own. Default value is Nothing. | ||||
| [pbBltFast As Boolean] | Optional. Whether to use BltFast when blitting or not. If this is False, Blt will be used instead. Default value is True. | ||||
| LoadCollection As String | psCollectionFilePath As String | The location of the collection file. |
|
||
| ReleaseCollection As Boolean | None |
|
|||
| EnumerateFonts As String | None |
|
|||
| SetCurrentFont As Boolean | psCurrentFont As String | The font name to set current. This function performs a case-insensitive trimmed search throughout the collection to find the correct font. |
|
||
| [pbUseTransparency As Boolean] | Optional. Whether to use transparency or not. Default value is True. | ||||
| [piZoomPercent As Integer] | Optional. The Zoom percentage to set for your font (only works with Blit and not BltFast (see Tips and Tricks for more information)). Setting this value will increase or decrease your characters by equal horizontal and vertical amounts. It must be (or will be forced to) a value between 50 and 200. Default value is 100 (100% or no zoom factor). | ||||
| DrawText As Boolean | plLeft As Long | The left coordinate of where to Blit the first character in the string. |
|
||
| plTop As Long | The top coordinate of where to Blit the first character in the string. | ||||
| psText As String | The actual string to Blit to the screen |
First you have to instantiate a DirectFont object. The best place to do this is the same place you instantiate your othere DirectX objects (most likely in your Form_Load event). To do this, you can use one of the methods used in a previous section (but I would suggest the first method).
| <1> | Set moDirectFont = New DirectFont |
This is pretty self explanatory...we're just creating a new instance. One really weird problem I've found, is that you have to instatiate your DirectFont object before your DirectX object. Get more information on this in the Tips and Tricks section below.
Next you have to initialize the object. In this step, I usually stick the two methods together since it makes sense to do so, however, if you are using mulitple collections for your one app, you might want to stick the LoadCollection call in a seperate function that determines which collection to load.
| <2> | 'Initialize DirectFont |
| <3> | Call moDirectFont.Initialize(moBackBufferSurface, moDirectDraw) |
| <4> | Call moDirectFont.LoadCollection(App.Path & "\fpstext.fnt") |
In line 3, moBackBufferSurface is the DirectDraw surface that I am blitting my characters to, and moDirectDraw is my instance of DirectDraw that I am passing to DirectFont to use. I usually pass it an instance of DirectDraw, so it doesn't have to create it's own (however, I left the functionality in there in case someone wanted it). I didn't specify the pbBltFast parameter, so I am letting it use it's default...BltFast.
In line 4, we load the collection. The only parameter specified is collection file's location. I've simply left my collection exposed in the applications directory, but your can store it your own Binary File Format (see one of Lucky's tutorials on how to do that), extract it to a temp file, and then load it, then remove the temp file.
Now we actually use the DirectFont object. Right before our Blit loop, we determine what font we are to use (based on the particular screen, let's say), and then we set our current font. It goes something like this...
| <5> | 'Select the current font |
| <6> | Call moDirectFont.SetCurrentFont("FPSText", True, 50) |
In line 6, we select our font. In my app that I was testing this out with, I was using the text to display a FPS counter. So I created a collection called fpstext.fnt (as in line 4), which contained one definition...FPSText. So here is where I specify the name. Now yes, it would be better programming practice to stick "FPSText" into a constant and use that contstant, but that would involve more HTML ;). The second parameter (although I really didn't need to specify it since it was optional) tells DirectFont to use the transparency in the definition. This will most often be the case (that's why it has a default value of True). The last parameter tells it to use a zoom of 50%. This will shrink the character in half when blitted. However, since I used BltFast earlier, this parameter is ignored when I do the actual blitting.
Here's the method you've all be waiting for. Somewhere in our Blit loop stick this line to draw the text to the screen.
| <7> | 'Draw the text to the screen |
| <8> | Call moDirectFont.DrawText(50, 100, miFPS & " FPS") |
Line 8 is the one which does all the work. The first and second parameter specify the starting coordinate for the string...in our case, start the top left corner of the string at (50,100). The third parameter is our actual string. In this case it concatenates the integer FPS counter with the string " FPS" and sends that to the method. The end result would be something like "64 FPS" in the letters of our font definition at coordinates (50,100). Pretty simple, eh?
And finally clean up. These lines would go in with all of your other cleanup code (unaquiring DirectInput objects, setting all DirectX objects to nothing, etc.)
| <9> | 'Unload DirectFont |
| <10> | Call moDirectFont.ReleaseCollection() |
| <11> | Set moDirectFont = Nothing |
And that's it..not bad for 11 lines of code. I was able to implement a graphical FPS counter that didn't lock up the surface every time I drew it to the screen, and it took me only a few minutes to add the logic.
Please feel free to contact me with any comments or questions about this tutorial or the tools used.
Previous Part - [1 2] - Next Part