前段时间在研究VGUI,所以也就找了点资料写了一个简单的HUD,提供给大家参考:
客户端项目中添加:hud_hello.cpp
并修改scripts/HudLayout.res文件,添加以下代码
代码我也没有完全弄明白,都是参照 SDK 和一些网络上面的教程做的,有什么不明白的可以,留言相互交流交流
参考资料: VGUI
客户端项目中添加:hud_hello.cpp
//
============Copyright ?2007-2008, RootCat, All rights reserved========================
//
//
// Purpose: 通过一个Hud显示当前玩家是飞行模式还是行走模式
//
// : $
//
// ============================================================================= //
//
//
// implementation of CHudHello class
//
#include " cbase.h "
#include " hud.h "
#include " hud_macros.h "
#include " view.h "
#include " iclientmode.h "
#include < vgui / IScheme.h >
#include < vgui / ISurface.h >
#include < vgui / ISystem.h >
#include < vgui_controls / Panel.h >
#include < vgui / ILocalize.h >
using namespace vgui;
#include " hudelement.h "
#include " ConVar.h "
// memdbgon must be the last include file in a .cpp file!!!
#include " tier0/memdbgon.h "
// -----------------------------------------------------------------------------
// Purpose: Hello panel
// -----------------------------------------------------------------------------
class CHudHello : public CHudElement, public vgui::Panel
... {
DECLARE_CLASS_SIMPLE( CHudHello,vgui::Panel);
public:
CHudHello( const char *pElementName );
virtual void Init( void );
virtual void VidInit( void );
virtual void Reset( void );
virtual void Paint( void );
protected:
virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
private:
// old variables
vgui::HScheme scheme; //The Scheme object to hold our scheme info.
} ;
DECLARE_HUDELEMENT(CHudHello);
// -----------------------------------------------------------------------------
// Purpose: Constructor
// -----------------------------------------------------------------------------
CHudHello::CHudHello( const char * pElementName ) : CHudElement( pElementName ), vgui::Panel( NULL, " HudHello " )
... {
scheme = vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme");
SetScheme(scheme); // Here we load up our scheme and set this element to use it. Using a different scheme than ClientScheme doesn't work right off the bat anyways, so... :)
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent ); // Our parent is the screen itself.
SetPos(158,432);
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Init()
... {
Reset();
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Reset()
... {
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::VidInit()
... {
Reset();
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::ApplySchemeSettings( vgui::IScheme * pScheme )
... {
BaseClass::ApplySchemeSettings( pScheme );
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Paint( void )
... {
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();//获取当前用户
if ( !pPlayer )
return;
wchar_t *pText = L"行走模式";
surface()->DrawSetColor( 247, 148, 28, 255 );
surface()->DrawSetTextColor( 247, 148, 28, 255 );
if ( pPlayer->GetMoveType() == MOVETYPE_NOCLIP )
...{
pText = L"飞行模式";
surface()->DrawSetColor( 255, 0, 0, 255 );
surface()->DrawSetTextColor( 255, 0, 0, 255 );
}
// get the right font handle for this scheme
vgui::IScheme *pScheme = vgui::scheme()->GetIScheme(GetScheme());
vgui::HFont hFont = pScheme->GetFont( "Default" );
surface()->DrawSetTextFont( hFont ); // set the font
surface()->DrawPrintText( pText, wcslen(pText) ); // print text这里是打印文字
surface()->DrawFilledRect( 10, 0,100 ,10 ); //x0,y0,x1,y1这里是绘制长方型
BaseClass::Paint();
}
//
// Purpose: 通过一个Hud显示当前玩家是飞行模式还是行走模式
//
// : $
//
// ============================================================================= //
//
//
// implementation of CHudHello class
//
#include " cbase.h "
#include " hud.h "
#include " hud_macros.h "
#include " view.h "
#include " iclientmode.h "
#include < vgui / IScheme.h >
#include < vgui / ISurface.h >
#include < vgui / ISystem.h >
#include < vgui_controls / Panel.h >
#include < vgui / ILocalize.h >
using namespace vgui;
#include " hudelement.h "
#include " ConVar.h "
// memdbgon must be the last include file in a .cpp file!!!
#include " tier0/memdbgon.h "
// -----------------------------------------------------------------------------
// Purpose: Hello panel
// -----------------------------------------------------------------------------
class CHudHello : public CHudElement, public vgui::Panel
... {
DECLARE_CLASS_SIMPLE( CHudHello,vgui::Panel);
public:
CHudHello( const char *pElementName );
virtual void Init( void );
virtual void VidInit( void );
virtual void Reset( void );
virtual void Paint( void );
protected:
virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
private:
// old variables
vgui::HScheme scheme; //The Scheme object to hold our scheme info.
} ;
DECLARE_HUDELEMENT(CHudHello);
// -----------------------------------------------------------------------------
// Purpose: Constructor
// -----------------------------------------------------------------------------
CHudHello::CHudHello( const char * pElementName ) : CHudElement( pElementName ), vgui::Panel( NULL, " HudHello " )
... {
scheme = vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme");
SetScheme(scheme); // Here we load up our scheme and set this element to use it. Using a different scheme than ClientScheme doesn't work right off the bat anyways, so... :)
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent ); // Our parent is the screen itself.
SetPos(158,432);
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Init()
... {
Reset();
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Reset()
... {
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::VidInit()
... {
Reset();
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::ApplySchemeSettings( vgui::IScheme * pScheme )
... {
BaseClass::ApplySchemeSettings( pScheme );
}
// -----------------------------------------------------------------------------
// Purpose:
// -----------------------------------------------------------------------------
void CHudHello::Paint( void )
... {
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();//获取当前用户
if ( !pPlayer )
return;
wchar_t *pText = L"行走模式";
surface()->DrawSetColor( 247, 148, 28, 255 );
surface()->DrawSetTextColor( 247, 148, 28, 255 );
if ( pPlayer->GetMoveType() == MOVETYPE_NOCLIP )
...{
pText = L"飞行模式";
surface()->DrawSetColor( 255, 0, 0, 255 );
surface()->DrawSetTextColor( 255, 0, 0, 255 );
}
// get the right font handle for this scheme
vgui::IScheme *pScheme = vgui::scheme()->GetIScheme(GetScheme());
vgui::HFont hFont = pScheme->GetFont( "Default" );
surface()->DrawSetTextFont( hFont ); // set the font
surface()->DrawPrintText( pText, wcslen(pText) ); // print text这里是打印文字
surface()->DrawFilledRect( 10, 0,100 ,10 ); //x0,y0,x1,y1这里是绘制长方型
BaseClass::Paint();
}
并修改scripts/HudLayout.res文件,添加以下代码
HudHello
{
"fieldName" "HudHello"
"visible" "1"
"enabled" "1"
"PaintBackgroundType" "2"
"text_xpos" "8"
"text_ypos" "20"
"digit_xpos" "50"
"digit_ypos" "2"
}
{
"fieldName" "HudHello"
"visible" "1"
"enabled" "1"
"PaintBackgroundType" "2"
"text_xpos" "8"
"text_ypos" "20"
"digit_xpos" "50"
"digit_ypos" "2"
}
代码我也没有完全弄明白,都是参照 SDK 和一些网络上面的教程做的,有什么不明白的可以,留言相互交流交流
参考资料: VGUI