Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9

DirectDraw: Drawing Text
By: Jack Hoxley
Written: May 2000
Download: DD_Text.Zip (10kb)

This section goes hand-in-hand with the primatives section; they both involve drawing graphics without any pre-drawn textures or pictures. This is a very simple method, but extremely useful.

Step 1: Setting the Font attributes

This is the most important part of drawing text on the screen, you must set the font that is to be used and any other options (such as Bold, Italic and Underline). You use a built in class that holds all the information, you then pass the name of this class to directX and it will apply it to the surface for when you draw some text.

Dim FontInfo As New StdFont 'Note the use of "New" - This is very important.

backbuffer.SetFontTransparency True
backbuffer.SetForeColor RGB(0, 0, 200)
FontInfo.Bold = True
FontInfo.Size = 20
FontInfo.Name = "Verdana"
backbuffer.SetFont FontInfo

The StdFont variable is similiar to the surface descriptions that are used when creating descriptions. You fill them out with all the details and apply it to the surface.
The SetForeColor method was used in the Primatives tutorial - if you haven't looked at that I suggest that you do. The SetFontTransparency value can be useful at times, but by default it's better not to use it. If it is enabled, every letter is square shaped with the letter drawn in the middle of it - looks very ugly (like a really, really old computer), but i'm sure that there's a use for it somewhere... Should you want to use it though, you can set the colour of the background using SetFontBackColor.

A note on the choice of fonts. The fonts that are used are true-type fonts from the windows\fonts\ folder. If you use an "exotic" font - ie, not one that everyone has, you'll need to supply it with your application when you distribute it. If the font is not available on the computer when the application is run directdraw will use the default system font.

Step 2: Drawing some text

Okay, you can now set up the attributes for some text, now you'll need to draw it to the screen. This is an extremely simple thing to do, just use this line:

Surface.DrawText(x As Long, y As Long, text As String, b As Boolean)

The X and Y parameters are fairly easy - they indicate the top-left of the text. The text paremeter can either be a precompiled string or can be normal text entered "Inside quotation marks". the b value tells Directdraw to append this text to the previous text. If the X and Y values are 0 and b is set to true directdraw will add the text to the end of what was last drawn to the screen.

Call Backbuffer.DrawText(10,10,"The Variable is: " & SomeVariable, false)

Above is the best way of drawing text to the screen, although you don't need the Call....(...) parts they seem to stop some errors with variable declaration.

That is all there is to drawing text on the screen, you can download a project demonstrating (in code) how to draw text from the downloads page, or the top of this page

DirectX 4 VB 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster