1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "hudelement.h"
#include <vgui_controls/Panel.h>
#include <vgui/ISurface.h>
#include "clientmode.h"
#include "c_dod_player.h"
#include "dod_hud_crosshair.h"
#include "materialsystem/imaterial.h"
#include "materialsystem/imesh.h"
#include "materialsystem/imaterialvar.h"
#include "mathlib/mathlib.h"
ConVar cl_crosshair_red( "cl_crosshair_red", "200", FCVAR_ARCHIVE );
ConVar cl_crosshair_green( "cl_crosshair_green", "200", FCVAR_ARCHIVE );
ConVar cl_crosshair_blue( "cl_crosshair_blue", "200", FCVAR_ARCHIVE );
ConVar cl_crosshair_alpha( "cl_crosshair_alpha", "200", FCVAR_ARCHIVE );
ConVar cl_crosshair_file( "cl_crosshair_file", "crosshair1", FCVAR_ARCHIVE );
ConVar cl_crosshair_scale( "cl_crosshair_scale", "32.0", FCVAR_ARCHIVE );
ConVar cl_crosshair_approach_speed( "cl_crosshair_approach_speed", "0.015" );
ConVar cl_dynamic_crosshair( "cl_dynamic_crosshair", "1", FCVAR_ARCHIVE );
using namespace vgui;
DECLARE_HUDELEMENT( CHudDODCrosshair );
CHudDODCrosshair::CHudDODCrosshair( const char *pName ) :
vgui::Panel( NULL, "HudDODCrosshair" ), CHudElement( pName )
{
SetParent( g_pClientMode->GetViewport() );
SetHiddenBits( HIDEHUD_PLAYERDEAD );
m_szPreviousCrosshair[0] = '\0';
m_pFrameVar = NULL;
m_flAccuracy = 0.1;
}
void CHudDODCrosshair::ApplySchemeSettings( IScheme *scheme )
{
BaseClass::ApplySchemeSettings( scheme );
SetSize( ScreenWidth(), ScreenHeight() );
}
void CHudDODCrosshair::LevelShutdown( void )
{
// forces m_pFrameVar to recreate next map
m_szPreviousCrosshair[0] = '\0';
if ( m_pCrosshair )
{
delete m_pCrosshair;
m_pCrosshair = NULL;
}
if ( m_pFrameVar )
{
delete m_pFrameVar;
m_pFrameVar = NULL;
}
}
void CHudDODCrosshair::Init()
{
m_iCrosshairTextureID = vgui::surface()->CreateNewTextureID();
}
bool CHudDODCrosshair::ShouldDraw()
{
C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
if ( !pPlayer )
return false;
CWeaponDODBase *pWeapon = pPlayer->GetActiveDODWeapon();
if ( !pWeapon )
return false;
return pWeapon->ShouldDrawCrosshair();
}
void CHudDODCrosshair::Paint()
{
C_DODPlayer *pPlayer = C_DODPlayer::GetLocalDODPlayer();
if( !pPlayer )
return;
const char *crosshairfile = cl_crosshair_file.GetString();
if ( !crosshairfile )
return;
if ( Q_stricmp( m_szPreviousCrosshair, crosshairfile ) != 0 )
{
char buf[256];
Q_snprintf( buf, sizeof(buf), "vgui/crosshairs/%s", crosshairfile );
vgui::surface()->DrawSetTextureFile( m_iCrosshairTextureID, buf, true, false );
if ( m_pCrosshair )
{
delete m_pCrosshair;
}
m_pCrosshair = vgui::surface()->DrawGetTextureMatInfoFactory( m_iCrosshairTextureID );
if ( !m_pCrosshair )
return;
if ( m_pFrameVar )
{
delete m_pFrameVar;
}
bool bFound = false;
m_pFrameVar = m_pCrosshair->FindVarFactory( "$frame", &bFound );
Assert( bFound );
m_nNumFrames = m_pCrosshair->GetNumAnimationFrames();
// save the name to compare with the cvar in the future
Q_strncpy( m_szPreviousCrosshair, crosshairfile, sizeof(m_szPreviousCrosshair) );
}
if ( m_pFrameVar )
{
if ( cl_dynamic_crosshair.GetBool() == false )
{
m_pFrameVar->SetIntValue( 0 );
}
else
{
CWeaponDODBase *pWeapon = pPlayer->GetActiveDODWeapon();
if ( !pWeapon )
return;
float accuracy = pWeapon->GetWeaponAccuracy( pPlayer->GetAbsVelocity().Length2D() );
float flMin = 0.02;
float flMax = 0.125;
accuracy = clamp( accuracy, flMin, flMax );
// approach this accuracy from our current accuracy
m_flAccuracy = Approach( accuracy, m_flAccuracy, cl_crosshair_approach_speed.GetFloat() );
float flFrame = RemapVal( m_flAccuracy, flMin, flMax, 0, m_nNumFrames-1 );
m_pFrameVar->SetIntValue( (int)flFrame );
}
}
Color clr( cl_crosshair_red.GetInt(), cl_crosshair_green.GetInt(), cl_crosshair_blue.GetInt(), 255 );
int screenWide, screenTall;
GetHudSize(screenWide, screenTall);
int iX = screenWide / 2;
int iY = screenTall / 2;
int iWidth, iHeight;
iWidth = iHeight = cl_crosshair_scale.GetInt();
vgui::surface()->DrawSetColor( clr );
vgui::surface()->DrawSetTexture( m_iCrosshairTextureID );
vgui::surface()->DrawTexturedRect( iX-iWidth, iY-iHeight, iX+iWidth, iY+iHeight );
vgui::surface()->DrawSetTexture(0);
}
|