5,660,782 members and growing! (14,617 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Beginner

Creating a HUD In Direct3D / C#: Part 1 - Text

By James Gupta

Teaches you how to create a HUD in Direct3D / C# - this first part goes through the basics of creating and rendering text
C#.NET 2.0, WinXP, Windows, .NET, WinForms, DirectX, Visual Studio, VS2005, Dev

Posted: 22 May 2006
Updated: 22 May 2006
Views: 32,118
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 1.54 Rating: 1.48 out of 5
8 votes, 72.7%
1
0 votes, 0.0%
2
1 vote, 9.1%
3
0 votes, 0.0%
4
2 votes, 18.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

In this series of tutorials, we will create a HUD (Heads-Up Display) in DirectX - this ranges from text, to statistics (FPS, time etc) and then onto more advanced controls such as buttons and textboxes.

Background

The code from these tutorials has been either taken from my Tac-Man game project or created specially for this siste

Using the code

You may use all of the code I provide as how you see fit, except to create another tutorial. You can use it as a sturdy(ish ;)) framework for your own applications, or print loads of copies off so that when I'm rich and famous you can sell them for $100 each ;)

Prerequisites

To do this series of tutorials, you will need:

  • C# Compiler (preferably Visual C# 2005 Express)
  • Managed DirectX 9.0 October SDK

OK, you will need to have completed the base tutorial series or at least have framework capable of setting up Direct3D. It is strongly advised that you at least read the first couple of tutorials from the "Beginning 2D game programming" section to get an understanding of some of the concepts I assume you understand in this tutorial.

Displaying Information

.NET provides a plethoria of GUI objects such as buttons, checkboxes and even calendars etc. However, in DirectX applications, not only are these ugly in a game, but it is also impracticle if not impossible because of DirectX's inability to share... So, most game-developers create their own HUD's, specially designed and coded for that application. In this tutorial we will simply learn how to create a class to display a string to our screen.

First, we obviously need a new class to go in the engine.

In DirectX, we use a sprite to render text, using a font object to define how the string is displayed. We also have other variables defining the text's style (bold, italic etc), size (defined within a rectangle) and color.

So, our class must contain all of the data used to display one "message", and the methods to initialise and render it. In this way, it will be similar to our Sprite class (assuming you have done the 2D tutorials).

public class hMessage
{
//The text to be displayed by the message
public string Text;
// The 2D location (on screen) of the message
public System.Drawing.Point Location;
//The actual font object to use
public Font Style;
public System.Drawing.Color Color; // The color of the text
public hMessage ( Device dev, System.Drawing.Size size, 
                  FontWeight weight, string fontname, 
                  string text, System.Drawing.Point loc,
                  System.Drawing.Color col )
{
Style = new Font ( dev, size.Height, size.Width, weight, 0, false, 
        CharacterSet.Default, Precision.Default, FontQuality.Default, 
        PitchAndFamily.FamilyDoNotCare, fontname );
Text = text;
Location = new System.Drawing.Point ( loc.X, loc.Y );
Color = col;
}
public void Render ( Sprite spr )
{
  Style.DrawText ( spr, Text, Location, Color );
}
}


So, not an overly complicated class, we have the text to display, its location on the screen, the style of font to be used and the color. As you can see, the render function is a wrapper for the Font.DrawText() method, which simply uses a sprite object and other variables affecting a font and displays it, very similar to the Sprite.Draw2D method, except the sprite is taken as an argument instead of being the calling object.

With this class implemented, build your engine and create a new windows application called HUD_1. Do the usual stuff, delete the designer file, have the window class inherit from Engine.gameWindow instead of the standard System.Windows.Forms.Form and change the simple Application.Run() to: 

using (Form1 gameForm = new Form1())
{
  Application.Run(gameForm);
} 
Now, back in your form class, replace the body of it's constructor with a simple call to our Initialise() method, using the standard settings (windowed = true). Add an hMessage object to our class, this will naturally be the object used to display our text.
Attatch an Paint event handler, and add the generic D3D stuff to it, this is:
device.BeginScene ( );
device.Clear ( Microsoft.DirectX.Direct3D.ClearFlags.Target, 
               Color.Black, 1.0f, 0 );

device.EndScene ( );
device.Present ( );
this.Invalidate ( );
The purpose behind doing this has already been explained in previous tutorials, so I will not go into it in detail here (for more information look at Beginning 2D Game Programming, Tutorials 1-2). Now, after the Clear() call, we need to add a function to draw our text. Pop in a sprite object to our class definition (I will call mine aSprite, nice and unique), and add the following line after Clear():

aMessage.Render ( aSprite );

Now, the only thing we need to do is initialise these members. In the Main class' using body, before the call to Application.Run(), initialise our sprite, the only member here is a link to the form's inherited Microsoft.DirectX.Direct3D.Device object, and then initialise our font member. As we created this class, you should understand the process, but for an idea of sizing / location etc I will provide the call used in the sample below:
gameForm.aMessage = new hMessage ( gameForm.device, 
         new System.Drawing.Size ( 48, 40 ), 
         FontWeight.Normal, "Ariel", "HELLO \nWORLD", 
         new System.Drawing.Point ( 50, 400 ), 
         System.Drawing.Color.Yellow ); 

You should now be able to run the application. I advise you to use this as a base to start playing around with different locations / sizes to give you an idea of the call.

See you in Tutorial 2, where we will learn to use / modify our class to display an application's "Vital Statistics", such as elapsed time, lives, and even graphics card information.


 For more Managed DirectX Tutorials on HUDS, 2D and 3D game programming and heightmapping, please visit my site http://www.just-code-it.net

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

James Gupta


I live in England, UK in a small town which only just got broadband...

In my spare time, I run a small website design, hosting and maintenance business at www.jamesgupta.com
Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralMountain out of a mole hillmemberbamboozle16:03 12 Jan '07  
GeneralRe: Mountain out of a mole hillmemberArthur M.14:08 20 Feb '08  
GeneralAny pre-built HUD images?memberJun Du6:19 23 May '06  
GeneralRe: Any pre-built HUD images?memberJames Gupta10:19 23 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 May 2006
Editor:
Copyright 2006 by James Gupta
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project