]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_screen.c
major speed gain in cavetest2 by checking if the portal is facing the wrong way
[divverent/darkplaces.git] / cl_screen.c
1
2 #include "quakedef.h"
3 #include "cl_video.h"
4
5 cvar_t scr_viewsize = {CVAR_SAVE, "viewsize","100"};
6 cvar_t scr_fov = {CVAR_SAVE, "fov","90"};       // 10 - 170
7 cvar_t scr_conspeed = {CVAR_SAVE, "scr_conspeed","900"}; // LordHavoc: quake used 300
8 cvar_t scr_centertime = {0, "scr_centertime","2"};
9 cvar_t scr_showram = {CVAR_SAVE, "showram","1"};
10 cvar_t scr_showturtle = {CVAR_SAVE, "showturtle","0"};
11 cvar_t scr_showpause = {CVAR_SAVE, "showpause","1"};
12 cvar_t scr_printspeed = {0, "scr_printspeed","8"};
13 cvar_t scr_2dresolution = {CVAR_SAVE, "scr_2dresolution", "1"};
14 cvar_t cl_avidemo = {0, "cl_avidemo", "0"};
15
16 qboolean        scr_initialized;                // ready to draw
17
18 float           scr_con_current;
19 float           scr_conlines;           // lines of console to display
20
21 int                     clearconsole;
22 int                     clearnotify;
23
24 qboolean        scr_drawloading = false;
25
26 static qbyte menuplyr_pixels[4096];
27
28 void DrawCrosshair(int num);
29 void V_CalcRefdef (void);
30 static void SCR_ScreenShot_f (void);
31 static void R_Envmap_f (void);
32
33 // backend
34 void R_ClearScreen(void);
35
36 /*
37 ===============================================================================
38
39 CENTER PRINTING
40
41 ===============================================================================
42 */
43
44 char            scr_centerstring[1024];
45 float           scr_centertime_start;   // for slow victory printing
46 float           scr_centertime_off;
47 int                     scr_center_lines;
48 int                     scr_erase_lines;
49 int                     scr_erase_center;
50
51 /*
52 ==============
53 SCR_CenterPrint
54
55 Called for important messages that should stay in the center of the screen
56 for a few moments
57 ==============
58 */
59 void SCR_CenterPrint (char *str)
60 {
61         strncpy (scr_centerstring, str, sizeof(scr_centerstring)-1);
62         scr_centertime_off = scr_centertime.value;
63         scr_centertime_start = cl.time;
64
65 // count the number of lines for centering
66         scr_center_lines = 1;
67         while (*str)
68         {
69                 if (*str == '\n')
70                         scr_center_lines++;
71                 str++;
72         }
73 }
74
75
76 void SCR_DrawCenterString (void)
77 {
78         char    *start;
79         int             l;
80         int             x, y;
81         int             remaining;
82
83 // the finale prints the characters one at a time
84         if (cl.intermission)
85                 remaining = scr_printspeed.value * (cl.time - scr_centertime_start);
86         else
87                 remaining = 9999;
88
89         scr_erase_center = 0;
90         start = scr_centerstring;
91
92         if (scr_center_lines <= 4)
93                 y = vid.conheight*0.35;
94         else
95                 y = 48;
96
97         do
98         {
99         // scan the width of the line
100                 for (l=0 ; l<40 ; l++)
101                         if (start[l] == '\n' || !start[l])
102                                 break;
103                 x = (vid.conwidth - l*8)/2;
104                 if (l > 0)
105                 {
106                         if (remaining < l)
107                                 l = remaining;
108                         DrawQ_String(x, y, start, l, 8, 8, 1, 1, 1, 1, 0);
109                         remaining -= l;
110                         if (remaining <= 0)
111                                 return;
112                 }
113
114                 y += 8;
115
116                 while (*start && *start != '\n')
117                         start++;
118
119                 if (!*start)
120                         break;
121                 start++;                // skip the \n
122         } while (1);
123 }
124
125 void SCR_CheckDrawCenterString (void)
126 {
127         if (scr_center_lines > scr_erase_lines)
128                 scr_erase_lines = scr_center_lines;
129
130         scr_centertime_off -= host_frametime;
131
132         // don't draw if this is a normal stats-screen intermission,
133         // only if it is not an intermission, or a finale intermission
134         if (cl.intermission == 1)
135                 return;
136         if (scr_centertime_off <= 0 && !cl.intermission)
137                 return;
138         if (key_dest != key_game)
139                 return;
140
141         SCR_DrawCenterString ();
142 }
143
144 /*
145 ==============
146 SCR_DrawTurtle
147 ==============
148 */
149 void SCR_DrawTurtle (void)
150 {
151         static int      count;
152
153         if (cls.state != ca_connected)
154                 return;
155
156         if (!scr_showturtle.integer)
157                 return;
158
159         if (host_frametime < 0.1)
160         {
161                 count = 0;
162                 return;
163         }
164
165         count++;
166         if (count < 3)
167                 return;
168
169         DrawQ_Pic (0, 0, "turtle", 0, 0, 1, 1, 1, 1, 0);
170 }
171
172 /*
173 ==============
174 SCR_DrawNet
175 ==============
176 */
177 void SCR_DrawNet (void)
178 {
179         if (cls.state != ca_connected)
180                 return;
181         if (realtime - cl.last_received_message < 0.3)
182                 return;
183         if (cls.demoplayback)
184                 return;
185
186         DrawQ_Pic (64, 0, "net", 0, 0, 1, 1, 1, 1, 0);
187 }
188
189 /*
190 ==============
191 DrawPause
192 ==============
193 */
194 void SCR_DrawPause (void)
195 {
196         cachepic_t      *pic;
197
198         if (cls.state != ca_connected)
199                 return;
200
201         if (!scr_showpause.integer)             // turn off for screenshots
202                 return;
203
204         if (!cl.paused)
205                 return;
206
207         pic = Draw_CachePic ("gfx/pause.lmp");
208         DrawQ_Pic ((vid.conwidth - pic->width)/2, (vid.conheight - pic->height)/2, "gfx/pause.lmp", 0, 0, 1, 1, 1, 1, 0);
209 }
210
211
212
213 /*
214 ==============
215 SCR_DrawLoading
216 ==============
217 */
218 void SCR_DrawLoading (void)
219 {
220         cachepic_t      *pic;
221
222         pic = Draw_CachePic ("gfx/loading.lmp");
223         DrawQ_Pic ((vid.conwidth - pic->width)/2, (vid.conheight - pic->height)/2, "gfx/loading.lmp", 0, 0, 1, 1, 1, 1, 0);
224 }
225
226
227
228 //=============================================================================
229
230
231 /*
232 ==================
233 SCR_SetUpToDrawConsole
234 ==================
235 */
236 void SCR_SetUpToDrawConsole (void)
237 {
238         Con_CheckResize ();
239
240         if (key_dest == key_game && cls.signon != SIGNONS)
241                 key_consoleactive |= KEY_CONSOLEACTIVE_FORCED;
242         else
243                 key_consoleactive &= ~KEY_CONSOLEACTIVE_FORCED;
244
245 // decide on the height of the console
246         if (key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
247                 scr_conlines = vid.conheight; // full screen
248         else if (key_consoleactive & KEY_CONSOLEACTIVE_USER)
249                 scr_conlines = vid.conheight/2; // half screen
250         else
251                 scr_conlines = 0;                               // none visible
252
253         if (scr_conspeed.value)
254         {
255                 if (scr_conlines < scr_con_current)
256                 {
257                         scr_con_current -= scr_conspeed.value*host_realframetime;
258                         if (scr_conlines > scr_con_current)
259                                 scr_con_current = scr_conlines;
260
261                 }
262                 else if (scr_conlines > scr_con_current)
263                 {
264                         scr_con_current += scr_conspeed.value*host_realframetime;
265                         if (scr_conlines < scr_con_current)
266                                 scr_con_current = scr_conlines;
267                 }
268         }
269         else
270                 scr_con_current = scr_conlines;
271 }
272
273 /*
274 ==================
275 SCR_DrawConsole
276 ==================
277 */
278 void SCR_DrawConsole (void)
279 {
280         if (scr_con_current)
281         {
282                 Con_DrawConsole (scr_con_current);
283                 clearconsole = 0;
284         }
285         else
286         {
287                 if (key_dest == key_game || key_dest == key_message)
288                         Con_DrawNotify ();      // only draw notify in game
289         }
290 }
291
292 /*
293 ===============
294 SCR_BeginLoadingPlaque
295
296 ================
297 */
298 void SCR_BeginLoadingPlaque (void)
299 {
300         if (scr_drawloading)
301                 return;
302
303         S_StopAllSounds (true);
304
305         scr_drawloading = true;
306         CL_UpdateScreen ();
307         scr_drawloading = true;
308         CL_UpdateScreen ();
309 }
310
311 //=============================================================================
312
313 char r_speeds_string[1024];
314 int speedstringcount, r_timereport_active;
315 double r_timereport_temp = 0, r_timereport_current = 0, r_timereport_start = 0;
316
317 void R_TimeReport(char *desc)
318 {
319         char tempbuf[256];
320         int length;
321         int t;
322
323         if (!r_timereport_active)
324                 return;
325
326         r_timereport_temp = r_timereport_current;
327         r_timereport_current = Sys_DoubleTime();
328         t = (int) ((r_timereport_current - r_timereport_temp) * 1000000.0);
329
330         sprintf(tempbuf, "%8i %s", t, desc);
331         length = strlen(tempbuf);
332         while (length < 20)
333                 tempbuf[length++] = ' ';
334         tempbuf[length] = 0;
335         if (speedstringcount + length > (vid.conwidth / 8))
336         {
337                 strcat(r_speeds_string, "\n");
338                 speedstringcount = 0;
339         }
340         // skip the space at the beginning if it's the first on the line
341         if (speedstringcount == 0)
342         {
343                 strcat(r_speeds_string, tempbuf + 1);
344                 speedstringcount = length - 1;
345         }
346         else
347         {
348                 strcat(r_speeds_string, tempbuf);
349                 speedstringcount += length;
350         }
351 }
352
353 void R_TimeReport_Start(void)
354 {
355         r_timereport_active = r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected;
356         r_speeds_string[0] = 0;
357         if (r_timereport_active)
358         {
359                 speedstringcount = 0;
360                 AngleVectors (r_refdef.viewangles, vpn, NULL, NULL);
361                 sprintf(r_speeds_string,
362                         "org:'%+8.2f %+8.2f %+8.2f' ang:'%+4.0f %+4.0f %+4.0f' dir:'%+2.3f %+2.3f %+2.3f'\n"
363                         "world:%6i faces%6i nodes%6i leafs%6i dlitwalls\n"
364                         "%5i models%5i bmodels%5i sprites%6i particles%4i dlights\n"
365                         "%6i modeltris%6i meshs%6i meshtris\n",
366                         r_refdef.vieworg[0], r_refdef.vieworg[1], r_refdef.vieworg[2], r_refdef.viewangles[0], r_refdef.viewangles[1], r_refdef.viewangles[2], vpn[0], vpn[1], vpn[2],
367                         c_faces, c_nodes, c_leafs, c_light_polys,
368                         c_models, c_bmodels, c_sprites, c_particles, c_dlights,
369                         c_alias_polys, c_meshs, c_meshtris);
370
371                 c_alias_polys = 0;
372                 c_light_polys = 0;
373                 c_faces = 0;
374                 c_nodes = 0;
375                 c_leafs = 0;
376                 c_models = 0;
377                 c_bmodels = 0;
378                 c_sprites = 0;
379                 c_particles = 0;
380
381                 r_timereport_start = Sys_DoubleTime();
382         }
383 }
384
385 void R_TimeReport_End(void)
386 {
387         r_timereport_current = r_timereport_start;
388         R_TimeReport("total");
389
390         if (r_timereport_active)
391         {
392                 int i, j, lines, y;
393                 lines = 1;
394                 for (i = 0;r_speeds_string[i];i++)
395                         if (r_speeds_string[i] == '\n')
396                                 lines++;
397                 y = vid.conheight - sb_lines - lines * 8;
398                 i = j = 0;
399                 DrawQ_Fill(0, y, vid.conwidth, lines * 8, 0, 0, 0, 0.5, 0);
400                 while (r_speeds_string[i])
401                 {
402                         j = i;
403                         while (r_speeds_string[i] && r_speeds_string[i] != '\n')
404                                 i++;
405                         if (i - j > 0)
406                                 DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0);
407                         if (r_speeds_string[i] == '\n')
408                                 i++;
409                         y += 8;
410                 }
411         }
412 }
413
414 /*
415 =================
416 SCR_SizeUp_f
417
418 Keybinding command
419 =================
420 */
421 void SCR_SizeUp_f (void)
422 {
423         Cvar_SetValue ("viewsize",scr_viewsize.value+10);
424 }
425
426
427 /*
428 =================
429 SCR_SizeDown_f
430
431 Keybinding command
432 =================
433 */
434 void SCR_SizeDown_f (void)
435 {
436         Cvar_SetValue ("viewsize",scr_viewsize.value-10);
437 }
438
439 void CL_Screen_Init(void)
440 {
441         qpic_t *dat;
442
443         Cvar_RegisterVariable (&scr_fov);
444         Cvar_RegisterVariable (&scr_viewsize);
445         Cvar_RegisterVariable (&scr_conspeed);
446         Cvar_RegisterVariable (&scr_showram);
447         Cvar_RegisterVariable (&scr_showturtle);
448         Cvar_RegisterVariable (&scr_showpause);
449         Cvar_RegisterVariable (&scr_centertime);
450         Cvar_RegisterVariable (&scr_printspeed);
451         Cvar_RegisterVariable (&scr_2dresolution);
452         Cvar_RegisterVariable (&cl_avidemo);
453
454         Cmd_AddCommand ("sizeup",SCR_SizeUp_f);
455         Cmd_AddCommand ("sizedown",SCR_SizeDown_f);
456         Cmd_AddCommand ("screenshot",SCR_ScreenShot_f);
457         Cmd_AddCommand ("envmap", R_Envmap_f);
458
459         scr_initialized = true;
460
461         // HACK HACK HACK
462         // load the image data for the player image in the config menu
463         dat = (qpic_t *)COM_LoadFile ("gfx/menuplyr.lmp", false);
464         if (!dat)
465                 Sys_Error("unable to load gfx/menuplyr.lmp");
466         SwapPic (dat);
467
468         if (dat->width*dat->height <= 4096)
469                 memcpy (menuplyr_pixels, dat->data, dat->width * dat->height);
470         else
471                 Con_Printf("gfx/menuplyr.lmp larger than 4k buffer");
472         Mem_Free(dat);
473 }
474
475 void DrawQ_Clear(void)
476 {
477         r_refdef.drawqueuesize = 0;
478 }
479
480 void DrawQ_Pic(float x, float y, char *picname, float width, float height, float red, float green, float blue, float alpha, int flags)
481 {
482         int size;
483         drawqueue_t *dq;
484         if (alpha < (1.0f / 255.0f) || !picname || !picname[0])
485                 return;
486         size = sizeof(*dq) + ((strlen(picname) + 1 + 3) & ~3);
487         if (r_refdef.drawqueuesize + size > r_refdef.maxdrawqueuesize)
488                 return;
489         red = bound(0, red, 1);
490         green = bound(0, green, 1);
491         blue = bound(0, blue, 1);
492         alpha = bound(0, alpha, 1);
493         dq = (void *)(r_refdef.drawqueue + r_refdef.drawqueuesize);
494         dq->size = size;
495         dq->command = DRAWQUEUE_PIC;
496         dq->flags = flags;
497         dq->color = ((unsigned int) (red * 255.0f) << 24) | ((unsigned int) (green * 255.0f) << 16) | ((unsigned int) (blue * 255.0f) << 8) | ((unsigned int) (alpha * 255.0f));
498         dq->x = x;
499         dq->y = y;
500         // if these are not zero, they override the pic's size
501         dq->scalex = width;
502         dq->scaley = height;
503         strcpy((char *)(dq + 1), picname);
504         r_refdef.drawqueuesize += dq->size;
505 }
506
507 void DrawQ_String(float x, float y, char *string, int maxlen, float scalex, float scaley, float red, float green, float blue, float alpha, int flags)
508 {
509         int size, len;
510         drawqueue_t *dq;
511         char *out;
512         if (alpha < (1.0f / 255.0f))
513                 return;
514         if (maxlen < 1)
515                 len = strlen(string);
516         else
517                 for (len = 0;len < maxlen && string[len];len++);
518         for (;len > 0 && string[0] == ' ';string++, x += scalex, len--);
519         for (;len > 0 && string[len - 1] == ' ';len--);
520         if (len < 1)
521                 return;
522         if (x >= vid.conwidth || y >= vid.conheight || x < (-scalex * maxlen) || y < (-scaley))
523                 return;
524         size = sizeof(*dq) + ((len + 1 + 3) & ~3);
525         if (r_refdef.drawqueuesize + size > r_refdef.maxdrawqueuesize)
526                 return;
527         red = bound(0, red, 1);
528         green = bound(0, green, 1);
529         blue = bound(0, blue, 1);
530         alpha = bound(0, alpha, 1);
531         dq = (void *)(r_refdef.drawqueue + r_refdef.drawqueuesize);
532         dq->size = size;
533         dq->command = DRAWQUEUE_STRING;
534         dq->flags = flags;
535         dq->color = ((unsigned int) (red * 255.0f) << 24) | ((unsigned int) (green * 255.0f) << 16) | ((unsigned int) (blue * 255.0f) << 8) | ((unsigned int) (alpha * 255.0f));
536         dq->x = x;
537         dq->y = y;
538         dq->scalex = scalex;
539         dq->scaley = scaley;
540         out = (char *)(dq + 1);
541         memcpy(out, string, len);
542         out[len] = 0;
543         r_refdef.drawqueuesize += dq->size;
544 }
545
546 void DrawQ_Fill (float x, float y, float w, float h, float red, float green, float blue, float alpha, int flags)
547 {
548         int size;
549         drawqueue_t *dq;
550         if (alpha < (1.0f / 255.0f))
551                 return;
552         size = sizeof(*dq) + 4;
553         if (r_refdef.drawqueuesize + size > r_refdef.maxdrawqueuesize)
554                 return;
555         red = bound(0, red, 1);
556         green = bound(0, green, 1);
557         blue = bound(0, blue, 1);
558         alpha = bound(0, alpha, 1);
559         dq = (void *)(r_refdef.drawqueue + r_refdef.drawqueuesize);
560         dq->size = size;
561         dq->command = DRAWQUEUE_PIC;
562         dq->flags = flags;
563         dq->color = ((unsigned int) (red * 255.0f) << 24) | ((unsigned int) (green * 255.0f) << 16) | ((unsigned int) (blue * 255.0f) << 8) | ((unsigned int) (alpha * 255.0f));
564         dq->x = x;
565         dq->y = y;
566         dq->scalex = w;
567         dq->scaley = h;
568         // empty pic name
569         *((char *)(dq + 1)) = 0;
570         r_refdef.drawqueuesize += dq->size;
571 }
572
573 void DrawQ_Mesh (drawqueuemesh_t *mesh, int flags)
574 {
575         int size;
576         void *p;
577         drawqueue_t *dq;
578         drawqueuemesh_t *m;
579         size = sizeof(*dq);
580         size += sizeof(drawqueuemesh_t);
581         size += sizeof(int) * mesh->numindices;
582         size += sizeof(float[3]) * mesh->numvertices;
583         size += sizeof(float[2]) * mesh->numvertices;
584         size += sizeof(float[4]) * mesh->numvertices;
585         if (r_refdef.drawqueuesize + size > r_refdef.maxdrawqueuesize)
586                 return;
587         dq = (void *)(r_refdef.drawqueue + r_refdef.drawqueuesize);
588         dq->size = size;
589         dq->command = DRAWQUEUE_MESH;
590         dq->flags = flags;
591         dq->color = 0;
592         dq->x = 0;
593         dq->y = 0;
594         dq->scalex = 0;
595         dq->scaley = 0;
596         p = (void *)(dq + 1);
597         m = p;(qbyte *)p += sizeof(drawqueuemesh_t);
598         m->numindices = mesh->numindices;
599         m->numvertices = mesh->numvertices;
600         m->texture = mesh->texture;
601         m->indices   = p;memcpy(m->indices  , mesh->indices  , m->numindices  * sizeof(int     ));(qbyte *)p += m->numindices  * sizeof(int     );
602         m->vertices  = p;memcpy(m->vertices , mesh->vertices , m->numvertices * sizeof(float[3]));(qbyte *)p += m->numvertices * sizeof(float[3]);
603         m->texcoords = p;memcpy(m->texcoords, mesh->texcoords, m->numvertices * sizeof(float[2]));(qbyte *)p += m->numvertices * sizeof(float[2]);
604         m->colors    = p;memcpy(m->colors   , mesh->colors   , m->numvertices * sizeof(float[4]));(qbyte *)p += m->numvertices * sizeof(float[4]);
605         r_refdef.drawqueuesize += dq->size;
606 }
607
608 /*
609 ====================
610 CalcFov
611 ====================
612 */
613 float CalcFov (float fov_x, float width, float height)
614 {
615         // calculate vision size and alter by aspect, then convert back to angle
616         return atan (height / (width / tan(fov_x/360*M_PI))) * 360 / M_PI;
617 }
618
619 /*
620 =================
621 SCR_CalcRefdef
622
623 Must be called whenever vid changes
624 Internal use only
625 =================
626 */
627 static void SCR_CalcRefdef (void)
628 {
629         float size;
630         int contents;
631
632 //========================================
633
634 // bound viewsize
635         if (scr_viewsize.value < 30)
636                 Cvar_Set ("viewsize","30");
637         if (scr_viewsize.value > 120)
638                 Cvar_Set ("viewsize","120");
639
640 // bound field of view
641         if (scr_fov.value < 10)
642                 Cvar_Set ("fov","10");
643         if (scr_fov.value > 170)
644                 Cvar_Set ("fov","170");
645
646 // intermission is always full screen
647         if (cl.intermission)
648         {
649                 size = 1;
650                 sb_lines = 0;
651         }
652         else
653         {
654                 if (scr_viewsize.value >= 120)
655                         sb_lines = 0;           // no status bar at all
656                 else if (scr_viewsize.value >= 110)
657                         sb_lines = 24;          // no inventory
658                 else
659                         sb_lines = 24+16+8;
660                 size = scr_viewsize.value * (1.0 / 100.0);
661         }
662
663         if (size >= 1)
664         {
665                 r_refdef.width = vid.realwidth;
666                 r_refdef.height = vid.realheight;
667                 r_refdef.x = 0;
668                 r_refdef.y = 0;
669         }
670         else
671         {
672                 r_refdef.width = vid.realwidth * size;
673                 r_refdef.height = vid.realheight * size;
674                 r_refdef.x = (vid.realwidth - r_refdef.width)/2;
675                 r_refdef.y = (vid.realheight - r_refdef.height)/2;
676         }
677
678         r_refdef.width = bound(0, r_refdef.width, vid.realwidth);
679         r_refdef.height = bound(0, r_refdef.height, vid.realheight);
680         r_refdef.x = bound(0, r_refdef.x, vid.realwidth - r_refdef.width) + vid.realx;
681         r_refdef.y = bound(0, r_refdef.y, vid.realheight - r_refdef.height) + vid.realy;
682
683         // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
684         r_refdef.fov_x = scr_fov.value * cl.viewzoom;
685         r_refdef.fov_y = CalcFov (r_refdef.fov_x, r_refdef.width, r_refdef.height);
686
687         if (cl.worldmodel)
688         {
689                 Mod_CheckLoaded(cl.worldmodel);
690                 contents = Mod_PointContents(r_refdef.vieworg, cl.worldmodel);
691                 if (contents != CONTENTS_EMPTY && contents != CONTENTS_SOLID)
692                 {
693                         r_refdef.fov_x *= (sin(cl.time * 4.7) * 0.015 + 0.985);
694                         r_refdef.fov_y *= (sin(cl.time * 3.0) * 0.015 + 0.985);
695                 }
696         }
697 }
698
699 /*
700 ==================
701 SCR_ScreenShot_f
702 ==================
703 */
704 void SCR_ScreenShot_f (void)
705 {
706         static int i = 0;
707         char filename[16];
708         char checkname[MAX_OSPATH];
709 //
710 // find a file name to save it to
711 //
712         for (; i<=9999 ; i++)
713         {
714                 sprintf (filename, "dp%04i.tga", i);
715                 sprintf (checkname, "%s/%s", com_gamedir, filename);
716                 if (Sys_FileTime(checkname) == -1)
717                         break;  // file doesn't exist
718         }
719         if (i==10000)
720         {
721                 Con_Printf ("SCR_ScreenShot_f: Couldn't create a TGA file\n");
722                 return;
723         }
724
725         if (SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight))
726                 Con_Printf ("Wrote %s\n", filename);
727         else
728                 Con_Printf ("unable to write %s\n", filename);
729 }
730
731 static int cl_avidemo_frame = 0;
732
733 void SCR_CaptureAVIDemo(void)
734 {
735         char filename[32];
736         sprintf(filename, "dpavi%06d.tga", cl_avidemo_frame);
737         if (SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight))
738                 cl_avidemo_frame++;
739         else
740         {
741                 Cvar_SetValueQuick(&cl_avidemo, 0);
742                 Con_Printf("avi saving failed on frame %i, out of disk space?  stopping avi demo catpure.\n", cl_avidemo_frame);
743                 cl_avidemo_frame = 0;
744         }
745 }
746
747 /*
748 ===============
749 R_Envmap_f
750
751 Grab six views for environment mapping tests
752 ===============
753 */
754 struct
755 {
756         float angles[3];
757         char *name;
758 }
759 envmapinfo[6] =
760 {
761         {{  0,   0, 0}, "ft"},
762         {{  0,  90, 0}, "rt"},
763         {{  0, 180, 0}, "bk"},
764         {{  0, 270, 0}, "lf"},
765         {{-90,  90, 0}, "up"},
766         {{ 90,  90, 0}, "dn"}
767 };
768
769 static void R_Envmap_f (void)
770 {
771         int j, size;
772         char filename[256], basename[256];
773
774         if (Cmd_Argc() != 3)
775         {
776                 Con_Printf ("envmap <basename> <size>: save out 6 cubic environment map images, usable with loadsky, note that size must one of 128, 256, 512, or 1024 and can't be bigger than your current resolution\n");
777                 return;
778         }
779
780         strcpy(basename, Cmd_Argv(1));
781         size = atoi(Cmd_Argv(2));
782         if (size != 128 && size != 256 && size != 512 && size != 1024)
783         {
784                 Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
785                 return;
786         }
787         if (size > vid.realwidth || size > vid.realheight)
788         {
789                 Con_Printf("envmap: your resolution is not big enough to render that size\n");
790                 return;
791         }
792
793         envmap = true;
794
795         r_refdef.x = 0;
796         r_refdef.y = 0;
797         r_refdef.width = size;
798         r_refdef.height = size;
799
800         r_refdef.fov_x = 90;
801         r_refdef.fov_y = 90;
802
803         for (j = 0;j < 6;j++)
804         {
805                 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
806                 VectorCopy(envmapinfo[j].angles, r_refdef.viewangles);
807                 R_ClearScreen();
808                 R_RenderView ();
809                 SCR_ScreenShot(filename, vid.realx, vid.realy, size, size);
810         }
811
812         envmap = false;
813 }
814
815 //=============================================================================
816
817 // LordHavoc: SHOWLMP stuff
818 #define SHOWLMP_MAXLABELS 256
819 typedef struct showlmp_s
820 {
821         qboolean        isactive;
822         float           x;
823         float           y;
824         char            label[32];
825         char            pic[128];
826 }
827 showlmp_t;
828
829 showlmp_t showlmp[SHOWLMP_MAXLABELS];
830
831 void SHOWLMP_decodehide(void)
832 {
833         int i;
834         qbyte *lmplabel;
835         lmplabel = MSG_ReadString();
836         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
837                 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
838                 {
839                         showlmp[i].isactive = false;
840                         return;
841                 }
842 }
843
844 void SHOWLMP_decodeshow(void)
845 {
846         int i, k;
847         qbyte lmplabel[256], picname[256];
848         float x, y;
849         strcpy(lmplabel,MSG_ReadString());
850         strcpy(picname, MSG_ReadString());
851         if (gamemode == GAME_NEHAHRA) // LordHavoc: nasty old legacy junk
852         {
853                 x = MSG_ReadByte();
854                 y = MSG_ReadByte();
855         }
856         else
857         {
858                 x = MSG_ReadShort();
859                 y = MSG_ReadShort();
860         }
861         k = -1;
862         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
863                 if (showlmp[i].isactive)
864                 {
865                         if (strcmp(showlmp[i].label, lmplabel) == 0)
866                         {
867                                 k = i;
868                                 break; // drop out to replace it
869                         }
870                 }
871                 else if (k < 0) // find first empty one to replace
872                         k = i;
873         if (k < 0)
874                 return; // none found to replace
875         // change existing one
876         showlmp[k].isactive = true;
877         strcpy(showlmp[k].label, lmplabel);
878         strcpy(showlmp[k].pic, picname);
879         showlmp[k].x = x;
880         showlmp[k].y = y;
881 }
882
883 void SHOWLMP_drawall(void)
884 {
885         int i;
886         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
887                 if (showlmp[i].isactive)
888                         DrawQ_Pic(showlmp[i].x, showlmp[i].y, showlmp[i].pic, 0, 0, 1, 1, 1, 1, 0);
889 }
890
891 void SHOWLMP_clear(void)
892 {
893         int i;
894         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
895                 showlmp[i].isactive = false;
896 }
897
898 void CL_SetupScreenSize(void)
899 {
900         static float old2dresolution = -1;
901
902         VID_GetWindowSize (&vid.realx, &vid.realy, &vid.realwidth, &vid.realheight);
903
904         VID_UpdateGamma(false);
905
906         if (scr_2dresolution.value != old2dresolution)
907         {
908                 Cvar_SetValue("scr_2dresolution", bound(0.0f, scr_2dresolution.value, 1.0f));
909                 old2dresolution = scr_2dresolution.value;
910         }
911
912         if (vid.realwidth > 320)
913         {
914                 vid.conwidth = (vid.realwidth - 320) * scr_2dresolution.value + 320;
915                 vid.conwidth = bound(320, vid.conwidth, vid.realwidth);
916         }
917         else
918                 vid.conwidth = 320;
919
920         if (vid.realheight > 240)
921         {
922                 vid.conheight = (vid.realheight - 240) * scr_2dresolution.value + 240;
923                 vid.conheight = bound(240, vid.conheight, vid.realheight);
924         }
925         else
926                 vid.conheight = 240;
927
928         SCR_SetUpToDrawConsole();
929
930         // determine size of refresh window
931         SCR_CalcRefdef();
932 }
933
934 void CL_UpdateScreen(void)
935 {
936         if (!scr_initialized || !con_initialized || vid_hidden)
937                 return;                         // not initialized yet
938
939         if (cl_avidemo.integer)
940                 SCR_CaptureAVIDemo();
941         else
942                 cl_avidemo_frame = 0;
943
944         if (cls.signon == SIGNONS)
945                 R_TimeReport("other");
946
947         CL_SetupScreenSize();
948
949         DrawQ_Clear();
950
951         V_UpdateBlends();
952         V_CalcRefdef ();
953
954         if (cls.signon == SIGNONS)
955                 R_TimeReport("setup");
956
957         //FIXME: force menu if nothing else to look at?
958         //if (key_dest == key_game && cls.signon != SIGNONS && cls.state == ca_disconnected)
959
960         if (scr_drawloading)
961         {
962                 scr_drawloading = false;
963                 SCR_DrawLoading();
964         }
965         else
966         {
967                 if (cls.signon == SIGNONS)
968                 {
969                         SCR_DrawNet ();
970                         SCR_DrawTurtle ();
971                         SCR_DrawPause ();
972                         Sbar_Draw();
973                         SHOWLMP_drawall();
974                         SCR_CheckDrawCenterString();
975                 }
976                 ui_draw();
977                 CL_DrawVideo();
978                 M_Draw();
979                 if (cls.signon == SIGNONS)
980                 {
981                         R_TimeReport("2d");
982                         R_TimeReport_End();
983                         R_TimeReport_Start();
984                 }
985         }
986         SCR_DrawConsole();
987
988         SCR_UpdateScreen();
989 }
990
991 void CL_Screen_NewMap(void)
992 {
993         SHOWLMP_clear();
994 }
995