]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_screen.c
added GL_DepthRange function and MATERIALFLAG_SHORTDEPTHRANGE (now used by viewmodels...
[divverent/darkplaces.git] / cl_screen.c
1
2 #include "quakedef.h"
3 #include "cl_video.h"
4 #include "image.h"
5 #include "jpeg.h"
6 #include "cl_collision.h"
7 #include "libcurl.h"
8 #include "csprogs.h"
9
10 // we have to include snd_main.h here only to get access to snd_renderbuffer->format.speed when writing the AVI headers
11 #include "snd_main.h"
12
13 cvar_t scr_viewsize = {CVAR_SAVE, "viewsize","100", "how large the view should be, 110 disables inventory bar, 120 disables status bar"};
14 cvar_t scr_fov = {CVAR_SAVE, "fov","90", "field of vision, 1-170 degrees, default 90, some players use 110-130"};
15 cvar_t scr_conalpha = {CVAR_SAVE, "scr_conalpha", "1", "opacity of console background"};
16 cvar_t scr_conbrightness = {CVAR_SAVE, "scr_conbrightness", "1", "brightness of console background (0 = black, 1 = image)"};
17 cvar_t scr_conforcewhiledisconnected = {0, "scr_conforcewhiledisconnected", "1", "forces fullscreen console while disconnected"};
18 cvar_t scr_menuforcewhiledisconnected = {0, "scr_menuforcewhiledisconnected", "0", "forces menu while disconnected"};
19 cvar_t scr_centertime = {0, "scr_centertime","2", "how long centerprint messages show"};
20 cvar_t scr_showram = {CVAR_SAVE, "showram","1", "show ram icon if low on surface cache memory (not used)"};
21 cvar_t scr_showturtle = {CVAR_SAVE, "showturtle","0", "show turtle icon when framerate is too low"};
22 cvar_t scr_showpause = {CVAR_SAVE, "showpause","1", "show pause icon when game is paused"};
23 cvar_t scr_showbrand = {0, "showbrand","0", "shows gfx/brand.tga in a corner of the screen (different values select different positions, including centered)"};
24 cvar_t scr_printspeed = {0, "scr_printspeed","0", "speed of intermission printing (episode end texts), a value of 0 disables the slow printing"};
25 cvar_t vid_conwidth = {CVAR_SAVE, "vid_conwidth", "640", "virtual width of 2D graphics system"};
26 cvar_t vid_conheight = {CVAR_SAVE, "vid_conheight", "480", "virtual height of 2D graphics system"};
27 cvar_t vid_pixelheight = {CVAR_SAVE, "vid_pixelheight", "1", "adjusts vertical field of vision to account for non-square pixels (1280x1024 on a CRT monitor for example)"};
28 cvar_t scr_screenshot_jpeg = {CVAR_SAVE, "scr_screenshot_jpeg","1", "save jpeg instead of targa"};
29 cvar_t scr_screenshot_jpeg_quality = {CVAR_SAVE, "scr_screenshot_jpeg_quality","0.9", "image quality of saved jpeg"};
30 cvar_t scr_screenshot_gammaboost = {CVAR_SAVE, "scr_screenshot_gammaboost","1", "gamma correction on saved screenshots and videos, 1.0 saves unmodified images"};
31 // scr_screenshot_name is defined in fs.c
32 cvar_t cl_capturevideo = {0, "cl_capturevideo", "0", "enables saving of video to a .avi file using uncompressed I420 colorspace and PCM audio, note that scr_screenshot_gammaboost affects the brightness of the output)"};
33 cvar_t cl_capturevideo_realtime = {0, "cl_capturevideo_realtime", "0", "causes video saving to operate in realtime (mostly useful while playing, not while capturing demos), this can produce a much lower quality video due to poor sound/video sync and will abort saving if your machine stalls for over 1 second"};
34 cvar_t cl_capturevideo_fps = {0, "cl_capturevideo_fps", "30", "how many frames per second to save (29.97 for NTSC, 30 for typical PC video, 15 can be useful)"};
35 cvar_t cl_capturevideo_number = {CVAR_SAVE, "cl_capturevideo_number", "1", "number to append to video filename, incremented each time a capture begins"};
36 cvar_t r_letterbox = {0, "r_letterbox", "0", "reduces vertical height of view to simulate a letterboxed movie effect (can be used by mods for cutscenes)"};
37 cvar_t r_stereo_separation = {0, "r_stereo_separation", "4", "separation distance of eyes in the world (negative values are only useful for cross-eyed viewing)"};
38 cvar_t r_stereo_sidebyside = {0, "r_stereo_sidebyside", "0", "side by side views for those who can't afford glasses but can afford eye strain (note: use a negative r_stereo_separation if you want cross-eyed viewing)"};
39 cvar_t r_stereo_redblue = {0, "r_stereo_redblue", "0", "red/blue anaglyph stereo glasses (note: most of these glasses are actually red/cyan, try that one too)"};
40 cvar_t r_stereo_redcyan = {0, "r_stereo_redcyan", "0", "red/cyan anaglyph stereo glasses, the kind given away at drive-in movies like Creature From The Black Lagoon In 3D"};
41 cvar_t r_stereo_redgreen = {0, "r_stereo_redgreen", "0", "red/green anaglyph stereo glasses (for those who don't mind yellow)"};
42 cvar_t r_stereo_angle = {0, "r_stereo_angle", "0", "separation angle of eyes (makes the views look different directions, as an example, 90 gives a 90 degree separation where the views are 45 degrees left and 45 degrees right)"};
43 cvar_t scr_zoomwindow = {CVAR_SAVE, "scr_zoomwindow", "0", "displays a zoomed in overlay window"};
44 cvar_t scr_zoomwindow_viewsizex = {CVAR_SAVE, "scr_zoomwindow_viewsizex", "20", "horizontal viewsize of zoom window"};
45 cvar_t scr_zoomwindow_viewsizey = {CVAR_SAVE, "scr_zoomwindow_viewsizey", "20", "vertical viewsize of zoom window"};
46 cvar_t scr_zoomwindow_fov = {CVAR_SAVE, "scr_zoomwindow_fov", "20", "fov of zoom window"};
47 cvar_t scr_stipple = {0, "scr_stipple", "0", "interlacing-like stippling of the display"};
48 cvar_t scr_refresh = {0, "scr_refresh", "1", "allows you to completely shut off rendering for benchmarking purposes"};
49
50
51 int jpeg_supported = false;
52
53 qboolean        scr_initialized;                // ready to draw
54
55 float           scr_con_current;
56
57 extern int      con_vislines;
58
59 static void SCR_ScreenShot_f (void);
60 static void R_Envmap_f (void);
61
62 // backend
63 void R_ClearScreen(void);
64
65 /*
66 ===============================================================================
67
68 CENTER PRINTING
69
70 ===============================================================================
71 */
72
73 char            scr_centerstring[MAX_INPUTLINE];
74 float           scr_centertime_start;   // for slow victory printing
75 float           scr_centertime_off;
76 int                     scr_center_lines;
77 int                     scr_erase_lines;
78 int                     scr_erase_center;
79
80 /*
81 ==============
82 SCR_CenterPrint
83
84 Called for important messages that should stay in the center of the screen
85 for a few moments
86 ==============
87 */
88 void SCR_CenterPrint(char *str)
89 {
90         strlcpy (scr_centerstring, str, sizeof (scr_centerstring));
91         scr_centertime_off = scr_centertime.value;
92         scr_centertime_start = cl.time;
93
94 // count the number of lines for centering
95         scr_center_lines = 1;
96         while (*str)
97         {
98                 if (*str == '\n')
99                         scr_center_lines++;
100                 str++;
101         }
102 }
103
104
105 void SCR_DrawCenterString (void)
106 {
107         char    *start;
108         int             l;
109         int             x, y;
110         int             remaining;
111         int             color;
112
113 // the finale prints the characters one at a time, except if printspeed is an absurdly high value
114         if (cl.intermission && scr_printspeed.value > 0 && scr_printspeed.value < 1000000)
115                 remaining = (int)(scr_printspeed.value * (cl.time - scr_centertime_start));
116         else
117                 remaining = 9999;
118
119         scr_erase_center = 0;
120         start = scr_centerstring;
121
122         if (remaining < 1)
123                 return;
124
125         if (scr_center_lines <= 4)
126                 y = (int)(vid_conheight.integer*0.35);
127         else
128                 y = 48;
129
130         color = -1;
131         do
132         {
133                 // scan the number of characters on the line, not counting color codes
134                 int chars = 0;
135                 for (l=0 ; l<vid_conwidth.integer/8 ; l++)
136                 {
137                         if (start[l] == '\n' || !start[l])
138                                 break;
139                         // color codes add no visible characters, so don't count them
140                         if (start[l] == STRING_COLOR_TAG && (start[l+1] >= '0' && start[l+1] <= '9'))
141                                 l++;
142                         else
143                                 chars++;
144                 }
145                 x = (vid_conwidth.integer - chars*8)/2;
146                 if (l > 0)
147                 {
148                         if (remaining < l)
149                                 l = remaining;
150                         DrawQ_ColoredString(x, y, start, l, 8, 8, 1, 1, 1, 1, 0, &color);
151                         remaining -= l;
152                         if (remaining <= 0)
153                                 return;
154                 }
155
156                 y += 8;
157
158                 while (*start && *start != '\n')
159                         start++;
160
161                 if (!*start)
162                         break;
163                 start++;                // skip the \n
164         } while (1);
165 }
166
167 void SCR_CheckDrawCenterString (void)
168 {
169         if (scr_center_lines > scr_erase_lines)
170                 scr_erase_lines = scr_center_lines;
171
172         if (cl.time > cl.oldtime)
173                 scr_centertime_off -= cl.time - cl.oldtime;
174
175         // don't draw if this is a normal stats-screen intermission,
176         // only if it is not an intermission, or a finale intermission
177         if (cl.intermission == 1)
178                 return;
179         if (scr_centertime_off <= 0 && !cl.intermission)
180                 return;
181         if (key_dest != key_game)
182                 return;
183
184         SCR_DrawCenterString ();
185 }
186
187 /*
188 ==============
189 SCR_DrawTurtle
190 ==============
191 */
192 void SCR_DrawTurtle (void)
193 {
194         static int      count;
195
196         if (cls.state != ca_connected)
197                 return;
198
199         if (!scr_showturtle.integer)
200                 return;
201
202         if (cl.realframetime < 0.1)
203         {
204                 count = 0;
205                 return;
206         }
207
208         count++;
209         if (count < 3)
210                 return;
211
212         DrawQ_Pic (0, 0, Draw_CachePic("gfx/turtle", true), 0, 0, 1, 1, 1, 1, 0);
213 }
214
215 /*
216 ==============
217 SCR_DrawNet
218 ==============
219 */
220 void SCR_DrawNet (void)
221 {
222         if (cls.state != ca_connected)
223                 return;
224         if (realtime - cl.last_received_message < 0.3)
225                 return;
226         if (cls.demoplayback)
227                 return;
228
229         DrawQ_Pic (64, 0, Draw_CachePic("gfx/net", true), 0, 0, 1, 1, 1, 1, 0);
230 }
231
232 /*
233 ==============
234 DrawPause
235 ==============
236 */
237 void SCR_DrawPause (void)
238 {
239         cachepic_t      *pic;
240
241         if (cls.state != ca_connected)
242                 return;
243
244         if (!scr_showpause.integer)             // turn off for screenshots
245                 return;
246
247         if (!cl.paused)
248                 return;
249
250         pic = Draw_CachePic ("gfx/pause", true);
251         DrawQ_Pic ((vid_conwidth.integer - pic->width)/2, (vid_conheight.integer - pic->height)/2, pic, 0, 0, 1, 1, 1, 1, 0);
252 }
253
254 /*
255 ==============
256 SCR_DrawBrand
257 ==============
258 */
259 void SCR_DrawBrand (void)
260 {
261         cachepic_t      *pic;
262         float           x, y;
263
264         if (!scr_showbrand.value)
265                 return;
266
267         pic = Draw_CachePic ("gfx/brand", true);
268
269         switch ((int)scr_showbrand.value)
270         {
271         case 1: // bottom left
272                 x = 0;
273                 y = vid_conheight.integer - pic->height;
274                 break;
275         case 2: // bottom centre
276                 x = (vid_conwidth.integer - pic->width) / 2;
277                 y = vid_conheight.integer - pic->height;
278                 break;
279         case 3: // bottom right
280                 x = vid_conwidth.integer - pic->width;
281                 y = vid_conheight.integer - pic->height;
282                 break;
283         case 4: // centre right
284                 x = vid_conwidth.integer - pic->width;
285                 y = (vid_conheight.integer - pic->height) / 2;
286                 break;
287         case 5: // top right
288                 x = vid_conwidth.integer - pic->width;
289                 y = 0;
290                 break;
291         case 6: // top centre
292                 x = (vid_conwidth.integer - pic->width) / 2;
293                 y = 0;
294                 break;
295         case 7: // top left
296                 x = 0;
297                 y = 0;
298                 break;
299         case 8: // centre left
300                 x = 0;
301                 y = (vid_conheight.integer - pic->height) / 2;
302                 break;
303         default:
304                 return;
305         }
306
307         DrawQ_Pic (x, y, pic, 0, 0, 1, 1, 1, 1, 0);
308 }
309
310 /*
311 ==============
312 SCR_DrawQWDownload
313 ==============
314 */
315 static int SCR_DrawQWDownload(int offset)
316 {
317         int len;
318         float x, y;
319         float size = 8;
320         char temp[256];
321         if (!cls.qw_downloadname[0])
322         {
323                 cls.qw_downloadspeedrate = 0;
324                 cls.qw_downloadspeedtime = realtime;
325                 cls.qw_downloadspeedcount = 0;
326                 return 0;
327         }
328         if (realtime >= cls.qw_downloadspeedtime + 1)
329         {
330                 cls.qw_downloadspeedrate = cls.qw_downloadspeedcount;
331                 cls.qw_downloadspeedtime = realtime;
332                 cls.qw_downloadspeedcount = 0;
333         }
334         if (cls.protocol == PROTOCOL_QUAKEWORLD)
335                 dpsnprintf(temp, sizeof(temp), "Downloading %s %3i%% (%i) at %i bytes/s\n", cls.qw_downloadname, cls.qw_downloadpercent, cls.qw_downloadmemorycursize, cls.qw_downloadspeedrate);
336         else
337                 dpsnprintf(temp, sizeof(temp), "Downloading %s %3i%% (%i/%i) at %i bytes/s\n", cls.qw_downloadname, cls.qw_downloadpercent, cls.qw_downloadmemorycursize, cls.qw_downloadmemorymaxsize, cls.qw_downloadspeedrate);
338         len = (int)strlen(temp);
339         x = (vid_conwidth.integer - len*size) / 2;
340         y = vid_conheight.integer - size - offset;
341         DrawQ_Pic(0, y, NULL, vid_conwidth.integer, size, 0, 0, 0, 0.5, 0);
342         DrawQ_String(x, y, temp, len, size, size, 1, 1, 1, 1, 0);
343         return 8;
344 }
345
346 /*
347 ==============
348 SCR_DrawCurlDownload
349 ==============
350 */
351 static int SCR_DrawCurlDownload(int offset)
352 {
353         int len;
354         int nDownloads;
355         int i;
356         float x, y;
357         float size = 8;
358         Curl_downloadinfo_t *downinfo;
359         char temp[256];
360         const char *addinfo;
361
362         downinfo = Curl_GetDownloadInfo(&nDownloads, &addinfo);
363         if(!downinfo)
364                 return 0;
365
366         y = vid_conheight.integer - size * nDownloads - offset;
367
368         if(addinfo)
369         {
370                 len = (int)strlen(addinfo);
371                 x = (vid_conwidth.integer - len*size) / 2;
372                 DrawQ_Pic(0, y - size, NULL, vid_conwidth.integer, size, 1, 1, 1, 0.8, 0);
373                 DrawQ_String(x, y - size, addinfo, len, size, size, 0, 0, 0, 1, 0);
374         }
375
376         for(i = 0; i != nDownloads; ++i)
377         {
378                 if(downinfo[i].queued)
379                         dpsnprintf(temp, sizeof(temp), "Still in queue: %s\n", downinfo[i].filename);
380                 else if(downinfo[i].progress <= 0)
381                         dpsnprintf(temp, sizeof(temp), "Downloading %s ...  ???.?%% @ %.1f KiB/s\n", downinfo[i].filename, downinfo[i].speed / 1024.0);
382                 else
383                         dpsnprintf(temp, sizeof(temp), "Downloading %s ...  %5.1f%% @ %.1f KiB/s\n", downinfo[i].filename, 100.0 * downinfo[i].progress, downinfo[i].speed / 1024.0);
384                 len = (int)strlen(temp);
385                 x = (vid_conwidth.integer - len*size) / 2;
386                 DrawQ_Pic(0, y + i * size, NULL, vid_conwidth.integer, size, 0, 0, 0, 0.8, 0);
387                 DrawQ_String(x, y + i * size, temp, len, size, size, 1, 1, 1, 1, 0);
388         }
389
390         Z_Free(downinfo);
391
392         return 8 * (nDownloads + (addinfo ? 1 : 0));
393 }
394
395 /*
396 ==============
397 SCR_DrawDownload
398 ==============
399 */
400 static void SCR_DrawDownload()
401 {
402         int offset = 0;
403         offset += SCR_DrawQWDownload(offset);
404         offset += SCR_DrawCurlDownload(offset);
405 }
406
407 //=============================================================================
408
409 /*
410 ==================
411 SCR_SetUpToDrawConsole
412 ==================
413 */
414 void SCR_SetUpToDrawConsole (void)
415 {
416         // lines of console to display
417         float conlines;
418         static int framecounter = 0;
419
420         Con_CheckResize ();
421
422         if (scr_menuforcewhiledisconnected.integer && key_dest == key_game && cls.state == ca_disconnected)
423         {
424                 if (framecounter >= 2)
425                         MR_ToggleMenu_f();
426                 else
427                         framecounter++;
428         }
429         else
430                 framecounter = 0;
431
432         if (scr_conforcewhiledisconnected.integer && key_dest == key_game && cls.signon != SIGNONS)
433                 key_consoleactive |= KEY_CONSOLEACTIVE_FORCED;
434         else
435                 key_consoleactive &= ~KEY_CONSOLEACTIVE_FORCED;
436
437 // decide on the height of the console
438         if (key_consoleactive & KEY_CONSOLEACTIVE_USER)
439                 conlines = vid_conheight.integer/2;     // half screen
440         else
441                 conlines = 0;                           // none visible
442
443         scr_con_current = conlines;
444 }
445
446 /*
447 ==================
448 SCR_DrawConsole
449 ==================
450 */
451 void SCR_DrawConsole (void)
452 {
453         if (key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
454         {
455                 // full screen
456                 Con_DrawConsole (vid_conheight.integer);
457         }
458         else if (scr_con_current)
459                 Con_DrawConsole ((int)scr_con_current);
460         else
461         {
462                 con_vislines = 0;
463                 if ((key_dest == key_game || key_dest == key_message) && !r_letterbox.value)
464                         Con_DrawNotify ();      // only draw notify in game
465         }
466 }
467
468 /*
469 ===============
470 SCR_BeginLoadingPlaque
471
472 ================
473 */
474 void SCR_BeginLoadingPlaque (void)
475 {
476         // save console log up to this point to log_file if it was set by configs
477         Log_Start();
478
479         Host_StartVideo();
480         S_StopAllSounds();
481         SCR_UpdateLoadingScreen(false);
482 }
483
484 //=============================================================================
485
486 char r_speeds_string[1024];
487 int speedstringcount, r_timereport_active;
488 double r_timereport_temp = 0, r_timereport_current = 0, r_timereport_start = 0;
489
490 void R_TimeReport(char *desc)
491 {
492         char tempbuf[256];
493         int length;
494         int t;
495
496         if (r_speeds.integer < 2 || !r_timereport_active)
497                 return;
498
499         CHECKGLERROR
500         qglFinish();CHECKGLERROR
501         r_timereport_temp = r_timereport_current;
502         r_timereport_current = Sys_DoubleTime();
503         t = (int) ((r_timereport_current - r_timereport_temp) * 1000000.0 + 0.5);
504
505         dpsnprintf(tempbuf, sizeof(tempbuf), "%8i %-11s", t, desc);
506         length = (int)strlen(tempbuf);
507         if (speedstringcount + length > (vid_conwidth.integer / 8))
508         {
509                 strlcat(r_speeds_string, "\n", sizeof(r_speeds_string));
510                 speedstringcount = 0;
511         }
512         strlcat(r_speeds_string, tempbuf, sizeof(r_speeds_string));
513         speedstringcount += length;
514 }
515
516 void R_TimeReport_Frame(void)
517 {
518         int i, j, lines, y;
519         cl_locnode_t *loc;
520
521         if (r_speeds_string[0])
522         {
523                 if (r_timereport_active)
524                 {
525                         r_timereport_current = r_timereport_start;
526                         R_TimeReport("total");
527                 }
528
529                 if (r_speeds_string[strlen(r_speeds_string)-1] == '\n')
530                         r_speeds_string[strlen(r_speeds_string)-1] = 0;
531                 lines = 1;
532                 for (i = 0;r_speeds_string[i];i++)
533                         if (r_speeds_string[i] == '\n')
534                                 lines++;
535                 y = vid_conheight.integer - sb_lines - lines * 8;
536                 i = j = 0;
537                 DrawQ_Pic(0, y, NULL, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
538                 while (r_speeds_string[i])
539                 {
540                         j = i;
541                         while (r_speeds_string[i] && r_speeds_string[i] != '\n')
542                                 i++;
543                         if (i - j > 0)
544                                 DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0);
545                         if (r_speeds_string[i] == '\n')
546                                 i++;
547                         y += 8;
548                 }
549                 r_speeds_string[0] = 0;
550                 r_timereport_active = false;
551         }
552         if (r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected)
553         {
554                 speedstringcount = 0;
555                 r_speeds_string[0] = 0;
556                 r_timereport_active = false;
557                 // put the location name in the r_speeds display as it greatly helps
558                 // when creating loc files
559                 loc = CL_Locs_FindNearest(cl.movement_origin);
560                 if (loc)
561                         sprintf(r_speeds_string + strlen(r_speeds_string), "Location: %s\n", loc->name);
562                 sprintf(r_speeds_string + strlen(r_speeds_string), "org:'%+8.2f %+8.2f %+8.2f' dir:'%+2.3f %+2.3f %+2.3f'\n", r_view.origin[0], r_view.origin[1], r_view.origin[2], r_view.forward[0], r_view.forward[1], r_view.forward[2]);
563                 sprintf(r_speeds_string + strlen(r_speeds_string), "%5i entities%6i surfaces%6i triangles%5i leafs%5i portals%6i particles\n", r_refdef.stats.entities, r_refdef.stats.entities_surfaces, r_refdef.stats.entities_triangles, r_refdef.stats.world_leafs, r_refdef.stats.world_portals, r_refdef.stats.particles);
564                 sprintf(r_speeds_string + strlen(r_speeds_string), "%4i lights%4i clears%4i scissored%7i light%7i shadow%7i dynamic\n", r_refdef.stats.lights, r_refdef.stats.lights_clears, r_refdef.stats.lights_scissored, r_refdef.stats.lights_lighttriangles, r_refdef.stats.lights_shadowtriangles, r_refdef.stats.lights_dynamicshadowtriangles);
565                 if (r_refdef.stats.bloom)
566                         sprintf(r_speeds_string + strlen(r_speeds_string), "rendered%6i meshes%8i triangles bloompixels%8i copied%8i drawn\n", r_refdef.stats.meshes, r_refdef.stats.meshes_elements / 3, r_refdef.stats.bloom_copypixels, r_refdef.stats.bloom_drawpixels);
567                 else
568                         sprintf(r_speeds_string + strlen(r_speeds_string), "rendered%6i meshes%8i triangles\n", r_refdef.stats.meshes, r_refdef.stats.meshes_elements / 3);
569
570                 memset(&r_refdef.stats, 0, sizeof(r_refdef.stats));
571
572                 if (r_speeds.integer >= 2)
573                 {
574                         r_timereport_active = true;
575                         r_timereport_start = r_timereport_current = Sys_DoubleTime();
576                 }
577         }
578 }
579
580 /*
581 =================
582 SCR_SizeUp_f
583
584 Keybinding command
585 =================
586 */
587 void SCR_SizeUp_f (void)
588 {
589         Cvar_SetValue ("viewsize",scr_viewsize.value+10);
590 }
591
592
593 /*
594 =================
595 SCR_SizeDown_f
596
597 Keybinding command
598 =================
599 */
600 void SCR_SizeDown_f (void)
601 {
602         Cvar_SetValue ("viewsize",scr_viewsize.value-10);
603 }
604
605 void CL_Screen_Init(void)
606 {
607         Cvar_RegisterVariable (&scr_fov);
608         Cvar_RegisterVariable (&scr_viewsize);
609         Cvar_RegisterVariable (&scr_conalpha);
610         Cvar_RegisterVariable (&scr_conbrightness);
611         Cvar_RegisterVariable (&scr_conforcewhiledisconnected);
612         Cvar_RegisterVariable (&scr_menuforcewhiledisconnected);
613         Cvar_RegisterVariable (&scr_showram);
614         Cvar_RegisterVariable (&scr_showturtle);
615         Cvar_RegisterVariable (&scr_showpause);
616         Cvar_RegisterVariable (&scr_showbrand);
617         Cvar_RegisterVariable (&scr_centertime);
618         Cvar_RegisterVariable (&scr_printspeed);
619         Cvar_RegisterVariable (&vid_conwidth);
620         Cvar_RegisterVariable (&vid_conheight);
621         Cvar_RegisterVariable (&vid_pixelheight);
622         Cvar_RegisterVariable (&scr_screenshot_jpeg);
623         Cvar_RegisterVariable (&scr_screenshot_jpeg_quality);
624         Cvar_RegisterVariable (&scr_screenshot_gammaboost);
625         Cvar_RegisterVariable (&cl_capturevideo);
626         Cvar_RegisterVariable (&cl_capturevideo_realtime);
627         Cvar_RegisterVariable (&cl_capturevideo_fps);
628         Cvar_RegisterVariable (&cl_capturevideo_number);
629         Cvar_RegisterVariable (&r_letterbox);
630         Cvar_RegisterVariable(&r_stereo_separation);
631         Cvar_RegisterVariable(&r_stereo_sidebyside);
632         Cvar_RegisterVariable(&r_stereo_redblue);
633         Cvar_RegisterVariable(&r_stereo_redcyan);
634         Cvar_RegisterVariable(&r_stereo_redgreen);
635         Cvar_RegisterVariable(&r_stereo_angle);
636         Cvar_RegisterVariable(&scr_zoomwindow);
637         Cvar_RegisterVariable(&scr_zoomwindow_viewsizex);
638         Cvar_RegisterVariable(&scr_zoomwindow_viewsizey);
639         Cvar_RegisterVariable(&scr_zoomwindow_fov);
640         Cvar_RegisterVariable(&scr_stipple);
641         Cvar_RegisterVariable(&scr_refresh);
642
643         Cmd_AddCommand ("sizeup",SCR_SizeUp_f, "increase view size (increases viewsize cvar)");
644         Cmd_AddCommand ("sizedown",SCR_SizeDown_f, "decrease view size (decreases viewsize cvar)");
645         Cmd_AddCommand ("screenshot",SCR_ScreenShot_f, "takes a screenshot of the next rendered frame");
646         Cmd_AddCommand ("envmap", R_Envmap_f, "render a cubemap (skybox) of the current scene");
647
648         scr_initialized = true;
649 }
650
651 /*
652 ==================
653 SCR_ScreenShot_f
654 ==================
655 */
656 void SCR_ScreenShot_f (void)
657 {
658         static int shotnumber;
659         static char oldname[MAX_QPATH];
660         char base[MAX_QPATH];
661         char filename[MAX_QPATH];
662         unsigned char *buffer1;
663         unsigned char *buffer2;
664         unsigned char *buffer3;
665         qboolean jpeg = (scr_screenshot_jpeg.integer != 0);
666
667         sprintf (base, "screenshots/%s", scr_screenshot_name.string);
668
669         if (strcmp (oldname, scr_screenshot_name.string))
670         {
671                 sprintf(oldname, "%s", scr_screenshot_name.string);
672                 shotnumber = 0;
673         }
674
675         // find a file name to save it to
676         for (;shotnumber < 1000000;shotnumber++)
677                 if (!FS_SysFileExists(va("%s/%s%06d.tga", fs_gamedir, base, shotnumber)) && !FS_SysFileExists(va("%s/%s%06d.jpg", fs_gamedir, base, shotnumber)))
678                         break;
679         if (shotnumber >= 1000000)
680         {
681                 Con_Print("SCR_ScreenShot_f: Couldn't create the image file\n");
682                 return;
683         }
684
685         sprintf(filename, "%s%06d.%s", base, shotnumber, jpeg ? "jpg" : "tga");
686
687         buffer1 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
688         buffer2 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
689         buffer3 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3 + 18);
690
691         if (SCR_ScreenShot (filename, buffer1, buffer2, buffer3, 0, 0, vid.width, vid.height, false, false, false, jpeg, true))
692                 Con_Printf("Wrote %s\n", filename);
693         else
694                 Con_Printf("unable to write %s\n", filename);
695
696         Mem_Free (buffer1);
697         Mem_Free (buffer2);
698         Mem_Free (buffer3);
699
700         shotnumber++;
701 }
702
703 static void SCR_CaptureVideo_RIFF_Start(void)
704 {
705         memset(&cls.capturevideo.riffbuffer, 0, sizeof(sizebuf_t));
706         cls.capturevideo.riffbuffer.maxsize = sizeof(cls.capturevideo.riffbufferdata);
707         cls.capturevideo.riffbuffer.data = cls.capturevideo.riffbufferdata;
708 }
709
710 static void SCR_CaptureVideo_RIFF_Flush(void)
711 {
712         if (cls.capturevideo.riffbuffer.cursize > 0)
713         {
714                 if (!FS_Write(cls.capturevideo.videofile, cls.capturevideo.riffbuffer.data, cls.capturevideo.riffbuffer.cursize))
715                         cls.capturevideo.error = true;
716                 cls.capturevideo.riffbuffer.cursize = 0;
717                 cls.capturevideo.riffbuffer.overflowed = false;
718         }
719 }
720
721 static void SCR_CaptureVideo_RIFF_WriteBytes(const unsigned char *data, size_t size)
722 {
723         SCR_CaptureVideo_RIFF_Flush();
724         if (!FS_Write(cls.capturevideo.videofile, data, size))
725                 cls.capturevideo.error = true;
726 }
727
728 static void SCR_CaptureVideo_RIFF_Write32(int n)
729 {
730         if (cls.capturevideo.riffbuffer.cursize + 4 > cls.capturevideo.riffbuffer.maxsize)
731                 SCR_CaptureVideo_RIFF_Flush();
732         MSG_WriteLong(&cls.capturevideo.riffbuffer, n);
733 }
734
735 static void SCR_CaptureVideo_RIFF_Write16(int n)
736 {
737         if (cls.capturevideo.riffbuffer.cursize + 2 > cls.capturevideo.riffbuffer.maxsize)
738                 SCR_CaptureVideo_RIFF_Flush();
739         MSG_WriteShort(&cls.capturevideo.riffbuffer, n);
740 }
741
742 static void SCR_CaptureVideo_RIFF_WriteFourCC(const char *chunkfourcc)
743 {
744         if (cls.capturevideo.riffbuffer.cursize + (int)strlen(chunkfourcc) > cls.capturevideo.riffbuffer.maxsize)
745                 SCR_CaptureVideo_RIFF_Flush();
746         MSG_WriteUnterminatedString(&cls.capturevideo.riffbuffer, chunkfourcc);
747 }
748
749 static void SCR_CaptureVideo_RIFF_WriteTerminatedString(const char *string)
750 {
751         if (cls.capturevideo.riffbuffer.cursize + (int)strlen(string) > cls.capturevideo.riffbuffer.maxsize)
752                 SCR_CaptureVideo_RIFF_Flush();
753         MSG_WriteString(&cls.capturevideo.riffbuffer, string);
754 }
755
756 static fs_offset_t SCR_CaptureVideo_RIFF_GetPosition(void)
757 {
758         SCR_CaptureVideo_RIFF_Flush();
759         return FS_Tell(cls.capturevideo.videofile);
760 }
761
762 static void SCR_CaptureVideo_RIFF_Push(const char *chunkfourcc, const char *listtypefourcc)
763 {
764         SCR_CaptureVideo_RIFF_WriteFourCC(chunkfourcc);
765         SCR_CaptureVideo_RIFF_Write32(0);
766         SCR_CaptureVideo_RIFF_Flush();
767         cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel++] = SCR_CaptureVideo_RIFF_GetPosition();
768         if (listtypefourcc)
769                 SCR_CaptureVideo_RIFF_WriteFourCC(listtypefourcc);
770 }
771
772 static void SCR_CaptureVideo_RIFF_Pop(void)
773 {
774         fs_offset_t offset;
775         int x;
776         unsigned char sizebytes[4];
777         // write out the chunk size and then return to the current file position
778         cls.capturevideo.riffstacklevel--;
779         offset = SCR_CaptureVideo_RIFF_GetPosition();
780         x = (int)(offset - (cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel]));
781         sizebytes[0] = (x) & 0xff;sizebytes[1] = (x >> 8) & 0xff;sizebytes[2] = (x >> 16) & 0xff;sizebytes[3] = (x >> 24) & 0xff;
782         FS_Seek(cls.capturevideo.videofile, -(x + 4), SEEK_END);
783         FS_Write(cls.capturevideo.videofile, sizebytes, 4);
784         FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
785         if (offset & 1)
786         {
787                 unsigned char c = 0;
788                 FS_Write(cls.capturevideo.videofile, &c, 1);
789         }
790 }
791
792 static void SCR_CaptureVideo_RIFF_IndexEntry(const char *chunkfourcc, int chunksize, int flags)
793 {
794         if (cls.capturevideo.riffstacklevel != 2)
795                 Sys_Error("SCR_Capturevideo_RIFF_IndexEntry: RIFF stack level is %i (should be 2)\n", cls.capturevideo.riffstacklevel);
796         if (cls.capturevideo.riffindexbuffer.cursize + 16 > cls.capturevideo.riffindexbuffer.maxsize)
797         {
798                 int oldsize = cls.capturevideo.riffindexbuffer.maxsize;
799                 unsigned char *olddata;
800                 olddata = cls.capturevideo.riffindexbuffer.data;
801                 cls.capturevideo.riffindexbuffer.maxsize = max(cls.capturevideo.riffindexbuffer.maxsize * 2, 4096);
802                 cls.capturevideo.riffindexbuffer.data = Mem_Alloc(tempmempool, cls.capturevideo.riffindexbuffer.maxsize);
803                 if (olddata)
804                 {
805                         memcpy(cls.capturevideo.riffindexbuffer.data, olddata, oldsize);
806                         Mem_Free(olddata);
807                 }
808         }
809         MSG_WriteUnterminatedString(&cls.capturevideo.riffindexbuffer, chunkfourcc);
810         MSG_WriteLong(&cls.capturevideo.riffindexbuffer, flags);
811         MSG_WriteLong(&cls.capturevideo.riffindexbuffer, (int)FS_Tell(cls.capturevideo.videofile) - cls.capturevideo.riffstackstartoffset[1]);
812         MSG_WriteLong(&cls.capturevideo.riffindexbuffer, chunksize);
813 }
814
815 static void SCR_CaptureVideo_RIFF_Finish(void)
816 {
817         // close the "movi" list
818         SCR_CaptureVideo_RIFF_Pop();
819         // write the idx1 chunk that we've been building while saving the frames
820         SCR_CaptureVideo_RIFF_Push("idx1", NULL);
821         SCR_CaptureVideo_RIFF_WriteBytes(cls.capturevideo.riffindexbuffer.data, cls.capturevideo.riffindexbuffer.cursize);
822         SCR_CaptureVideo_RIFF_Pop();
823         cls.capturevideo.riffindexbuffer.cursize = 0;
824         // pop the RIFF chunk itself
825         while (cls.capturevideo.riffstacklevel > 0)
826                 SCR_CaptureVideo_RIFF_Pop();
827         SCR_CaptureVideo_RIFF_Flush();
828 }
829
830 static void SCR_CaptureVideo_RIFF_OverflowCheck(int framesize)
831 {
832         fs_offset_t cursize;
833         if (cls.capturevideo.riffstacklevel != 2)
834                 Sys_Error("SCR_CaptureVideo_RIFF_OverflowCheck: chunk stack leakage!\n");
835         // check where we are in the file
836         SCR_CaptureVideo_RIFF_Flush();
837         cursize = SCR_CaptureVideo_RIFF_GetPosition() - cls.capturevideo.riffstackstartoffset[0];
838         // if this would overflow the windows limit of 1GB per RIFF chunk, we need
839         // to close the current RIFF chunk and open another for future frames
840         if (8 + cursize + framesize + cls.capturevideo.riffindexbuffer.cursize + 8 > 1<<30)
841         {
842                 SCR_CaptureVideo_RIFF_Finish();
843                 // begin a new 1GB extended section of the AVI
844                 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX");
845                 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
846         }
847 }
848
849 void SCR_CaptureVideo_BeginVideo(void)
850 {
851         double gamma, g;
852         int width = vid.width, height = vid.height, x;
853         unsigned int i;
854         if (cls.capturevideo.active)
855                 return;
856         memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
857         // soundrate is figured out on the first SoundFrame
858         cls.capturevideo.active = true;
859         cls.capturevideo.starttime = realtime;
860         cls.capturevideo.framerate = bound(1, cl_capturevideo_fps.value, 1000);
861         cls.capturevideo.soundrate = S_GetSoundRate();
862         cls.capturevideo.frame = 0;
863         cls.capturevideo.soundsampleframe = 0;
864         cls.capturevideo.realtime = cl_capturevideo_realtime.integer != 0;
865         cls.capturevideo.buffer = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * (3+3+3) + 18);
866         gamma = 1.0/scr_screenshot_gammaboost.value;
867         dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/dpvideo%03i", cl_capturevideo_number.integer);
868         Cvar_SetValueQuick(&cl_capturevideo_number, cl_capturevideo_number.integer + 1);
869
870         /*
871         for (i = 0;i < 256;i++)
872         {
873                 unsigned char j = (unsigned char)bound(0, 255*pow(i/255.0, gamma), 255);
874                 cls.capturevideo.rgbgammatable[0][i] = j;
875                 cls.capturevideo.rgbgammatable[1][i] = j;
876                 cls.capturevideo.rgbgammatable[2][i] = j;
877         }
878         */
879 /*
880 R = Y + 1.4075 * (Cr - 128);
881 G = Y + -0.3455 * (Cb - 128) + -0.7169 * (Cr - 128);
882 B = Y + 1.7790 * (Cb - 128);
883 Y = R *  .299 + G *  .587 + B *  .114;
884 Cb = R * -.169 + G * -.332 + B *  .500 + 128.;
885 Cr = R *  .500 + G * -.419 + B * -.0813 + 128.;
886 */
887         for (i = 0;i < 256;i++)
888         {
889                 g = 255*pow(i/255.0, gamma);
890                 // Y weights from RGB
891                 cls.capturevideo.rgbtoyuvscaletable[0][0][i] = (short)(g *  0.299);
892                 cls.capturevideo.rgbtoyuvscaletable[0][1][i] = (short)(g *  0.587);
893                 cls.capturevideo.rgbtoyuvscaletable[0][2][i] = (short)(g *  0.114);
894                 // Cb weights from RGB
895                 cls.capturevideo.rgbtoyuvscaletable[1][0][i] = (short)(g * -0.169);
896                 cls.capturevideo.rgbtoyuvscaletable[1][1][i] = (short)(g * -0.332);
897                 cls.capturevideo.rgbtoyuvscaletable[1][2][i] = (short)(g *  0.500);
898                 // Cr weights from RGB
899                 cls.capturevideo.rgbtoyuvscaletable[2][0][i] = (short)(g *  0.500);
900                 cls.capturevideo.rgbtoyuvscaletable[2][1][i] = (short)(g * -0.419);
901                 cls.capturevideo.rgbtoyuvscaletable[2][2][i] = (short)(g * -0.0813);
902                 // range reduction of YCbCr to valid signal range
903                 cls.capturevideo.yuvnormalizetable[0][i] = 16 + i * (236-16) / 256;
904                 cls.capturevideo.yuvnormalizetable[1][i] = 16 + i * (240-16) / 256;
905                 cls.capturevideo.yuvnormalizetable[2][i] = 16 + i * (240-16) / 256;
906         }
907
908         //if (cl_capturevideo_)
909         //{
910         //}
911         //else
912         {
913                 cls.capturevideo.format = CAPTUREVIDEOFORMAT_AVI_I420;
914                 cls.capturevideo.videofile = FS_Open (va("%s.avi", cls.capturevideo.basename), "wb", false, true);
915                 SCR_CaptureVideo_RIFF_Start();
916                 // enclosing RIFF chunk (there can be multiple of these in >1GB files, the later ones are "AVIX" instead of "AVI " and have no header/stream info)
917                 SCR_CaptureVideo_RIFF_Push("RIFF", "AVI ");
918                 // AVI main header
919                 SCR_CaptureVideo_RIFF_Push("LIST", "hdrl");
920                 SCR_CaptureVideo_RIFF_Push("avih", NULL);
921                 SCR_CaptureVideo_RIFF_Write32((int)(1000000.0 / cls.capturevideo.framerate)); // microseconds per frame
922                 SCR_CaptureVideo_RIFF_Write32(0); // max bytes per second
923                 SCR_CaptureVideo_RIFF_Write32(0); // padding granularity
924                 SCR_CaptureVideo_RIFF_Write32(0x910); // flags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
925                 cls.capturevideo.videofile_totalframes_offset1 = SCR_CaptureVideo_RIFF_GetPosition();
926                 SCR_CaptureVideo_RIFF_Write32(0); // total frames
927                 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
928                 if (cls.capturevideo.soundrate)
929                         SCR_CaptureVideo_RIFF_Write32(2); // number of streams
930                 else
931                         SCR_CaptureVideo_RIFF_Write32(1); // number of streams
932                 SCR_CaptureVideo_RIFF_Write32(0); // suggested buffer size
933                 SCR_CaptureVideo_RIFF_Write32(width); // width
934                 SCR_CaptureVideo_RIFF_Write32(height); // height
935                 SCR_CaptureVideo_RIFF_Write32(0); // reserved[0]
936                 SCR_CaptureVideo_RIFF_Write32(0); // reserved[1]
937                 SCR_CaptureVideo_RIFF_Write32(0); // reserved[2]
938                 SCR_CaptureVideo_RIFF_Write32(0); // reserved[3]
939                 SCR_CaptureVideo_RIFF_Pop();
940                 // video stream info
941                 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
942                 SCR_CaptureVideo_RIFF_Push("strh", "vids");
943                 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // stream fourcc (I420 colorspace, uncompressed)
944                 SCR_CaptureVideo_RIFF_Write32(0); // flags
945                 SCR_CaptureVideo_RIFF_Write16(0); // priority
946                 SCR_CaptureVideo_RIFF_Write16(0); // language
947                 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
948                 // find an ideal divisor for the framerate
949                 for (x = 1;x < 1000;x++)
950                         if (cls.capturevideo.framerate * x == floor(cls.capturevideo.framerate * x))
951                                 break;
952                 SCR_CaptureVideo_RIFF_Write32(x); // samples/second divisor
953                 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.framerate * x)); // samples/second multiplied by divisor
954                 SCR_CaptureVideo_RIFF_Write32(0); // start
955                 cls.capturevideo.videofile_totalframes_offset2 = SCR_CaptureVideo_RIFF_GetPosition();
956                 SCR_CaptureVideo_RIFF_Write32(0); // length
957                 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // suggested buffer size
958                 SCR_CaptureVideo_RIFF_Write32(0); // quality
959                 SCR_CaptureVideo_RIFF_Write32(0); // sample size
960                 SCR_CaptureVideo_RIFF_Write16(0); // frame left
961                 SCR_CaptureVideo_RIFF_Write16(0); // frame top
962                 SCR_CaptureVideo_RIFF_Write16(width); // frame right
963                 SCR_CaptureVideo_RIFF_Write16(height); // frame bottom
964                 SCR_CaptureVideo_RIFF_Pop();
965                 // video stream format
966                 SCR_CaptureVideo_RIFF_Push("strf", NULL);
967                 SCR_CaptureVideo_RIFF_Write32(40); // BITMAPINFO struct size
968                 SCR_CaptureVideo_RIFF_Write32(width); // width
969                 SCR_CaptureVideo_RIFF_Write32(height); // height
970                 SCR_CaptureVideo_RIFF_Write16(3); // planes
971                 SCR_CaptureVideo_RIFF_Write16(12); // bitcount
972                 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // compression
973                 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // size of image
974                 SCR_CaptureVideo_RIFF_Write32(0); // x pixels per meter
975                 SCR_CaptureVideo_RIFF_Write32(0); // y pixels per meter
976                 SCR_CaptureVideo_RIFF_Write32(0); // color used
977                 SCR_CaptureVideo_RIFF_Write32(0); // color important
978                 SCR_CaptureVideo_RIFF_Pop();
979                 SCR_CaptureVideo_RIFF_Pop();
980                 if (cls.capturevideo.soundrate)
981                 {
982                         // audio stream info
983                         SCR_CaptureVideo_RIFF_Push("LIST", "strl");
984                         SCR_CaptureVideo_RIFF_Push("strh", "auds");
985                         SCR_CaptureVideo_RIFF_Write32(1); // stream fourcc (PCM audio, uncompressed)
986                         SCR_CaptureVideo_RIFF_Write32(0); // flags
987                         SCR_CaptureVideo_RIFF_Write16(0); // priority
988                         SCR_CaptureVideo_RIFF_Write16(0); // language
989                         SCR_CaptureVideo_RIFF_Write32(0); // initial frames
990                         SCR_CaptureVideo_RIFF_Write32(1); // samples/second divisor
991                         SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.soundrate)); // samples/second multiplied by divisor
992                         SCR_CaptureVideo_RIFF_Write32(0); // start
993                         cls.capturevideo.videofile_totalsampleframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
994                         SCR_CaptureVideo_RIFF_Write32(0); // length
995                         SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 2); // suggested buffer size (this is a half second)
996                         SCR_CaptureVideo_RIFF_Write32(0); // quality
997                         SCR_CaptureVideo_RIFF_Write32(4); // sample size
998                         SCR_CaptureVideo_RIFF_Write16(0); // frame left
999                         SCR_CaptureVideo_RIFF_Write16(0); // frame top
1000                         SCR_CaptureVideo_RIFF_Write16(0); // frame right
1001                         SCR_CaptureVideo_RIFF_Write16(0); // frame bottom
1002                         SCR_CaptureVideo_RIFF_Pop();
1003                         // audio stream format
1004                         SCR_CaptureVideo_RIFF_Push("strf", NULL);
1005                         SCR_CaptureVideo_RIFF_Write16(1); // format (uncompressed PCM?)
1006                         SCR_CaptureVideo_RIFF_Write16(2); // channels (stereo)
1007                         SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate); // sampleframes per second
1008                         SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 4); // average bytes per second
1009                         SCR_CaptureVideo_RIFF_Write16(4); // block align
1010                         SCR_CaptureVideo_RIFF_Write16(16); // bits per sample
1011                         SCR_CaptureVideo_RIFF_Write16(0); // size
1012                         SCR_CaptureVideo_RIFF_Pop();
1013                         SCR_CaptureVideo_RIFF_Pop();
1014                 }
1015                 // close the AVI header list
1016                 SCR_CaptureVideo_RIFF_Pop();
1017                 // software that produced this AVI video file
1018                 SCR_CaptureVideo_RIFF_Push("LIST", "INFO");
1019                 SCR_CaptureVideo_RIFF_Push("ISFT", NULL);
1020                 SCR_CaptureVideo_RIFF_WriteTerminatedString(engineversion);
1021                 SCR_CaptureVideo_RIFF_Pop();
1022                 // enable this junk filler if you like the LIST movi to always begin at 4KB in the file (why?)
1023 #if 0
1024                 SCR_CaptureVideo_RIFF_Push("JUNK", NULL);
1025                 x = 4096 - SCR_CaptureVideo_RIFF_GetPosition();
1026                 while (x > 0)
1027                 {
1028                         const char *junkfiller = "[ DarkPlaces junk data ]";
1029                         int i = min(x, (int)strlen(junkfiller));
1030                         SCR_CaptureVideo_RIFF_WriteBytes((const unsigned char *)junkfiller, i);
1031                         x -= i;
1032                 }
1033                 SCR_CaptureVideo_RIFF_Pop();
1034 #endif
1035                 SCR_CaptureVideo_RIFF_Pop();
1036                 // begin the actual video section now
1037                 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1038                 // we're done with the headers now...
1039                 SCR_CaptureVideo_RIFF_Flush();
1040                 if (cls.capturevideo.riffstacklevel != 2)
1041                         Sys_Error("SCR_CaptureVideo_BeginVideo: broken AVI writing code (stack level is %i (should be 2) at end of headers)\n", cls.capturevideo.riffstacklevel);
1042         }
1043
1044         switch(cls.capturevideo.format)
1045         {
1046         case CAPTUREVIDEOFORMAT_AVI_I420:
1047                 break;
1048         default:
1049                 break;
1050         }
1051 }
1052
1053 void SCR_CaptureVideo_EndVideo(void)
1054 {
1055         if (!cls.capturevideo.active)
1056                 return;
1057         cls.capturevideo.active = false;
1058         if (cls.capturevideo.videofile)
1059         {
1060                 switch(cls.capturevideo.format)
1061                 {
1062                 case CAPTUREVIDEOFORMAT_AVI_I420:
1063                         // close any open chunks
1064                         SCR_CaptureVideo_RIFF_Finish();
1065                         // go back and fix the video frames and audio samples fields
1066                         FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset1, SEEK_SET);
1067                         SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1068                         SCR_CaptureVideo_RIFF_Flush();
1069                         FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset2, SEEK_SET);
1070                         SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1071                         SCR_CaptureVideo_RIFF_Flush();
1072                         if (cls.capturevideo.soundrate)
1073                         {
1074                                 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalsampleframes_offset, SEEK_SET);
1075                                 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundsampleframe);
1076                                 SCR_CaptureVideo_RIFF_Flush();
1077                         }
1078                         break;
1079                 default:
1080                         break;
1081                 }
1082                 FS_Close(cls.capturevideo.videofile);
1083                 cls.capturevideo.videofile = NULL;
1084         }
1085
1086         if (cls.capturevideo.buffer)
1087         {
1088                 Mem_Free (cls.capturevideo.buffer);
1089                 cls.capturevideo.buffer = NULL;
1090         }
1091
1092         if (cls.capturevideo.riffindexbuffer.data)
1093         {
1094                 Mem_Free(cls.capturevideo.riffindexbuffer.data);
1095                 cls.capturevideo.riffindexbuffer.data = NULL;
1096         }
1097
1098         memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1099 }
1100
1101 // converts from RGB24 to I420 colorspace (identical to YV12 except chroma plane order is reversed), this colorspace is handled by the Intel(r) 4:2:0 codec on Windows
1102 void SCR_CaptureVideo_ConvertFrame_RGB_to_I420_flip(int width, int height, unsigned char *instart, unsigned char *outstart)
1103 {
1104         int x, y;
1105         int outoffset = (width/2)*(height/2);
1106         unsigned char *b, *out;
1107         // process one line at a time, and CbCr every other line at 2 pixel intervals
1108         for (y = 0;y < height;y++)
1109         {
1110                 // 1x1 Y
1111                 for (b = instart + (height-1-y)*width*3, out = outstart + y*width, x = 0;x < width;x++, b += 3, out++)
1112                         *out = cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[0][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[0][2][b[2]]];
1113                 if ((y & 1) == 0)
1114                 {
1115                         // 2x2 Cr and Cb planes
1116 #if 0
1117                         // low quality, no averaging
1118                         for (b = instart + (height-2-y)*width*3, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 6, out++)
1119                         {
1120                                 // Cr
1121                                 out[0        ] = cls.capturevideo.yuvnormalizetable[1][cls.capturevideo.rgbtoyuvscaletable[1][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[1][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[1][2][b[2]] + 128];
1122                                 // Cb
1123                                 out[outoffset] = cls.capturevideo.yuvnormalizetable[2][cls.capturevideo.rgbtoyuvscaletable[2][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[2][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[2][2][b[2]] + 128];
1124                         }
1125 #else
1126                         // high quality, averaging
1127                         int inpitch = width*3;
1128                         for (b = instart + (height-2-y)*width*3, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 6, out++)
1129                         {
1130                                 int blockr, blockg, blockb;
1131                                 blockr = (b[0] + b[3] + b[inpitch+0] + b[inpitch+3]) >> 2;
1132                                 blockg = (b[1] + b[4] + b[inpitch+1] + b[inpitch+4]) >> 2;
1133                                 blockb = (b[2] + b[5] + b[inpitch+2] + b[inpitch+5]) >> 2;
1134                                 // Cr
1135                                 out[0        ] = cls.capturevideo.yuvnormalizetable[1][cls.capturevideo.rgbtoyuvscaletable[1][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[1][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[1][2][blockb] + 128];
1136                                 // Cb
1137                                 out[outoffset] = cls.capturevideo.yuvnormalizetable[2][cls.capturevideo.rgbtoyuvscaletable[2][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[2][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[2][2][blockb] + 128];
1138                         }
1139 #endif
1140                 }
1141         }
1142 }
1143
1144 qboolean SCR_CaptureVideo_VideoFrame(int newframenum)
1145 {
1146         int x = 0, y = 0, width = vid.width, height = vid.height;
1147         unsigned char *in, *out;
1148         CHECKGLERROR
1149         //return SCR_ScreenShot(filename, cls.capturevideo.buffer, cls.capturevideo.buffer + vid.width * vid.height * 3, cls.capturevideo.buffer + vid.width * vid.height * 6, 0, 0, vid.width, vid.height, false, false, false, jpeg, true);
1150         // speed is critical here, so do saving as directly as possible
1151         switch (cls.capturevideo.format)
1152         {
1153         case CAPTUREVIDEOFORMAT_AVI_I420:
1154                 // if there's no videofile we have to just give up, and abort saving
1155                 if (!cls.capturevideo.videofile)
1156                         return false;
1157                 // FIXME: width/height must be multiple of 2, enforce this?
1158                 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, cls.capturevideo.buffer);CHECKGLERROR
1159                 in = cls.capturevideo.buffer;
1160                 out = cls.capturevideo.buffer + width*height*3;
1161                 SCR_CaptureVideo_ConvertFrame_RGB_to_I420_flip(width, height, in, out);
1162                 x = width*height+(width/2)*(height/2)*2;
1163                 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1164                 for (;cls.capturevideo.frame < newframenum;cls.capturevideo.frame++)
1165                 {
1166                         SCR_CaptureVideo_RIFF_IndexEntry("00dc", x, 0x10); // AVIIF_KEYFRAME
1167                         SCR_CaptureVideo_RIFF_Push("00dc", NULL);
1168                         SCR_CaptureVideo_RIFF_WriteBytes(out, x);
1169                         SCR_CaptureVideo_RIFF_Pop();
1170                 }
1171                 return true;
1172         default:
1173                 return false;
1174         }
1175 }
1176
1177 void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate)
1178 {
1179         int x;
1180         cls.capturevideo.soundrate = rate;
1181         cls.capturevideo.soundsampleframe += length;
1182         switch (cls.capturevideo.format)
1183         {
1184         case CAPTUREVIDEOFORMAT_AVI_I420:
1185                 x = length*4;
1186                 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1187                 SCR_CaptureVideo_RIFF_IndexEntry("01wb", x, 0x10); // AVIIF_KEYFRAME
1188                 SCR_CaptureVideo_RIFF_Push("01wb", NULL);
1189                 SCR_CaptureVideo_RIFF_WriteBytes(bufstereo16le, x);
1190                 SCR_CaptureVideo_RIFF_Pop();
1191                 break;
1192         default:
1193                 break;
1194         }
1195 }
1196
1197 void SCR_CaptureVideo(void)
1198 {
1199         int newframenum;
1200         if (cl_capturevideo.integer && r_render.integer)
1201         {
1202                 if (!cls.capturevideo.active)
1203                         SCR_CaptureVideo_BeginVideo();
1204                 if (cls.capturevideo.framerate != cl_capturevideo_fps.value)
1205                 {
1206                         Con_Printf("You can not change the video framerate while recording a video.\n");
1207                         Cvar_SetValueQuick(&cl_capturevideo_fps, cls.capturevideo.framerate);
1208                 }
1209                 // for AVI saving we have to make sure that sound is saved before video
1210                 if (cls.capturevideo.soundrate && !cls.capturevideo.soundsampleframe)
1211                         return;
1212                 if (cls.capturevideo.realtime)
1213                 {
1214                         // preserve sound sync by duplicating frames when running slow
1215                         newframenum = (int)((realtime - cls.capturevideo.starttime) * cls.capturevideo.framerate);
1216                 }
1217                 else
1218                         newframenum = cls.capturevideo.frame + 1;
1219                 // if falling behind more than one second, stop
1220                 if (newframenum - cls.capturevideo.frame > (int)ceil(cls.capturevideo.framerate))
1221                 {
1222                         Cvar_SetValueQuick(&cl_capturevideo, 0);
1223                         Con_Printf("video saving failed on frame %i, your machine is too slow for this capture speed.\n", cls.capturevideo.frame);
1224                         SCR_CaptureVideo_EndVideo();
1225                         return;
1226                 }
1227                 // write frames
1228                 SCR_CaptureVideo_VideoFrame(newframenum);
1229                 if (cls.capturevideo.error)
1230                 {
1231                         Cvar_SetValueQuick(&cl_capturevideo, 0);
1232                         Con_Printf("video saving failed on frame %i, out of disk space? stopping video capture.\n", cls.capturevideo.frame);
1233                         SCR_CaptureVideo_EndVideo();
1234                 }
1235         }
1236         else if (cls.capturevideo.active)
1237                 SCR_CaptureVideo_EndVideo();
1238 }
1239
1240 /*
1241 ===============
1242 R_Envmap_f
1243
1244 Grab six views for environment mapping tests
1245 ===============
1246 */
1247 struct envmapinfo_s
1248 {
1249         float angles[3];
1250         char *name;
1251         qboolean flipx, flipy, flipdiagonaly;
1252 }
1253 envmapinfo[12] =
1254 {
1255         {{  0,   0, 0}, "rt", false, false, false},
1256         {{  0, 270, 0}, "ft", false, false, false},
1257         {{  0, 180, 0}, "lf", false, false, false},
1258         {{  0,  90, 0}, "bk", false, false, false},
1259         {{-90, 180, 0}, "up",  true,  true, false},
1260         {{ 90, 180, 0}, "dn",  true,  true, false},
1261
1262         {{  0,   0, 0}, "px",  true,  true,  true},
1263         {{  0,  90, 0}, "py", false,  true, false},
1264         {{  0, 180, 0}, "nx", false, false,  true},
1265         {{  0, 270, 0}, "ny",  true, false, false},
1266         {{-90, 180, 0}, "pz", false, false,  true},
1267         {{ 90, 180, 0}, "nz", false, false,  true}
1268 };
1269
1270 static void R_Envmap_f (void)
1271 {
1272         int j, size;
1273         char filename[MAX_QPATH], basename[MAX_QPATH];
1274         unsigned char *buffer1;
1275         unsigned char *buffer2;
1276         unsigned char *buffer3;
1277
1278         if (Cmd_Argc() != 3)
1279         {
1280                 Con_Print("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");
1281                 return;
1282         }
1283
1284         strlcpy (basename, Cmd_Argv(1), sizeof (basename));
1285         size = atoi(Cmd_Argv(2));
1286         if (size != 128 && size != 256 && size != 512 && size != 1024)
1287         {
1288                 Con_Print("envmap: size must be one of 128, 256, 512, or 1024\n");
1289                 return;
1290         }
1291         if (size > vid.width || size > vid.height)
1292         {
1293                 Con_Print("envmap: your resolution is not big enough to render that size\n");
1294                 return;
1295         }
1296
1297         r_refdef.envmap = true;
1298
1299         R_UpdateVariables();
1300
1301         r_view.x = 0;
1302         r_view.y = 0;
1303         r_view.z = 0;
1304         r_view.width = size;
1305         r_view.height = size;
1306         r_view.depth = 1;
1307
1308         r_view.frustum_x = tan(90 * M_PI / 360.0);
1309         r_view.frustum_y = tan(90 * M_PI / 360.0);
1310
1311         buffer1 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1312         buffer2 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1313         buffer3 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3 + 18);
1314
1315         for (j = 0;j < 12;j++)
1316         {
1317                 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1318                 Matrix4x4_CreateFromQuakeEntity(&r_view.matrix, r_view.origin[0], r_view.origin[1], r_view.origin[2], envmapinfo[j].angles[0], envmapinfo[j].angles[1], envmapinfo[j].angles[2], 1);
1319                 R_ClearScreen();
1320                 R_Mesh_Start();
1321                 R_RenderView();
1322                 R_Mesh_Finish();
1323                 SCR_ScreenShot(filename, buffer1, buffer2, buffer3, 0, vid.height - (r_view.y + r_view.height), size, size, envmapinfo[j].flipx, envmapinfo[j].flipy, envmapinfo[j].flipdiagonaly, false, false);
1324         }
1325
1326         Mem_Free (buffer1);
1327         Mem_Free (buffer2);
1328         Mem_Free (buffer3);
1329
1330         r_refdef.envmap = false;
1331 }
1332
1333 //=============================================================================
1334
1335 // LordHavoc: SHOWLMP stuff
1336 #define SHOWLMP_MAXLABELS 256
1337 typedef struct showlmp_s
1338 {
1339         qboolean        isactive;
1340         float           x;
1341         float           y;
1342         char            label[32];
1343         char            pic[128];
1344 }
1345 showlmp_t;
1346
1347 showlmp_t showlmp[SHOWLMP_MAXLABELS];
1348
1349 void SHOWLMP_decodehide(void)
1350 {
1351         int i;
1352         char *lmplabel;
1353         lmplabel = MSG_ReadString();
1354         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1355                 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
1356                 {
1357                         showlmp[i].isactive = false;
1358                         return;
1359                 }
1360 }
1361
1362 void SHOWLMP_decodeshow(void)
1363 {
1364         int i, k;
1365         char lmplabel[256], picname[256];
1366         float x, y;
1367         strlcpy (lmplabel,MSG_ReadString(), sizeof (lmplabel));
1368         strlcpy (picname, MSG_ReadString(), sizeof (picname));
1369         if (gamemode == GAME_NEHAHRA) // LordHavoc: nasty old legacy junk
1370         {
1371                 x = MSG_ReadByte();
1372                 y = MSG_ReadByte();
1373         }
1374         else
1375         {
1376                 x = MSG_ReadShort();
1377                 y = MSG_ReadShort();
1378         }
1379         k = -1;
1380         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1381                 if (showlmp[i].isactive)
1382                 {
1383                         if (strcmp(showlmp[i].label, lmplabel) == 0)
1384                         {
1385                                 k = i;
1386                                 break; // drop out to replace it
1387                         }
1388                 }
1389                 else if (k < 0) // find first empty one to replace
1390                         k = i;
1391         if (k < 0)
1392                 return; // none found to replace
1393         // change existing one
1394         showlmp[k].isactive = true;
1395         strlcpy (showlmp[k].label, lmplabel, sizeof (showlmp[k].label));
1396         strlcpy (showlmp[k].pic, picname, sizeof (showlmp[k].pic));
1397         showlmp[k].x = x;
1398         showlmp[k].y = y;
1399 }
1400
1401 void SHOWLMP_drawall(void)
1402 {
1403         int i;
1404         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1405                 if (showlmp[i].isactive)
1406                         DrawQ_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic, true), 0, 0, 1, 1, 1, 1, 0);
1407 }
1408
1409 void SHOWLMP_clear(void)
1410 {
1411         int i;
1412         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1413                 showlmp[i].isactive = false;
1414 }
1415
1416 /*
1417 ==============================================================================
1418
1419                                                 SCREEN SHOTS
1420
1421 ==============================================================================
1422 */
1423
1424 qboolean SCR_ScreenShot(char *filename, unsigned char *buffer1, unsigned char *buffer2, unsigned char *buffer3, int x, int y, int width, int height, qboolean flipx, qboolean flipy, qboolean flipdiagonal, qboolean jpeg, qboolean gammacorrect)
1425 {
1426         int     indices[3] = {0,1,2};
1427         qboolean ret;
1428
1429         if (!r_render.integer)
1430                 return false;
1431
1432         CHECKGLERROR
1433         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer1);CHECKGLERROR
1434
1435         if (scr_screenshot_gammaboost.value != 1 && gammacorrect)
1436         {
1437                 int i;
1438                 double igamma = 1.0 / scr_screenshot_gammaboost.value;
1439                 unsigned char ramp[256];
1440                 for (i = 0;i < 256;i++)
1441                         ramp[i] = (unsigned char) (pow(i * (1.0 / 255.0), igamma) * 255.0);
1442                 for (i = 0;i < width*height*3;i++)
1443                         buffer1[i] = ramp[buffer1[i]];
1444         }
1445
1446         Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
1447
1448         if (jpeg)
1449                 ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
1450         else
1451                 ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer2, buffer3);
1452
1453         return ret;
1454 }
1455
1456 //=============================================================================
1457
1458 void R_ClearScreen(void)
1459 {
1460         // clear to black
1461         CHECKGLERROR
1462         if (r_refdef.fogenabled)
1463         {
1464                 qglClearColor(r_refdef.fogcolor[0],r_refdef.fogcolor[1],r_refdef.fogcolor[2],0);CHECKGLERROR
1465         }
1466         else
1467         {
1468                 qglClearColor(0,0,0,0);CHECKGLERROR
1469         }
1470         qglClearDepth(1);CHECKGLERROR
1471         if (gl_stencil)
1472         {
1473                 // LordHavoc: we use a stencil centered around 128 instead of 0,
1474                 // to avoid clamping interfering with strange shadow volume
1475                 // drawing orders
1476                 qglClearStencil(128);CHECKGLERROR
1477         }
1478         // clear the screen
1479         if (r_render.integer)
1480                 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));
1481         // set dithering mode
1482         if (gl_dither.integer)
1483         {
1484                 qglEnable(GL_DITHER);CHECKGLERROR
1485         }
1486         else
1487         {
1488                 qglDisable(GL_DITHER);CHECKGLERROR
1489         }
1490 }
1491
1492 qboolean CL_VM_UpdateView (void);
1493 void SCR_DrawConsole (void);
1494 void R_Shadow_EditLights_DrawSelectedLightProperties(void);
1495
1496 int r_stereo_side;
1497
1498 void SCR_DrawScreen (void)
1499 {
1500         R_Mesh_Start();
1501
1502         if (r_timereport_active)
1503                 R_TimeReport("setup");
1504
1505         R_UpdateVariables();
1506
1507         if (cls.signon == SIGNONS)
1508         {
1509                 float size;
1510
1511                 size = scr_viewsize.value * (1.0 / 100.0);
1512                 size = min(size, 1);
1513
1514                 if (r_stereo_sidebyside.integer)
1515                 {
1516                         r_view.width = (int)(vid.width * size / 2.5);
1517                         r_view.height = (int)(vid.height * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100));
1518                         r_view.depth = 1;
1519                         r_view.x = (int)((vid.width - r_view.width * 2.5) * 0.5);
1520                         r_view.y = (int)((vid.height - r_view.height)/2);
1521                         r_view.z = 0;
1522                         if (r_stereo_side)
1523                                 r_view.x += (int)(r_view.width * 1.5);
1524                 }
1525                 else
1526                 {
1527                         r_view.width = (int)(vid.width * size);
1528                         r_view.height = (int)(vid.height * size * (1 - bound(0, r_letterbox.value, 100) / 100));
1529                         r_view.depth = 1;
1530                         r_view.x = (int)((vid.width - r_view.width)/2);
1531                         r_view.y = (int)((vid.height - r_view.height)/2);
1532                         r_view.z = 0;
1533                 }
1534
1535                 // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
1536                 // LordHavoc: this is designed to produce widescreen fov values
1537                 // when the screen is wider than 4/3 width/height aspect, to do
1538                 // this it simply assumes the requested fov is the vertical fov
1539                 // for a 4x3 display, if the ratio is not 4x3 this makes the fov
1540                 // higher/lower according to the ratio
1541                 r_view.frustum_y = tan(scr_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
1542                 r_view.frustum_x = r_view.frustum_y * (float)r_view.width / (float)r_view.height / vid_pixelheight.value;
1543
1544                 r_view.frustum_x *= r_refdef.frustumscale_x;
1545                 r_view.frustum_y *= r_refdef.frustumscale_y;
1546
1547                 if(!CL_VM_UpdateView())
1548                         R_RenderView();
1549
1550                 if (scr_zoomwindow.integer)
1551                 {
1552                         float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
1553                         float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
1554                         r_view.width = (int)(vid.width * sizex);
1555                         r_view.height = (int)(vid.height * sizey);
1556                         r_view.depth = 1;
1557                         r_view.x = (int)((vid.width - r_view.width)/2);
1558                         r_view.y = 0;
1559                         r_view.z = 0;
1560
1561                         r_view.frustum_y = tan(scr_zoomwindow_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
1562                         r_view.frustum_x = r_view.frustum_y * vid_pixelheight.value * (float)r_view.width / (float)r_view.height;
1563
1564                         r_view.frustum_x *= r_refdef.frustumscale_x;
1565                         r_view.frustum_y *= r_refdef.frustumscale_y;
1566
1567                         if(!CL_VM_UpdateView())
1568                                 R_RenderView();
1569                 }
1570         }
1571
1572         if (!r_stereo_sidebyside.integer)
1573         {
1574                 r_view.width = vid.width;
1575                 r_view.height = vid.height;
1576                 r_view.depth = 1;
1577                 r_view.x = 0;
1578                 r_view.y = 0;
1579                 r_view.z = 0;
1580         }
1581
1582         // draw 2D stuff
1583
1584         if (cls.signon == SIGNONS)
1585         {
1586                 SCR_DrawNet ();
1587                 SCR_DrawTurtle ();
1588                 SCR_DrawPause ();
1589                 if (!r_letterbox.value)
1590                         Sbar_Draw();
1591                 SHOWLMP_drawall();
1592                 SCR_CheckDrawCenterString();
1593         }
1594         MR_Draw();
1595         CL_DrawVideo();
1596         R_Shadow_EditLights_DrawSelectedLightProperties();
1597
1598         SCR_DrawConsole();
1599
1600         SCR_DrawBrand();
1601
1602         SCR_DrawDownload();
1603
1604         if (r_timereport_active)
1605                 R_TimeReport("2d");
1606
1607         if (cls.signon == SIGNONS)
1608                 R_TimeReport_Frame();
1609
1610         DrawQ_Finish();
1611
1612         R_DrawGamma();
1613
1614         R_Mesh_Finish();
1615
1616         if (r_timereport_active)
1617                 R_TimeReport("meshfinish");
1618 }
1619
1620 void SCR_UpdateLoadingScreen (qboolean clear)
1621 {
1622         float x, y;
1623         cachepic_t *pic;
1624         float vertex3f[12];
1625         float texcoord2f[8];
1626         // don't do anything if not initialized yet
1627         if (vid_hidden || !scr_refresh.integer)
1628                 return;
1629         CHECKGLERROR
1630         qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
1631         //qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
1632         //qglDepthMask(1);CHECKGLERROR
1633         qglColorMask(1,1,1,1);CHECKGLERROR
1634         qglClearColor(0,0,0,0);CHECKGLERROR
1635         // when starting up a new video mode, make sure the screen is cleared to black
1636         if (clear)
1637         {
1638                 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
1639         }
1640         //qglDisable(GL_CULL_FACE);CHECKGLERROR
1641         //R_ClearScreen();
1642         R_Textures_Frame();
1643         GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
1644         R_Mesh_Start();
1645         R_Mesh_Matrix(&identitymatrix);
1646         // draw the loading plaque
1647         pic = Draw_CachePic("gfx/loading", true);
1648         x = (vid_conwidth.integer - pic->width)/2;
1649         y = (vid_conheight.integer - pic->height)/2;
1650         GL_Color(1,1,1,1);
1651         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1652         GL_DepthRange(0, 1);
1653         GL_DepthTest(false);
1654         R_Mesh_VertexPointer(vertex3f, 0, 0);
1655         R_Mesh_ColorPointer(NULL, 0, 0);
1656         R_Mesh_ResetTextureState();
1657         R_Mesh_TexBind(0, R_GetTexture(pic->tex));
1658         R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
1659         vertex3f[2] = vertex3f[5] = vertex3f[8] = vertex3f[11] = 0;
1660         vertex3f[0] = vertex3f[9] = x;
1661         vertex3f[1] = vertex3f[4] = y;
1662         vertex3f[3] = vertex3f[6] = x + pic->width;
1663         vertex3f[7] = vertex3f[10] = y + pic->height;
1664         texcoord2f[0] = 0;texcoord2f[1] = 0;
1665         texcoord2f[2] = 1;texcoord2f[3] = 0;
1666         texcoord2f[4] = 1;texcoord2f[5] = 1;
1667         texcoord2f[6] = 0;texcoord2f[7] = 1;
1668         if (vid.stereobuffer)
1669         {
1670                 qglDrawBuffer(GL_FRONT_LEFT);
1671                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
1672                 qglDrawBuffer(GL_FRONT_RIGHT);
1673                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
1674         }
1675         else
1676         {
1677                 qglDrawBuffer(GL_FRONT);
1678                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
1679         }
1680         R_Mesh_Finish();
1681         // refresh
1682         // not necessary when rendering to GL_FRONT buffers
1683         //VID_Finish(false);
1684         // however this IS necessary on Windows Vista
1685         qglFinish();
1686 }
1687
1688 void CL_UpdateScreen(void)
1689 {
1690         float conwidth, conheight;
1691
1692         if (vid_hidden || !scr_refresh.integer)
1693                 return;
1694
1695         if (!scr_initialized || !con_initialized)
1696                 return;                         // not initialized yet
1697
1698         conwidth = bound(320, vid_conwidth.value, 2048);
1699         conheight = bound(200, vid_conheight.value, 1536);
1700         if (vid_conwidth.value != conwidth)
1701                 Cvar_SetValue("vid_conwidth", conwidth);
1702         if (vid_conheight.value != conheight)
1703                 Cvar_SetValue("vid_conheight", conheight);
1704
1705         // bound viewsize
1706         if (scr_viewsize.value < 30)
1707                 Cvar_Set ("viewsize","30");
1708         if (scr_viewsize.value > 120)
1709                 Cvar_Set ("viewsize","120");
1710
1711         // bound field of view
1712         if (scr_fov.value < 1)
1713                 Cvar_Set ("fov","1");
1714         if (scr_fov.value > 170)
1715                 Cvar_Set ("fov","170");
1716
1717         // validate r_textureunits cvar
1718         if (r_textureunits.integer > gl_textureunits)
1719                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
1720         if (r_textureunits.integer < 1)
1721                 Cvar_SetValueQuick(&r_textureunits, 1);
1722
1723         // validate gl_combine cvar
1724         if (gl_combine.integer && !gl_combine_extension)
1725                 Cvar_SetValueQuick(&gl_combine, 0);
1726
1727         // intermission is always full screen
1728         if (cl.intermission)
1729                 sb_lines = 0;
1730         else
1731         {
1732                 if (scr_viewsize.value >= 120)
1733                         sb_lines = 0;           // no status bar at all
1734                 else if (scr_viewsize.value >= 110)
1735                         sb_lines = 24;          // no inventory
1736                 else
1737                         sb_lines = 24+16+8;
1738         }
1739
1740         r_view.colormask[0] = 1;
1741         r_view.colormask[1] = 1;
1742         r_view.colormask[2] = 1;
1743
1744         if (r_timereport_active)
1745                 R_TimeReport("other");
1746
1747         SCR_SetUpToDrawConsole();
1748
1749         if (r_timereport_active)
1750                 R_TimeReport("start");
1751
1752         CHECKGLERROR
1753         qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
1754         qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
1755         qglDepthMask(1);CHECKGLERROR
1756         qglColorMask(1,1,1,1);CHECKGLERROR
1757         qglClearColor(0,0,0,0);CHECKGLERROR
1758         qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
1759
1760         if(scr_stipple.integer)
1761         {
1762                 GLubyte stipple[128];
1763                 int i, s, width, parts;
1764                 static int frame = 0;
1765                 ++frame;
1766
1767                 s = scr_stipple.integer;
1768                 parts = (s & 007);
1769                 width = (s & 070) >> 3;
1770
1771                 qglEnable(GL_POLYGON_STIPPLE); // 0x0B42
1772                 for(i = 0; i < 128; ++i)
1773                 {
1774                         int line = i/4;
1775                         stipple[i] = (((line >> width) + frame) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
1776                 }
1777                 qglPolygonStipple(stipple);
1778         }
1779         else
1780                 qglDisable(GL_POLYGON_STIPPLE);
1781
1782         if (r_timereport_active)
1783                 R_TimeReport("clear");
1784
1785         qglDrawBuffer(GL_BACK);
1786
1787         if (vid.stereobuffer || r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
1788         {
1789                 matrix4x4_t originalmatrix = r_view.matrix;
1790                 matrix4x4_t offsetmatrix;
1791                 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * 0.5f, 0, 0, r_stereo_angle.value * 0.5f, 0, 1);
1792                 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
1793
1794                 if (r_stereo_sidebyside.integer)
1795                         r_stereo_side = 0;
1796
1797                 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
1798                 {
1799                         r_view.colormask[0] = 1;
1800                         r_view.colormask[1] = 0;
1801                         r_view.colormask[2] = 0;
1802                 }
1803
1804                 if (vid.stereobuffer)
1805                         qglDrawBuffer(GL_BACK_RIGHT);
1806
1807                 SCR_DrawScreen();
1808
1809                 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * -0.5f, 0, 0, r_stereo_angle.value * -0.5f, 0, 1);
1810                 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
1811
1812                 if (r_stereo_sidebyside.integer)
1813                         r_stereo_side = 1;
1814
1815                 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
1816                 {
1817                         r_view.colormask[0] = 0;
1818                         r_view.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
1819                         r_view.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
1820                 }
1821
1822                 if (vid.stereobuffer)
1823                         qglDrawBuffer(GL_BACK_LEFT);
1824
1825                 SCR_DrawScreen();
1826
1827                 r_view.matrix = originalmatrix;
1828         }
1829         else
1830                 SCR_DrawScreen();
1831
1832         SCR_CaptureVideo();
1833
1834         VID_Finish(true);
1835         if (r_timereport_active)
1836                 R_TimeReport("finish");
1837 }
1838
1839 void CL_Screen_NewMap(void)
1840 {
1841         SHOWLMP_clear();
1842 }