Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 DirectX9.0c DrawText (Alternative)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Knight Chat X
Squire

USA
24 Posts

Posted - Nov 20 2004 :  2:23:49 PM  Show Profile  Visit Knight Chat X's Homepage  Reply with Quote

When I converted to DirectX 9.0c ran into the same problem with DrawText as others have had, it's very strict with parameters, while working with font declarations I finally got it to work as expected, when I finally got it down to where the syntax worked, the program ran but didn't display text, I later found this to be because of the DrawTextFormat settings, some settings will render the font while others won't. I've moved onto other things and now I can't find the class I made to work with DrawText easier, but even when things were going well I realized DrawText was incredibly impacting my frame rates slowing down and not doing well with memory performance, and the more text displayed the worse it was so I went in search of an alternative and considered using a generated text mesh, and behold it works like a charm.

Basically it's setup like this:

'Global Variables.
Private mesh3DText As Mesh = Nothing 'Mesh to draw 3d text.
Private fontName As String = "Arial" 'Name of the font you wish to use.
Private FontSize As Integer = 10 'Size of the font.
Private textMaterial As Material 'Material to color text.
Private matFont As Matrix = Matrix.Identity 'Matrix to position and scale. text.
Private currentFont As System.Drawing.Font 'Variable that stores the font.

'Put this code where your Direct3D device is initialized.
'Create a new font using the given parameters..
currentFont = New System.Drawing.Font(fontName, FontSize)

'Create 3D Text Mesh.
mesh3DText = Mesh.TextFromFont(mobjDX9, currentFont, "This is a test message...", 0.001F, 0.4F)

'Create the material that will be used for the mesh.
textMaterial = New Material
textMaterial.AmbientColor = New ColorValue(0, 16, 180, 255)
textMaterial.DiffuseColor = New ColorValue(0, 16, 180, 255)

'Now put this in the render portion of the program to render the text.
'Set text mesh rotation.
Dim matRot As New Matrix
matRot.RotateYawPitchRoll(0, 0, 0) 'XYZ

'Set text mesh scale.
Dim matScale As New Matrix
matScale.Scale(5.0F, 5.0F, 1.0F) 'XYZ (Note: To change font depth change Z, a higher value will result in more 3D look while lower results in a flatter 2D look.)

'Set text mesh position.
Dim matPos As New Matrix
matPos.Translate(-20, -10, 0) 'XYZ

'Combine all matrix calculations.
mobjDX9.Transform.World = Matrix.Multiply(Matrix.Multiply(matRot, matScale), matPos)

mobjDX9.Material = textMaterial 'Set material for render.
mesh3DText.DrawSubset(0) 'Render Mesh/DrawText.

:cool:
:D

The net is getting bigger and bigger.

Knight Chat X
Squire

USA
24 Posts

Posted - Nov 23 2004 :  01:47:25 AM  Show Profile  Visit Knight Chat X's Homepage  Reply with Quote
In the last post it might be a little difficult to get font size accuracy settings just right, and the method works better for larger sized fonts or awkward fonts such as for a comic style game like Spiderman or Hulk where accuracy and a solid look is not an issue.

Now if you want straight DrawText method I've come up with a simple class you can use, it contains basic size changes and 2D positioning which should be adequate for displaying text, the default font is Arial, feel free to modify the code to add more complicated font features.

'Simple DirectX 9.0c DrawText Class


#Region "Imports"
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Direct3D = Microsoft.DirectX.Direct3D
#End Region

Public Class clsDrawText

#Region "Variables"
Private customD3DFont As Microsoft.DirectX.Direct3D.Font
Public fontName As String = "Arial"
Public FontSize As Integer = 10
Private FontHeight As Integer = -10
#End Region

#Region "Create New Font"
Public Sub CreateFont(ByVal devD3DDevice As Device, Optional ByVal intFontSize As Integer = 10)
FontSize = intFontSize
FontHeight = -intFontSize
customD3DFont = New Microsoft.DirectX.Direct3D.Font(devD3DDevice, FontHeight, 0, FontWeight.Bold, 1, False, CharacterSet.Default, Precision.Default, FontQuality.Default, PitchAndFamily.DefaultPitch Or PitchAndFamily.FamilyDoNotCare, fontName)
End Sub
#End Region


#Region "DrawText"
Public Sub DrawText(ByVal strText As String, ByVal X As Integer, ByVal Y As Integer, ByVal col As Color)
customD3DFont.DrawText(Nothing, strText, X, Y, col)
End Sub
#End Region
End Class


To Use:

'Put this in the global variables section of your main window.
Private objDrawText As New clsDrawText 'Used for drawing text.

'Put this where your Direct3D device initialization code is.
objDrawText.CreateFont(device) 'Create the font. (device is your Direct3D device.)

'Put this in the render section.
objDrawText.DrawText("This is just a test message...", 1, 10, System.Drawing.Color.White)

That's it, enjoy!





The net is getting bigger and bigger.
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Nov 23 2004 :  11:10:10 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
Thanks for posting that! You can also use the [ code ] tag so that the text is automatically formmated with the default colors.
Go to Top of Page

salvapatuel
Neophyte

United Kingdom
1 Posts

Posted - Dec 15 2004 :  3:59:08 PM  Show Profile  Click to see salvapatuel's MSN Messenger address  Reply with Quote
thanks!

I was having problems with DrawText clearing my sprites and flickering the screen sometimes.

Your function just works :)

thanks
Salva
Go to Top of Page

Knight Chat X
Squire

USA
24 Posts

Posted - May 20 2005 :  01:41:45 AM  Show Profile  Visit Knight Chat X's Homepage  Reply with Quote
It's been awhile since I made this post but I forgot to mention that one cool thing is that you can re-use the DrawText method to render multiple modified versions of the text without needing to create more objects, you can create the font object once then do multiple renders, for example:

I want text to show at 1,10 with the color of white:
objDrawText.DrawText("This is just a test message...", 1, 10, System.Drawing.Color.White)

Ok, but now I want to draw different text in another place in a different color but using the same object:
objDrawText.DrawText("This is another test message...", 1, 50, System.Drawing.Color.Green)

The same concept can be used for normal mesh objects, if I'm correct, the ability to re-use objects like this could save resources, as you can make modifications to an already loaded object that only displays the changes on a per render basis without requiring loading extra copies into memory, the effect is desired changes are seen visually on display but not done internally. Maybe like a brush!

The net is getting bigger and bigger.

Edited by - Knight Chat X on May 20 2005 01:46:54 AM
Go to Top of Page

Almar
Moderator

Netherlands
192 Posts

Posted - May 28 2005 :  4:52:26 PM  Show Profile  Visit Almar's Homepage  Send Almar an ICQ Message  Reply with Quote
Hm, I'd like to point out that DX9 has some kind of caching option. The CustomUI sample uses it, and keeps the FPS high. I ported the CustomUI sample to DX8, which lacked that option. Therefore I got poor performance.

I think it buffers the ext on a sprite object or so.
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - May 28 2005 :  5:54:38 PM  Show Profile  Reply with Quote
Cool, Almar's still alive

That remembers me... I need to talk to you about YALG... like how much success did it have?

I think I have the solution for YALG to be widespread


Whatever. Who knows...
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.14 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.