Phoenix, Drawing Text on the Screen
Saturday, May 07 2005
Hello again, here I go with the next post in the Phoenix Series...
What I am going to go through today is a small wrapper I use to display test to the Managed DirectX Device. I know that this can be done with the standard libraries but as I am trying to build separate systems that I can use as a sort of engine for use in other projects I thought I would put together a wrapper. I have used this class in both a 2D system and a 3D system for displaying Debug Text and it works fine.
What I need for the project and future projects is a method to display plain text to the screen; this could be used for simple status messages or for debug text. For this project I am going to use it for Debug Text. Looking at what I wanted I decided that If I could set up the text fonts and colors at the beginning of the program and either send to a procedure the string I wanted displayed with the location to display it or set up a standard location in the beginning of the program and just pass the text to the procedure it would make it easier to work with.... so here it goes...
BTW: For those who would like to look at the code first I have posted the code for the class here.
Creating the DrawText Class
To start with create another class file for the project and call it DrawText.cs at the same time add your references for the main Managed DirectX Classes (Microsoft.DirectX, Microsoft.DirectX.Direct3D, and Microsoft.DirectX.Direct3DX). Next is to make the class inherit the IDisposable interface, this will allow us to clean up the objects once we are finished with them, to do this change the definition of the class to the following.
public class DrawText : IDisposableThe next step is to set up the main variables that we are going to use in the class, I am not going to go through these as I have commented the code as I have gone alone, and you should all be able to work it out. Add the below code just under the classes creation.
// Track whether Dispose has been called.private bool disposed = false;
// Font for the text to be Displayed in,
// Defaults to Arial 14 point
private System.Drawing.Font textFont = new System.Drawing.Font("Arial",8.0f,FontStyle.Regular);
private int xPosition = 0; // Default x position of the text
private int yPosition = 0; // Default y position of the text
private Color color; // set up the variable to hold the text color
// Set up the DirectX Font Objects
private Microsoft.DirectX.Direct3D.Font dxFont = null;
Now we need to add in the constructors for the class, in doing this I have created several different constructors so that this simple class can be used in many different types of applications.
/// <summary>/// Create the DrawText object and link it to the
/// DirectX Device
/// </summary>
/// <param name="device">DirectX Device to display the Text on</param>
public DrawText(Device device)
{
this.dxFont = new Microsoft.DirectX.Direct3D.Font(device,textFont);
} /// <summary>
/// Create the DrawText object and link it to the
/// DirectX Device
/// </summary>
/// <param name="device">DirectX Device to display the Text on</param>
/// <param name="xPos">X Position on the DirectX Device</param>
/// <param name="yPos">Y Position on the DirectX Device</param>
public DrawText(Device device, int xPos, int yPos)
{
this.xPosition = xPos;
this.yPosition = yPos;
this.dxFont = new Microsoft.DirectX.Direct3D.Font(device,textFont);
} /// <summary>
/// Create the DrawText object and link it to the
/// DirectX Device
/// </summary>
/// <param name="device">DirectX Device to display the Text on</param>
/// <param name="xPos">X Position on the DirectX Device</param>
/// <param name="yPos">Y Position on the DirectX Device</param>
/// <param name="c">Color of the text to be drawm</param>
public DrawText(Device device, int xPos, int yPos, Color c)
{
this.xPosition = xPos;
this.yPosition = yPos;
this.color = c;
this.dxFont = new Microsoft.DirectX.Direct3D.Font(device, textFont);
}
And the final part of the main code is as follows, the draw functions.
/// <summary>/// Draw Text onto the DirectX Device using the
/// positions that were entered at Construction.
/// </summary>
/// <param name="DisplayText">Text to Display on the Screen</param>
/// <param name="c">Color of the Text to be Displayed</param>
public void Draw(string DisplayText, Color c)
{
dxFont.DrawText(null, DisplayText, xPosition, yPosition, c);
} /// <summary>
/// Draw the Text onto the DirectX Device, using the
/// Co-ordinates and color supplied
/// </summary>
/// <param name="DisplayText"></param>
/// <param name="x">X Position on the DirectX Device</param>
/// <param name="y">Y Position on the DirectX Device</param>
/// <param name="c">Color of the Text to be Displayed</param>
public void Draw(string DisplayText, int x, int y, Color c)
{
dxFont.DrawText(null,DisplayText,x,y,c);
}
Before I do finish up with this code I also need to add in the code that will implement the dispose functions for the class.
// Implement IDisposable.// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
} private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
this.dxFont.Dispose();
this.textFont.Dispose();
this.xPosition = 0;
this.yPosition = 0;
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
}
disposed = true;
} ~DrawText()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
Using the DrawText.cs Files.
There, Done. Close of the class and namespace and the class is complete. To use this class to me is pretty simple after adding it to the project all you need to do is create the text object that you want to use, then in your render loop use the draw function to draw it onto the screen.
Add the following line to the code just under where you have created the DirectX Device,
private DrawText DebugText; // Drawtext Object for Debug TextNext inside the Initialize Graphics Function add the following line of code to create the object and set up the defaults. Now I know that the class has some extra constructors but in this example I am going to only use the one for the moment.
// Set up the Debug Text ObjectDebugText = new DrawText(device);
The last step to using the class is to add the following lines of code to the render loop for the application. Remember that if you draw this first the graphics that you draw from that point on will draw over the top of the text, so if you do want this for debug text (As I do Here) add the code just before you do the end scene call.
DebugText.Draw(tempString, 10, 10, Color.White);That should be it, if you run the application now; you should see that background as well as a Debug text message on the screen.
Finishing Up.
Ok that is it with this post for the moment. I do have some more that will hopefully start to come quickly over the next week or so, these include the animated Background (Which I have working and it runs well), and Changing the Input to use DirectInput instead of over riding the on key events for the application. If anyone would like me to post the code before I complete the articles drop me a line in the forums or here and I will.
Similar Posts
- Phoenix, Adding to the Background
- Phoenix, Setting up the DirectX Device
- Phoenix, Drawing the Main Background for the Project
