Jump to content
  • Advertisement
Sign in to follow this  

DirectX Text/Font Wrapper Code

This topic is 4931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not sure if this is the right place to post this, but I am anyway... I got tired of writing loads of code to create fonts and write text on-screen, so I wrote these 2 functions to create a font and draw text on-screen in 3 lines. The first: CreateD3DFont
Public Function CreateD3DFont(FontName As String, Size As Long, Bold As Boolean, Italic As Boolean, Underline As Boolean, Strikethrough As Boolean) As D3DXFont

    Dim NewFont     As New StdFont 'new font definition
    Dim TempFont    As IFont 'temporary font
    With NewFont 'fill the font definition with data from arguments
        .Name = FontName
        .Size = Size
        .Bold = Bold
        .Italic = Italic
        .Underline = Underline
        .Strikethrough = Strikethrough
    End With
    Set TempFont = NewFont 'copy the font definition to a temporary font
    Set CreateD3DFont = D3DX.CreateFont(D3DD, TempFont.hFont) 'create and return the DirectX Font from the temporary font
    Set NewFont = Nothing 'destroy the font definition
    Set TempFont = Nothing 'and the temporary font
End Function
To use it: Dim RandomFont As D3DXFont 'declare your font Set RandomFont = CreateD3DFont("Times New Roman", 14, True, False, False, False) 'create the font using the CreateD3DFont function. And now for drawing it on-screen...DrawText.
Public Sub DrawText(sText As String, fFont As D3DXFont, Colour As Long, LeftPos As Long, TopPos As Long, Format As CONST_DTFLAGS, DestroyFont As Boolean)

    Dim TextRect    As RECT
    TextRect.Left = LeftPos 'the left position of where the text will be drawn
    TextRect.Top = TopPos 'the top position of where the text will be drawn
    'draw the text using TextRect to specify position
    D3DX.DrawText fFont, Colour, sText, TextRect, Format Or DT_NOCLIP
    If DestroyFont = True Then
        'if font was created inline using CreateD3DFont
        '(unadvisable but there for a quicky) then destroy it
        Set fFont = Nothing
    End If
End Sub
and to use it simply insert this into your rendering function: DrawText "Text message", RandomFont, D3DColorXRGB(255, 0, 0), 10, 10, 0&, False You can use the two together like so... DrawText "Message", CreateD3DFont("Arial", 12, False, False, False, False), D3DColorXRGB(128, 128, 128), 10, 10, 0&, True using inline font creation and destruction, but i doubt it's a good idea to create and destroy a font 60 times per second. Anyway, let me know what you think. Degra

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!