-
Notifications
You must be signed in to change notification settings - Fork 39
/
Utils.h
414 lines (356 loc) · 9.09 KB
/
Utils.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
utils.h - draw helper
Copyright (C) 2010 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef UTILS_H
#define UTILS_H
//extern ui_enginefuncs_t g_engfuncs;
//extern ui_textfuncs_t g_textfuncs;
#include "enginecallback_menu.h"
#include "gameinfo.h"
#include "FontManager.h"
#include "BMPUtils.h"
#include "miniutl.h"
#if 0
#include <tgmath.h>
#endif
#define FILE_GLOBAL static
#define DLL_GLOBAL
#define MAX_INFO_STRING 256 // engine limit
#define RAD2DEG( x ) ((float)(x) * (float)(180.f / (float)M_PI))
#define DEG2RAD( x ) ((float)(x) * (float)((float)M_PI / 180.f))
//
// How did I ever live without ASSERT?
//
#ifdef _DEBUG
void DBG_AssertFunction( bool fExpr, const char* szExpr, const char* szFile, int szLine, const char* szMessage );
#define ASSERT( f ) DBG_AssertFunction( f, #f, __FILE__, __LINE__, NULL )
#define ASSERTSZ( f, sz ) DBG_AssertFunction( f, #f, __FILE__, __LINE__, sz )
#else
#define ASSERT( f )
#define ASSERTSZ( f, sz )
#endif
extern ui_globalvars_t *gpGlobals;
// exports
extern int UI_VidInit( void );
extern void UI_Init( void );
extern void UI_Shutdown( void );
extern void UI_UpdateMenu( float flTime );
extern void UI_KeyEvent( int key, int down );
extern void UI_MouseMove( int x, int y );
extern void UI_SetActiveMenu( int fActive );
extern void UI_AddServerToList( netadr_t adr, const char *info );
extern void UI_GetCursorPos( int *pos_x, int *pos_y );
extern void UI_SetCursorPos( int pos_x, int pos_y );
extern void UI_ShowCursor( int show );
extern void UI_CharEvent( int key );
extern int UI_MouseInRect( void );
extern int UI_IsVisible( void );
extern int UI_CreditsActive( void );
extern void UI_FinalCredits( void );
// extended exports
void UI_MenuResetPing_f( void );
void UI_ConnectionWarning_f( void );
void UI_ShowMessageBox( const char *text );
void UI_UpdateDialog( int preferStore );
void UI_ConnectionProgress_Disconnect( void );
void UI_ConnectionProgress_Download( const char *pszFileName, const char *pszServerName, int iCurrent, int iTotal, const char *comment );
void UI_ConnectionProgress_DownloadEnd( void );
void UI_ConnectionProgress_Precache( void );
void UI_ConnectionProgress_Connect( const char *server );
void UI_ConnectionProgress_ChangeLevel( void );
void UI_ConnectionProgress_ParseServerInfo( const char *server );
// defined as exported to keep compatibility with old interface
extern "C" EXPORT void AddTouchButtonToList( const char *name, const char *texture, const char *command, unsigned char *color, int flags );
// ScreenHeight returns the height of the screen, in ppos.xels
#define ScreenHeight ((float)(gpGlobals->scrHeight))
// ScreenWidth returns the width of the screen, in ppos.xels
#define ScreenWidth ((float)(gpGlobals->scrWidth))
#define Alpha( x ) ( ((x) & 0xFF000000 ) >> 24 )
#define Red( x ) ( ((x) & 0xFF0000) >> 16 )
#define Green( x ) ( ((x) & 0xFF00 ) >> 8 )
#define Blue( x ) ( ((x) & 0xFF ) >> 0 )
inline unsigned int PackRGBA( const unsigned int r, const unsigned int g, const unsigned int b, const unsigned int a )
{
return ((a)<<24|(r)<<16|(g)<<8|(b));
}
inline unsigned int PackRGB( const unsigned int r, const unsigned int g, const unsigned int b )
{
return PackRGBA( r, g, b, 0xFF );
}
inline void UnpackRGB( int &r, int &g, int &b, const unsigned int ulRGB )
{
r = (ulRGB & 0xFF0000) >> 16;
g = (ulRGB & 0xFF00) >> 8;
b = (ulRGB & 0xFF) >> 0;
}
inline void UnpackRGBA( int &r, int &g, int &b, int &a, const unsigned int ulRGBA )
{
a = (ulRGBA & 0xFF000000) >> 24;
UnpackRGB( r, g, b, ulRGBA );
}
inline unsigned int PackAlpha( const unsigned int ulRGB, const unsigned int ulAlpha )
{
return (ulRGB & 0x00FFFFFF)|(ulAlpha<<24);
}
inline unsigned int UnpackAlpha( const unsigned int ulRGBA )
{
return ((ulRGBA & 0xFF000000) >> 24);
}
inline float InterpVal( const float from, const float to, const float frac )
{
return from + (to - from) * frac;
}
inline unsigned int InterpColor( const unsigned int from, const unsigned int to, const float frac )
{
return PackRGBA(
InterpVal( Red( from ), Red( to ), frac ),
InterpVal( Green( from ), Green( to ), frac ),
InterpVal( Blue( from ), Blue( to ), frac ),
InterpVal( Alpha( from ), Alpha( to ), frac ) );
}
inline float RemapVal( const float val, const float A, const float B, const float C, const float D)
{
return C + (D - C) * (val - A) / (B - A);
}
extern const unsigned int g_iColorTable[8];
int colorstricmp( const char *a, const char *b );
int colorstrcmp( const char *a, const char *b );
int ColorStrlen( const char *str ); // returns string length without color symbols
void COM_FileBase( const char *in, char *out, size_t size );
int UI_FadeAlpha( int starttime, int endtime );
const char *Info_ValueForKey( const char *s, const char *key );
int KEY_GetKey( const char *binding ); // ripped out from engine
char *StringCopy( const char *input ); // copy string into new memory
int COM_CompareSaves( const void **a, const void **b );
void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len );
void UI_EnableTextInput( bool enable );
void UI_LoadCustomStrings( void );
const char *L( const char *szStr ); // L means Localize!
void UI_FreeCustomStrings( void );
inline size_t Q_strncpy( char *dst, const char *src, size_t size )
{
size_t len;
if( unlikely( !dst || !src || !size ))
return 0;
len = strlen( src );
if( len + 1 > size ) // check if truncate
{
memcpy( dst, src, size - 1 );
dst[size - 1] = 0;
}
else memcpy( dst, src, len + 1 );
return len; // count does not include NULL
}
#define CS_SIZE 64 // size of one config string
#define CS_TIME 16 // size of time string
#define MAX_SCOREBOARDNAME 32 // engine and dlls allows only 32 chars
// color strings
#define ColorIndex( c ) ((( c ) - '0' ) & 7 )
#define IsColorString( p ) ( p && *( p ) == '^' && *(( p ) + 1) && *(( p ) + 1) >= '0' && *(( p ) + 1 ) <= '9' )
// stringize utilites
#define STR( x ) #x
#define STR2( x ) STR( x )
namespace UI
{
namespace Key
{
#ifndef K_A_BUTTON
#define K_A_BUTTON K_AUX1
#define K_B_BUTTON K_AUX2
#endif // K_A_BUTTON
inline bool IsEscape( int key )
{
switch( key )
{
case K_ESCAPE:
case K_B_BUTTON:
return true;
}
return false;
}
inline bool IsEnter( int key )
{
switch( key )
{
case K_ENTER:
case K_KP_ENTER:
case K_A_BUTTON:
return true;
}
return false;
}
inline bool IsBackspace( int key )
{
switch( key )
{
case K_BACKSPACE:
case K_X_BUTTON:
return true;
}
return false;
}
inline bool IsDelete( int key, bool ignoreBackspace = false )
{
if( key == K_DEL )
return true;
if( !ignoreBackspace )
return UI::Key::IsBackspace( key );
return false;
}
inline bool IsHome( int key )
{
switch( key )
{
case K_HOME:
case K_KP_HOME:
return true;
}
return false;
}
inline bool IsEnd( int key )
{
switch( key )
{
case K_HOME:
case K_KP_HOME:
return true;
}
return false;
}
inline bool IsLeftMouse( int key )
{
return key == K_MOUSE1;
}
inline bool IsMouse( int key )
{
switch( key )
{
case K_MOUSE1:
case K_MOUSE2:
case K_MOUSE3:
case K_MOUSE4:
case K_MOUSE5:
return true;
}
return false;
}
inline bool IsUpArrow( int key )
{
switch( key )
{
case K_UPARROW:
case K_KP_UPARROW:
case K_DPAD_UP:
return true;
}
return false;
}
inline bool IsDownArrow( int key )
{
switch( key )
{
case K_DOWNARROW:
case K_KP_DOWNARROW:
case K_DPAD_DOWN:
return true;
}
return false;
}
inline bool IsLeftArrow( int key )
{
switch( key )
{
case K_LEFTARROW:
case K_KP_LEFTARROW:
case K_DPAD_LEFT:
return true;
}
return false;
}
inline bool IsRightArrow( int key )
{
switch( key )
{
case K_RIGHTARROW:
case K_KP_RIGHTARROW:
case K_DPAD_RIGHT:
return true;
}
return false;
}
inline bool IsNavigationKey( int key )
{
return IsUpArrow( key ) ||
IsDownArrow( key ) ||
IsLeftArrow( key ) ||
IsRightArrow( key ) ||
key == K_TAB;
}
inline bool IsPageUp( int key )
{
switch( key )
{
case K_PGUP:
case K_KP_PGUP:
case K_L1_BUTTON:
return true;
}
return false;
}
inline bool IsPageDown( int key )
{
switch( key )
{
case K_PGDN:
case K_KP_PGDN:
case K_R1_BUTTON:
return true;
}
return false;
}
inline bool IsConsole( int key )
{
switch( key )
{
case '`':
case '~':
return true;
}
return false;
}
inline bool IsInsert( int key )
{
switch( key )
{
case K_INS:
case K_KP_INS:
return true;
}
return false;
}
inline bool IsAlphaNum( int key )
{
return ( key >= '0' && key <= '9' ) || ( key >= 'a' && key <= 'z' );
}
}
namespace Names
{
bool CheckIsNameValid( const char *name );
}
}
extern const int table_cp1251[64];
int Con_UtfProcessChar(int in );
int Con_UtfMoveLeft( const char *str, int pos );
int Con_UtfMoveRight( const char *str, int pos, int length );
char *Q_pretifymem( float value, int digitsafterdecimal );
#define Q_memprint( val ) Q_pretifymem( val, 2 )
#endif//UTILS_H