forked from FWGS/mainui_cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.cpp
678 lines (558 loc) · 11.4 KB
/
Utils.cpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
Copyright (C) 1997-2001 Id Software, Inc.
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 2
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// ui_qmenu.c -- Quake menu framework
#include "extdll_menu.h"
#include "BaseMenu.h"
#include "Utils.h"
#include "keydefs.h"
#include "BtnsBMPTable.h"
#if defined _WIN32
#undef GetParent
#include <winlite.h>
#elif defined __APPLE__
#include <sys/time.h>
#elif defined __DOS__
// nothing
#else
#include <time.h>
#endif
#ifdef _DEBUG
void DBG_AssertFunction( bool fExpr, const char* szExpr, const char* szFile, int szLine, const char* szMessage )
{
if( fExpr ) return;
char szOut[512];
if( szMessage != NULL )
sprintf( szOut, "ASSERT FAILED:\n %s \n(%s@%d)\n%s", szExpr, szFile, szLine, szMessage );
else sprintf( szOut, "ASSERT FAILED:\n %s \n(%s@%d)", szExpr, szFile, szLine );
Host_Error( szOut );
}
#endif // DEBUG
int colorstrcmp( const char *s1, const char *s2 )
{
int c1, c2;
if( s1 == NULL )
{
if( s2 == NULL )
return 0;
else return -1;
}
else if( s2 == NULL )
{
return 1;
}
do {
if( IsColorString( s1 ))
{
s1 += 2;
}
if( IsColorString( s2 ))
{
s2 += 2;
}
c1 = *s1++;
c2 = *s2++;
if( c1 != c2 ) return c1 < c2 ? -1 : 1;
} while( c1 );
// strings are equal
return 0;
}
int colorstricmp( const char *s1, const char *s2 )
{
int c1, c2;
if( s1 == NULL )
{
if( s2 == NULL )
return 0;
else return -1;
}
else if( s2 == NULL )
{
return 1;
}
do {
if( IsColorString( s1 ))
{
s1 += 2;
}
if( IsColorString( s2 ))
{
s2 += 2;
}
c1 = *s1++;
c2 = *s2++;
if( c1 != c2 )
{
if( c1 >= 'a' && c1 <= 'z' ) c1 -= ('a' - 'A');
if( c2 >= 'a' && c2 <= 'z' ) c2 -= ('a' - 'A');
if( c1 != c2 ) return c1 < c2 ? -1 : 1;
}
} while( c1 );
// strings are equal
return 0;
}
int ColorStrlen( const char *str )
{
const char *p;
if( !str )
return 0;
int len = 0;
p = str;
EngFuncs::UtfProcessChar( 0 );
while( *p )
{
if( IsColorString( p ))
{
p += 2;
continue;
}
p++;
if( EngFuncs::UtfProcessChar( (unsigned char) *p ) )
len++;
}
EngFuncs::UtfProcessChar( 0 );
return len;
}
int ColorPrexfixCount( const char *str )
{
const char *p;
if( !str )
return 0;
int len = 0;
p = str;
//EngFuncs::UtfProcessChar(0);
while( *p )
{
if( IsColorString( p ))
{
len += 2;
p += 2;
continue;
}
//if(!EngFuncs::UtfProcessChar((unsigned char)*p))
//len++;
p++;
}
//EngFuncs::UtfProcessChar(0);
return len;
}
char *StringCopy( const char *input )
{
if( !input ) return NULL;
char *out = new char[strlen( input ) + 1];
strcpy( out, input );
return out;
}
/*
============
COM_CompareSaves
============
*/
int COM_CompareSaves( const void **a, const void **b )
{
char *file1, *file2;
file1 = (char *)*a;
file2 = (char *)*b;
int bResult;
EngFuncs::CompareFileTime( file2, file1, &bResult );
return bResult;
}
/*
============
COM_FileBase
============
*/
// Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
void COM_FileBase ( const char *in, char *out )
{
int len, start, end;
len = strlen( in );
// scan backward for '.'
end = len - 1;
while ( end && in[end] != '.' && in[end] != '/' && in[end] != '\\' )
end--;
if ( in[end] != '.' ) // no '.', copy to end
end = len-1;
else
end--; // Found ',', copy to left of '.'
// Scan backward for '/'
start = len-1;
while ( start >= 0 && in[start] != '/' && in[start] != '\\' )
start--;
if ( in[start] != '/' && in[start] != '\\' )
start = 0;
else
start++;
// Length of new sting
len = end - start + 1;
// Copy partial string
strncpy( out, &in[start], len );
// Terminate it
out[len] = 0;
}
/*
===============
Info_ValueForKey
Searches the string for the given
key and returns the associated value, or an empty string.
===============
*/
const char *Info_ValueForKey( const char *s, const char *key )
{
char pkey[MAX_INFO_STRING];
static char value[2][MAX_INFO_STRING]; // use two buffers so compares work without stomping on each other
static int valueindex;
char *o;
valueindex ^= 1;
if( *s == '\\' ) s++;
while( 1 )
{
o = pkey;
while( *s != '\\' && *s != '\n' )
{
if( !*s ) return "";
*o++ = *s++;
}
*o = 0;
s++;
o = value[valueindex];
while( *s != '\\' && *s != '\n' && *s )
{
if( !*s ) return "";
*o++ = *s++;
}
*o = 0;
if( !strcmp( key, pkey ))
return value[valueindex];
if( !*s ) return "";
s++;
}
}
/*
===================
Key_GetKey
===================
*/
int KEY_GetKey( const char *binding )
{
const char *b;
if ( !binding )
return -1;
for ( int i = 0; i < 256; i++ )
{
b = EngFuncs::KEY_GetBinding( i );
if( !b ) continue;
if( !stricmp( binding, b ))
return i;
}
return -1;
}
/*
================
UI_FadeAlpha
================
*/
int UI_FadeAlpha( int starttime, int endtime )
{
int time, fade_time;
if( starttime == 0 )
{
return 0xFFFFFFFF;
}
time = ( gpGlobals->time * 1000 ) - starttime;
if( time >= endtime )
{
return 0x00FFFFFF;
}
// fade time is 1/4 of endtime
fade_time = endtime / 4;
fade_time = bound( 300, fade_time, 10000 );
int alpha;
// fade out
if(( endtime - time ) < fade_time )
alpha = bound( 0, (( endtime - time ) * 1.0f / fade_time ) * 255, 255 );
else alpha = 255;
return PackRGBA( 255, 255, 255, alpha );
}
void UI_EnableTextInput( bool enable )
{
uiStatic.textInput = enable;
EngFuncs::EnableTextInput( enable );
}
// Doesn't need anymore
/*
void *operator new( size_t a )
{
return MALLOC( a );
}
void *operator new[]( size_t a )
{
return MALLOC( a );
}
void operator delete( void *ptr )
{
if( ptr ) FREE( ptr );
}
void operator delete[]( void *ptr )
{
if( ptr ) FREE( ptr );
}
*/
CBMP* CBMP::LoadFile( const char *filename )
{
int length = 0;
bmp_t *bmp = (bmp_t*)EngFuncs::COM_LoadFile( filename, &length );
// cannot load
if( !bmp )
return NULL;
// too small for BMP
if( (size_t)length < sizeof( bmp_t ))
return NULL;
// not a BMP
if( bmp->id[0] != 'B' || bmp->id[1] != 'M' )
return NULL;
// bogus data
if( !bmp->width || !bmp->height )
return NULL;
CBMP *ret = new CBMP( bmp->width, bmp->height );
memcpy( ret->GetBitmap(), bmp, length );
EngFuncs::COM_FreeFile( bmp );
return ret;
}
const int table_cp1251[64] = {
0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x007F, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,
0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,
0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457
};
/*
============================
Con_UtfProcessChar
Ripped from engine.
Xash3D FWGS uses multibyte, converting it to current single-byte encoding
Converting to single-byte not necessary anymore, as UI uses custom font render which works with 32-bit chars
============================
*/
int Con_UtfProcessChar( int in )
{
if( UI_IsXashFWGS() )
{
static int m = -1, k = 0; //multibyte state
static int uc = 0; //unicode char
if( !in )
{
m = -1;
k = 0;
uc = 0;
return 0;
}
// Get character length
if(m == -1)
{
uc = 0;
if( in >= 0xF8 )
{
return 0;
}
else if( in >= 0xF0 )
{
uc = in & 0x07;
m = 3;
}
else if( in >= 0xE0 )
{
uc = in & 0x0F;
m = 2;
}
else if( in >= 0xC0 )
{
uc = in & 0x1F;
m = 1;
}
else if( in <= 0x7F )
{
return in; // ascii
}
// return 0 if we need more chars to decode one
k = 0;
return 0;
}
// get more chars
else if( k <= m )
{
uc <<= 6;
uc += in & 0x3F;
k++;
}
if( in > 0xBF || m < 0 )
{
m = -1;
return 0;
}
if( k == m )
{
k = m = -1;
return uc;
// not implemented yet
// return '?';
}
return 0;
}
else
{
if( in >= 0x80 && in <= 0xBF )
{
return table_cp1251[in - 0x80];
}
else if( in >= 0xC0 && in <= 0xFF )
{
return in - 0xC0 + 0x410;
}
return in;
}
}
/*
=================
Con_UtfMoveLeft
get position of previous printful char
=================
*/
int Con_UtfMoveLeft( const char *str, int pos )
{
int i, k = 0;
// int j;
Con_UtfProcessChar( 0 );
if(pos == 1) return 0;
for( i = 0; i < pos-1; i++ )
if( Con_UtfProcessChar( (unsigned char)str[i] ) )
k = i+1;
Con_UtfProcessChar( 0 );
return k;
}
/*
=================
Con_UtfMoveRight
get next of previous printful char
=================
*/
int Con_UtfMoveRight( const char *str, int pos, int length )
{
int i;
Con_UtfProcessChar( 0 );
for( i = pos; i <= length; i++ )
{
if( Con_UtfProcessChar( (unsigned char)str[i] ) )
return i+1;
}
Con_UtfProcessChar( 0 );
return pos + 1;
}
bool UI::Names::CheckIsNameValid(const char *name)
{
if( !name || !*name )
return false;
// exclude some default names, that may be set from engine or just come with config files
static struct
{
const char *name;
bool substring;
} prohibitedNames[] =
{
{ "default", false, },
{ "unnamed", false, },
{ "Player", false, },
{ "<Warrior> Player", false, },
{ "Shinji", false, },
{ "CSDuragiCOM", true },
{ "Nero Claudius", true }, // *purrrt* you found a secret area!
};
for( size_t i = 0; i < V_ARRAYSIZE( prohibitedNames ); i++ )
{
if( prohibitedNames[i].substring )
{
if( strstr( name, prohibitedNames[i].name ) )
{
return false;
}
}
else
{
if( !stricmp( name, prohibitedNames[i].name ) )
{
return false;
}
}
}
return true;
}
void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len )
{
char c;
int scripting = (int)EngFuncs::GetCvarFloat( "cmd_scripting" );
while( (c = *oldCommand++) && len > 1 )
{
if( c == '"' )
{
*newCommand++ = '\\';
len--;
}
if( scripting && c == '$')
{
*newCommand++ = '$';
len--;
}
*newCommand++ = c;
len--;
}
*newCommand++ = 0;
}
/*
================
Sys_DoubleTime
================
*/
double Sys_DoubleTime( void )
{
if( UI_IsXashFWGS())
{
return EngFuncs::DoubleTime();
}
#if defined _WIN32
static LARGE_INTEGER g_PerformanceFrequency;
static LARGE_INTEGER g_ClockStart;
LARGE_INTEGER CurrentTime;
if( !g_PerformanceFrequency.QuadPart )
{
QueryPerformanceFrequency( &g_PerformanceFrequency );
QueryPerformanceCounter( &g_ClockStart );
}
QueryPerformanceCounter( &CurrentTime );
return (double)( CurrentTime.QuadPart - g_ClockStart.QuadPart ) / (double)( g_PerformanceFrequency.QuadPart );
#elif defined __DOS__
// fallback when no time api
return gpGlobals->time + 0.01;
#elif defined __APPLE__
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) tv.tv_sec + (double) tv.tv_usec/1000000.0;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double) ts.tv_sec + (double) ts.tv_nsec/1000000000.0;
#endif
}