VGUI Health bar

From Valve Developer Community
Revision as of 22:42, 15 January 2009 by Midletguy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

VGUI health bar

Well the VGUI Image Progress bar didn't help me mutch because i had problems with updating the Health bar.Buti found on a one russian website a Russian tutorial on how to make a VGUI Health Bar.This tutorial is purely a Copy N Paste tutorial.And it works on both EP1 and OB engines.

Code

First create a hud_hull.cpp in your CLIENT part your code.And paste this to hud_hull.cpp #include "cbase.h"

  1. include "hud.h"
  2. include "hud_hull.h"
  3. include "hud_macros.h"

//#include "c_sdk_player.h"

  1. include "iclientmode.h"
  2. include <vgui_controls/AnimationController.h>
  3. include <vgui/ISurface.h>
  4. include <vgui/ILocalize.h>

using namespace vgui;

  1. include "tier0/memdbgon.h"

DECLARE_HUDELEMENT( CHudHull );

  1. define HULL_INIT 80

//------------------------------------------------------------------------ // Purpose: Constructor //------------------------------------------------------------------------ CHudHull::CHudHull( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "HudHull" )

{ vgui::Panel *pParent = g_pClientMode->GetViewport(); SetParent( pParent ); SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );

}

//------------------------------------------------------------------------ // Purpose: //------------------------------------------------------------------------ void CHudHull::Init( void )

{ m_flHull = HULL_INIT; m_nHullLow = -1; SetBgColor(Color(0,0,0,128));

}

//------------------------------------------------------------------------ // Purpose: //----------------------------------------------------------------------- void CHudHull::Reset( void )

{ Init(); }

//------------------------------------------------------------------------ // Purpose: //------------------------------------------------------------------------ void CHudHull::OnThink( void ) {

float newHull = 0; C_BasePlayer *local = C_BasePlayer::GetLocalPlayer(); if ( !local ) return;

newHull = local->GetHealth();

// Only update the fade if we've changed health if ( newHull == m_flHull ) { return; } m_flHull = newHull;

}


//------------------------------------------------------------------------

// Purpose: draws the power bar

//------------------------------------------------------------------------

void CHudHull::Paint()

{

// get bar chunks int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap); int enabledChunks = (int)((float)chunkCount * (m_flHull / 100.0f) + 0.5f ); // draw the suit power bar

surface()->DrawSetColor( m_HullColor ); int xpos = m_flBarInsetX, ypos = m_flBarInsetY; {for (int i = 0; i < enabledChunks; i++)

{

surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight ); xpos += (m_flBarChunkWidth + m_flBarChunkGap);

}}

// draw the exhausted portion of the bar.

surface()->DrawSetColor( Color( m_HullColor[0], m_HullColor[1], m_HullColor[2], m_iHullDisabledAlpha ) ); {for (int i = enabledChunks; i < chunkCount; i++)

{

surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight ); xpos += (m_flBarChunkWidth + m_flBarChunkGap);

}}

// draw our name surface()->DrawSetTextFont(m_hTextFont); surface()->DrawSetTextColor(m_HullColor); surface()->DrawSetTextPos(text_xpos, text_ypos);

//wchar_t *tempString = vgui::localize()->Find("#Valve_Hud_AUX_POWER");


surface()->DrawPrintText(L"HULL", wcslen(L"HULL"));

}