The Source engine is a 3D game engine developed by Valve Corporation. Its unique features include a large degree of modularity and flexibility, an artist-driven, shader-based renderer, accurate lip sync and facial expression technology, and a powerful, efficient and completely network-enabled physics system.
This tutorial shows you how to change the HUD by adding a graphic. Adding a graphic to the HUD isn't that difficult. However, it is recommended that you know your way around the Material Creation process.
Posted by bob_gneu on Nov 28th, 2008
Basic Client Side Coding.
In general, adding graphics to the HUD follows the same procedure as adding any material to Source-based games, so create your graphic at your own discretion. The difference is that we are going to give vtex some extra parameters in the .txt file accompanying the .tga.
We will use the following setup as an example.
In this case, we are telling vtex...
Do not make mip-levels for this texture. Simply because you will not be viewing the HUD from varying distances.
Do not use compression on this texture. This prevents artifacting in our graphic.
Do not use lower quality versions of this texture in lower DirectX versions. This is obvious as the difference in performance is marginal.
Once you have the appropriate variables set up, we are now going to run vtex. It is recommended for clarity that you generate your graphics in the /materials/HUD/ directory (if it does not exist, create it).
For the .VMT, we are going to use the following setup.
With the .vmt and hence the material completed, all that's left is to change the HUD and add a class. Let's start with the class.
Open up your copy of MSVS. Do a search for class CHud. You should be able to cycle through a number of files. I recommend you keep all these stock and if you edit, back them up so you can keep them for reference.
All we are trying to do is put a graphic on the screen. In Half-Life 2 there is a function we have to call in order to get this to happen, called paint. We are going to need a variable to act as a place holder for our sprite so that we can call it in the game via the paint function. It would also be nice to be able to turn this on and off, so we are going to learn how to use cvars to control HUD elements in this way.
Here is our example class definition.
Drop the above code into hud_import.h. Remember, the simpler you keep things now, the easier it will be to understand your code later if you have to alter it.
Now, create a new .cpp file called hud_import.cpp. Here are the required includes and pre-function statements (top of your .cpp file):
In order to kick this off we have to load the texture pointer variable. The best place for that is the constructor of the class.
What we are going to do:
Here is how it's done.
Our graphic starts out invisible, so we have to paint it now. Let's go on to the paint function. This one, although simpler, has a very important part associated with sizing and spacing to deal with.
First, we set the paint border to false. Next we draw our texture onto our surface. The next line is the important part. You actually define where you put your panel and how large it is in your HudLayout.res file. This last statement is attached to the image itself. You set its values like this...
You would put your own values in there according to the size of your picture.
There are two steps to go, our cvar toggle as well as editing the HudLayout.res.
First, let's define the cvar. Go back up to the top of the cpp file and paste this:
show_beta is the variable name that we're using as an example. We're actually creating the series of pictures on the second post.
This variable will allow us to type show_beta 1 into the console.
But what will that do for us, we need a function to test it.
Not too complex, just a traditional boolean status. And since its something that can change at any time, we should put it into our think function so it is tested always.
And that's it. Build, so we can move onto the .res.
Open up /scripts/HudLayout.res under your mod's main directory and paste the following in it.
There are a number of settings to choose from. For more information on VGUI schemes, you can read VGUI Documentation#Schemes.
Booting the game now, you should be looking at your HUD, with a graphic in the upper right corner of the screen drawn with rounded corners.