Team Icons

From Valve Developer Community
Jump to: navigation, search
English (en)Deutsch (de)polski (pl)português do Brasil (pt-br)русский (ru)
... Icon-Important.png

This tutorial aims on how to accomplish having Hud Element display an image depending on what team you're on. For example, if the player is on one team, display 'this image', but if the player is on the other team, display 'this image'. Here's what you'll achieve: Hud icon example.png

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:

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

The HUD Element

Create two files in the client-side project named whatever you like but for this tutorial we'll be using Rteamicon.cpp & Rteamicon.h.

Now open up Rteamicon.cpp and start with putting the header in:

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

#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 we'll be using "Paint Background" because if you're going to use normal "Paint" you're going to have a black transparent background.

//-----------------------------------------------------------
// 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 complete on the actual hud element, to make this all work we've got to finish the header file. Open up Rteamicon.h and place this in there:

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

#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 materials

Now that the hud is working, we can move onto getting the actual image on the hud. it's already in there, but it's not defined. So go ahead and open up your "scripts/HudLayout.res" file and place this inside:

	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

You should now have a good hud element which displays an icon on your HUD of what team you're on.