Skip to content

HUD Tutorial Part the Second: Getting the HUD to work in a new project

 Prototype By: Priyesh Dixit (ScreenObject) and Hunter Hale (HUD)
Engineering By:
Amanda Chaffin
Code Review By: Jason Hardman
DLL and Docs By: Amanda Chaffin and Jason Hardman
 

Summary:

The HUD DLL is an extensible heads up display for XNA platform games that can be used for any type of single player game. Self contained, users merely add the DLL, a couple of references, some method calls and the HUD appears on the screen, ready to go. Of course, you will still need to change the graphics, the text, and build in math for things like the health bar but this system is easily integrated into any XNA game of any type.

Download:

The zip files for the HUD

The Demo project: Requires XNA, Visual Studios, and DirectX SDK to run

All instructions that follow will be based on your use of the game1.cs class (which is renamed to HUDDemo in my images.  The first thing that you need to do is open your game1.cs class and add your using statement as shown in Figure 3.

Figure 3: Using Statement

Figure 3: Using Statement

Oh, but there is so much more to do!

After you have added the using statement, you have to declare the HUD, a viewport, and a string with the CurrentDirectory location in it (Figure 4).

Variable Declaration

Figure 4: Variable Declaration

Now, drop down to your LoadContent() method in your game1 file (or whatever your top level class is) and initialize your variables (see Figure 5).  The reason that we have to initialize in LoadContent() instead of in the traditional Initialize() is because we need the graphics.GraphicsDevice.Viewport to exist, which it does not exist until after all the Initialization() calls have finished.

You need to get the viewport from the GraphicsDevice, add the file name of the HUD xml file to the filename string, and then declare the new HUD, passing in the game, the game’s spriteBatch, the viewport, and the filename (which, if you did a plus equals [+= ] and put the HUD.xml file in the correct location, will allow the HUD to find the file in the debug folder – don’t worry, this does work on an Xbox as well).

Figure 5: Load Content

Figure 5: Initialize Content

 
The last thing that you need to do is tell the HUD to draw itself so drop down to your Draw call and call the HUD’s draw call (Figure 6).

Figure 6: Draw Call

Figure 6: Draw Call

Hit RUN and you should see something like Figure 7.

Figure 7: The Game window with the HUD Displaying

Figure 7: The Game window with the HUD Displaying

{ 1 } Comments

  1. Arianamula | May 13, 2009 at 11:22 PM | Permalink

    beautiiful blog merciiiiii

{ 1 } Trackback

  1. WulfKat : HUD Tutorial | March 16, 2009 at 8:56 PM | Permalink

    [...] Part the Second: Getting the HUD to work in a new project. [...]

Post a Comment

You must be logged in to post a comment.