发新话题
打印

[原创]一个HUD的简单实现

[原创]一个HUD的简单实现

前段时间在研究VGUI,所以也就找了点资料写了一个简单的HUD,提供给大家参考:
客户端项目中添加:hud_hello.cpp
[CODE]
//============Copyright ?2007-2008, RootCat, All rights reserved========================//
//
// Purpose: 通过一个Hud显示当前玩家是飞行模式还是行走模式
//
// $NoKeywords: $
//
//=============================================================================//
//
//
// 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:anel
{
        DECLARE_CLASS_SIMPLE( CHudHello,vgui:anel);

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:anel( 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:anel *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:aint( 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:aint();
}
[/CODE]

并修改scripts/HudLayout.res文件,添加以下代码:
[CODE]
        HudHello
        {
                "fieldName"                "HudHello"
                "visible" "1"
                "enabled" "1"

                "aintBackgroundType"        "2"
               
                "text_xpos" "8"
                "text_ypos" "20"
                "digit_xpos" "50"
                "digit_ypos" "2"
        }
[/CODE]

代码我也没有完全弄明白,都是参照SDK和一些网络上面的教程做的,有什么不明白的可以,留言相互交流交流:p

参考资料:VGUI

TOP

好文章,学习中....

TOP

发新话题