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