Team Icons

From Valve Developer Community
Revision as of 12:13, 17 June 2007 by Giles (talk | contribs) (Categorised)
Jump to: navigation, search

I'm going to explain to you how to accomplish having Hud Element display an image depending on what team you're on. For example, "If i'm on team COMBINE display ***.vtf, else if i'm on team REBELS display ***.vtf instead" This will be ran constantly all the time so there's no need scrub the hud or anything like that.

Prerequisites

For this tutorial to work, you'll need to have 2 vtf images with configured vmt settings. Example of vmt is below if you need it.

My VMT settings are as follows:

"UnlitGeneric"
{
	"$basetexture" "vgui/hud/teamicons/iraqiran"
        "$translucent" "1"
        "$ignorez" "1" 
}

The HUD Element

Ok so go ahead and create two files in the client-side project named whatever you like but for this tutorial i'm using Rteamicon.cpp & Rteamicon.h.

Now open up Rteamicon.cpp and let's start off with the header files...

// ======= *********-|RECOIL|-********* =======//
// Programmer: John Stuart                   
// About:                         
// Notes:                                     
// ============================================//

#include "hud.h"
#include "cbase.h"
#include "Rteamicon.h"
#include "iclientmode.h"
#include "hud_macros.h"
#include "c_team.h"
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"
#include "hl2mp_gamerules.h"
#include "tier0/memdbgon.h"

using namespace vgui;

DECLARE_HUDELEMENT( CRTeamIcon );

Now that we have the header files we need to create the constructer. Just below the using namespace vgui; paste this.

//-----------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------
CRTeamIcon::CRTeamIcon( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "CRTeamIcon" )
{
   Panel *pParent = g_pClientMode->GetViewport();
   SetParent( pParent );   
   
   SetVisible( true );
   SetEnabled( true );

   // This defines m_nIraqIcon as "vgui/hud/teamicons/iraqiran" which calls the vtf.. duh.
   m_nIraqIcon = vgui::surface()->CreateNewTextureID();
   vgui::surface()->DrawSetTextureFile( m_nIraqIcon, "vgui/hud/teamicons/iraqiran" , true, false);

   // You get the deal now..
   m_nBritishIcon = vgui::surface()->CreateNewTextureID();
   vgui::surface()->DrawSetTextureFile( m_nBritishIcon, "vgui/hud/teamicons/ukusa" , true, false);

   SetHiddenBits( HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}

Now that the constructer is created, we can go ahead and create the main function for this to work, for this example i'm using "Paint Background" because if you're going to use normal "Paint" you're going to have a black transparent background which for i didn't want.

//-----------------------------------------------------------
// Purpose: If pPlayer (me) is on team Combine, display Iraq icon
// Purpose+: else if i'm on team Rebels display British icon.
//-----------------------------------------------------------
void CRTeamIcon::PaintBackground()
{  
   SetBgColor(Color(0,0,0,0));
   
   CBasePlayer *pPlayer = UTIL_PlayerByIndex(engine->GetLocalPlayer());

      if(pPlayer->GetTeamNumber() == TEAM_COMBINE)
	  {
           vgui::surface()->DrawSetColor(GetFgColor());
	   vgui::surface()->DrawSetTexture( m_nIraqIcon );
	   vgui::surface()->DrawTexturedRect(0, 0, GetWide(), GetTall());
	   //DevMsg("Team Combine Icon Displayed\n"); // Spams console hehe :D
	  }
	  else if(pPlayer->GetTeamNumber() == TEAM_REBELS)
	  {
           vgui::surface()->DrawSetColor(GetFgColor());
	   vgui::surface()->DrawSetTexture( m_nBritishIcon );
	   vgui::surface()->DrawTexturedRect(0, 0, GetWide(), GetTall());
	   //DevMsg("Team Rebels Icon Displayed\n"); 
	  }

      SetPaintBorderEnabled(false);

	  BaseClass::PaintBackground();
}

Now we've nearly completed the actual hud element, to make this all work we've got to get that header file done or else you'll be torn to pieces by errors. So open up Rteamicon.h and place this in there and hit save.

// ======= *********-|RECOIL|-********* =======//
// Programmer: John Stuart                   
// About:                         
// Notes:                                     
// ============================================//

#include "hudelement.h"
#include <vgui_controls/Panel.h>

using namespace vgui;

//-----------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------
class CRTeamIcon : public CHudElement, public Panel
{
   DECLARE_CLASS_SIMPLE( CRTeamIcon, Panel );

   public:

       CRTeamIcon( const char *pElementName );

   protected:

       virtual void PaintBackground();

       int m_nIraqIcon;
       int m_nBritishIcon;

};

The Fiddly Bits

Now that we've got that working, we can move onto getting the actual image on the hud.. Well it's already there it's just not defined. So go ahead and open up your "scripts/HudLayout.res" file and someone around place the following lines of text.

	CRTeamIcon
	{
		"fieldName" "CRTeamIcon"
	      "xpos" "r62"
		"ypos" "0"
		"wide" "64"
		"tall" "32"
		"visible" "1"
		"enabled" "1"
      
		"PaintBackgroundType"   "2"
	}

Of course you can place your image anywhere you like and what size you like by editing the xpos,ypos,wide,tall to your settings/needs.

The Result

I hope this has helped you create a good hud element which display an icon on your hud of what team you're on. Here's an example of mine.

thisiswhathappenswhenyods9.png

Author: John Stuart