6 #include "cl_collision.h"
10 // we have to include snd_main.h here only to get access to snd_renderbuffer->format.speed when writing the AVI headers
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)"};
53 #define AVI_MASTER_INDEX_SIZE 640 // GB ought to be enough for anyone
55 int jpeg_supported = false;
57 qboolean scr_initialized; // ready to draw
59 float scr_con_current;
60 int scr_con_margin_bottom;
62 extern int con_vislines;
64 static void SCR_ScreenShot_f (void);
65 static void R_Envmap_f (void);
68 void R_ClearScreen(void);
71 ===============================================================================
75 ===============================================================================
78 char scr_centerstring[MAX_INPUTLINE];
79 float scr_centertime_start; // for slow victory printing
80 float scr_centertime_off;
89 Called for important messages that should stay in the center of the screen
93 void SCR_CenterPrint(char *str)
95 strlcpy (scr_centerstring, str, sizeof (scr_centerstring));
96 scr_centertime_off = scr_centertime.value;
97 scr_centertime_start = cl.time;
99 // count the number of lines for centering
100 scr_center_lines = 1;
110 void SCR_DrawCenterString (void)
117 if(cl.intermission == 2) // in finale,
118 if(sb_showscores) // make TAB hide the finale message (sb_showscores overrides finale in sbar.c)
121 // the finale prints the characters one at a time, except if printspeed is an absurdly high value
122 if (cl.intermission && scr_printspeed.value > 0 && scr_printspeed.value < 1000000)
123 remaining = (int)(scr_printspeed.value * (cl.time - scr_centertime_start));
127 scr_erase_center = 0;
128 start = scr_centerstring;
133 if (scr_center_lines <= 4)
134 y = (int)(vid_conheight.integer*0.35);
141 // scan the number of characters on the line, not counting color codes
142 char *newline = strchr(start, '\n');
143 int l = newline ? (newline - start) : (int)strlen(start);
144 float width = DrawQ_TextWidth_Font(start, l, 8, 8, false, FONT_CENTERPRINT);
146 x = (vid_conwidth.integer - width)/2;
151 DrawQ_String_Font(x, y, start, l, 8, 8, 1, 1, 1, 1, 0, &color, false, FONT_CENTERPRINT);
160 start = newline + 1; // skip the \n
164 void SCR_CheckDrawCenterString (void)
166 if (scr_center_lines > scr_erase_lines)
167 scr_erase_lines = scr_center_lines;
169 if (cl.time > cl.oldtime)
170 scr_centertime_off -= cl.time - cl.oldtime;
172 // don't draw if this is a normal stats-screen intermission,
173 // only if it is not an intermission, or a finale intermission
174 if (cl.intermission == 1)
176 if (scr_centertime_off <= 0 && !cl.intermission)
178 if (key_dest != key_game)
181 SCR_DrawCenterString ();
184 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])
186 int j, k, x, y, index, offset, height;
187 // draw the bar graph itself
188 // advance the packet counter because it is the latest packet column being
189 // built up and should come last
190 packetcounter = (packetcounter + 1) % NETGRAPH_PACKETS;
191 for (j = 0;j < NETGRAPH_PACKETS;j++)
193 x = graphx + j * barwidth;
194 y = graphy + barheight;
195 index = (packetcounter + j) % NETGRAPH_PACKETS;
196 if (parameters[0][index] == NETGRAPH_LOSTPACKET)
197 DrawQ_Fill(x, y - barheight, barwidth, barheight, 1, 0, 0, 1, 0);
198 else if (parameters[0][index] == NETGRAPH_CHOKEDPACKET)
199 DrawQ_Fill(x, y - min(2, barheight), barwidth, min(2, barheight), 1, 1, 0, 1, 0);
203 for (k = 0;k < numparameters;k++)
205 height = (parameters[k][index] + bardivide - 1) / bardivide;
206 height = min(height, barheight - offset);
209 DrawQ_Fill(x, y - offset, barwidth, height, parametercolors[k][0], parametercolors[k][1], parametercolors[k][2], parametercolors[k][3], 0);
215 const float netgraphcolors[3][4] =
222 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)
225 const int *parameters[3];
227 DrawQ_Fill(graphx , graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
228 DrawQ_Fill(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
229 // draw the bar graphs
231 parameters[0] = conn->incoming_unreliablesize;
232 parameters[1] = conn->incoming_reliablesize;
233 parameters[2] = conn->incoming_acksize;
234 SCR_DrawNetGraph_DrawGraph(graphx, graphy, barwidth, barheight, bardivide, labelincoming, textsize, conn->incoming_packetcounter, numparameters, parameters, netgraphcolors);
235 parameters[0] = conn->outgoing_unreliablesize;
236 parameters[1] = conn->outgoing_reliablesize;
237 parameters[2] = conn->outgoing_acksize;
238 SCR_DrawNetGraph_DrawGraph(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth, barheight, bardivide, labeloutgoing, textsize, conn->outgoing_packetcounter, numparameters, parameters, netgraphcolors);
240 DrawQ_String(graphx , graphy + barheight, labelincoming, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
241 DrawQ_String(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy + barheight, labeloutgoing, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
244 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)
247 const int *parameters[3];
249 DrawQ_Fill(graphx , graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
250 DrawQ_Fill(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
251 // draw the bar graphs
253 parameters[0] = conn->outgoing_unreliablesize;
254 parameters[1] = conn->outgoing_reliablesize;
255 parameters[2] = conn->outgoing_acksize;
256 SCR_DrawNetGraph_DrawGraph(graphx , graphy, barwidth, barheight, bardivide, labeloutgoing, textsize, conn->outgoing_packetcounter, numparameters, parameters, netgraphcolors);
257 parameters[0] = conn->incoming_unreliablesize;
258 parameters[1] = conn->incoming_reliablesize;
259 parameters[2] = conn->incoming_acksize;
260 SCR_DrawNetGraph_DrawGraph(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth, barheight, bardivide, labelincoming, textsize, conn->incoming_packetcounter, numparameters, parameters, netgraphcolors);
262 DrawQ_String(graphx , graphy + barheight, labeloutgoing, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
263 DrawQ_String(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy + barheight, labelincoming, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
271 void SCR_DrawNetGraph (void)
273 int i, separator1, separator2, barwidth, barheight, bardivide, netgraph_x, netgraph_y, textsize, index, netgraphsperrow;
275 if (cls.state != ca_connected)
279 if (!shownetgraph.integer)
289 netgraphsperrow = (vid_conwidth.integer + separator2) / (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
290 netgraphsperrow = max(netgraphsperrow, 1);
293 netgraph_x = (vid_conwidth.integer + separator2) - (1 + (index % netgraphsperrow)) * (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
294 netgraph_y = (vid_conheight.integer - 48 + separator2) - (1 + (index / netgraphsperrow)) * (barheight + textsize + separator2);
295 SCR_DrawNetGraph_DrawConnection_Client(cls.netcon, netgraph_x, netgraph_y, barwidth, barheight, bardivide, "incoming", separator1, "outgoing", textsize);
298 if (sv.active && shownetgraph.integer >= 2)
300 for (i = 0;i < svs.maxclients;i++)
302 if (!svs.clients[i].netconnection)
304 netgraph_x = (vid_conwidth.integer + separator2) - (1 + (index % netgraphsperrow)) * (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
305 netgraph_y = (vid_conheight.integer - 48 + separator2) - (1 + (index / netgraphsperrow)) * (barheight + textsize + separator2);
306 SCR_DrawNetGraph_DrawConnection_Server(svs.clients[i].netconnection, netgraph_x, netgraph_y, barwidth, barheight, bardivide, va("%s", svs.clients[i].name), separator1, "", textsize);
317 void SCR_DrawTurtle (void)
321 if (cls.state != ca_connected)
324 if (!scr_showturtle.integer)
327 if (cl.realframetime < 0.1)
337 DrawQ_Pic (0, 0, Draw_CachePic("gfx/turtle", true), 0, 0, 1, 1, 1, 1, 0);
345 void SCR_DrawNet (void)
347 if (cls.state != ca_connected)
349 if (realtime - cl.last_received_message < 0.3)
351 if (cls.demoplayback)
354 DrawQ_Pic (64, 0, Draw_CachePic("gfx/net", true), 0, 0, 1, 1, 1, 1, 0);
362 void SCR_DrawPause (void)
366 if (cls.state != ca_connected)
369 if (!scr_showpause.integer) // turn off for screenshots
375 pic = Draw_CachePic ("gfx/pause", true);
376 DrawQ_Pic ((vid_conwidth.integer - pic->width)/2, (vid_conheight.integer - pic->height)/2, pic, 0, 0, 1, 1, 1, 1, 0);
384 void SCR_DrawBrand (void)
389 if (!scr_showbrand.value)
392 pic = Draw_CachePic ("gfx/brand", true);
394 switch ((int)scr_showbrand.value)
396 case 1: // bottom left
398 y = vid_conheight.integer - pic->height;
400 case 2: // bottom centre
401 x = (vid_conwidth.integer - pic->width) / 2;
402 y = vid_conheight.integer - pic->height;
404 case 3: // bottom right
405 x = vid_conwidth.integer - pic->width;
406 y = vid_conheight.integer - pic->height;
408 case 4: // centre right
409 x = vid_conwidth.integer - pic->width;
410 y = (vid_conheight.integer - pic->height) / 2;
413 x = vid_conwidth.integer - pic->width;
416 case 6: // top centre
417 x = (vid_conwidth.integer - pic->width) / 2;
424 case 8: // centre left
426 y = (vid_conheight.integer - pic->height) / 2;
432 DrawQ_Pic (x, y, pic, 0, 0, 1, 1, 1, 1, 0);
440 static int SCR_DrawQWDownload(int offset)
442 // sync with SCR_DownloadHeight
448 if (!cls.qw_downloadname[0])
450 cls.qw_downloadspeedrate = 0;
451 cls.qw_downloadspeedtime = realtime;
452 cls.qw_downloadspeedcount = 0;
455 if (realtime >= cls.qw_downloadspeedtime + 1)
457 cls.qw_downloadspeedrate = cls.qw_downloadspeedcount;
458 cls.qw_downloadspeedtime = realtime;
459 cls.qw_downloadspeedcount = 0;
461 if (cls.protocol == PROTOCOL_QUAKEWORLD)
462 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);
464 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);
465 len = (int)strlen(temp);
466 x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, size, size, 0, FONT_INFOBAR)) / 2;
467 y = vid_conheight.integer - size - offset;
468 DrawQ_Fill(0, y, vid_conwidth.integer, size, 0, 0, 0, cls.signon == SIGNONS ? 0.5 : 1, 0);
469 DrawQ_String_Font(x, y, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR);
478 static int SCR_DrawCurlDownload(int offset)
480 // sync with SCR_DownloadHeight
486 Curl_downloadinfo_t *downinfo;
490 downinfo = Curl_GetDownloadInfo(&nDownloads, &addinfo);
494 y = vid_conheight.integer - size * nDownloads - offset;
498 len = (int)strlen(addinfo);
499 x = (vid_conwidth.integer - DrawQ_TextWidth_Font(addinfo, len, size, size, false, FONT_INFOBAR)) / 2;
500 DrawQ_Fill(0, y - size, vid_conwidth.integer, size, 1, 1, 1, cls.signon == SIGNONS ? 0.8 : 1, 0);
501 DrawQ_String_Font(x, y - size, addinfo, len, size, size, 0, 0, 0, 1, 0, NULL, true, FONT_INFOBAR);
504 for(i = 0; i != nDownloads; ++i)
506 if(downinfo[i].queued)
507 dpsnprintf(temp, sizeof(temp), "Still in queue: %s\n", downinfo[i].filename);
508 else if(downinfo[i].progress <= 0)
509 dpsnprintf(temp, sizeof(temp), "Downloading %s ... ???.?%% @ %.1f KiB/s\n", downinfo[i].filename, downinfo[i].speed / 1024.0);
511 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);
512 len = (int)strlen(temp);
513 x = (vid_conwidth.integer - DrawQ_TextWidth_Font(temp, len, size, size, false, FONT_INFOBAR)) / 2;
514 DrawQ_Fill(0, y + i * size, vid_conwidth.integer, size, 0, 0, 0, cls.signon == SIGNONS ? 0.5 : 1, 0);
515 DrawQ_String_Font(x, y + i * size, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true, FONT_INFOBAR);
520 return 8 * (nDownloads + (addinfo ? 1 : 0));
528 static void SCR_DrawDownload()
531 offset += SCR_DrawQWDownload(offset);
532 offset += SCR_DrawCurlDownload(offset);
533 if(offset != scr_con_margin_bottom)
534 Con_DPrintf("broken console margin calculation: %d != %d\n", offset, scr_con_margin_bottom);
537 static int SCR_DownloadHeight()
540 Curl_downloadinfo_t *downinfo;
544 if(cls.qw_downloadname[0])
547 downinfo = Curl_GetDownloadInfo(&nDownloads, &addinfo);
550 offset += 8 * (nDownloads + (addinfo ? 1 : 0));
557 //=============================================================================
561 SCR_SetUpToDrawConsole
564 void SCR_SetUpToDrawConsole (void)
566 // lines of console to display
568 static int framecounter = 0;
572 if (scr_menuforcewhiledisconnected.integer && key_dest == key_game && cls.state == ca_disconnected)
574 if (framecounter >= 2)
582 if (scr_conforcewhiledisconnected.integer && key_dest == key_game && cls.signon != SIGNONS)
583 key_consoleactive |= KEY_CONSOLEACTIVE_FORCED;
585 key_consoleactive &= ~KEY_CONSOLEACTIVE_FORCED;
587 // decide on the height of the console
588 if (key_consoleactive & KEY_CONSOLEACTIVE_USER)
589 conlines = vid_conheight.integer/2; // half screen
591 conlines = 0; // none visible
593 scr_con_current = conlines;
601 void SCR_DrawConsole (void)
603 scr_con_margin_bottom = SCR_DownloadHeight();
604 if (key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
607 Con_DrawConsole (vid_conheight.integer - scr_con_margin_bottom);
609 else if (scr_con_current)
610 Con_DrawConsole (min((int)scr_con_current, vid_conheight.integer - scr_con_margin_bottom));
617 SCR_BeginLoadingPlaque
621 void SCR_BeginLoadingPlaque (void)
623 // save console log up to this point to log_file if it was set by configs
627 SCR_UpdateLoadingScreen(false);
630 //=============================================================================
632 char r_speeds_timestring[1024];
633 int speedstringcount, r_timereport_active;
634 double r_timereport_temp = 0, r_timereport_current = 0, r_timereport_start = 0;
635 int r_speeds_longestitem = 0;
637 void R_TimeReport(char *desc)
643 if (r_speeds.integer < 2 || !r_timereport_active)
647 if (r_speeds.integer == 2)
650 r_timereport_temp = r_timereport_current;
651 r_timereport_current = Sys_DoubleTime();
652 t = (int) ((r_timereport_current - r_timereport_temp) * 1000000.0 + 0.5);
654 length = dpsnprintf(tempbuf, sizeof(tempbuf), "%8i %s", t, desc);
655 length = min(length, (int)sizeof(tempbuf) - 1);
656 if (r_speeds_longestitem < length)
657 r_speeds_longestitem = length;
658 for (;length < r_speeds_longestitem;length++)
659 tempbuf[length] = ' ';
662 if (speedstringcount + length > (vid_conwidth.integer / 8))
664 strlcat(r_speeds_timestring, "\n", sizeof(r_speeds_timestring));
665 speedstringcount = 0;
667 strlcat(r_speeds_timestring, tempbuf, sizeof(r_speeds_timestring));
668 speedstringcount += length;
671 void R_TimeReport_BeginFrame(void)
673 speedstringcount = 0;
674 r_speeds_timestring[0] = 0;
675 r_timereport_active = false;
676 memset(&r_refdef.stats, 0, sizeof(r_refdef.stats));
678 if (r_speeds.integer >= 2 && cls.signon == SIGNONS && cls.state == ca_connected)
680 r_timereport_active = true;
681 r_timereport_start = r_timereport_current = Sys_DoubleTime();
685 void R_TimeReport_EndFrame(void)
692 if (r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected)
694 // put the location name in the r_speeds display as it greatly helps
695 // when creating loc files
696 loc = CL_Locs_FindNearest(cl.movement_origin);
698 sprintf(string + strlen(string), "Location: %s\n", loc->name);
699 sprintf(string + strlen(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]);
700 sprintf(string + strlen(string), "%7i surfaces%7i triangles %5i entities (%7i surfaces%7i triangles)\n", r_refdef.stats.world_surfaces, r_refdef.stats.world_triangles, r_refdef.stats.entities, r_refdef.stats.entities_surfaces, r_refdef.stats.entities_triangles);
701 sprintf(string + strlen(string), "%5i leafs%5i portals%6i particles%6i decals\n", r_refdef.stats.world_leafs, r_refdef.stats.world_portals, r_refdef.stats.particles, r_refdef.stats.decals);
702 sprintf(string + strlen(string), "%7i lightmap updates (%7i pixels)\n", r_refdef.stats.lightmapupdates, r_refdef.stats.lightmapupdatepixels);
703 sprintf(string + strlen(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);
704 if (r_refdef.stats.bloom)
705 sprintf(string + strlen(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);
707 sprintf(string + strlen(string), "rendered%6i meshes%8i triangles\n", r_refdef.stats.meshes, r_refdef.stats.meshes_elements / 3);
708 strlcat(string, r_speeds_timestring, sizeof(string));
710 memset(&r_refdef.stats, 0, sizeof(r_refdef.stats));
712 speedstringcount = 0;
713 r_speeds_timestring[0] = 0;
714 r_timereport_active = false;
716 if (r_speeds.integer >= 2)
718 r_timereport_active = true;
719 r_timereport_start = r_timereport_current = Sys_DoubleTime();
725 if (string[strlen(string)-1] == '\n')
726 string[strlen(string)-1] = 0;
728 for (i = 0;string[i];i++)
729 if (string[i] == '\n')
731 y = vid_conheight.integer - sb_lines - lines * 8;
733 DrawQ_Fill(0, y, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
737 while (string[i] && string[i] != '\n')
740 DrawQ_String(0, y, string + j, i - j, 8, 8, 1, 1, 1, 1, 0, NULL, true);
741 if (string[i] == '\n')
755 void SCR_SizeUp_f (void)
757 Cvar_SetValue ("viewsize",scr_viewsize.value+10);
768 void SCR_SizeDown_f (void)
770 Cvar_SetValue ("viewsize",scr_viewsize.value-10);
773 void SCR_CaptureVideo_EndVideo(void);
774 void CL_Screen_Shutdown(void)
776 SCR_CaptureVideo_EndVideo();
779 void CL_Screen_Init(void)
781 Cvar_RegisterVariable (&scr_fov);
782 Cvar_RegisterVariable (&scr_viewsize);
783 Cvar_RegisterVariable (&scr_conalpha);
784 Cvar_RegisterVariable (&scr_conbrightness);
785 Cvar_RegisterVariable (&scr_conforcewhiledisconnected);
786 Cvar_RegisterVariable (&scr_menuforcewhiledisconnected);
787 Cvar_RegisterVariable (&scr_showram);
788 Cvar_RegisterVariable (&scr_showturtle);
789 Cvar_RegisterVariable (&scr_showpause);
790 Cvar_RegisterVariable (&scr_showbrand);
791 Cvar_RegisterVariable (&scr_centertime);
792 Cvar_RegisterVariable (&scr_printspeed);
793 Cvar_RegisterVariable (&vid_conwidth);
794 Cvar_RegisterVariable (&vid_conheight);
795 Cvar_RegisterVariable (&vid_pixelheight);
796 Cvar_RegisterVariable (&scr_screenshot_jpeg);
797 Cvar_RegisterVariable (&scr_screenshot_jpeg_quality);
798 Cvar_RegisterVariable (&scr_screenshot_gammaboost);
799 Cvar_RegisterVariable (&cl_capturevideo);
800 Cvar_RegisterVariable (&cl_capturevideo_width);
801 Cvar_RegisterVariable (&cl_capturevideo_height);
802 Cvar_RegisterVariable (&cl_capturevideo_realtime);
803 Cvar_RegisterVariable (&cl_capturevideo_fps);
804 Cvar_RegisterVariable (&cl_capturevideo_number);
805 Cvar_RegisterVariable (&r_letterbox);
806 Cvar_RegisterVariable(&r_stereo_separation);
807 Cvar_RegisterVariable(&r_stereo_sidebyside);
808 Cvar_RegisterVariable(&r_stereo_redblue);
809 Cvar_RegisterVariable(&r_stereo_redcyan);
810 Cvar_RegisterVariable(&r_stereo_redgreen);
811 Cvar_RegisterVariable(&r_stereo_angle);
812 Cvar_RegisterVariable(&scr_zoomwindow);
813 Cvar_RegisterVariable(&scr_zoomwindow_viewsizex);
814 Cvar_RegisterVariable(&scr_zoomwindow_viewsizey);
815 Cvar_RegisterVariable(&scr_zoomwindow_fov);
816 Cvar_RegisterVariable(&scr_stipple);
817 Cvar_RegisterVariable(&scr_refresh);
818 Cvar_RegisterVariable(&shownetgraph);
820 Cmd_AddCommand ("sizeup",SCR_SizeUp_f, "increase view size (increases viewsize cvar)");
821 Cmd_AddCommand ("sizedown",SCR_SizeDown_f, "decrease view size (decreases viewsize cvar)");
822 Cmd_AddCommand ("screenshot",SCR_ScreenShot_f, "takes a screenshot of the next rendered frame");
823 Cmd_AddCommand ("envmap", R_Envmap_f, "render a cubemap (skybox) of the current scene");
825 scr_initialized = true;
833 void SCR_ScreenShot_f (void)
835 static int shotnumber;
836 static char oldname[MAX_QPATH];
837 char base[MAX_QPATH];
838 char filename[MAX_QPATH];
839 unsigned char *buffer1;
840 unsigned char *buffer2;
841 unsigned char *buffer3;
842 qboolean jpeg = (scr_screenshot_jpeg.integer != 0);
844 sprintf (base, "screenshots/%s", scr_screenshot_name.string);
846 if (strcmp (oldname, scr_screenshot_name.string))
848 sprintf(oldname, "%s", scr_screenshot_name.string);
852 // find a file name to save it to
853 for (;shotnumber < 1000000;shotnumber++)
854 if (!FS_SysFileExists(va("%s/%s%06d.tga", fs_gamedir, base, shotnumber)) && !FS_SysFileExists(va("%s/%s%06d.jpg", fs_gamedir, base, shotnumber)))
856 if (shotnumber >= 1000000)
858 Con_Print("SCR_ScreenShot_f: Couldn't create the image file\n");
862 sprintf(filename, "%s%06d.%s", base, shotnumber, jpeg ? "jpg" : "tga");
864 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
865 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
866 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3 + 18);
868 if (SCR_ScreenShot (filename, buffer1, buffer2, buffer3, 0, 0, vid.width, vid.height, false, false, false, jpeg, true))
869 Con_Printf("Wrote %s\n", filename);
871 Con_Printf("unable to write %s\n", filename);
880 static void SCR_CaptureVideo_RIFF_Start(void)
882 memset(&cls.capturevideo.riffbuffer, 0, sizeof(sizebuf_t));
883 cls.capturevideo.riffbuffer.maxsize = sizeof(cls.capturevideo.riffbufferdata);
884 cls.capturevideo.riffbuffer.data = cls.capturevideo.riffbufferdata;
887 static void SCR_CaptureVideo_RIFF_Flush(void)
889 if (cls.capturevideo.riffbuffer.cursize > 0)
891 if (!FS_Write(cls.capturevideo.videofile, cls.capturevideo.riffbuffer.data, cls.capturevideo.riffbuffer.cursize))
892 cls.capturevideo.error = true;
893 cls.capturevideo.riffbuffer.cursize = 0;
894 cls.capturevideo.riffbuffer.overflowed = false;
898 static void SCR_CaptureVideo_RIFF_WriteBytes(const unsigned char *data, size_t size)
900 SCR_CaptureVideo_RIFF_Flush();
901 if (!FS_Write(cls.capturevideo.videofile, data, size))
902 cls.capturevideo.error = true;
905 static void SCR_CaptureVideo_RIFF_Write32(int n)
907 if (cls.capturevideo.riffbuffer.cursize + 4 > cls.capturevideo.riffbuffer.maxsize)
908 SCR_CaptureVideo_RIFF_Flush();
909 MSG_WriteLong(&cls.capturevideo.riffbuffer, n);
912 static void SCR_CaptureVideo_RIFF_Write16(int n)
914 if (cls.capturevideo.riffbuffer.cursize + 2 > cls.capturevideo.riffbuffer.maxsize)
915 SCR_CaptureVideo_RIFF_Flush();
916 MSG_WriteShort(&cls.capturevideo.riffbuffer, n);
919 static void SCR_CaptureVideo_RIFF_WriteFourCC(const char *chunkfourcc)
921 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(chunkfourcc) > cls.capturevideo.riffbuffer.maxsize)
922 SCR_CaptureVideo_RIFF_Flush();
923 MSG_WriteUnterminatedString(&cls.capturevideo.riffbuffer, chunkfourcc);
926 static void SCR_CaptureVideo_RIFF_WriteTerminatedString(const char *string)
928 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(string) > cls.capturevideo.riffbuffer.maxsize)
929 SCR_CaptureVideo_RIFF_Flush();
930 MSG_WriteString(&cls.capturevideo.riffbuffer, string);
933 static fs_offset_t SCR_CaptureVideo_RIFF_GetPosition(void)
935 SCR_CaptureVideo_RIFF_Flush();
936 return FS_Tell(cls.capturevideo.videofile);
939 static void SCR_CaptureVideo_RIFF_Push(const char *chunkfourcc, const char *listtypefourcc)
941 SCR_CaptureVideo_RIFF_WriteFourCC(chunkfourcc);
942 SCR_CaptureVideo_RIFF_Write32(0);
943 SCR_CaptureVideo_RIFF_Flush();
944 cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel++] = SCR_CaptureVideo_RIFF_GetPosition();
946 SCR_CaptureVideo_RIFF_WriteFourCC(listtypefourcc);
949 static void SCR_CaptureVideo_RIFF_Pop(void)
953 unsigned char sizebytes[4];
954 // write out the chunk size and then return to the current file position
955 cls.capturevideo.riffstacklevel--;
956 offset = SCR_CaptureVideo_RIFF_GetPosition();
957 x = (int)(offset - (cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel]));
958 sizebytes[0] = (x) & 0xff;sizebytes[1] = (x >> 8) & 0xff;sizebytes[2] = (x >> 16) & 0xff;sizebytes[3] = (x >> 24) & 0xff;
959 FS_Seek(cls.capturevideo.videofile, -(x + 4), SEEK_END);
960 FS_Write(cls.capturevideo.videofile, sizebytes, 4);
961 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
965 FS_Write(cls.capturevideo.videofile, &c, 1);
969 static void GrowBuf(sizebuf_t *buf, int extralen)
971 if(buf->cursize + extralen > buf->maxsize)
973 int oldsize = buf->maxsize;
974 unsigned char *olddata;
976 buf->maxsize = max(buf->maxsize * 2, 4096);
977 buf->data = Mem_Alloc(tempmempool, buf->maxsize);
980 memcpy(buf->data, olddata, oldsize);
986 static void SCR_CaptureVideo_RIFF_IndexEntry(const char *chunkfourcc, int chunksize, int flags)
988 if (cls.capturevideo.riffstacklevel != 2)
989 Sys_Error("SCR_Capturevideo_RIFF_IndexEntry: RIFF stack level is %i (should be 2)\n", cls.capturevideo.riffstacklevel);
990 GrowBuf(&cls.capturevideo.riffindexbuffer, 16);
991 SCR_CaptureVideo_RIFF_Flush();
992 MSG_WriteUnterminatedString(&cls.capturevideo.riffindexbuffer, chunkfourcc);
993 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, flags);
994 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, (int)FS_Tell(cls.capturevideo.videofile) - cls.capturevideo.riffstackstartoffset[1]);
995 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, chunksize);
998 static void SCR_CaptureVideo_RIFF_MakeIxChunk(const char *fcc, const char *dwChunkId, fs_offset_t masteridx_counter, int *masteridx_count, fs_offset_t masteridx_start)
1002 fs_offset_t ix = SCR_CaptureVideo_RIFF_GetPosition();
1005 if(*masteridx_count >= AVI_MASTER_INDEX_SIZE)
1008 nMatching = 0; // go through index and enumerate them
1009 for(i = 0; i < cls.capturevideo.riffindexbuffer.cursize; i += 16)
1010 if(!memcmp(cls.capturevideo.riffindexbuffer.data + i, dwChunkId, 4))
1013 SCR_CaptureVideo_RIFF_Push(fcc, NULL);
1014 SCR_CaptureVideo_RIFF_Write16(2); // wLongsPerEntry
1015 SCR_CaptureVideo_RIFF_Write16(0x0100); // bIndexType=1, bIndexSubType=0
1016 SCR_CaptureVideo_RIFF_Write32(nMatching); // nEntriesInUse
1017 SCR_CaptureVideo_RIFF_WriteFourCC(dwChunkId); // dwChunkId
1018 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.videofile_ix_movistart & (fs_offset_t) 0xFFFFFFFFu);
1019 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) cls.capturevideo.videofile_ix_movistart) >> 32);
1020 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved
1022 for(i = 0; i < cls.capturevideo.riffindexbuffer.cursize; i += 16)
1023 if(!memcmp(cls.capturevideo.riffindexbuffer.data + i, dwChunkId, 4))
1025 unsigned int *p = (unsigned int *) (cls.capturevideo.riffindexbuffer.data + i);
1026 unsigned int flags = p[1];
1027 unsigned int rpos = p[2];
1028 unsigned int size = p[3];
1029 size &= ~0x80000000;
1030 if(!(flags & 0x10)) // no keyframe?
1032 SCR_CaptureVideo_RIFF_Write32(rpos + 8);
1033 SCR_CaptureVideo_RIFF_Write32(size);
1036 SCR_CaptureVideo_RIFF_Pop();
1037 pos = SCR_CaptureVideo_RIFF_GetPosition();
1038 SCR_CaptureVideo_RIFF_Flush();
1040 FS_Seek(cls.capturevideo.videofile, masteridx_start + 16 * *masteridx_count, SEEK_SET);
1041 SCR_CaptureVideo_RIFF_Write32(ix & (fs_offset_t) 0xFFFFFFFFu);
1042 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) ix) >> 32);
1043 SCR_CaptureVideo_RIFF_Write32(pos - ix);
1044 SCR_CaptureVideo_RIFF_Write32(nMatching);
1045 SCR_CaptureVideo_RIFF_Flush();
1047 FS_Seek(cls.capturevideo.videofile, masteridx_counter, SEEK_SET);
1048 SCR_CaptureVideo_RIFF_Write32(++*masteridx_count);
1049 SCR_CaptureVideo_RIFF_Flush();
1051 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
1054 static void SCR_CaptureVideo_RIFF_Finish(qboolean final)
1056 // close the "movi" list
1057 SCR_CaptureVideo_RIFF_Pop();
1058 if(cls.capturevideo.videofile_ix_master_video_inuse_offset)
1059 SCR_CaptureVideo_RIFF_MakeIxChunk("ix00", "00dc", cls.capturevideo.videofile_ix_master_video_inuse_offset, &cls.capturevideo.videofile_ix_master_video_inuse, cls.capturevideo.videofile_ix_master_video_start_offset);
1060 if(cls.capturevideo.videofile_ix_master_audio_inuse_offset)
1061 SCR_CaptureVideo_RIFF_MakeIxChunk("ix01", "01wb", cls.capturevideo.videofile_ix_master_audio_inuse_offset, &cls.capturevideo.videofile_ix_master_audio_inuse, cls.capturevideo.videofile_ix_master_audio_start_offset);
1062 // write the idx1 chunk that we've been building while saving the frames (for old style players)
1063 if(final && cls.capturevideo.videofile_firstchunkframes_offset)
1064 // TODO replace index creating by OpenDML ix##/##ix/indx chunk so it works for more than one AVI part too
1066 SCR_CaptureVideo_RIFF_Push("idx1", NULL);
1067 SCR_CaptureVideo_RIFF_WriteBytes(cls.capturevideo.riffindexbuffer.data, cls.capturevideo.riffindexbuffer.cursize);
1068 SCR_CaptureVideo_RIFF_Pop();
1070 cls.capturevideo.riffindexbuffer.cursize = 0;
1071 // pop the RIFF chunk itself
1072 while (cls.capturevideo.riffstacklevel > 0)
1073 SCR_CaptureVideo_RIFF_Pop();
1074 SCR_CaptureVideo_RIFF_Flush();
1075 if(cls.capturevideo.videofile_firstchunkframes_offset)
1077 Con_DPrintf("Finishing first chunk (%d frames)\n", cls.capturevideo.frame);
1078 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_firstchunkframes_offset, SEEK_SET);
1079 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1080 SCR_CaptureVideo_RIFF_Flush();
1081 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
1082 cls.capturevideo.videofile_firstchunkframes_offset = 0;
1085 Con_DPrintf("Finishing another chunk (%d frames)\n", cls.capturevideo.frame);
1088 static void SCR_CaptureVideo_RIFF_OverflowCheck(int framesize)
1090 fs_offset_t cursize, curfilesize;
1091 if (cls.capturevideo.riffstacklevel != 2)
1092 Sys_Error("SCR_CaptureVideo_RIFF_OverflowCheck: chunk stack leakage!\n");
1093 // check where we are in the file
1094 SCR_CaptureVideo_RIFF_Flush();
1095 cursize = SCR_CaptureVideo_RIFF_GetPosition() - cls.capturevideo.riffstackstartoffset[0];
1096 curfilesize = SCR_CaptureVideo_RIFF_GetPosition();
1098 // if this would overflow the windows limit of 1GB per RIFF chunk, we need
1099 // to close the current RIFF chunk and open another for future frames
1100 if (8 + cursize + framesize + cls.capturevideo.riffindexbuffer.cursize + 8 + cls.capturevideo.riffindexbuffer.cursize + 64 > 1<<30) // note that the Ix buffer takes less space... I just don't dare to / 2 here now... sorry, maybe later
1102 SCR_CaptureVideo_RIFF_Finish(false);
1103 // begin a new 1GB extended section of the AVI
1104 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX");
1105 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1106 cls.capturevideo.videofile_ix_movistart = cls.capturevideo.riffstackstartoffset[1];
1110 static void FindFraction(double val, int *num, int *denom, int denomMax)
1115 bestdiff = fabs(val);
1119 for(i = 1; i <= denomMax; ++i)
1121 int inum = floor(0.5 + val * i);
1122 double diff = fabs(val - inum / (double)i);
1132 void SCR_CaptureVideo_BeginVideo(void)
1134 double gamma, g, aspect;
1135 int width = cl_capturevideo_width.integer, height = cl_capturevideo_height.integer;
1138 if (cls.capturevideo.active)
1140 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1141 // soundrate is figured out on the first SoundFrame
1143 if(width == 0 && height != 0)
1144 width = (int) (height * (double)vid.width / ((double)vid.height * vid_pixelheight.value)); // keep aspect
1145 if(width != 0 && height == 0)
1146 height = (int) (width * ((double)vid.height * vid_pixelheight.value) / (double)vid.width); // keep aspect
1148 if(width < 2 || width > vid.width) // can't scale up
1150 if(height < 2 || height > vid.height) // can't scale up
1151 height = vid.height;
1153 aspect = vid.width / (vid.height * vid_pixelheight.value);
1155 // ensure it's all even; if not, scale down a little
1161 cls.capturevideo.width = width;
1162 cls.capturevideo.height = height;
1163 cls.capturevideo.active = true;
1164 cls.capturevideo.starttime = realtime;
1165 cls.capturevideo.framerate = bound(1, cl_capturevideo_fps.value, 1000);
1166 cls.capturevideo.soundrate = S_GetSoundRate();
1167 cls.capturevideo.frame = 0;
1168 cls.capturevideo.soundsampleframe = 0;
1169 cls.capturevideo.realtime = cl_capturevideo_realtime.integer != 0;
1170 cls.capturevideo.screenbuffer = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 4);
1171 cls.capturevideo.outbuffer = (unsigned char *)Mem_Alloc(tempmempool, width * height * (4+4) + 18);
1172 gamma = 1.0/scr_screenshot_gammaboost.value;
1173 dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/dpvideo%03i", cl_capturevideo_number.integer);
1174 Cvar_SetValueQuick(&cl_capturevideo_number, cl_capturevideo_number.integer + 1);
1177 for (i = 0;i < 256;i++)
1179 unsigned char j = (unsigned char)bound(0, 255*pow(i/255.0, gamma), 255);
1180 cls.capturevideo.rgbgammatable[0][i] = j;
1181 cls.capturevideo.rgbgammatable[1][i] = j;
1182 cls.capturevideo.rgbgammatable[2][i] = j;
1186 R = Y + 1.4075 * (Cr - 128);
1187 G = Y + -0.3455 * (Cb - 128) + -0.7169 * (Cr - 128);
1188 B = Y + 1.7790 * (Cb - 128);
1189 Y = R * .299 + G * .587 + B * .114;
1190 Cb = R * -.169 + G * -.332 + B * .500 + 128.;
1191 Cr = R * .500 + G * -.419 + B * -.0813 + 128.;
1193 for (i = 0;i < 256;i++)
1195 g = 255*pow(i/255.0, gamma);
1196 // Y weights from RGB
1197 cls.capturevideo.rgbtoyuvscaletable[0][0][i] = (short)(g * 0.299);
1198 cls.capturevideo.rgbtoyuvscaletable[0][1][i] = (short)(g * 0.587);
1199 cls.capturevideo.rgbtoyuvscaletable[0][2][i] = (short)(g * 0.114);
1200 // Cb weights from RGB
1201 cls.capturevideo.rgbtoyuvscaletable[1][0][i] = (short)(g * -0.169);
1202 cls.capturevideo.rgbtoyuvscaletable[1][1][i] = (short)(g * -0.332);
1203 cls.capturevideo.rgbtoyuvscaletable[1][2][i] = (short)(g * 0.500);
1204 // Cr weights from RGB
1205 cls.capturevideo.rgbtoyuvscaletable[2][0][i] = (short)(g * 0.500);
1206 cls.capturevideo.rgbtoyuvscaletable[2][1][i] = (short)(g * -0.419);
1207 cls.capturevideo.rgbtoyuvscaletable[2][2][i] = (short)(g * -0.0813);
1208 // range reduction of YCbCr to valid signal range
1209 cls.capturevideo.yuvnormalizetable[0][i] = 16 + i * (236-16) / 256;
1210 cls.capturevideo.yuvnormalizetable[1][i] = 16 + i * (240-16) / 256;
1211 cls.capturevideo.yuvnormalizetable[2][i] = 16 + i * (240-16) / 256;
1214 //if (cl_capturevideo_)
1219 cls.capturevideo.format = CAPTUREVIDEOFORMAT_AVI_I420;
1220 cls.capturevideo.videofile = FS_Open (va("%s.avi", cls.capturevideo.basename), "wb", false, true);
1221 SCR_CaptureVideo_RIFF_Start();
1222 // 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)
1223 SCR_CaptureVideo_RIFF_Push("RIFF", "AVI ");
1225 SCR_CaptureVideo_RIFF_Push("LIST", "hdrl");
1226 SCR_CaptureVideo_RIFF_Push("avih", NULL);
1227 SCR_CaptureVideo_RIFF_Write32((int)(1000000.0 / cls.capturevideo.framerate)); // microseconds per frame
1228 SCR_CaptureVideo_RIFF_Write32(0); // max bytes per second
1229 SCR_CaptureVideo_RIFF_Write32(0); // padding granularity
1230 SCR_CaptureVideo_RIFF_Write32(0x910); // flags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
1231 cls.capturevideo.videofile_firstchunkframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
1232 SCR_CaptureVideo_RIFF_Write32(0); // total frames
1233 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1234 if (cls.capturevideo.soundrate)
1235 SCR_CaptureVideo_RIFF_Write32(2); // number of streams
1237 SCR_CaptureVideo_RIFF_Write32(1); // number of streams
1238 SCR_CaptureVideo_RIFF_Write32(0); // suggested buffer size
1239 SCR_CaptureVideo_RIFF_Write32(width); // width
1240 SCR_CaptureVideo_RIFF_Write32(height); // height
1241 SCR_CaptureVideo_RIFF_Write32(0); // reserved[0]
1242 SCR_CaptureVideo_RIFF_Write32(0); // reserved[1]
1243 SCR_CaptureVideo_RIFF_Write32(0); // reserved[2]
1244 SCR_CaptureVideo_RIFF_Write32(0); // reserved[3]
1245 SCR_CaptureVideo_RIFF_Pop();
1246 // video stream info
1247 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
1248 SCR_CaptureVideo_RIFF_Push("strh", "vids");
1249 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // stream fourcc (I420 colorspace, uncompressed)
1250 SCR_CaptureVideo_RIFF_Write32(0); // flags
1251 SCR_CaptureVideo_RIFF_Write16(0); // priority
1252 SCR_CaptureVideo_RIFF_Write16(0); // language
1253 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1254 // find an ideal divisor for the framerate
1255 FindFraction(cls.capturevideo.framerate, &n, &d, 1000);
1256 SCR_CaptureVideo_RIFF_Write32(d); // samples/second divisor
1257 SCR_CaptureVideo_RIFF_Write32(n); // samples/second multiplied by divisor
1258 SCR_CaptureVideo_RIFF_Write32(0); // start
1259 cls.capturevideo.videofile_totalframes_offset1 = SCR_CaptureVideo_RIFF_GetPosition();
1260 SCR_CaptureVideo_RIFF_Write32(0); // length
1261 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // suggested buffer size
1262 SCR_CaptureVideo_RIFF_Write32(0); // quality
1263 SCR_CaptureVideo_RIFF_Write32(0); // sample size
1264 SCR_CaptureVideo_RIFF_Write16(0); // frame left
1265 SCR_CaptureVideo_RIFF_Write16(0); // frame top
1266 SCR_CaptureVideo_RIFF_Write16(width); // frame right
1267 SCR_CaptureVideo_RIFF_Write16(height); // frame bottom
1268 SCR_CaptureVideo_RIFF_Pop();
1269 // video stream format
1270 SCR_CaptureVideo_RIFF_Push("strf", NULL);
1271 SCR_CaptureVideo_RIFF_Write32(40); // BITMAPINFO struct size
1272 SCR_CaptureVideo_RIFF_Write32(width); // width
1273 SCR_CaptureVideo_RIFF_Write32(height); // height
1274 SCR_CaptureVideo_RIFF_Write16(3); // planes
1275 SCR_CaptureVideo_RIFF_Write16(12); // bitcount
1276 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // compression
1277 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // size of image
1278 SCR_CaptureVideo_RIFF_Write32(0); // x pixels per meter
1279 SCR_CaptureVideo_RIFF_Write32(0); // y pixels per meter
1280 SCR_CaptureVideo_RIFF_Write32(0); // color used
1281 SCR_CaptureVideo_RIFF_Write32(0); // color important
1282 SCR_CaptureVideo_RIFF_Pop();
1284 SCR_CaptureVideo_RIFF_Push("indx", NULL);
1285 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
1286 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
1287 cls.capturevideo.videofile_ix_master_video_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
1288 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
1289 SCR_CaptureVideo_RIFF_WriteFourCC("00dc"); // dwChunkId
1290 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
1291 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
1292 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
1293 cls.capturevideo.videofile_ix_master_video_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
1294 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
1295 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
1296 SCR_CaptureVideo_RIFF_Pop();
1297 // extended format (aspect!)
1298 SCR_CaptureVideo_RIFF_Push("vprp", NULL);
1299 SCR_CaptureVideo_RIFF_Write32(0); // VideoFormatToken
1300 SCR_CaptureVideo_RIFF_Write32(0); // VideoStandard
1301 SCR_CaptureVideo_RIFF_Write32((int)cls.capturevideo.framerate); // dwVerticalRefreshRate (bogus)
1302 SCR_CaptureVideo_RIFF_Write32(width); // dwHTotalInT
1303 SCR_CaptureVideo_RIFF_Write32(height); // dwVTotalInLines
1304 FindFraction(aspect, &n, &d, 1000);
1305 SCR_CaptureVideo_RIFF_Write32((n << 16) | d); // dwFrameAspectRatio // TODO a word
1306 SCR_CaptureVideo_RIFF_Write32(width); // dwFrameWidthInPixels
1307 SCR_CaptureVideo_RIFF_Write32(height); // dwFrameHeightInLines
1308 SCR_CaptureVideo_RIFF_Write32(1); // nFieldPerFrame
1309 SCR_CaptureVideo_RIFF_Write32(width); // CompressedBMWidth
1310 SCR_CaptureVideo_RIFF_Write32(height); // CompressedBMHeight
1311 SCR_CaptureVideo_RIFF_Write32(width); // ValidBMHeight
1312 SCR_CaptureVideo_RIFF_Write32(height); // ValidBMWidth
1313 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffset
1314 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYOffset
1315 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffsetInT
1316 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYValidStartLine
1317 SCR_CaptureVideo_RIFF_Pop();
1318 SCR_CaptureVideo_RIFF_Pop();
1319 if (cls.capturevideo.soundrate)
1321 // audio stream info
1322 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
1323 SCR_CaptureVideo_RIFF_Push("strh", "auds");
1324 SCR_CaptureVideo_RIFF_Write32(1); // stream fourcc (PCM audio, uncompressed)
1325 SCR_CaptureVideo_RIFF_Write32(0); // flags
1326 SCR_CaptureVideo_RIFF_Write16(0); // priority
1327 SCR_CaptureVideo_RIFF_Write16(0); // language
1328 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1329 SCR_CaptureVideo_RIFF_Write32(1); // samples/second divisor
1330 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.soundrate)); // samples/second multiplied by divisor
1331 SCR_CaptureVideo_RIFF_Write32(0); // start
1332 cls.capturevideo.videofile_totalsampleframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
1333 SCR_CaptureVideo_RIFF_Write32(0); // length
1334 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 2); // suggested buffer size (this is a half second)
1335 SCR_CaptureVideo_RIFF_Write32(0); // quality
1336 SCR_CaptureVideo_RIFF_Write32(4); // sample size
1337 SCR_CaptureVideo_RIFF_Write16(0); // frame left
1338 SCR_CaptureVideo_RIFF_Write16(0); // frame top
1339 SCR_CaptureVideo_RIFF_Write16(0); // frame right
1340 SCR_CaptureVideo_RIFF_Write16(0); // frame bottom
1341 SCR_CaptureVideo_RIFF_Pop();
1342 // audio stream format
1343 SCR_CaptureVideo_RIFF_Push("strf", NULL);
1344 SCR_CaptureVideo_RIFF_Write16(1); // format (uncompressed PCM?)
1345 SCR_CaptureVideo_RIFF_Write16(2); // channels (stereo)
1346 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate); // sampleframes per second
1347 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 4); // average bytes per second
1348 SCR_CaptureVideo_RIFF_Write16(4); // block align
1349 SCR_CaptureVideo_RIFF_Write16(16); // bits per sample
1350 SCR_CaptureVideo_RIFF_Write16(0); // size
1351 SCR_CaptureVideo_RIFF_Pop();
1353 SCR_CaptureVideo_RIFF_Push("indx", NULL);
1354 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
1355 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
1356 cls.capturevideo.videofile_ix_master_audio_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
1357 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
1358 SCR_CaptureVideo_RIFF_WriteFourCC("01wb"); // dwChunkId
1359 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
1360 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
1361 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
1362 cls.capturevideo.videofile_ix_master_audio_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
1363 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
1364 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
1365 SCR_CaptureVideo_RIFF_Pop();
1366 SCR_CaptureVideo_RIFF_Pop();
1369 cls.capturevideo.videofile_ix_master_audio_inuse = cls.capturevideo.videofile_ix_master_video_inuse = 0;
1371 // extended header (for total #frames)
1372 SCR_CaptureVideo_RIFF_Push("LIST", "odml");
1373 SCR_CaptureVideo_RIFF_Push("dmlh", NULL);
1374 cls.capturevideo.videofile_totalframes_offset2 = SCR_CaptureVideo_RIFF_GetPosition();
1375 SCR_CaptureVideo_RIFF_Write32(0);
1376 SCR_CaptureVideo_RIFF_Pop();
1377 SCR_CaptureVideo_RIFF_Pop();
1379 // close the AVI header list
1380 SCR_CaptureVideo_RIFF_Pop();
1381 // software that produced this AVI video file
1382 SCR_CaptureVideo_RIFF_Push("LIST", "INFO");
1383 SCR_CaptureVideo_RIFF_Push("ISFT", NULL);
1384 SCR_CaptureVideo_RIFF_WriteTerminatedString(engineversion);
1385 SCR_CaptureVideo_RIFF_Pop();
1386 // enable this junk filler if you like the LIST movi to always begin at 4KB in the file (why?)
1388 SCR_CaptureVideo_RIFF_Push("JUNK", NULL);
1389 x = 4096 - SCR_CaptureVideo_RIFF_GetPosition();
1392 const char *junkfiller = "[ DarkPlaces junk data ]";
1393 int i = min(x, (int)strlen(junkfiller));
1394 SCR_CaptureVideo_RIFF_WriteBytes((const unsigned char *)junkfiller, i);
1397 SCR_CaptureVideo_RIFF_Pop();
1399 SCR_CaptureVideo_RIFF_Pop();
1400 // begin the actual video section now
1401 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1402 cls.capturevideo.videofile_ix_movistart = cls.capturevideo.riffstackstartoffset[1];
1403 // we're done with the headers now...
1404 SCR_CaptureVideo_RIFF_Flush();
1405 if (cls.capturevideo.riffstacklevel != 2)
1406 Sys_Error("SCR_CaptureVideo_BeginVideo: broken AVI writing code (stack level is %i (should be 2) at end of headers)\n", cls.capturevideo.riffstacklevel);
1409 switch(cls.capturevideo.format)
1411 case CAPTUREVIDEOFORMAT_AVI_I420:
1418 void SCR_CaptureVideo_EndVideo(void)
1420 if (!cls.capturevideo.active)
1422 cls.capturevideo.active = false;
1423 if (cls.capturevideo.videofile)
1425 switch(cls.capturevideo.format)
1427 case CAPTUREVIDEOFORMAT_AVI_I420:
1428 // close any open chunks
1429 SCR_CaptureVideo_RIFF_Finish(true);
1430 // go back and fix the video frames and audio samples fields
1431 Con_DPrintf("Finishing capture (%d frames, %d audio frames)\n", cls.capturevideo.frame, cls.capturevideo.soundsampleframe);
1432 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset1, SEEK_SET);
1433 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1434 SCR_CaptureVideo_RIFF_Flush();
1435 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset2, SEEK_SET);
1436 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1437 SCR_CaptureVideo_RIFF_Flush();
1438 if (cls.capturevideo.soundrate)
1440 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalsampleframes_offset, SEEK_SET);
1441 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundsampleframe);
1442 SCR_CaptureVideo_RIFF_Flush();
1448 FS_Close(cls.capturevideo.videofile);
1449 cls.capturevideo.videofile = NULL;
1452 if (cls.capturevideo.screenbuffer)
1454 Mem_Free (cls.capturevideo.screenbuffer);
1455 cls.capturevideo.screenbuffer = NULL;
1458 if (cls.capturevideo.outbuffer)
1460 Mem_Free (cls.capturevideo.outbuffer);
1461 cls.capturevideo.outbuffer = NULL;
1464 if (cls.capturevideo.riffindexbuffer.data)
1466 Mem_Free(cls.capturevideo.riffindexbuffer.data);
1467 cls.capturevideo.riffindexbuffer.data = NULL;
1470 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1473 // converts from BGRA32 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
1474 void SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(int width, int height, unsigned char *instart, unsigned char *outstart)
1477 int blockr, blockg, blockb;
1478 int outoffset = (width/2)*(height/2);
1479 unsigned char *b, *out;
1480 // process one line at a time, and CbCr every other line at 2 pixel intervals
1481 for (y = 0;y < height;y++)
1484 for (b = instart + (height-1-y)*width*4, out = outstart + y*width, x = 0;x < width;x++, b += 4, out++)
1489 *out = cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[0][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[0][2][blockb]];
1493 // 2x2 Cr and Cb planes
1494 int inpitch = width*4;
1495 for (b = instart + (height-2-y)*width*4, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 8, out++)
1497 blockr = (b[2] + b[6] + b[inpitch+2] + b[inpitch+6]) >> 2;
1498 blockg = (b[1] + b[5] + b[inpitch+1] + b[inpitch+5]) >> 2;
1499 blockb = (b[0] + b[4] + b[inpitch+0] + b[inpitch+4]) >> 2;
1501 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];
1503 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];
1509 static void SCR_ScaleDownBGRA(unsigned char *in, int inw, int inh, unsigned char *out, int outw, int outh)
1511 // TODO optimize this function
1516 // memcpy is faster than me
1517 if(inw == outw && inh == outh)
1519 memcpy(out, in, 4 * inw * inh);
1523 // otherwise: a box filter
1524 area = (float)outw * (float)outh / (float)inw / (float)inh;
1525 for(y = 0; y < outh; ++y)
1527 float iny0 = y / (float)outh * inh; int iny0_i = floor(iny0);
1528 float iny1 = (y+1) / (float)outh * inh; int iny1_i = ceil(iny1);
1529 for(x = 0; x < outw; ++x)
1531 float inx0 = x / (float)outw * inw; int inx0_i = floor(inx0);
1532 float inx1 = (x+1) / (float)outw * inw; int inx1_i = ceil(inx1);
1533 float r = 0, g = 0, b = 0, alpha = 0;
1536 for(yy = iny0_i; yy < iny1_i; ++yy)
1538 float ya = min(yy+1, iny1) - max(iny0, yy);
1539 for(xx = inx0_i; xx < inx1_i; ++xx)
1541 float a = ya * (min(xx+1, inx1) - max(inx0, xx));
1542 r += a * in[4*(xx + inw * yy)+0];
1543 g += a * in[4*(xx + inw * yy)+1];
1544 b += a * in[4*(xx + inw * yy)+2];
1545 alpha += a * in[4*(xx + inw * yy)+3];
1549 out[4*(x + outw * y)+0] = r * area;
1550 out[4*(x + outw * y)+1] = g * area;
1551 out[4*(x + outw * y)+2] = b * area;
1552 out[4*(x + outw * y)+3] = alpha * area;
1557 qboolean SCR_CaptureVideo_VideoFrame(int newframenum)
1559 int x = 0, y = 0, width = cls.capturevideo.width, height = cls.capturevideo.height;
1560 unsigned char *in, *out;
1562 //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);
1563 // speed is critical here, so do saving as directly as possible
1564 switch (cls.capturevideo.format)
1566 case CAPTUREVIDEOFORMAT_AVI_I420:
1567 // if there's no videofile we have to just give up, and abort saving
1568 if (!cls.capturevideo.videofile)
1570 // FIXME: width/height must be multiple of 2, enforce this?
1571 qglReadPixels (x, y, vid.width, vid.height, GL_BGRA, GL_UNSIGNED_BYTE, cls.capturevideo.screenbuffer);CHECKGLERROR
1572 SCR_ScaleDownBGRA (cls.capturevideo.screenbuffer, vid.width, vid.height, cls.capturevideo.outbuffer, width, height);
1573 in = cls.capturevideo.outbuffer;
1574 out = cls.capturevideo.outbuffer + width*height*4;
1575 SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(width, height, in, out);
1576 x = width*height+(width/2)*(height/2)*2;
1577 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1578 for (;cls.capturevideo.frame < newframenum;cls.capturevideo.frame++)
1580 SCR_CaptureVideo_RIFF_IndexEntry("00dc", x, 0x10); // AVIIF_KEYFRAME
1581 SCR_CaptureVideo_RIFF_Push("00dc", NULL);
1582 SCR_CaptureVideo_RIFF_WriteBytes(out, x);
1583 SCR_CaptureVideo_RIFF_Pop();
1591 void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate)
1594 cls.capturevideo.soundrate = rate;
1595 cls.capturevideo.soundsampleframe += length;
1596 switch (cls.capturevideo.format)
1598 case CAPTUREVIDEOFORMAT_AVI_I420:
1600 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1601 SCR_CaptureVideo_RIFF_IndexEntry("01wb", x, 0x10); // AVIIF_KEYFRAME
1602 SCR_CaptureVideo_RIFF_Push("01wb", NULL);
1603 SCR_CaptureVideo_RIFF_WriteBytes(bufstereo16le, x);
1604 SCR_CaptureVideo_RIFF_Pop();
1611 void SCR_CaptureVideo(void)
1614 if (cl_capturevideo.integer && r_render.integer)
1616 if (!cls.capturevideo.active)
1617 SCR_CaptureVideo_BeginVideo();
1618 if (cls.capturevideo.framerate != cl_capturevideo_fps.value)
1620 Con_Printf("You can not change the video framerate while recording a video.\n");
1621 Cvar_SetValueQuick(&cl_capturevideo_fps, cls.capturevideo.framerate);
1623 // for AVI saving we have to make sure that sound is saved before video
1624 if (cls.capturevideo.soundrate && !cls.capturevideo.soundsampleframe)
1626 if (cls.capturevideo.realtime)
1628 // preserve sound sync by duplicating frames when running slow
1629 newframenum = (int)((realtime - cls.capturevideo.starttime) * cls.capturevideo.framerate);
1632 newframenum = cls.capturevideo.frame + 1;
1633 // if falling behind more than one second, stop
1634 if (newframenum - cls.capturevideo.frame > (int)ceil(cls.capturevideo.framerate))
1636 Cvar_SetValueQuick(&cl_capturevideo, 0);
1637 Con_Printf("video saving failed on frame %i, your machine is too slow for this capture speed.\n", cls.capturevideo.frame);
1638 SCR_CaptureVideo_EndVideo();
1642 SCR_CaptureVideo_VideoFrame(newframenum);
1643 if (cls.capturevideo.error)
1645 Cvar_SetValueQuick(&cl_capturevideo, 0);
1646 Con_Printf("video saving failed on frame %i, out of disk space? stopping video capture.\n", cls.capturevideo.frame);
1647 SCR_CaptureVideo_EndVideo();
1650 else if (cls.capturevideo.active)
1651 SCR_CaptureVideo_EndVideo();
1658 Grab six views for environment mapping tests
1665 qboolean flipx, flipy, flipdiagonaly;
1669 {{ 0, 0, 0}, "rt", false, false, false},
1670 {{ 0, 270, 0}, "ft", false, false, false},
1671 {{ 0, 180, 0}, "lf", false, false, false},
1672 {{ 0, 90, 0}, "bk", false, false, false},
1673 {{-90, 180, 0}, "up", true, true, false},
1674 {{ 90, 180, 0}, "dn", true, true, false},
1676 {{ 0, 0, 0}, "px", true, true, true},
1677 {{ 0, 90, 0}, "py", false, true, false},
1678 {{ 0, 180, 0}, "nx", false, false, true},
1679 {{ 0, 270, 0}, "ny", true, false, false},
1680 {{-90, 180, 0}, "pz", false, false, true},
1681 {{ 90, 180, 0}, "nz", false, false, true}
1684 static void R_Envmap_f (void)
1687 char filename[MAX_QPATH], basename[MAX_QPATH];
1688 unsigned char *buffer1;
1689 unsigned char *buffer2;
1690 unsigned char *buffer3;
1692 if (Cmd_Argc() != 3)
1694 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");
1698 strlcpy (basename, Cmd_Argv(1), sizeof (basename));
1699 size = atoi(Cmd_Argv(2));
1700 if (size != 128 && size != 256 && size != 512 && size != 1024)
1702 Con_Print("envmap: size must be one of 128, 256, 512, or 1024\n");
1705 if (size > vid.width || size > vid.height)
1707 Con_Print("envmap: your resolution is not big enough to render that size\n");
1711 r_refdef.envmap = true;
1713 R_UpdateVariables();
1718 r_view.width = size;
1719 r_view.height = size;
1721 r_view.useperspective = true;
1723 r_view.frustum_x = tan(90 * M_PI / 360.0);
1724 r_view.frustum_y = tan(90 * M_PI / 360.0);
1726 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1727 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1728 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3 + 18);
1730 for (j = 0;j < 12;j++)
1732 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1733 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);
1738 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);
1745 r_refdef.envmap = false;
1748 //=============================================================================
1750 void SHOWLMP_decodehide(void)
1754 lmplabel = MSG_ReadString();
1755 for (i = 0;i < cl.num_showlmps;i++)
1756 if (cl.showlmps[i].isactive && strcmp(cl.showlmps[i].label, lmplabel) == 0)
1758 cl.showlmps[i].isactive = false;
1763 void SHOWLMP_decodeshow(void)
1766 char lmplabel[256], picname[256];
1768 strlcpy (lmplabel,MSG_ReadString(), sizeof (lmplabel));
1769 strlcpy (picname, MSG_ReadString(), sizeof (picname));
1770 if (gamemode == GAME_NEHAHRA) // LordHavoc: nasty old legacy junk
1777 x = MSG_ReadShort();
1778 y = MSG_ReadShort();
1780 if (!cl.showlmps || cl.num_showlmps >= cl.max_showlmps)
1782 showlmp_t *oldshowlmps = cl.showlmps;
1783 cl.max_showlmps += 16;
1784 cl.showlmps = Mem_Alloc(cls.levelmempool, cl.max_showlmps * sizeof(showlmp_t));
1785 if (cl.num_showlmps)
1786 memcpy(cl.showlmps, oldshowlmps, cl.num_showlmps * sizeof(showlmp_t));
1788 Mem_Free(oldshowlmps);
1790 for (k = 0;k < cl.max_showlmps;k++)
1791 if (cl.showlmps[k].isactive && !strcmp(cl.showlmps[k].label, lmplabel))
1793 if (k == cl.max_showlmps)
1794 for (k = 0;k < cl.max_showlmps;k++)
1795 if (!cl.showlmps[k].isactive)
1797 cl.showlmps[k].isactive = true;
1798 strlcpy (cl.showlmps[k].label, lmplabel, sizeof (cl.showlmps[k].label));
1799 strlcpy (cl.showlmps[k].pic, picname, sizeof (cl.showlmps[k].pic));
1800 cl.showlmps[k].x = x;
1801 cl.showlmps[k].y = y;
1802 cl.num_showlmps = max(cl.num_showlmps, k + 1);
1805 void SHOWLMP_drawall(void)
1808 for (i = 0;i < cl.num_showlmps;i++)
1809 if (cl.showlmps[i].isactive)
1810 DrawQ_Pic(cl.showlmps[i].x, cl.showlmps[i].y, Draw_CachePic(cl.showlmps[i].pic, true), 0, 0, 1, 1, 1, 1, 0);
1814 ==============================================================================
1818 ==============================================================================
1821 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)
1823 int indices[3] = {0,1,2};
1826 if (!r_render.integer)
1830 qglReadPixels (x, y, width, height, jpeg ? GL_RGB : GL_BGR, GL_UNSIGNED_BYTE, buffer1);CHECKGLERROR
1832 if (scr_screenshot_gammaboost.value != 1 && gammacorrect)
1835 double igamma = 1.0 / scr_screenshot_gammaboost.value;
1836 unsigned char ramp[256];
1837 for (i = 0;i < 256;i++)
1838 ramp[i] = (unsigned char) (pow(i * (1.0 / 255.0), igamma) * 255.0);
1839 for (i = 0;i < width*height*3;i++)
1840 buffer1[i] = ramp[buffer1[i]];
1843 Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
1846 ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
1848 ret = Image_WriteTGABGR_preflipped (filename, width, height, buffer2, buffer3);
1853 //=============================================================================
1855 void R_ClearScreen(void)
1859 if (r_refdef.fogenabled)
1861 qglClearColor(r_refdef.fogcolor[0],r_refdef.fogcolor[1],r_refdef.fogcolor[2],0);CHECKGLERROR
1865 qglClearColor(0,0,0,0);CHECKGLERROR
1867 qglClearDepth(1);CHECKGLERROR
1870 // LordHavoc: we use a stencil centered around 128 instead of 0,
1871 // to avoid clamping interfering with strange shadow volume
1873 qglClearStencil(128);CHECKGLERROR
1876 if (r_render.integer)
1877 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));
1878 // set dithering mode
1879 if (gl_dither.integer)
1881 qglEnable(GL_DITHER);CHECKGLERROR
1885 qglDisable(GL_DITHER);CHECKGLERROR
1889 qboolean CL_VM_UpdateView (void);
1890 void SCR_DrawConsole (void);
1891 void R_Shadow_EditLights_DrawSelectedLightProperties(void);
1895 void SCR_DrawScreen (void)
1899 R_TimeReport_BeginFrame();
1901 R_UpdateVariables();
1903 // Quake uses clockwise winding, so these are swapped
1904 r_view.cullface_front = GL_BACK;
1905 r_view.cullface_back = GL_FRONT;
1907 if (cls.signon == SIGNONS)
1911 size = scr_viewsize.value * (1.0 / 100.0);
1912 size = min(size, 1);
1914 if (r_stereo_sidebyside.integer)
1916 r_view.width = (int)(vid.width * size / 2.5);
1917 r_view.height = (int)(vid.height * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100));
1919 r_view.x = (int)((vid.width - r_view.width * 2.5) * 0.5);
1920 r_view.y = (int)((vid.height - r_view.height)/2);
1923 r_view.x += (int)(r_view.width * 1.5);
1927 r_view.width = (int)(vid.width * size);
1928 r_view.height = (int)(vid.height * size * (1 - bound(0, r_letterbox.value, 100) / 100));
1930 r_view.x = (int)((vid.width - r_view.width)/2);
1931 r_view.y = (int)((vid.height - r_view.height)/2);
1935 // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
1936 // LordHavoc: this is designed to produce widescreen fov values
1937 // when the screen is wider than 4/3 width/height aspect, to do
1938 // this it simply assumes the requested fov is the vertical fov
1939 // for a 4x3 display, if the ratio is not 4x3 this makes the fov
1940 // higher/lower according to the ratio
1941 r_view.useperspective = true;
1942 r_view.frustum_y = tan(scr_fov.value * M_PI / 360.0) * (3.0/4.0) * cl.viewzoom;
1943 r_view.frustum_x = r_view.frustum_y * (float)r_view.width / (float)r_view.height / vid_pixelheight.value;
1945 r_view.frustum_x *= r_refdef.frustumscale_x;
1946 r_view.frustum_y *= r_refdef.frustumscale_y;
1948 if(!CL_VM_UpdateView())
1951 if (scr_zoomwindow.integer)
1953 float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
1954 float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
1955 r_view.width = (int)(vid.width * sizex);
1956 r_view.height = (int)(vid.height * sizey);
1958 r_view.x = (int)((vid.width - r_view.width)/2);
1962 r_view.useperspective = true;
1963 r_view.frustum_y = tan(scr_zoomwindow_fov.value * M_PI / 360.0) * (3.0/4.0) * cl.viewzoom;
1964 r_view.frustum_x = r_view.frustum_y * vid_pixelheight.value * (float)r_view.width / (float)r_view.height;
1966 r_view.frustum_x *= r_refdef.frustumscale_x;
1967 r_view.frustum_y *= r_refdef.frustumscale_y;
1969 if(!CL_VM_UpdateView())
1974 if (!r_stereo_sidebyside.integer)
1976 r_view.width = vid.width;
1977 r_view.height = vid.height;
1982 r_view.useperspective = false;
1986 if(!scr_con_current && !(key_consoleactive & KEY_CONSOLEACTIVE_FORCED))
1987 if ((key_dest == key_game || key_dest == key_message) && !r_letterbox.value)
1988 Con_DrawNotify (); // only draw notify in game
1990 if (cls.signon == SIGNONS)
1995 if (!r_letterbox.value)
1998 SCR_CheckDrawCenterString();
2000 SCR_DrawNetGraph ();
2003 R_Shadow_EditLights_DrawSelectedLightProperties();
2011 if (r_timereport_active)
2014 if (cls.signon == SIGNONS)
2015 R_TimeReport_EndFrame();
2024 void SCR_UpdateLoadingScreen (qboolean clear)
2029 float texcoord2f[8];
2030 // don't do anything if not initialized yet
2031 if (vid_hidden || !scr_refresh.integer)
2034 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
2035 //qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
2036 //qglDepthMask(1);CHECKGLERROR
2037 qglColorMask(1,1,1,1);CHECKGLERROR
2038 qglClearColor(0,0,0,0);CHECKGLERROR
2039 // when starting up a new video mode, make sure the screen is cleared to black
2042 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2044 //qglDisable(GL_CULL_FACE);CHECKGLERROR
2047 GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
2049 R_Mesh_Matrix(&identitymatrix);
2050 // draw the loading plaque
2051 pic = Draw_CachePic("gfx/loading", true);
2052 x = (vid_conwidth.integer - pic->width)/2;
2053 y = (vid_conheight.integer - pic->height)/2;
2055 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2056 GL_DepthRange(0, 1);
2057 GL_PolygonOffset(0, 0);
2058 GL_DepthTest(false);
2059 R_Mesh_VertexPointer(vertex3f, 0, 0);
2060 R_Mesh_ColorPointer(NULL, 0, 0);
2061 R_Mesh_ResetTextureState();
2062 R_Mesh_TexBind(0, R_GetTexture(pic->tex));
2063 R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
2064 vertex3f[2] = vertex3f[5] = vertex3f[8] = vertex3f[11] = 0;
2065 vertex3f[0] = vertex3f[9] = x;
2066 vertex3f[1] = vertex3f[4] = y;
2067 vertex3f[3] = vertex3f[6] = x + pic->width;
2068 vertex3f[7] = vertex3f[10] = y + pic->height;
2069 texcoord2f[0] = 0;texcoord2f[1] = 0;
2070 texcoord2f[2] = 1;texcoord2f[3] = 0;
2071 texcoord2f[4] = 1;texcoord2f[5] = 1;
2072 texcoord2f[6] = 0;texcoord2f[7] = 1;
2073 if (vid.stereobuffer)
2075 qglDrawBuffer(GL_FRONT_LEFT);
2076 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2077 qglDrawBuffer(GL_FRONT_RIGHT);
2078 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2082 qglDrawBuffer(GL_FRONT);
2083 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2087 // not necessary when rendering to GL_FRONT buffers
2088 //VID_Finish(false);
2089 // however this IS necessary on Windows Vista
2093 void CL_UpdateScreen(void)
2095 float conwidth, conheight;
2097 if (vid_hidden || !scr_refresh.integer)
2100 if (!scr_initialized || !con_initialized)
2101 return; // not initialized yet
2103 conwidth = bound(320, vid_conwidth.value, 2048);
2104 conheight = bound(200, vid_conheight.value, 1536);
2105 if (vid_conwidth.value != conwidth)
2106 Cvar_SetValue("vid_conwidth", conwidth);
2107 if (vid_conheight.value != conheight)
2108 Cvar_SetValue("vid_conheight", conheight);
2111 if (scr_viewsize.value < 30)
2112 Cvar_Set ("viewsize","30");
2113 if (scr_viewsize.value > 120)
2114 Cvar_Set ("viewsize","120");
2116 // bound field of view
2117 if (scr_fov.value < 1)
2118 Cvar_Set ("fov","1");
2119 if (scr_fov.value > 170)
2120 Cvar_Set ("fov","170");
2122 // validate r_textureunits cvar
2123 if (r_textureunits.integer > gl_textureunits)
2124 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
2125 if (r_textureunits.integer < 1)
2126 Cvar_SetValueQuick(&r_textureunits, 1);
2128 // validate gl_combine cvar
2129 if (gl_combine.integer && !gl_combine_extension)
2130 Cvar_SetValueQuick(&gl_combine, 0);
2132 // intermission is always full screen
2133 if (cl.intermission)
2137 if (scr_viewsize.value >= 120)
2138 sb_lines = 0; // no status bar at all
2139 else if (scr_viewsize.value >= 110)
2140 sb_lines = 24; // no inventory
2145 r_view.colormask[0] = 1;
2146 r_view.colormask[1] = 1;
2147 r_view.colormask[2] = 1;
2149 SCR_SetUpToDrawConsole();
2152 qglDrawBuffer(GL_BACK);CHECKGLERROR
2153 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
2154 qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
2155 qglDepthMask(1);CHECKGLERROR
2156 qglColorMask(1,1,1,1);CHECKGLERROR
2157 qglClearColor(0,0,0,0);CHECKGLERROR
2159 r_view.clear = false;
2161 if(scr_stipple.integer)
2163 GLubyte stipple[128];
2164 int i, s, width, parts;
2165 static int frame = 0;
2168 s = scr_stipple.integer;
2170 width = (s & 070) >> 3;
2172 qglEnable(GL_POLYGON_STIPPLE); // 0x0B42
2173 for(i = 0; i < 128; ++i)
2176 stipple[i] = (((line >> width) + frame) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
2178 qglPolygonStipple(stipple);
2181 qglDisable(GL_POLYGON_STIPPLE);
2183 if (vid.stereobuffer || r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
2185 matrix4x4_t originalmatrix = r_view.matrix;
2186 matrix4x4_t offsetmatrix;
2187 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * 0.5f, 0, 0, r_stereo_angle.value * 0.5f, 0, 1);
2188 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
2190 if (r_stereo_sidebyside.integer)
2193 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
2195 r_view.colormask[0] = 1;
2196 r_view.colormask[1] = 0;
2197 r_view.colormask[2] = 0;
2200 if (vid.stereobuffer)
2201 qglDrawBuffer(GL_BACK_RIGHT);
2205 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * -0.5f, 0, 0, r_stereo_angle.value * -0.5f, 0, 1);
2206 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
2208 if (r_stereo_sidebyside.integer)
2211 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
2213 r_view.colormask[0] = 0;
2214 r_view.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
2215 r_view.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
2218 if (vid.stereobuffer)
2219 qglDrawBuffer(GL_BACK_LEFT);
2223 r_view.matrix = originalmatrix;
2233 void CL_Screen_NewMap(void)