Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

686 lines
14 KiB

7 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// hud.cpp
//
// implementation of CHud class
//
//LRC - define to help track what calls are made on changelevel, save/restore, etc
//#define ENGINE_DEBUG
#include "hud.h"
#include "cl_util.h"
#include <string.h>
#include <stdio.h>
#include "parsemsg.h"
9 years ago
#include "hud_servers.h"
7 years ago
#include "demo.h"
#include "demo_api.h"
cvar_t *hud_textmode;
float g_hud_text_color[3];
9 years ago
9 years ago
extern client_sprite_t *GetSpriteList( client_sprite_t *pList, const char *psz, int iRes, int iCount );
7 years ago
extern cvar_t *sensitivity;
cvar_t *cl_lw = NULL;
cvar_t *cl_viewbob = NULL;
7 years ago
9 years ago
void ShutdownInput( void );
7 years ago
9 years ago
//DECLARE_MESSAGE( m_Logo, Logo )
int __MsgFunc_Logo( const char *pszName, int iSize, void *pbuf )
7 years ago
{
9 years ago
return gHUD.MsgFunc_Logo( pszName, iSize, pbuf );
7 years ago
}
9 years ago
//DECLARE_MESSAGE( m_Logo, Logo )
7 years ago
//LRC
int __MsgFunc_HUDColor(const char *pszName, int iSize, void *pbuf)
{
return gHUD.MsgFunc_HUDColor(pszName, iSize, pbuf );
}
//LRC
int __MsgFunc_SetFog(const char *pszName, int iSize, void *pbuf)
{
gHUD.MsgFunc_SetFog( pszName, iSize, pbuf );
return 1;
}
//LRC
int __MsgFunc_KeyedDLight(const char *pszName, int iSize, void *pbuf)
{
gHUD.MsgFunc_KeyedDLight( pszName, iSize, pbuf );
return 1;
}
//LRC
int __MsgFunc_AddShine(const char *pszName, int iSize, void *pbuf)
{
gHUD.MsgFunc_AddShine( pszName, iSize, pbuf );
return 1;
}
//LRC
int __MsgFunc_SetSky(const char *pszName, int iSize, void *pbuf)
{
gHUD.MsgFunc_SetSky( pszName, iSize, pbuf );
return 1;
}
9 years ago
int __MsgFunc_ResetHUD( const char *pszName, int iSize, void *pbuf )
7 years ago
{
#ifdef ENGINE_DEBUG
CONPRINT("## ResetHUD\n");
#endif
9 years ago
return gHUD.MsgFunc_ResetHUD( pszName, iSize, pbuf );
7 years ago
}
9 years ago
int __MsgFunc_InitHUD( const char *pszName, int iSize, void *pbuf )
7 years ago
{
#ifdef ENGINE_DEBUG
CONPRINT("## InitHUD\n");
#endif
gHUD.MsgFunc_InitHUD( pszName, iSize, pbuf );
return 1;
}
9 years ago
int __MsgFunc_ViewMode( const char *pszName, int iSize, void *pbuf )
7 years ago
{
gHUD.MsgFunc_ViewMode( pszName, iSize, pbuf );
return 1;
}
9 years ago
int __MsgFunc_SetFOV( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return gHUD.MsgFunc_SetFOV( pszName, iSize, pbuf );
}
9 years ago
int __MsgFunc_Concuss( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return gHUD.MsgFunc_Concuss( pszName, iSize, pbuf );
}
9 years ago
int __MsgFunc_GameMode( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return gHUD.MsgFunc_GameMode( pszName, iSize, pbuf );
}
// TFFree Command Menu
9 years ago
void __CmdFunc_OpenCommandMenu( void )
7 years ago
{
}
// TFC "special" command
9 years ago
void __CmdFunc_InputPlayerSpecial( void )
7 years ago
{
}
9 years ago
void __CmdFunc_CloseCommandMenu( void )
7 years ago
{
}
void __CmdFunc_ForceCloseCommandMenu( void )
{
}
void __CmdFunc_ToggleServerBrowser( void )
{
}
// TFFree Command Menu Message Handlers
9 years ago
int __MsgFunc_ValClass( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_TeamNames( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_Feign( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_Detpack( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_VGUIMenu( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
/*
int __MsgFunc_MOTD(const char *pszName, int iSize, void *pbuf)
{
return 0;
}
*/
9 years ago
int __MsgFunc_BuildSt( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_RandomPC( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_ServerName( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
/*
int __MsgFunc_ScoreInfo(const char *pszName, int iSize, void *pbuf)
{
return 0;
}
int __MsgFunc_TeamScore(const char *pszName, int iSize, void *pbuf)
{
return 0;
}
int __MsgFunc_TeamInfo(const char *pszName, int iSize, void *pbuf)
{
return 0;
}
*/
9 years ago
int __MsgFunc_Spectator( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
9 years ago
int __MsgFunc_AllowSpec( const char *pszName, int iSize, void *pbuf )
7 years ago
{
return 0;
}
// This is called every time the DLL is loaded
9 years ago
void CHud::Init( void )
7 years ago
{
#ifdef ENGINE_DEBUG
CONPRINT("## CHud::Init\n");
#endif
HOOK_MESSAGE( Logo );
HOOK_MESSAGE( ResetHUD );
HOOK_MESSAGE( GameMode );
HOOK_MESSAGE( InitHUD );
HOOK_MESSAGE( ViewMode );
HOOK_MESSAGE( SetFOV );
HOOK_MESSAGE( Concuss );
HOOK_MESSAGE( HUDColor ); //LRC
HOOK_MESSAGE( SetFog ); //LRC
HOOK_MESSAGE( KeyedDLight ); //LRC
HOOK_MESSAGE( AddShine ); //LRC
HOOK_MESSAGE( SetSky ); //LRC
// TFFree CommandMenu
HOOK_COMMAND( "+commandmenu", OpenCommandMenu );
HOOK_COMMAND( "-commandmenu", CloseCommandMenu );
HOOK_COMMAND( "ForceCloseCommandMenu", ForceCloseCommandMenu );
HOOK_COMMAND( "special", InputPlayerSpecial );
HOOK_COMMAND( "togglebrowser", ToggleServerBrowser );
HOOK_MESSAGE( ValClass );
HOOK_MESSAGE( TeamNames );
HOOK_MESSAGE( Feign );
HOOK_MESSAGE( Detpack );
//HOOK_MESSAGE( MOTD );
HOOK_MESSAGE( BuildSt );
HOOK_MESSAGE( RandomPC );
HOOK_MESSAGE( ServerName );
//HOOK_MESSAGE( ScoreInfo );
//HOOK_MESSAGE( TeamScore );
//HOOK_MESSAGE( TeamInfo );
HOOK_MESSAGE( Spectator );
HOOK_MESSAGE( AllowSpec );
// VGUI Menus
HOOK_MESSAGE( VGUIMenu );
CVAR_CREATE( "hud_classautokill", "1", FCVAR_ARCHIVE | FCVAR_USERINFO ); // controls whether or not to suicide immediately on TF class switch
CVAR_CREATE( "hud_takesshots", "0", FCVAR_ARCHIVE ); // controls whether or not to automatically take screenshots at the end of a round
9 years ago
hud_textmode = CVAR_CREATE ( "hud_textmode", "0", FCVAR_ARCHIVE );
7 years ago
m_iLogo = 0;
m_iFOV = 0;
m_iHUDColor = 0x00FFA000; //255,160,0 -- LRC
CVAR_CREATE( "zoom_sensitivity_ratio", "1.2", 0 );
default_fov = CVAR_CREATE( "default_fov", "90", 0 );
m_pCvarStealMouse = CVAR_CREATE( "hud_capturemouse", "1", FCVAR_ARCHIVE );
m_pCvarDraw = CVAR_CREATE( "hud_draw", "1", FCVAR_ARCHIVE );
cl_lw = gEngfuncs.pfnGetCvarPointer( "cl_lw" );
cl_viewbob = CVAR_CREATE( "cl_viewbob", "0", FCVAR_ARCHIVE );
7 years ago
m_pSpriteList = NULL;
m_pShinySurface = NULL; //LRC
// Clear any old HUD list
9 years ago
if( m_pHudList )
7 years ago
{
HUDLIST *pList;
while ( m_pHudList )
{
pList = m_pHudList;
m_pHudList = m_pHudList->pNext;
free( pList );
}
m_pHudList = NULL;
}
// In case we get messages before the first update -- time will be valid
m_flTime = 1.0;
m_iNoConsolePrint = 0;
m_Ammo.Init();
m_Health.Init();
m_SayText.Init();
m_Spectator.Init();
m_Geiger.Init();
m_Train.Init();
m_Battery.Init();
m_Flash.Init();
m_Message.Init();
m_StatusBar.Init();
m_DeathNotice.Init();
m_AmmoSecondary.Init();
m_TextMessage.Init();
m_StatusIcons.Init();
m_MOTD.Init();
m_Scoreboard.Init();
m_Particle.Init(); // (LRC) -- 30/08/02 November235: Particles to Order
m_Menu.Init();
9 years ago
MsgFunc_ResetHUD( 0, 0, NULL );
7 years ago
}
// CHud destructor
// cleans up memory allocated for m_rg* arrays
9 years ago
CHud::~CHud()
7 years ago
{
#ifdef ENGINE_DEBUG
CONPRINT("## CHud::destructor\n");
#endif
9 years ago
delete[] m_rghSprites;
delete[] m_rgrcRects;
delete[] m_rgszSpriteNames;
7 years ago
9 years ago
if( m_pHudList )
7 years ago
{
HUDLIST *pList;
9 years ago
while( m_pHudList )
7 years ago
{
pList = m_pHudList;
m_pHudList = m_pHudList->pNext;
free( pList );
}
m_pHudList = NULL;
}
}
// GetSpriteIndex()
// searches through the sprite list loaded from hud.txt for a name matching SpriteName
// returns an index into the gHUD.m_rghSprites[] array
// returns 0 if sprite not found
9 years ago
int CHud::GetSpriteIndex( const char *SpriteName )
7 years ago
{
// look through the loaded sprite name list for SpriteName
9 years ago
for( int i = 0; i < m_iSpriteCount; i++ )
7 years ago
{
9 years ago
if( strncmp( SpriteName, m_rgszSpriteNames + ( i * MAX_SPRITE_NAME_LENGTH), MAX_SPRITE_NAME_LENGTH ) == 0 )
7 years ago
return i;
}
return -1; // invalid sprite
}
9 years ago
void CHud::VidInit( void )
7 years ago
{
#ifdef ENGINE_DEBUG
CONPRINT("## CHud::VidInit\n");
#endif
int j;
7 years ago
m_scrinfo.iSize = sizeof(m_scrinfo);
9 years ago
GetScreenInfo( &m_scrinfo );
7 years ago
// ----------
// Load Sprites
// ---------
9 years ago
//m_hsprFont = LoadSprite("sprites/%d_font.spr");
7 years ago
m_hsprLogo = 0;
m_hsprCursor = 0;
9 years ago
if( ScreenWidth < 640 )
7 years ago
m_iRes = 320;
else
m_iRes = 640;
// Only load this once
9 years ago
if( !m_pSpriteList )
7 years ago
{
// we need to load the hud.txt, and all sprites within
9 years ago
m_pSpriteList = SPR_GetList( "sprites/hud.txt", &m_iSpriteCountAllRes );
7 years ago
9 years ago
if( m_pSpriteList )
7 years ago
{
// count the number of sprites of the appropriate res
m_iSpriteCount = 0;
client_sprite_t *p = m_pSpriteList;
9 years ago
for( j = 0; j < m_iSpriteCountAllRes; j++ )
7 years ago
{
9 years ago
if( p->iRes == m_iRes )
7 years ago
m_iSpriteCount++;
p++;
}
// allocated memory for sprite handle arrays
m_rghSprites = new HSPRITE[m_iSpriteCount];
m_rgrcRects = new wrect_t[m_iSpriteCount];
m_rgszSpriteNames = new char[m_iSpriteCount * MAX_SPRITE_NAME_LENGTH];
p = m_pSpriteList;
int index = 0;
9 years ago
for( j = 0; j < m_iSpriteCountAllRes; j++ )
7 years ago
{
9 years ago
if( p->iRes == m_iRes )
7 years ago
{
char sz[256];
9 years ago
sprintf( sz, "sprites/%s.spr", p->szSprite );
m_rghSprites[index] = SPR_Load( sz );
7 years ago
m_rgrcRects[index] = p->rc;
strncpy( &m_rgszSpriteNames[index * MAX_SPRITE_NAME_LENGTH], p->szName, MAX_SPRITE_NAME_LENGTH );
index++;
}
p++;
}
}
}
else
{
// we have already have loaded the sprite reference from hud.txt, but
// we need to make sure all the sprites have been loaded (we've gone through a transition, or loaded a save game)
client_sprite_t *p = m_pSpriteList;
9 years ago
// count the number of sprites of the appropriate res
m_iSpriteCount = 0;
9 years ago
for( j = 0; j < m_iSpriteCountAllRes; j++ )
9 years ago
{
9 years ago
if( p->iRes == m_iRes )
9 years ago
m_iSpriteCount++;
p++;
}
9 years ago
delete[] m_rghSprites;
delete[] m_rgrcRects;
delete[] m_rgszSpriteNames;
9 years ago
// allocated memory for sprite handle arrays
m_rghSprites = new HSPRITE[m_iSpriteCount];
m_rgrcRects = new wrect_t[m_iSpriteCount];
m_rgszSpriteNames = new char[m_iSpriteCount * MAX_SPRITE_NAME_LENGTH];
p = m_pSpriteList;
7 years ago
int index = 0;
9 years ago
for( j = 0; j < m_iSpriteCountAllRes; j++ )
7 years ago
{
9 years ago
if( p->iRes == m_iRes )
7 years ago
{
char sz[256];
sprintf( sz, "sprites/%s.spr", p->szSprite );
9 years ago
m_rghSprites[index] = SPR_Load( sz );
9 years ago
m_rgrcRects[index] = p->rc;
strncpy( &m_rgszSpriteNames[index * MAX_SPRITE_NAME_LENGTH], p->szName, MAX_SPRITE_NAME_LENGTH );
7 years ago
index++;
}
p++;
}
}
// assumption: number_1, number_2, etc, are all listed and loaded sequentially
m_HUD_number_0 = GetSpriteIndex( "number_0" );
if( m_HUD_number_0 == -1 )
{
const char *msg = "There is something wrong with your game data! Please, reinstall\n";
if( HUD_MessageBox( msg ) )
{
gEngfuncs.pfnClientCmd( "quit\n" );
}
return;
}
7 years ago
m_iFontHeight = m_rgrcRects[m_HUD_number_0].bottom - m_rgrcRects[m_HUD_number_0].top;
m_Ammo.VidInit();
m_Health.VidInit();
m_Spectator.VidInit();
m_Geiger.VidInit();
m_Train.VidInit();
m_Battery.VidInit();
m_Flash.VidInit();
m_Message.VidInit();
m_StatusBar.VidInit();
m_DeathNotice.VidInit();
m_SayText.VidInit();
m_Menu.VidInit();
m_AmmoSecondary.VidInit();
m_TextMessage.VidInit();
m_StatusIcons.VidInit();
m_Scoreboard.VidInit();
m_MOTD.VidInit();
m_Particle.VidInit(); // (LRC) -- 30/08/02 November235: Particles to Order
}
9 years ago
int CHud::MsgFunc_Logo( const char *pszName, int iSize, void *pbuf )
7 years ago
{
BEGIN_READ( pbuf, iSize );
// update Train data
m_iLogo = READ_BYTE();
return 1;
}
//LRC
int CHud::MsgFunc_HUDColor(const char *pszName, int iSize, void *pbuf)
{
BEGIN_READ( pbuf, iSize );
m_iHUDColor = READ_LONG();
return 1;
}
float g_lastFOV = 0.0;
/*
============
COM_FileBase
============
*/
// Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
9 years ago
void COM_FileBase ( const char *in, char *out )
7 years ago
{
int len, start, end;
len = strlen( in );
9 years ago
7 years ago
// scan backward for '.'
end = len - 1;
9 years ago
while( end && in[end] != '.' && in[end] != '/' && in[end] != '\\' )
7 years ago
end--;
9 years ago
if( in[end] != '.' ) // no '.', copy to end
end = len - 1;
7 years ago
else
end--; // Found ',', copy to left of '.'
// Scan backward for '/'
9 years ago
start = len - 1;
while( start >= 0 && in[start] != '/' && in[start] != '\\' )
7 years ago
start--;
9 years ago
if( in[start] != '/' && in[start] != '\\' )
7 years ago
start = 0;
else
start++;
// Length of new sting
len = end - start + 1;
// Copy partial string
strncpy( out, &in[start], len );
9 years ago
7 years ago
// Terminate it
out[len] = 0;
}
/*
=================
HUD_IsGame
=================
*/
int HUD_IsGame( const char *game )
{
const char *gamedir;
9 years ago
char gd[1024];
7 years ago
gamedir = gEngfuncs.pfnGetGameDirectory();
9 years ago
if( gamedir && gamedir[0] )
7 years ago
{
COM_FileBase( gamedir, gd );
9 years ago
if( !stricmp( gd, game ) )
7 years ago
return 1;
}
return 0;
}
/*
=====================
HUD_GetFOV
Returns last FOV
=====================
*/
float HUD_GetFOV( void )
{
9 years ago
if( gEngfuncs.pDemoAPI->IsRecording() )
7 years ago
{
// Write it
int i = 0;
9 years ago
unsigned char buf[100];
7 years ago
// Active
9 years ago
*(float *)&buf[i] = g_lastFOV;
i += sizeof(float);
7 years ago
Demo_WriteBuffer( TYPE_ZOOM, i, buf );
}
9 years ago
if( gEngfuncs.pDemoAPI->IsPlayingback() )
7 years ago
{
g_lastFOV = g_demozoom;
}
return g_lastFOV;
}
9 years ago
int CHud::MsgFunc_SetFOV( const char *pszName, int iSize, void *pbuf )
7 years ago
{
BEGIN_READ( pbuf, iSize );
int newfov = READ_BYTE();
int def_fov = CVAR_GET_FLOAT( "default_fov" );
g_lastFOV = newfov;
9 years ago
if( newfov == 0 )
7 years ago
{
m_iFOV = def_fov;
}
else
{
m_iFOV = newfov;
}
// the clients fov is actually set in the client data update section of the hud
// Set a new sensitivity
9 years ago
if( m_iFOV == def_fov )
7 years ago
{
// reset to saved sensitivity
m_flMouseSensitivity = 0;
}
else
{
// set a new sensitivity that is proportional to the change from the FOV default
m_flMouseSensitivity = sensitivity->value * ((float)newfov / (float)def_fov) * CVAR_GET_FLOAT("zoom_sensitivity_ratio");
}
return 1;
}
9 years ago
void CHud::AddHudElem( CHudBase *phudelem )
7 years ago
{
HUDLIST *pdl, *ptemp;
9 years ago
//phudelem->Think();
7 years ago
9 years ago
if( !phudelem )
7 years ago
return;
9 years ago
pdl = (HUDLIST *)malloc( sizeof(HUDLIST) );
if( !pdl )
7 years ago
return;
9 years ago
memset( pdl, 0, sizeof(HUDLIST) );
7 years ago
pdl->p = phudelem;
9 years ago
if( !m_pHudList )
7 years ago
{
m_pHudList = pdl;
return;
}
ptemp = m_pHudList;
9 years ago
while( ptemp->pNext )
7 years ago
ptemp = ptemp->pNext;
ptemp->pNext = pdl;
}
float CHud::GetSensitivity( void )
{
return m_flMouseSensitivity;
}