Debian Codegraph @

code/ui/ui_atoms.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 /**********************************************************************
24 UI_ATOMS.C
25
26 User interface building blocks and support functions.
27 **********************************************************************/
28 #include "ui_local.h"
29
30 qboolean m_entersound; // after a frame, so caching won't disrupt the sound
31
32 void QDECL Com_Error( int level, const char *error, ... ) {
33 va_list argptr;
34 char text[1024];
35
36 va_start (argptr, error);
37 Q_vsnprintf (text, sizeof(text), error, argptr);
38 va_end (argptr);
39
40 trap_Error( text );
41 }
42
43 void QDECL Com_Printf( const char *msg, ... ) {
44 va_list argptr;
45 char text[1024];
46
47 va_start (argptr, msg);
48 Q_vsnprintf (text, sizeof(text), msg, argptr);
49 va_end (argptr);
50
51 trap_Print( text );
52 }
53
54 qboolean newUI = qfalse;
55
56
57 /*
58 =================
59 UI_ClampCvar
60 =================
61 */
62 float UI_ClampCvar( float min, float max, float value )
63 {
64 if ( value < min ) return min;
65 if ( value > max ) return max;
66 return value;
67 }
68
69 /*
70 =================
71 UI_StartDemoLoop
72 =================
73 */
74 void UI_StartDemoLoop( void ) {
75 trap_Cmd_ExecuteText( EXEC_APPEND, "d1\n" );
76 }
77
78
79 #ifndef MISSIONPACK
80 static void NeedCDAction( qboolean result ) {
81 if ( !result ) {
82 trap_Cmd_ExecuteText( EXEC_APPEND, "quit\n" );
83 }
84 }
85 #endif // MISSIONPACK
86
87 #ifndef MISSIONPACK
88 static void NeedCDKeyAction( qboolean result ) {
89 if ( !result ) {
90 trap_Cmd_ExecuteText( EXEC_APPEND, "quit\n" );
91 }
92 }
93 #endif // MISSIONPACK
94
95 char *UI_Argv( int arg ) {
96 static char buffer[MAX_STRING_CHARS];
97
98 trap_Argv( arg, buffer, sizeof( buffer ) );
99
100 return buffer;
101 }
102
103
104 char *UI_Cvar_VariableString( const char *var_name ) {
105 static char buffer[MAX_STRING_CHARS];
106
107 trap_Cvar_VariableStringBuffer( var_name, buffer, sizeof( buffer ) );
108
109 return buffer;
110 }
111
112
113
114 void UI_SetBestScores(postGameInfo_t *newInfo, qboolean postGame) {
115 trap_Cvar_Set("ui_scoreAccuracy", va("%i%%", newInfo->accuracy));
116 trap_Cvar_Set("ui_scoreImpressives", va("%i", newInfo->impressives));
117 trap_Cvar_Set("ui_scoreExcellents", va("%i", newInfo->excellents));
118 trap_Cvar_Set("ui_scoreDefends", va("%i", newInfo->defends));
119 trap_Cvar_Set("ui_scoreAssists", va("%i", newInfo->assists));
120 trap_Cvar_Set("ui_scoreGauntlets", va("%i", newInfo->gauntlets));
121 trap_Cvar_Set("ui_scoreScore", va("%i", newInfo->score));
122 trap_Cvar_Set("ui_scorePerfect", va("%i", newInfo->perfects));
123 trap_Cvar_Set("ui_scoreTeam", va("%i to %i", newInfo->redScore, newInfo->blueScore));
124 trap_Cvar_Set("ui_scoreBase", va("%i", newInfo->baseScore));
125 trap_Cvar_Set("ui_scoreTimeBonus", va("%i", newInfo->timeBonus));
126 trap_Cvar_Set("ui_scoreSkillBonus", va("%i", newInfo->skillBonus));
127 trap_Cvar_Set("ui_scoreShutoutBonus", va("%i", newInfo->shutoutBonus));
128 trap_Cvar_Set("ui_scoreTime", va("%02i:%02i", newInfo->time / 60, newInfo->time % 60));
129 trap_Cvar_Set("ui_scoreCaptures", va("%i", newInfo->captures));
130 if (postGame) {
131 trap_Cvar_Set("ui_scoreAccuracy2", va("%i%%", newInfo->accuracy));
132 trap_Cvar_Set("ui_scoreImpressives2", va("%i", newInfo->impressives));
133 trap_Cvar_Set("ui_scoreExcellents2", va("%i", newInfo->excellents));
134 trap_Cvar_Set("ui_scoreDefends2", va("%i", newInfo->defends));
135 trap_Cvar_Set("ui_scoreAssists2", va("%i", newInfo->assists));
136 trap_Cvar_Set("ui_scoreGauntlets2", va("%i", newInfo->gauntlets));
137 trap_Cvar_Set("ui_scoreScore2", va("%i", newInfo->score));
138 trap_Cvar_Set("ui_scorePerfect2", va("%i", newInfo->perfects));
139 trap_Cvar_Set("ui_scoreTeam2", va("%i to %i", newInfo->redScore, newInfo->blueScore));
140 trap_Cvar_Set("ui_scoreBase2", va("%i", newInfo->baseScore));
141 trap_Cvar_Set("ui_scoreTimeBonus2", va("%i", newInfo->timeBonus));
142 trap_Cvar_Set("ui_scoreSkillBonus2", va("%i", newInfo->skillBonus));
143 trap_Cvar_Set("ui_scoreShutoutBonus2", va("%i", newInfo->shutoutBonus));
144 trap_Cvar_Set("ui_scoreTime2", va("%02i:%02i", newInfo->time / 60, newInfo->time % 60));
145 trap_Cvar_Set("ui_scoreCaptures2", va("%i", newInfo->captures));
146 }
147 }
148
149 void UI_LoadBestScores(const char *map, int game)
150 {
151 char fileName[MAX_QPATH];
152 fileHandle_t f;
153 postGameInfo_t newInfo;
154 int protocol, protocolLegacy;
155
156 memset(&newInfo, 0, sizeof(postGameInfo_t));
157 Com_sprintf(fileName, MAX_QPATH, "games/%s_%i.game", map, game);
158 if (trap_FS_FOpenFile(fileName, &f, FS_READ) >= 0) {
159 int size = 0;
160 trap_FS_Read(&size, sizeof(int), f);
161 if (size == sizeof(postGameInfo_t)) {
162 trap_FS_Read(&newInfo, sizeof(postGameInfo_t), f);
163 }
164 trap_FS_FCloseFile(f);
165 }
166 UI_SetBestScores(&newInfo, qfalse);
167
168 uiInfo.demoAvailable = qfalse;
169
170 protocolLegacy = trap_Cvar_VariableValue("com_legacyprotocol");
171 protocol = trap_Cvar_VariableValue("com_protocol");
172
173 if(!protocol)
174 protocol = trap_Cvar_VariableValue("protocol");
175 if(protocolLegacy == protocol)
176 protocolLegacy = 0;
177
178 Com_sprintf(fileName, MAX_QPATH, "demos/%s_%d.%s%d", map, game, DEMOEXT, protocol);
179 if(trap_FS_FOpenFile(fileName, &f, FS_READ) >= 0)
180 {
181 uiInfo.demoAvailable = qtrue;
182 trap_FS_FCloseFile(f);
183 }
184 else if(protocolLegacy > 0)
185 {
186 Com_sprintf(fileName, MAX_QPATH, "demos/%s_%d.%s%d", map, game, DEMOEXT, protocolLegacy);
187 if (trap_FS_FOpenFile(fileName, &f, FS_READ) >= 0)
188 {
189 uiInfo.demoAvailable = qtrue;
190 trap_FS_FCloseFile(f);
191 }
192 }
193 }
194
195 /*
196 ===============
197 UI_ClearScores
198 ===============
199 */
200 void UI_ClearScores(void) {
201 char gameList[4096];
202 char *gameFile;
203 int i, len, count, size;
204 fileHandle_t f;
205 postGameInfo_t newInfo;
206
207 count = trap_FS_GetFileList( "games", "game", gameList, sizeof(gameList) );
208
209 size = sizeof(postGameInfo_t);
210 memset(&newInfo, 0, size);
211
212 if (count > 0) {
213 gameFile = gameList;
214 for ( i = 0; i < count; i++ ) {
215 len = strlen(gameFile);
216 if (trap_FS_FOpenFile(va("games/%s",gameFile), &f, FS_WRITE) >= 0) {
217 trap_FS_Write(&size, sizeof(int), f);
218 trap_FS_Write(&newInfo, size, f);
219 trap_FS_FCloseFile(f);
220 }
221 gameFile += len + 1;
222 }
223 }
224
225 UI_SetBestScores(&newInfo, qfalse);
226
227 }
228
229
230
231 static void UI_Cache_f( void ) {
232 Display_CacheAll();
233 }
234
235 /*
236 =======================
237 UI_CalcPostGameStats
238 =======================
239 */
240 static void UI_CalcPostGameStats( void ) {
241 char map[MAX_QPATH];
242 char fileName[MAX_QPATH];
243 char info[MAX_INFO_STRING];
244 fileHandle_t f;
245 int size, game, time, adjustedTime;
246 postGameInfo_t oldInfo;
247 postGameInfo_t newInfo;
248 qboolean newHigh = qfalse;
249
250 trap_GetConfigString( CS_SERVERINFO, info, sizeof(info) );
251 Q_strncpyz( map, Info_ValueForKey( info, "mapname" ), sizeof(map) );
252 game = atoi(Info_ValueForKey(info, "g_gametype"));
253
254 // compose file name
255 Com_sprintf(fileName, MAX_QPATH, "games/%s_%i.game", map, game);
256 // see if we have one already
257 memset(&oldInfo, 0, sizeof(postGameInfo_t));
258 if (trap_FS_FOpenFile(fileName, &f, FS_READ) >= 0) {
259 // if so load it
260 size = 0;
261 trap_FS_Read(&size, sizeof(int), f);
262 if (size == sizeof(postGameInfo_t)) {
263 trap_FS_Read(&oldInfo, sizeof(postGameInfo_t), f);
264 }
265 trap_FS_FCloseFile(f);
266 }
267
268 newInfo.accuracy = atoi(UI_Argv(3));
269 newInfo.impressives = atoi(UI_Argv(4));
270 newInfo.excellents = atoi(UI_Argv(5));
271 newInfo.defends = atoi(UI_Argv(6));
272 newInfo.assists = atoi(UI_Argv(7));
273 newInfo.gauntlets = atoi(UI_Argv(8));
274 newInfo.baseScore = atoi(UI_Argv(9));
275 newInfo.perfects = atoi(UI_Argv(10));
276 newInfo.redScore = atoi(UI_Argv(11));
277 newInfo.blueScore = atoi(UI_Argv(12));
278 time = atoi(UI_Argv(13));
279 newInfo.captures = atoi(UI_Argv(14));
280
281 newInfo.time = (time - trap_Cvar_VariableValue("ui_matchStartTime")) / 1000;
282 adjustedTime = uiInfo.mapList[ui_currentMap.integer].timeToBeat[game];
283 if (newInfo.time < adjustedTime) {
284 newInfo.timeBonus = (adjustedTime - newInfo.time) * 10;
285 } else {
286 newInfo.timeBonus = 0;
287 }
288
289 if (newInfo.redScore > newInfo.blueScore && newInfo.blueScore <= 0) {
290 newInfo.shutoutBonus = 100;
291 } else {
292 newInfo.shutoutBonus = 0;
293 }
294
295 newInfo.skillBonus = trap_Cvar_VariableValue("g_spSkill");
296 if (newInfo.skillBonus <= 0) {
297 newInfo.skillBonus = 1;
298 }
299 newInfo.score = newInfo.baseScore + newInfo.shutoutBonus + newInfo.timeBonus;
300 newInfo.score *= newInfo.skillBonus;
301
302 // see if the score is higher for this one
303 newHigh = (newInfo.redScore > newInfo.blueScore && newInfo.score > oldInfo.score);
304
305 if (newHigh) {
306 // if so write out the new one
307 uiInfo.newHighScoreTime = uiInfo.uiDC.realTime + 20000;
308 if (trap_FS_FOpenFile(fileName, &f, FS_WRITE) >= 0) {
309 size = sizeof(postGameInfo_t);
310 trap_FS_Write(&size, sizeof(int), f);
311 trap_FS_Write(&newInfo, sizeof(postGameInfo_t), f);
312 trap_FS_FCloseFile(f);
313 }
314 }
315
316 if (newInfo.time < oldInfo.time) {
317 uiInfo.newBestTime = uiInfo.uiDC.realTime + 20000;
318 }
319
320 // put back all the ui overrides
321 trap_Cvar_Set("capturelimit", UI_Cvar_VariableString("ui_saveCaptureLimit"));
322 trap_Cvar_Set("fraglimit", UI_Cvar_VariableString("ui_saveFragLimit"));
323 trap_Cvar_Set("cg_drawTimer", UI_Cvar_VariableString("ui_drawTimer"));
324 trap_Cvar_Set("g_doWarmup", UI_Cvar_VariableString("ui_doWarmup"));
325 trap_Cvar_Set("g_Warmup", UI_Cvar_VariableString("ui_Warmup"));
326 trap_Cvar_Set("sv_pure", UI_Cvar_VariableString("ui_pure"));
327 trap_Cvar_Set("g_friendlyFire", UI_Cvar_VariableString("ui_friendlyFire"));
328
329 UI_SetBestScores(&newInfo, qtrue);
330 UI_ShowPostGame(newHigh);
331
332
333 }
334
335
336 /*
337 =================
338 UI_ConsoleCommand
339 =================
340 */
341 qboolean UI_ConsoleCommand( int realTime ) {
342 char *cmd;
343
344 uiInfo.uiDC.frameTime = realTime - uiInfo.uiDC.realTime;
345 uiInfo.uiDC.realTime = realTime;
346
347 cmd = UI_Argv( 0 );
348
349 // ensure minimum menu data is available
350 //Menu_Cache();
351
352 if ( Q_stricmp (cmd, "ui_test") == 0 ) {
353 UI_ShowPostGame(qtrue);
354 return qtrue;
355 }
356
357 if ( Q_stricmp (cmd, "ui_report") == 0 ) {
358 UI_Report();
359 return qtrue;
360 }
361
362 if ( Q_stricmp (cmd, "ui_load") == 0 ) {
363 UI_Load();
364 return qtrue;
365 }
366
367 if ( Q_stricmp (cmd, "remapShader") == 0 ) {
368 if (trap_Argc() == 4) {
369 char shader1[MAX_QPATH];
370 char shader2[MAX_QPATH];
371 char shader3[MAX_QPATH];
372
373 Q_strncpyz(shader1, UI_Argv(1), sizeof(shader1));
374 Q_strncpyz(shader2, UI_Argv(2), sizeof(shader2));
375 Q_strncpyz(shader3, UI_Argv(3), sizeof(shader3));
376
377 trap_R_RemapShader(shader1, shader2, shader3);
378 return qtrue;
379 }
380 }
381
382 if ( Q_stricmp (cmd, "postgame") == 0 ) {
383 UI_CalcPostGameStats();
384 return qtrue;
385 }
386
387 if ( Q_stricmp (cmd, "ui_cache") == 0 ) {
388 UI_Cache_f();
389 return qtrue;
390 }
391
392 if ( Q_stricmp (cmd, "ui_teamOrders") == 0 ) {
393 //UI_TeamOrdersMenu_f();
394 return qtrue;
395 }
396
397
398 if ( Q_stricmp (cmd, "ui_cdkey") == 0 ) {
399 //UI_CDKeyMenu_f();
400 return qtrue;
401 }
402
403 return qfalse;
404 }
405
406 /*
407 =================
408 UI_Shutdown
409 =================
410 */
411 void UI_Shutdown( void ) {
412 }
413
414 /*
415 ================
416 UI_AdjustFrom640
417
418 Adjusted for resolution and screen aspect ratio
419 ================
420 */
421 void UI_AdjustFrom640( float *x, float *y, float *w, float *h ) {
422 // expect valid pointers
423 *x = *x * uiInfo.uiDC.xscale + uiInfo.uiDC.bias;
424 *y *= uiInfo.uiDC.yscale;
425 *w *= uiInfo.uiDC.xscale;
426 *h *= uiInfo.uiDC.yscale;
427 }
428
429 void UI_DrawNamedPic( float x, float y, float width, float height, const char *picname ) {
430 qhandle_t hShader;
431
432 hShader = trap_R_RegisterShaderNoMip( picname );
433 UI_AdjustFrom640( &x, &y, &width, &height );
434 trap_R_DrawStretchPic( x, y, width, height, 0, 0, 1, 1, hShader );
435 }
436
437 void UI_DrawHandlePic( float x, float y, float w, float h, qhandle_t hShader ) {
438 float s0;
439 float s1;
440 float t0;
441 float t1;
442
443 if( w < 0 ) { // flip about vertical
444 w = -w;
445 s0 = 1;
446 s1 = 0;
447 }
448 else {
449 s0 = 0;
450 s1 = 1;
451 }
452
453 if( h < 0 ) { // flip about horizontal
454 h = -h;
455 t0 = 1;
456 t1 = 0;
457 }
458 else {
459 t0 = 0;
460 t1 = 1;
461 }
462
463 UI_AdjustFrom640( &x, &y, &w, &h );
464 trap_R_DrawStretchPic( x, y, w, h, s0, t0, s1, t1, hShader );
465 }
466
467 /*
468 ================
469 UI_FillRect
470
471 Coordinates are 640*480 virtual values
472 =================
473 */
474 void UI_FillRect( float x, float y, float width, float height, const float *color ) {
475 trap_R_SetColor( color );
476
477 UI_AdjustFrom640( &x, &y, &width, &height );
478 trap_R_DrawStretchPic( x, y, width, height, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
479
480 trap_R_SetColor( NULL );
481 }
482
483 void UI_DrawSides(float x, float y, float w, float h) {
484 UI_AdjustFrom640( &x, &y, &w, &h );
485 trap_R_DrawStretchPic( x, y, 1, h, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
486 trap_R_DrawStretchPic( x + w - 1, y, 1, h, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
487 }
488
489 void UI_DrawTopBottom(float x, float y, float w, float h) {
490 UI_AdjustFrom640( &x, &y, &w, &h );
491 trap_R_DrawStretchPic( x, y, w, 1, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
492 trap_R_DrawStretchPic( x, y + h - 1, w, 1, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
493 }
494 /*
495 ================
496 UI_DrawRect
497
498 Coordinates are 640*480 virtual values
499 =================
500 */
501 void UI_DrawRect( float x, float y, float width, float height, const float *color ) {
502 trap_R_SetColor( color );
503
504 UI_DrawTopBottom(x, y, width, height);
505 UI_DrawSides(x, y, width, height);
506
507 trap_R_SetColor( NULL );
508 }
509
510 void UI_SetColor( const float *rgba ) {
511 trap_R_SetColor( rgba );
512 }
513
514 void UI_UpdateScreen( void ) {
515 trap_UpdateScreen();
516 }
517
518
519 void UI_DrawTextBox (int x, int y, int width, int lines)
520 {
521 UI_FillRect( x + BIGCHAR_WIDTH/2, y + BIGCHAR_HEIGHT/2, ( width + 1 ) * BIGCHAR_WIDTH, ( lines + 1 ) * BIGCHAR_HEIGHT, colorBlack );
522 UI_DrawRect( x + BIGCHAR_WIDTH/2, y + BIGCHAR_HEIGHT/2, ( width + 1 ) * BIGCHAR_WIDTH, ( lines + 1 ) * BIGCHAR_HEIGHT, colorWhite );
523 }
524
525 qboolean UI_CursorInRect (int x, int y, int width, int height)
526 {
527 if (uiInfo.uiDC.cursorx < x ||
528 uiInfo.uiDC.cursory < y ||
529 uiInfo.uiDC.cursorx > x+width ||
530 uiInfo.uiDC.cursory > y+height)
531 return qfalse;
532
533 return qtrue;
534 }