Wayback Machine
Jul AUG FEB
Previous capture 8 Next capture
2006 2007 2008
4 captures
8 Aug 07 - 11 Jun 08
sparklines
Close Help

Mazor's VGUI HUD Tutorial

Part I

Stuff in red is half life code

Stuff in green is Mazor's code

Stuff in blue is comments

Stuff in orange is files

Stuff in white is just normal text

 

Ok, now, for starters, this tutorial will explain how to set the HUD up, and just get the basis. It will not tell how to add elements. Adding elements is in my second tutorial. By the end of reading all of the tuts involved with making a VGUI HUD that I have done, you will be able to make something similar to this: http://www.spudgunmod.net/newhud3.jpg

Ok, lets get started. Open your cl_dll project.

Add a new header file called something like: vgui_newhud.h or something.

Then, add these lines in there for starters:

// newhud.h

#include<VGUI_Panel.h>
#include<VGUI_TablePanel.h>
#include<VGUI_HeaderPanel.h>
#include<VGUI_TextGrid.h>
#include<VGUI_Label.h>

class NewHUD : public Panel
{
private:

// TODO: Add any HUD elements you want here, later explained in part II

public:
NewHUD(int x,int y,int wide,int tall);
virtual void setSize(int wide,int tall);

void Update( void );
void RebuildData( void );
void Initialize( void );
void Open( void )
{
RebuildData();
setVisible(true);
}

};

Ok, now go into the file: VGUI_TeamFotressViewport.h

At the top, find this:

class CClassMenuPanel;
class CTeamMenuPanel;

then add:

// newhud

class NewHUD;

// newhud

Find the lines:

CCommandMenu *m_pCurrentCommandMenu;
float m_flMenuOpenTime;
float m_flScoreBoardLastUpdated;

and add:

// newhud

float m_flNewHUDLastUpdated;

// newhud

Now find:

void CreateCommandMenu( void );
void CreateScoreBoard( void );

and add to the end:

// newhud

void CreateNewHUD( void );

// newhud

Now find this:


void ShowScoreBoard( void );
void HideScoreBoard( void );
bool IsScoreBoardVisible( void );

and add this to the end:

// newhud
bool IsNewHUDVisible( void );
void ShowNewHUD( void );

// newhud

Now find these lines:

CTeamMenuPanel *m_pTeamMenu;
CClassMenuPanel *m_pClassMenu;
ScorePanel *m_pScoreBoard;

then add:

// newhud
NewHUD *m_pNewHUD;

// newhud

Ok, now that your done with that, lets open up: VGUI_TeamFortessViewport.cpp

// newhud

#include "vgui_newhud.h"

// newhud

Scroll through the file and find:

m_pSpectatorMenu = NULL;
m_pCurrentMenu = NULL;
m_pCurrentCommandMenu = NULL;

That should be in:

TeamFortressViewport::TeamFortressViewport(int x,int y,int wide,int tall) : Panel(x,y,wide,tall), m_SchemeManager(wide,tall)

After the line: m_pCurrentCommandMenu = NULL;

Add this:

// newhud

m_pNewHUD = NULL;

// newhud

Now down towards the bottom of the same function, you will see:

CreateCommandMenu();
CreateServerBrowser();
CreateSpectatorMenu();

right after that put in:

// newhud

CreateNewHUD();

// newhud

Now, find:

if (m_pClassMenu)
{
m_pClassMenu->Initialize();
}
if (m_pScoreBoard)
{
m_pScoreBoard->Initialize();
HideScoreBoard();
}

right after all that add this:

// newhud

if (m_pNewHUD)
{
m_pNewHUD->Initialize();
ShowNewHUD();
m_pNewHUD->setVisible(true);
}

//newhud

Ok, now towards the bottom of that same function, find this:

// Clear out some data
m_iGotAllMOTD = true;
m_iRandomPC = false;
m_flScoreBoardLastUpdated = 0;

after that block of code, add this:

// newhud

m_flNewHUDLastUpdated = 0;

// newhud

Now find this block of code:

//-----------------------------------------------------------------------------
// Purpose: Bring up the scoreboard
//-----------------------------------------------------------------------------
void TeamFortressViewport::ShowScoreBoard( void )
{
if (m_pScoreBoard)
{
// No Scoreboard in single-player
if ( gEngfuncs.GetMaxClients() > 1 )
{
m_pScoreBoard->Open();
UpdateCursorState();
}
}
}

Add this after it:

// newhud

void TeamFortressViewport::ShowNewHUD( void )
{
if (m_pNewHUD)
{
m_pNewHUD->Open();
m_pNewHUD->setVisible(true);
}
}

// newhud

Further down, still in VGUI_TeamFortressViewport.cpp, find this:

//-----------------------------------------------------------------------------
// Purpose: Returns true if the scoreboard is up
//-----------------------------------------------------------------------------
bool TeamFortressViewport::IsScoreBoardVisible( void )
{
if (m_pScoreBoard)
return m_pScoreBoard->isVisible();

return false;
}

After that block of code, add this:

// newhud

bool TeamFortressViewport::IsNewHUDVisible( void )
{
if (m_pNewHUD)
return m_pNewHUD->isVisible();

return false;
}

// newhud

Now find this:

//======================================================================
void TeamFortressViewport::CreateScoreBoard( void )
{
m_pScoreBoard = new ScorePanel(SBOARD_INDENT_X,SBOARD_INDENT_Y, ScreenWidth - (SBOARD_INDENT_X * 2), ScreenHeight - (SBOARD_INDENT_Y * 2));
m_pScoreBoard->setParent(this);
m_pScoreBoard->setVisible(false);
}

RIGHT above that block of code, enter this:

// newhud

void TeamFortressViewport::CreateNewHUD( void )
{
m_pNewHUD = new NewHUD(0,0,ScreenWidth, ScreenHeight);
m_pNewHUD->setParent(this);
m_pNewHUD->setVisible(true);
}

// newhud

Ok, what that did was create a panel that spans the whole width and height of the screen. This will give you a 'canvas' so-to-speak that you can just 'paint' your new HUD on. It won't be visible to the player (if you follow this tut correctly). The only visible thing that the players will see are the HUD elements that you add. Anyway, lets continue...

Now, in the function: void TeamFortressViewport::UpdateOnPlayerInfo( )

find this set of code:

if (m_pClassMenu)
m_pClassMenu->Update();
if (m_pScoreBoard)
m_pScoreBoard->Update();

Then add this:

// newhud

if (m_pNewHUD)
m_pNewHUD->Update();

// newhud

Now find this code:

// Update the Scoreboard, if it's visible
if ( m_pScoreBoard->isVisible() && (m_flScoreBoardLastUpdated < gHUD.m_flTime) )
{
m_pScoreBoard->Update();
m_flScoreBoardLastUpdated = gHUD.m_flTime + 0.5;
}

right after all that, add this:

// newhud

if (m_pNewHUD->isVisible() && (m_flNewHUDLastUpdated< gHUD.m_flTime) )
{
m_pNewHUD->Update();
m_flNewHUDLastUpdated = gHUD.m_flTime + 0.1; // we want this to update quick
}

// newhud

Ok, that will update our HUD every 0.1 second. For comparason, the scoreboard is 0.5 seconds, so, twice a second. We are updating the HUD 10 times per second.

Ok, that should be it for VGUI_TeamFortressViewport.cpp, now lets open: vgui_ScorePanel.cpp

This is where the bulk of the code will reside. That is just the way I chose to do it, you CAN put it in a separate file if you would like, but you'll need these includes:

#include<VGUI_LineBorder.h>

#include "hud.h"
#include "cl_util.h"
#include "const.h"
#include "entity_state.h"
#include "cl_entity.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_newhud.h"

Ok, now that that little bit is out of the way, lets continute working...

Add this include to the include list in vgui_ScorePanel.cpp

// newhud

#include "vgui_newhud.h"

//newhud

Find this bit of code further down:

case 3:
sprintf(sz, "%d", g_PlayerExtraInfo[ m_iSortedRows[row] ].deaths );
break;
case 4:
sprintf(sz, "%d", g_PlayerInfoList[ m_iSortedRows[row] ].ping );
break;
default:
strcpy(sz, "");
}

m_pLabel->setText(sz);
}

return m_pLabel;
}

Right after all that, add this block of code:

// newhud

NewHUD::NewHUD(int x, int y, int wide, int tall) : Panel(x,y,wide,tall)
{

setBgColor( 0,0,0,255 ); // We don't want a dark screen with a hud!

// TODO: Add your HUD elements using VGUI

Initialize();

}
//-----------------------------------------------------------------------------
// Purpose: Called each time a new level is started.
//-----------------------------------------------------------------------------
void NewHUD::Initialize( void )
{

// TODO: Clear out all the values here

}
void NewHUD::setSize(int wide,int tall)
{
Panel::setSize(wide,tall);
}
void NewHUD::Update()
{

// TODO: Set your values to what you want them to be in here, this is called 10 times per second, keep the values fresh

}
void NewHUD::RebuildData()
{
Update();
}

// newhud

This is what actually MAKES your new HUD. That is it for now, you have a basis to build from. If you already know VGUI, start adding your labels/imagelabels/transpanels/whatever you want in the function: NewHUD::NewHUD(int x, int y, int wide, int tall) : Panel(x,y,wide,tall)

If you do NOT know VGUI, I'd suggest reading the series of VGUI tuts that rkzad put out:

http://hlci.valveworld.com/index.php?site=vguicltuts

If you want to learn how to add the HUD elements themselves, read the second part to this TUT.

THANKS TO DEEPFLAME FOR THE CORRECTIONS!

Any Q's, comments or gripes, send them here: mazor@spudgunmod.net