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;
61 extern int con_vislines;
63 static void SCR_ScreenShot_f (void);
64 static void R_Envmap_f (void);
67 void R_ClearScreen(void);
70 ===============================================================================
74 ===============================================================================
77 char scr_centerstring[MAX_INPUTLINE];
78 float scr_centertime_start; // for slow victory printing
79 float scr_centertime_off;
88 Called for important messages that should stay in the center of the screen
92 void SCR_CenterPrint(char *str)
94 strlcpy (scr_centerstring, str, sizeof (scr_centerstring));
95 scr_centertime_off = scr_centertime.value;
96 scr_centertime_start = cl.time;
98 // count the number of lines for centering
109 void SCR_DrawCenterString (void)
116 if(cl.intermission == 2) // in finale,
117 if(sb_showscores) // make TAB hide the finale message (sb_showscores overrides finale in sbar.c)
120 // the finale prints the characters one at a time, except if printspeed is an absurdly high value
121 if (cl.intermission && scr_printspeed.value > 0 && scr_printspeed.value < 1000000)
122 remaining = (int)(scr_printspeed.value * (cl.time - scr_centertime_start));
126 scr_erase_center = 0;
127 start = scr_centerstring;
132 if (scr_center_lines <= 4)
133 y = (int)(vid_conheight.integer*0.35);
140 // scan the number of characters on the line, not counting color codes
141 char *newline = strchr(start, '\n');
142 int l = newline ? (newline - start) : (int)strlen(start);
143 int chars = COM_StringLengthNoColors(start, l, NULL);
145 x = (vid_conwidth.integer - chars*8)/2;
150 DrawQ_String(x, y, start, l, 8, 8, 1, 1, 1, 1, 0, &color, false);
159 start = newline + 1; // skip the \n
163 void SCR_CheckDrawCenterString (void)
165 if (scr_center_lines > scr_erase_lines)
166 scr_erase_lines = scr_center_lines;
168 if (cl.time > cl.oldtime)
169 scr_centertime_off -= cl.time - cl.oldtime;
171 // don't draw if this is a normal stats-screen intermission,
172 // only if it is not an intermission, or a finale intermission
173 if (cl.intermission == 1)
175 if (scr_centertime_off <= 0 && !cl.intermission)
177 if (key_dest != key_game)
180 SCR_DrawCenterString ();
183 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])
185 int j, k, x, y, index, offset, height;
186 // draw the bar graph itself
187 // advance the packet counter because it is the latest packet column being
188 // built up and should come last
189 packetcounter = (packetcounter + 1) % NETGRAPH_PACKETS;
190 for (j = 0;j < NETGRAPH_PACKETS;j++)
192 x = graphx + j * barwidth;
193 y = graphy + barheight;
194 index = (packetcounter + j) % NETGRAPH_PACKETS;
195 if (parameters[0][index] == NETGRAPH_LOSTPACKET)
196 DrawQ_Fill(x, y - barheight, barwidth, barheight, 1, 0, 0, 1, 0);
197 else if (parameters[0][index] == NETGRAPH_CHOKEDPACKET)
198 DrawQ_Fill(x, y - min(2, barheight), barwidth, min(2, barheight), 1, 1, 0, 1, 0);
202 for (k = 0;k < numparameters;k++)
204 height = (parameters[k][index] + bardivide - 1) / bardivide;
205 height = min(height, barheight - offset);
208 DrawQ_Fill(x, y - offset, barwidth, height, parametercolors[k][0], parametercolors[k][1], parametercolors[k][2], parametercolors[k][3], 0);
214 const float netgraphcolors[3][4] =
221 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)
224 const int *parameters[3];
226 DrawQ_Fill(graphx , graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
227 DrawQ_Fill(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
228 // draw the bar graphs
230 parameters[0] = conn->incoming_unreliablesize;
231 parameters[1] = conn->incoming_reliablesize;
232 parameters[2] = conn->incoming_acksize;
233 SCR_DrawNetGraph_DrawGraph(graphx, graphy, barwidth, barheight, bardivide, labelincoming, textsize, conn->incoming_packetcounter, numparameters, parameters, netgraphcolors);
234 parameters[0] = conn->outgoing_unreliablesize;
235 parameters[1] = conn->outgoing_reliablesize;
236 parameters[2] = conn->outgoing_acksize;
237 SCR_DrawNetGraph_DrawGraph(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth, barheight, bardivide, labeloutgoing, textsize, conn->outgoing_packetcounter, numparameters, parameters, netgraphcolors);
239 DrawQ_String(graphx , graphy + barheight, labelincoming, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
240 DrawQ_String(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy + barheight, labeloutgoing, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
243 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)
246 const int *parameters[3];
248 DrawQ_Fill(graphx , graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
249 DrawQ_Fill(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth * NETGRAPH_PACKETS, barheight + textsize, 0, 0, 0, 0.5, 0);
250 // draw the bar graphs
252 parameters[0] = conn->outgoing_unreliablesize;
253 parameters[1] = conn->outgoing_reliablesize;
254 parameters[2] = conn->outgoing_acksize;
255 SCR_DrawNetGraph_DrawGraph(graphx , graphy, barwidth, barheight, bardivide, labeloutgoing, textsize, conn->outgoing_packetcounter, numparameters, parameters, netgraphcolors);
256 parameters[0] = conn->incoming_unreliablesize;
257 parameters[1] = conn->incoming_reliablesize;
258 parameters[2] = conn->incoming_acksize;
259 SCR_DrawNetGraph_DrawGraph(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy, barwidth, barheight, bardivide, labelincoming, textsize, conn->incoming_packetcounter, numparameters, parameters, netgraphcolors);
261 DrawQ_String(graphx , graphy + barheight, labeloutgoing, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
262 DrawQ_String(graphx + barwidth * NETGRAPH_PACKETS + separator, graphy + barheight, labelincoming, 0, textsize, textsize, 1, 1, 1, 1, 0, NULL, false);
270 void SCR_DrawNetGraph (void)
272 int i, separator1, separator2, barwidth, barheight, bardivide, netgraph_x, netgraph_y, textsize, index, netgraphsperrow;
274 if (cls.state != ca_connected)
278 if (!shownetgraph.integer)
288 netgraphsperrow = (vid_conwidth.integer + separator2) / (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
289 netgraphsperrow = max(netgraphsperrow, 1);
292 netgraph_x = (vid_conwidth.integer + separator2) - (1 + (index % netgraphsperrow)) * (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
293 netgraph_y = (vid_conheight.integer - 48 + separator2) - (1 + (index / netgraphsperrow)) * (barheight + textsize + separator2);
294 SCR_DrawNetGraph_DrawConnection_Client(cls.netcon, netgraph_x, netgraph_y, barwidth, barheight, bardivide, "incoming", separator1, "outgoing", textsize);
297 if (sv.active && shownetgraph.integer >= 2)
299 for (i = 0;i < svs.maxclients;i++)
301 if (!svs.clients[i].netconnection)
303 netgraph_x = (vid_conwidth.integer + separator2) - (1 + (index % netgraphsperrow)) * (barwidth * NETGRAPH_PACKETS * 2 + separator1 + separator2);
304 netgraph_y = (vid_conheight.integer - 48 + separator2) - (1 + (index / netgraphsperrow)) * (barheight + textsize + separator2);
305 SCR_DrawNetGraph_DrawConnection_Server(svs.clients[i].netconnection, netgraph_x, netgraph_y, barwidth, barheight, bardivide, va("%s", svs.clients[i].name), separator1, "", textsize);
316 void SCR_DrawTurtle (void)
320 if (cls.state != ca_connected)
323 if (!scr_showturtle.integer)
326 if (cl.realframetime < 0.1)
336 DrawQ_Pic (0, 0, Draw_CachePic("gfx/turtle", true), 0, 0, 1, 1, 1, 1, 0);
344 void SCR_DrawNet (void)
346 if (cls.state != ca_connected)
348 if (realtime - cl.last_received_message < 0.3)
350 if (cls.demoplayback)
353 DrawQ_Pic (64, 0, Draw_CachePic("gfx/net", true), 0, 0, 1, 1, 1, 1, 0);
361 void SCR_DrawPause (void)
365 if (cls.state != ca_connected)
368 if (!scr_showpause.integer) // turn off for screenshots
374 pic = Draw_CachePic ("gfx/pause", true);
375 DrawQ_Pic ((vid_conwidth.integer - pic->width)/2, (vid_conheight.integer - pic->height)/2, pic, 0, 0, 1, 1, 1, 1, 0);
383 void SCR_DrawBrand (void)
388 if (!scr_showbrand.value)
391 pic = Draw_CachePic ("gfx/brand", true);
393 switch ((int)scr_showbrand.value)
395 case 1: // bottom left
397 y = vid_conheight.integer - pic->height;
399 case 2: // bottom centre
400 x = (vid_conwidth.integer - pic->width) / 2;
401 y = vid_conheight.integer - pic->height;
403 case 3: // bottom right
404 x = vid_conwidth.integer - pic->width;
405 y = vid_conheight.integer - pic->height;
407 case 4: // centre right
408 x = vid_conwidth.integer - pic->width;
409 y = (vid_conheight.integer - pic->height) / 2;
412 x = vid_conwidth.integer - pic->width;
415 case 6: // top centre
416 x = (vid_conwidth.integer - pic->width) / 2;
423 case 8: // centre left
425 y = (vid_conheight.integer - pic->height) / 2;
431 DrawQ_Pic (x, y, pic, 0, 0, 1, 1, 1, 1, 0);
439 static int SCR_DrawQWDownload(int offset)
445 if (!cls.qw_downloadname[0])
447 cls.qw_downloadspeedrate = 0;
448 cls.qw_downloadspeedtime = realtime;
449 cls.qw_downloadspeedcount = 0;
452 if (realtime >= cls.qw_downloadspeedtime + 1)
454 cls.qw_downloadspeedrate = cls.qw_downloadspeedcount;
455 cls.qw_downloadspeedtime = realtime;
456 cls.qw_downloadspeedcount = 0;
458 if (cls.protocol == PROTOCOL_QUAKEWORLD)
459 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);
461 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);
462 len = (int)strlen(temp);
463 x = (vid_conwidth.integer - len*size) / 2;
464 y = vid_conheight.integer - size - offset;
465 DrawQ_Fill(0, y, vid_conwidth.integer, size, 0, 0, 0, 0.5, 0);
466 DrawQ_String(x, y, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true);
475 static int SCR_DrawCurlDownload(int offset)
482 Curl_downloadinfo_t *downinfo;
486 downinfo = Curl_GetDownloadInfo(&nDownloads, &addinfo);
490 y = vid_conheight.integer - size * nDownloads - offset;
494 len = (int)strlen(addinfo);
495 x = (vid_conwidth.integer - len*size) / 2;
496 DrawQ_Fill(0, y - size, vid_conwidth.integer, size, 1, 1, 1, 0.8, 0);
497 DrawQ_String(x, y - size, addinfo, len, size, size, 0, 0, 0, 1, 0, NULL, true);
500 for(i = 0; i != nDownloads; ++i)
502 if(downinfo[i].queued)
503 dpsnprintf(temp, sizeof(temp), "Still in queue: %s\n", downinfo[i].filename);
504 else if(downinfo[i].progress <= 0)
505 dpsnprintf(temp, sizeof(temp), "Downloading %s ... ???.?%% @ %.1f KiB/s\n", downinfo[i].filename, downinfo[i].speed / 1024.0);
507 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);
508 len = (int)strlen(temp);
509 x = (vid_conwidth.integer - len*size) / 2;
510 DrawQ_Fill(0, y + i * size, vid_conwidth.integer, size, 0, 0, 0, 0.8, 0);
511 DrawQ_String(x, y + i * size, temp, len, size, size, 1, 1, 1, 1, 0, NULL, true);
516 return 8 * (nDownloads + (addinfo ? 1 : 0));
524 static void SCR_DrawDownload()
527 offset += SCR_DrawQWDownload(offset);
528 offset += SCR_DrawCurlDownload(offset);
531 //=============================================================================
535 SCR_SetUpToDrawConsole
538 void SCR_SetUpToDrawConsole (void)
540 // lines of console to display
542 static int framecounter = 0;
546 if (scr_menuforcewhiledisconnected.integer && key_dest == key_game && cls.state == ca_disconnected)
548 if (framecounter >= 2)
556 if (scr_conforcewhiledisconnected.integer && key_dest == key_game && cls.signon != SIGNONS)
557 key_consoleactive |= KEY_CONSOLEACTIVE_FORCED;
559 key_consoleactive &= ~KEY_CONSOLEACTIVE_FORCED;
561 // decide on the height of the console
562 if (key_consoleactive & KEY_CONSOLEACTIVE_USER)
563 conlines = vid_conheight.integer/2; // half screen
565 conlines = 0; // none visible
567 scr_con_current = conlines;
575 void SCR_DrawConsole (void)
577 if (key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
580 Con_DrawConsole (vid_conheight.integer);
582 else if (scr_con_current)
583 Con_DrawConsole ((int)scr_con_current);
587 if ((key_dest == key_game || key_dest == key_message) && !r_letterbox.value)
588 Con_DrawNotify (); // only draw notify in game
594 SCR_BeginLoadingPlaque
598 void SCR_BeginLoadingPlaque (void)
600 // save console log up to this point to log_file if it was set by configs
604 SCR_UpdateLoadingScreen(false);
607 //=============================================================================
609 char r_speeds_string[1024];
610 int speedstringcount, r_timereport_active;
611 double r_timereport_temp = 0, r_timereport_current = 0, r_timereport_start = 0;
613 void R_TimeReport(char *desc)
619 if (r_speeds.integer < 2 || !r_timereport_active)
623 qglFinish();CHECKGLERROR
624 r_timereport_temp = r_timereport_current;
625 r_timereport_current = Sys_DoubleTime();
626 t = (int) ((r_timereport_current - r_timereport_temp) * 1000000.0 + 0.5);
628 dpsnprintf(tempbuf, sizeof(tempbuf), "%8i %-11s", t, desc);
629 length = (int)strlen(tempbuf);
630 if (speedstringcount + length > (vid_conwidth.integer / 8))
632 strlcat(r_speeds_string, "\n", sizeof(r_speeds_string));
633 speedstringcount = 0;
635 strlcat(r_speeds_string, tempbuf, sizeof(r_speeds_string));
636 speedstringcount += length;
639 void R_TimeReport_Frame(void)
644 if (r_speeds_string[0])
646 if (r_timereport_active)
648 r_timereport_current = r_timereport_start;
649 R_TimeReport("total");
652 if (r_speeds_string[strlen(r_speeds_string)-1] == '\n')
653 r_speeds_string[strlen(r_speeds_string)-1] = 0;
655 for (i = 0;r_speeds_string[i];i++)
656 if (r_speeds_string[i] == '\n')
658 y = vid_conheight.integer - sb_lines - lines * 8;
660 DrawQ_Fill(0, y, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
661 while (r_speeds_string[i])
664 while (r_speeds_string[i] && r_speeds_string[i] != '\n')
667 DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0, NULL, true);
668 if (r_speeds_string[i] == '\n')
672 r_speeds_string[0] = 0;
673 r_timereport_active = false;
675 if (r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected)
677 speedstringcount = 0;
678 r_speeds_string[0] = 0;
679 r_timereport_active = false;
680 // put the location name in the r_speeds display as it greatly helps
681 // when creating loc files
682 loc = CL_Locs_FindNearest(cl.movement_origin);
684 sprintf(r_speeds_string + strlen(r_speeds_string), "Location: %s\n", loc->name);
685 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]);
686 sprintf(r_speeds_string + strlen(r_speeds_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);
687 sprintf(r_speeds_string + strlen(r_speeds_string), "%5ileafs%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);
688 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);
689 if (r_refdef.stats.bloom)
690 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);
692 sprintf(r_speeds_string + strlen(r_speeds_string), "rendered%6i meshes%8i triangles\n", r_refdef.stats.meshes, r_refdef.stats.meshes_elements / 3);
694 memset(&r_refdef.stats, 0, sizeof(r_refdef.stats));
696 if (r_speeds.integer >= 2)
698 r_timereport_active = true;
699 r_timereport_start = r_timereport_current = Sys_DoubleTime();
711 void SCR_SizeUp_f (void)
713 Cvar_SetValue ("viewsize",scr_viewsize.value+10);
724 void SCR_SizeDown_f (void)
726 Cvar_SetValue ("viewsize",scr_viewsize.value-10);
729 void SCR_CaptureVideo_EndVideo(void);
730 void CL_Screen_Shutdown(void)
732 SCR_CaptureVideo_EndVideo();
735 void CL_Screen_Init(void)
737 Cvar_RegisterVariable (&scr_fov);
738 Cvar_RegisterVariable (&scr_viewsize);
739 Cvar_RegisterVariable (&scr_conalpha);
740 Cvar_RegisterVariable (&scr_conbrightness);
741 Cvar_RegisterVariable (&scr_conforcewhiledisconnected);
742 Cvar_RegisterVariable (&scr_menuforcewhiledisconnected);
743 Cvar_RegisterVariable (&scr_showram);
744 Cvar_RegisterVariable (&scr_showturtle);
745 Cvar_RegisterVariable (&scr_showpause);
746 Cvar_RegisterVariable (&scr_showbrand);
747 Cvar_RegisterVariable (&scr_centertime);
748 Cvar_RegisterVariable (&scr_printspeed);
749 Cvar_RegisterVariable (&vid_conwidth);
750 Cvar_RegisterVariable (&vid_conheight);
751 Cvar_RegisterVariable (&vid_pixelheight);
752 Cvar_RegisterVariable (&scr_screenshot_jpeg);
753 Cvar_RegisterVariable (&scr_screenshot_jpeg_quality);
754 Cvar_RegisterVariable (&scr_screenshot_gammaboost);
755 Cvar_RegisterVariable (&cl_capturevideo);
756 Cvar_RegisterVariable (&cl_capturevideo_width);
757 Cvar_RegisterVariable (&cl_capturevideo_height);
758 Cvar_RegisterVariable (&cl_capturevideo_realtime);
759 Cvar_RegisterVariable (&cl_capturevideo_fps);
760 Cvar_RegisterVariable (&cl_capturevideo_number);
761 Cvar_RegisterVariable (&r_letterbox);
762 Cvar_RegisterVariable(&r_stereo_separation);
763 Cvar_RegisterVariable(&r_stereo_sidebyside);
764 Cvar_RegisterVariable(&r_stereo_redblue);
765 Cvar_RegisterVariable(&r_stereo_redcyan);
766 Cvar_RegisterVariable(&r_stereo_redgreen);
767 Cvar_RegisterVariable(&r_stereo_angle);
768 Cvar_RegisterVariable(&scr_zoomwindow);
769 Cvar_RegisterVariable(&scr_zoomwindow_viewsizex);
770 Cvar_RegisterVariable(&scr_zoomwindow_viewsizey);
771 Cvar_RegisterVariable(&scr_zoomwindow_fov);
772 Cvar_RegisterVariable(&scr_stipple);
773 Cvar_RegisterVariable(&scr_refresh);
774 Cvar_RegisterVariable(&shownetgraph);
776 Cmd_AddCommand ("sizeup",SCR_SizeUp_f, "increase view size (increases viewsize cvar)");
777 Cmd_AddCommand ("sizedown",SCR_SizeDown_f, "decrease view size (decreases viewsize cvar)");
778 Cmd_AddCommand ("screenshot",SCR_ScreenShot_f, "takes a screenshot of the next rendered frame");
779 Cmd_AddCommand ("envmap", R_Envmap_f, "render a cubemap (skybox) of the current scene");
781 scr_initialized = true;
789 void SCR_ScreenShot_f (void)
791 static int shotnumber;
792 static char oldname[MAX_QPATH];
793 char base[MAX_QPATH];
794 char filename[MAX_QPATH];
795 unsigned char *buffer1;
796 unsigned char *buffer2;
797 unsigned char *buffer3;
798 qboolean jpeg = (scr_screenshot_jpeg.integer != 0);
800 sprintf (base, "screenshots/%s", scr_screenshot_name.string);
802 if (strcmp (oldname, scr_screenshot_name.string))
804 sprintf(oldname, "%s", scr_screenshot_name.string);
808 // find a file name to save it to
809 for (;shotnumber < 1000000;shotnumber++)
810 if (!FS_SysFileExists(va("%s/%s%06d.tga", fs_gamedir, base, shotnumber)) && !FS_SysFileExists(va("%s/%s%06d.jpg", fs_gamedir, base, shotnumber)))
812 if (shotnumber >= 1000000)
814 Con_Print("SCR_ScreenShot_f: Couldn't create the image file\n");
818 sprintf(filename, "%s%06d.%s", base, shotnumber, jpeg ? "jpg" : "tga");
820 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
821 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
822 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3 + 18);
824 if (SCR_ScreenShot (filename, buffer1, buffer2, buffer3, 0, 0, vid.width, vid.height, false, false, false, jpeg, true))
825 Con_Printf("Wrote %s\n", filename);
827 Con_Printf("unable to write %s\n", filename);
836 static void SCR_CaptureVideo_RIFF_Start(void)
838 memset(&cls.capturevideo.riffbuffer, 0, sizeof(sizebuf_t));
839 cls.capturevideo.riffbuffer.maxsize = sizeof(cls.capturevideo.riffbufferdata);
840 cls.capturevideo.riffbuffer.data = cls.capturevideo.riffbufferdata;
843 static void SCR_CaptureVideo_RIFF_Flush(void)
845 if (cls.capturevideo.riffbuffer.cursize > 0)
847 if (!FS_Write(cls.capturevideo.videofile, cls.capturevideo.riffbuffer.data, cls.capturevideo.riffbuffer.cursize))
848 cls.capturevideo.error = true;
849 cls.capturevideo.riffbuffer.cursize = 0;
850 cls.capturevideo.riffbuffer.overflowed = false;
854 static void SCR_CaptureVideo_RIFF_WriteBytes(const unsigned char *data, size_t size)
856 SCR_CaptureVideo_RIFF_Flush();
857 if (!FS_Write(cls.capturevideo.videofile, data, size))
858 cls.capturevideo.error = true;
861 static void SCR_CaptureVideo_RIFF_Write32(int n)
863 if (cls.capturevideo.riffbuffer.cursize + 4 > cls.capturevideo.riffbuffer.maxsize)
864 SCR_CaptureVideo_RIFF_Flush();
865 MSG_WriteLong(&cls.capturevideo.riffbuffer, n);
868 static void SCR_CaptureVideo_RIFF_Write16(int n)
870 if (cls.capturevideo.riffbuffer.cursize + 2 > cls.capturevideo.riffbuffer.maxsize)
871 SCR_CaptureVideo_RIFF_Flush();
872 MSG_WriteShort(&cls.capturevideo.riffbuffer, n);
875 static void SCR_CaptureVideo_RIFF_WriteFourCC(const char *chunkfourcc)
877 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(chunkfourcc) > cls.capturevideo.riffbuffer.maxsize)
878 SCR_CaptureVideo_RIFF_Flush();
879 MSG_WriteUnterminatedString(&cls.capturevideo.riffbuffer, chunkfourcc);
882 static void SCR_CaptureVideo_RIFF_WriteTerminatedString(const char *string)
884 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(string) > cls.capturevideo.riffbuffer.maxsize)
885 SCR_CaptureVideo_RIFF_Flush();
886 MSG_WriteString(&cls.capturevideo.riffbuffer, string);
889 static fs_offset_t SCR_CaptureVideo_RIFF_GetPosition(void)
891 SCR_CaptureVideo_RIFF_Flush();
892 return FS_Tell(cls.capturevideo.videofile);
895 static void SCR_CaptureVideo_RIFF_Push(const char *chunkfourcc, const char *listtypefourcc)
897 SCR_CaptureVideo_RIFF_WriteFourCC(chunkfourcc);
898 SCR_CaptureVideo_RIFF_Write32(0);
899 SCR_CaptureVideo_RIFF_Flush();
900 cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel++] = SCR_CaptureVideo_RIFF_GetPosition();
902 SCR_CaptureVideo_RIFF_WriteFourCC(listtypefourcc);
905 static void SCR_CaptureVideo_RIFF_Pop(void)
909 unsigned char sizebytes[4];
910 // write out the chunk size and then return to the current file position
911 cls.capturevideo.riffstacklevel--;
912 offset = SCR_CaptureVideo_RIFF_GetPosition();
913 x = (int)(offset - (cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel]));
914 sizebytes[0] = (x) & 0xff;sizebytes[1] = (x >> 8) & 0xff;sizebytes[2] = (x >> 16) & 0xff;sizebytes[3] = (x >> 24) & 0xff;
915 FS_Seek(cls.capturevideo.videofile, -(x + 4), SEEK_END);
916 FS_Write(cls.capturevideo.videofile, sizebytes, 4);
917 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
921 FS_Write(cls.capturevideo.videofile, &c, 1);
925 static void GrowBuf(sizebuf_t *buf, int extralen)
927 if(buf->cursize + extralen > buf->maxsize)
929 int oldsize = buf->maxsize;
930 unsigned char *olddata;
932 buf->maxsize = max(buf->maxsize * 2, 4096);
933 buf->data = Mem_Alloc(tempmempool, buf->maxsize);
936 memcpy(buf->data, olddata, oldsize);
942 static void SCR_CaptureVideo_RIFF_IndexEntry(const char *chunkfourcc, int chunksize, int flags)
944 if (cls.capturevideo.riffstacklevel != 2)
945 Sys_Error("SCR_Capturevideo_RIFF_IndexEntry: RIFF stack level is %i (should be 2)\n", cls.capturevideo.riffstacklevel);
946 GrowBuf(&cls.capturevideo.riffindexbuffer, 16);
947 SCR_CaptureVideo_RIFF_Flush();
948 MSG_WriteUnterminatedString(&cls.capturevideo.riffindexbuffer, chunkfourcc);
949 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, flags);
950 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, (int)FS_Tell(cls.capturevideo.videofile) - cls.capturevideo.riffstackstartoffset[1]);
951 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, chunksize);
954 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)
958 fs_offset_t ix = SCR_CaptureVideo_RIFF_GetPosition();
961 if(*masteridx_count >= AVI_MASTER_INDEX_SIZE)
964 nMatching = 0; // go through index and enumerate them
965 for(i = 0; i < cls.capturevideo.riffindexbuffer.cursize; i += 16)
966 if(!memcmp(cls.capturevideo.riffindexbuffer.data + i, dwChunkId, 4))
969 SCR_CaptureVideo_RIFF_Push(fcc, NULL);
970 SCR_CaptureVideo_RIFF_Write16(2); // wLongsPerEntry
971 SCR_CaptureVideo_RIFF_Write16(0x0100); // bIndexType=1, bIndexSubType=0
972 SCR_CaptureVideo_RIFF_Write32(nMatching); // nEntriesInUse
973 SCR_CaptureVideo_RIFF_WriteFourCC(dwChunkId); // dwChunkId
974 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.videofile_ix_movistart & (fs_offset_t) 0xFFFFFFFFu);
975 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) cls.capturevideo.videofile_ix_movistart) >> 32);
976 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved
978 for(i = 0; i < cls.capturevideo.riffindexbuffer.cursize; i += 16)
979 if(!memcmp(cls.capturevideo.riffindexbuffer.data + i, dwChunkId, 4))
981 unsigned int *p = (unsigned int *) (cls.capturevideo.riffindexbuffer.data + i);
982 unsigned int flags = p[1];
983 unsigned int rpos = p[2];
984 unsigned int size = p[3];
986 if(!(flags & 0x10)) // no keyframe?
988 SCR_CaptureVideo_RIFF_Write32(rpos + 8);
989 SCR_CaptureVideo_RIFF_Write32(size);
992 SCR_CaptureVideo_RIFF_Pop();
993 pos = SCR_CaptureVideo_RIFF_GetPosition();
994 SCR_CaptureVideo_RIFF_Flush();
996 FS_Seek(cls.capturevideo.videofile, masteridx_start + 16 * *masteridx_count, SEEK_SET);
997 SCR_CaptureVideo_RIFF_Write32(ix & (fs_offset_t) 0xFFFFFFFFu);
998 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) ix) >> 32);
999 SCR_CaptureVideo_RIFF_Write32(pos - ix);
1000 SCR_CaptureVideo_RIFF_Write32(nMatching);
1001 SCR_CaptureVideo_RIFF_Flush();
1003 FS_Seek(cls.capturevideo.videofile, masteridx_counter, SEEK_SET);
1004 SCR_CaptureVideo_RIFF_Write32(++*masteridx_count);
1005 SCR_CaptureVideo_RIFF_Flush();
1007 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
1010 static void SCR_CaptureVideo_RIFF_Finish(qboolean final)
1012 // close the "movi" list
1013 SCR_CaptureVideo_RIFF_Pop();
1014 if(cls.capturevideo.videofile_ix_master_video_inuse_offset)
1015 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);
1016 if(cls.capturevideo.videofile_ix_master_audio_inuse_offset)
1017 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);
1018 // write the idx1 chunk that we've been building while saving the frames (for old style players)
1019 if(final && cls.capturevideo.videofile_firstchunkframes_offset)
1020 // TODO replace index creating by OpenDML ix##/##ix/indx chunk so it works for more than one AVI part too
1022 SCR_CaptureVideo_RIFF_Push("idx1", NULL);
1023 SCR_CaptureVideo_RIFF_WriteBytes(cls.capturevideo.riffindexbuffer.data, cls.capturevideo.riffindexbuffer.cursize);
1024 SCR_CaptureVideo_RIFF_Pop();
1026 cls.capturevideo.riffindexbuffer.cursize = 0;
1027 // pop the RIFF chunk itself
1028 while (cls.capturevideo.riffstacklevel > 0)
1029 SCR_CaptureVideo_RIFF_Pop();
1030 SCR_CaptureVideo_RIFF_Flush();
1031 if(cls.capturevideo.videofile_firstchunkframes_offset)
1033 Con_DPrintf("Finishing first chunk (%d frames)\n", cls.capturevideo.frame);
1034 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_firstchunkframes_offset, SEEK_SET);
1035 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1036 SCR_CaptureVideo_RIFF_Flush();
1037 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
1038 cls.capturevideo.videofile_firstchunkframes_offset = 0;
1041 Con_DPrintf("Finishing another chunk (%d frames)\n", cls.capturevideo.frame);
1044 static void SCR_CaptureVideo_RIFF_OverflowCheck(int framesize)
1046 fs_offset_t cursize, curfilesize;
1047 if (cls.capturevideo.riffstacklevel != 2)
1048 Sys_Error("SCR_CaptureVideo_RIFF_OverflowCheck: chunk stack leakage!\n");
1049 // check where we are in the file
1050 SCR_CaptureVideo_RIFF_Flush();
1051 cursize = SCR_CaptureVideo_RIFF_GetPosition() - cls.capturevideo.riffstackstartoffset[0];
1052 curfilesize = SCR_CaptureVideo_RIFF_GetPosition();
1054 // if this would overflow the windows limit of 1GB per RIFF chunk, we need
1055 // to close the current RIFF chunk and open another for future frames
1056 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
1058 SCR_CaptureVideo_RIFF_Finish(false);
1059 // begin a new 1GB extended section of the AVI
1060 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX");
1061 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1062 cls.capturevideo.videofile_ix_movistart = cls.capturevideo.riffstackstartoffset[1];
1066 static void FindFraction(double val, int *num, int *denom, int denomMax)
1071 bestdiff = fabs(val);
1075 for(i = 1; i <= denomMax; ++i)
1077 int inum = floor(0.5 + val * i);
1078 double diff = fabs(val - inum / (double)i);
1088 void SCR_CaptureVideo_BeginVideo(void)
1090 double gamma, g, aspect;
1091 int width = cl_capturevideo_width.integer, height = cl_capturevideo_height.integer;
1094 if (cls.capturevideo.active)
1096 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1097 // soundrate is figured out on the first SoundFrame
1099 if(width == 0 && height != 0)
1100 width = (int) (height * (double)vid.width / ((double)vid.height * vid_pixelheight.value)); // keep aspect
1101 if(width != 0 && height == 0)
1102 height = (int) (width * ((double)vid.height * vid_pixelheight.value) / (double)vid.width); // keep aspect
1104 if(width < 2 || width > vid.width) // can't scale up
1106 if(height < 2 || height > vid.height) // can't scale up
1107 height = vid.height;
1109 aspect = vid.width / (vid.height * vid_pixelheight.value);
1111 // ensure it's all even; if not, scale down a little
1117 cls.capturevideo.width = width;
1118 cls.capturevideo.height = height;
1119 cls.capturevideo.active = true;
1120 cls.capturevideo.starttime = realtime;
1121 cls.capturevideo.framerate = bound(1, cl_capturevideo_fps.value, 1000);
1122 cls.capturevideo.soundrate = S_GetSoundRate();
1123 cls.capturevideo.frame = 0;
1124 cls.capturevideo.soundsampleframe = 0;
1125 cls.capturevideo.realtime = cl_capturevideo_realtime.integer != 0;
1126 cls.capturevideo.screenbuffer = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 4);
1127 cls.capturevideo.outbuffer = (unsigned char *)Mem_Alloc(tempmempool, width * height * (4+4) + 18);
1128 gamma = 1.0/scr_screenshot_gammaboost.value;
1129 dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/dpvideo%03i", cl_capturevideo_number.integer);
1130 Cvar_SetValueQuick(&cl_capturevideo_number, cl_capturevideo_number.integer + 1);
1133 for (i = 0;i < 256;i++)
1135 unsigned char j = (unsigned char)bound(0, 255*pow(i/255.0, gamma), 255);
1136 cls.capturevideo.rgbgammatable[0][i] = j;
1137 cls.capturevideo.rgbgammatable[1][i] = j;
1138 cls.capturevideo.rgbgammatable[2][i] = j;
1142 R = Y + 1.4075 * (Cr - 128);
1143 G = Y + -0.3455 * (Cb - 128) + -0.7169 * (Cr - 128);
1144 B = Y + 1.7790 * (Cb - 128);
1145 Y = R * .299 + G * .587 + B * .114;
1146 Cb = R * -.169 + G * -.332 + B * .500 + 128.;
1147 Cr = R * .500 + G * -.419 + B * -.0813 + 128.;
1149 for (i = 0;i < 256;i++)
1151 g = 255*pow(i/255.0, gamma);
1152 // Y weights from RGB
1153 cls.capturevideo.rgbtoyuvscaletable[0][0][i] = (short)(g * 0.299);
1154 cls.capturevideo.rgbtoyuvscaletable[0][1][i] = (short)(g * 0.587);
1155 cls.capturevideo.rgbtoyuvscaletable[0][2][i] = (short)(g * 0.114);
1156 // Cb weights from RGB
1157 cls.capturevideo.rgbtoyuvscaletable[1][0][i] = (short)(g * -0.169);
1158 cls.capturevideo.rgbtoyuvscaletable[1][1][i] = (short)(g * -0.332);
1159 cls.capturevideo.rgbtoyuvscaletable[1][2][i] = (short)(g * 0.500);
1160 // Cr weights from RGB
1161 cls.capturevideo.rgbtoyuvscaletable[2][0][i] = (short)(g * 0.500);
1162 cls.capturevideo.rgbtoyuvscaletable[2][1][i] = (short)(g * -0.419);
1163 cls.capturevideo.rgbtoyuvscaletable[2][2][i] = (short)(g * -0.0813);
1164 // range reduction of YCbCr to valid signal range
1165 cls.capturevideo.yuvnormalizetable[0][i] = 16 + i * (236-16) / 256;
1166 cls.capturevideo.yuvnormalizetable[1][i] = 16 + i * (240-16) / 256;
1167 cls.capturevideo.yuvnormalizetable[2][i] = 16 + i * (240-16) / 256;
1170 //if (cl_capturevideo_)
1175 cls.capturevideo.format = CAPTUREVIDEOFORMAT_AVI_I420;
1176 cls.capturevideo.videofile = FS_Open (va("%s.avi", cls.capturevideo.basename), "wb", false, true);
1177 SCR_CaptureVideo_RIFF_Start();
1178 // 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)
1179 SCR_CaptureVideo_RIFF_Push("RIFF", "AVI ");
1181 SCR_CaptureVideo_RIFF_Push("LIST", "hdrl");
1182 SCR_CaptureVideo_RIFF_Push("avih", NULL);
1183 SCR_CaptureVideo_RIFF_Write32((int)(1000000.0 / cls.capturevideo.framerate)); // microseconds per frame
1184 SCR_CaptureVideo_RIFF_Write32(0); // max bytes per second
1185 SCR_CaptureVideo_RIFF_Write32(0); // padding granularity
1186 SCR_CaptureVideo_RIFF_Write32(0x910); // flags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
1187 cls.capturevideo.videofile_firstchunkframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
1188 SCR_CaptureVideo_RIFF_Write32(0); // total frames
1189 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1190 if (cls.capturevideo.soundrate)
1191 SCR_CaptureVideo_RIFF_Write32(2); // number of streams
1193 SCR_CaptureVideo_RIFF_Write32(1); // number of streams
1194 SCR_CaptureVideo_RIFF_Write32(0); // suggested buffer size
1195 SCR_CaptureVideo_RIFF_Write32(width); // width
1196 SCR_CaptureVideo_RIFF_Write32(height); // height
1197 SCR_CaptureVideo_RIFF_Write32(0); // reserved[0]
1198 SCR_CaptureVideo_RIFF_Write32(0); // reserved[1]
1199 SCR_CaptureVideo_RIFF_Write32(0); // reserved[2]
1200 SCR_CaptureVideo_RIFF_Write32(0); // reserved[3]
1201 SCR_CaptureVideo_RIFF_Pop();
1202 // video stream info
1203 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
1204 SCR_CaptureVideo_RIFF_Push("strh", "vids");
1205 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // stream fourcc (I420 colorspace, uncompressed)
1206 SCR_CaptureVideo_RIFF_Write32(0); // flags
1207 SCR_CaptureVideo_RIFF_Write16(0); // priority
1208 SCR_CaptureVideo_RIFF_Write16(0); // language
1209 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1210 // find an ideal divisor for the framerate
1211 FindFraction(cls.capturevideo.framerate, &n, &d, 1000);
1212 SCR_CaptureVideo_RIFF_Write32(d); // samples/second divisor
1213 SCR_CaptureVideo_RIFF_Write32(n); // samples/second multiplied by divisor
1214 SCR_CaptureVideo_RIFF_Write32(0); // start
1215 cls.capturevideo.videofile_totalframes_offset1 = SCR_CaptureVideo_RIFF_GetPosition();
1216 SCR_CaptureVideo_RIFF_Write32(0); // length
1217 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // suggested buffer size
1218 SCR_CaptureVideo_RIFF_Write32(0); // quality
1219 SCR_CaptureVideo_RIFF_Write32(0); // sample size
1220 SCR_CaptureVideo_RIFF_Write16(0); // frame left
1221 SCR_CaptureVideo_RIFF_Write16(0); // frame top
1222 SCR_CaptureVideo_RIFF_Write16(width); // frame right
1223 SCR_CaptureVideo_RIFF_Write16(height); // frame bottom
1224 SCR_CaptureVideo_RIFF_Pop();
1225 // video stream format
1226 SCR_CaptureVideo_RIFF_Push("strf", NULL);
1227 SCR_CaptureVideo_RIFF_Write32(40); // BITMAPINFO struct size
1228 SCR_CaptureVideo_RIFF_Write32(width); // width
1229 SCR_CaptureVideo_RIFF_Write32(height); // height
1230 SCR_CaptureVideo_RIFF_Write16(3); // planes
1231 SCR_CaptureVideo_RIFF_Write16(12); // bitcount
1232 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // compression
1233 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // size of image
1234 SCR_CaptureVideo_RIFF_Write32(0); // x pixels per meter
1235 SCR_CaptureVideo_RIFF_Write32(0); // y pixels per meter
1236 SCR_CaptureVideo_RIFF_Write32(0); // color used
1237 SCR_CaptureVideo_RIFF_Write32(0); // color important
1238 SCR_CaptureVideo_RIFF_Pop();
1240 SCR_CaptureVideo_RIFF_Push("indx", NULL);
1241 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
1242 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
1243 cls.capturevideo.videofile_ix_master_video_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
1244 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
1245 SCR_CaptureVideo_RIFF_WriteFourCC("00dc"); // dwChunkId
1246 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
1247 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
1248 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
1249 cls.capturevideo.videofile_ix_master_video_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
1250 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
1251 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
1252 SCR_CaptureVideo_RIFF_Pop();
1253 // extended format (aspect!)
1254 SCR_CaptureVideo_RIFF_Push("vprp", NULL);
1255 SCR_CaptureVideo_RIFF_Write32(0); // VideoFormatToken
1256 SCR_CaptureVideo_RIFF_Write32(0); // VideoStandard
1257 SCR_CaptureVideo_RIFF_Write32((int)cls.capturevideo.framerate); // dwVerticalRefreshRate (bogus)
1258 SCR_CaptureVideo_RIFF_Write32(width); // dwHTotalInT
1259 SCR_CaptureVideo_RIFF_Write32(height); // dwVTotalInLines
1260 FindFraction(aspect, &n, &d, 1000);
1261 SCR_CaptureVideo_RIFF_Write32((n << 16) | d); // dwFrameAspectRatio // TODO a word
1262 SCR_CaptureVideo_RIFF_Write32(width); // dwFrameWidthInPixels
1263 SCR_CaptureVideo_RIFF_Write32(height); // dwFrameHeightInLines
1264 SCR_CaptureVideo_RIFF_Write32(1); // nFieldPerFrame
1265 SCR_CaptureVideo_RIFF_Write32(width); // CompressedBMWidth
1266 SCR_CaptureVideo_RIFF_Write32(height); // CompressedBMHeight
1267 SCR_CaptureVideo_RIFF_Write32(width); // ValidBMHeight
1268 SCR_CaptureVideo_RIFF_Write32(height); // ValidBMWidth
1269 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffset
1270 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYOffset
1271 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffsetInT
1272 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYValidStartLine
1273 SCR_CaptureVideo_RIFF_Pop();
1274 SCR_CaptureVideo_RIFF_Pop();
1275 if (cls.capturevideo.soundrate)
1277 // audio stream info
1278 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
1279 SCR_CaptureVideo_RIFF_Push("strh", "auds");
1280 SCR_CaptureVideo_RIFF_Write32(1); // stream fourcc (PCM audio, uncompressed)
1281 SCR_CaptureVideo_RIFF_Write32(0); // flags
1282 SCR_CaptureVideo_RIFF_Write16(0); // priority
1283 SCR_CaptureVideo_RIFF_Write16(0); // language
1284 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
1285 SCR_CaptureVideo_RIFF_Write32(1); // samples/second divisor
1286 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.soundrate)); // samples/second multiplied by divisor
1287 SCR_CaptureVideo_RIFF_Write32(0); // start
1288 cls.capturevideo.videofile_totalsampleframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
1289 SCR_CaptureVideo_RIFF_Write32(0); // length
1290 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 2); // suggested buffer size (this is a half second)
1291 SCR_CaptureVideo_RIFF_Write32(0); // quality
1292 SCR_CaptureVideo_RIFF_Write32(4); // sample size
1293 SCR_CaptureVideo_RIFF_Write16(0); // frame left
1294 SCR_CaptureVideo_RIFF_Write16(0); // frame top
1295 SCR_CaptureVideo_RIFF_Write16(0); // frame right
1296 SCR_CaptureVideo_RIFF_Write16(0); // frame bottom
1297 SCR_CaptureVideo_RIFF_Pop();
1298 // audio stream format
1299 SCR_CaptureVideo_RIFF_Push("strf", NULL);
1300 SCR_CaptureVideo_RIFF_Write16(1); // format (uncompressed PCM?)
1301 SCR_CaptureVideo_RIFF_Write16(2); // channels (stereo)
1302 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate); // sampleframes per second
1303 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 4); // average bytes per second
1304 SCR_CaptureVideo_RIFF_Write16(4); // block align
1305 SCR_CaptureVideo_RIFF_Write16(16); // bits per sample
1306 SCR_CaptureVideo_RIFF_Write16(0); // size
1307 SCR_CaptureVideo_RIFF_Pop();
1309 SCR_CaptureVideo_RIFF_Push("indx", NULL);
1310 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
1311 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
1312 cls.capturevideo.videofile_ix_master_audio_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
1313 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
1314 SCR_CaptureVideo_RIFF_WriteFourCC("01wb"); // dwChunkId
1315 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
1316 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
1317 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
1318 cls.capturevideo.videofile_ix_master_audio_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
1319 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
1320 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
1321 SCR_CaptureVideo_RIFF_Pop();
1322 SCR_CaptureVideo_RIFF_Pop();
1325 cls.capturevideo.videofile_ix_master_audio_inuse = cls.capturevideo.videofile_ix_master_video_inuse = 0;
1327 // extended header (for total #frames)
1328 SCR_CaptureVideo_RIFF_Push("LIST", "odml");
1329 SCR_CaptureVideo_RIFF_Push("dmlh", NULL);
1330 cls.capturevideo.videofile_totalframes_offset2 = SCR_CaptureVideo_RIFF_GetPosition();
1331 SCR_CaptureVideo_RIFF_Write32(0);
1332 SCR_CaptureVideo_RIFF_Pop();
1333 SCR_CaptureVideo_RIFF_Pop();
1335 // close the AVI header list
1336 SCR_CaptureVideo_RIFF_Pop();
1337 // software that produced this AVI video file
1338 SCR_CaptureVideo_RIFF_Push("LIST", "INFO");
1339 SCR_CaptureVideo_RIFF_Push("ISFT", NULL);
1340 SCR_CaptureVideo_RIFF_WriteTerminatedString(engineversion);
1341 SCR_CaptureVideo_RIFF_Pop();
1342 // enable this junk filler if you like the LIST movi to always begin at 4KB in the file (why?)
1344 SCR_CaptureVideo_RIFF_Push("JUNK", NULL);
1345 x = 4096 - SCR_CaptureVideo_RIFF_GetPosition();
1348 const char *junkfiller = "[ DarkPlaces junk data ]";
1349 int i = min(x, (int)strlen(junkfiller));
1350 SCR_CaptureVideo_RIFF_WriteBytes((const unsigned char *)junkfiller, i);
1353 SCR_CaptureVideo_RIFF_Pop();
1355 SCR_CaptureVideo_RIFF_Pop();
1356 // begin the actual video section now
1357 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1358 cls.capturevideo.videofile_ix_movistart = cls.capturevideo.riffstackstartoffset[1];
1359 // we're done with the headers now...
1360 SCR_CaptureVideo_RIFF_Flush();
1361 if (cls.capturevideo.riffstacklevel != 2)
1362 Sys_Error("SCR_CaptureVideo_BeginVideo: broken AVI writing code (stack level is %i (should be 2) at end of headers)\n", cls.capturevideo.riffstacklevel);
1365 switch(cls.capturevideo.format)
1367 case CAPTUREVIDEOFORMAT_AVI_I420:
1374 void SCR_CaptureVideo_EndVideo(void)
1376 if (!cls.capturevideo.active)
1378 cls.capturevideo.active = false;
1379 if (cls.capturevideo.videofile)
1381 switch(cls.capturevideo.format)
1383 case CAPTUREVIDEOFORMAT_AVI_I420:
1384 // close any open chunks
1385 SCR_CaptureVideo_RIFF_Finish(true);
1386 // go back and fix the video frames and audio samples fields
1387 Con_DPrintf("Finishing capture (%d frames, %d audio frames)\n", cls.capturevideo.frame, cls.capturevideo.soundsampleframe);
1388 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset1, SEEK_SET);
1389 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1390 SCR_CaptureVideo_RIFF_Flush();
1391 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset2, SEEK_SET);
1392 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1393 SCR_CaptureVideo_RIFF_Flush();
1394 if (cls.capturevideo.soundrate)
1396 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalsampleframes_offset, SEEK_SET);
1397 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundsampleframe);
1398 SCR_CaptureVideo_RIFF_Flush();
1404 FS_Close(cls.capturevideo.videofile);
1405 cls.capturevideo.videofile = NULL;
1408 if (cls.capturevideo.screenbuffer)
1410 Mem_Free (cls.capturevideo.screenbuffer);
1411 cls.capturevideo.screenbuffer = NULL;
1414 if (cls.capturevideo.outbuffer)
1416 Mem_Free (cls.capturevideo.outbuffer);
1417 cls.capturevideo.outbuffer = NULL;
1420 if (cls.capturevideo.riffindexbuffer.data)
1422 Mem_Free(cls.capturevideo.riffindexbuffer.data);
1423 cls.capturevideo.riffindexbuffer.data = NULL;
1426 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1429 // 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
1430 void SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(int width, int height, unsigned char *instart, unsigned char *outstart)
1433 int blockr, blockg, blockb;
1434 int outoffset = (width/2)*(height/2);
1435 unsigned char *b, *out;
1436 // process one line at a time, and CbCr every other line at 2 pixel intervals
1437 for (y = 0;y < height;y++)
1440 for (b = instart + (height-1-y)*width*4, out = outstart + y*width, x = 0;x < width;x++, b += 4, out++)
1445 *out = cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[0][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[0][2][blockb]];
1449 // 2x2 Cr and Cb planes
1450 int inpitch = width*4;
1451 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++)
1453 blockr = (b[2] + b[6] + b[inpitch+2] + b[inpitch+6]) >> 2;
1454 blockg = (b[1] + b[5] + b[inpitch+1] + b[inpitch+5]) >> 2;
1455 blockb = (b[0] + b[4] + b[inpitch+0] + b[inpitch+4]) >> 2;
1457 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];
1459 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];
1465 static void SCR_ScaleDownBGRA(unsigned char *in, int inw, int inh, unsigned char *out, int outw, int outh)
1467 // TODO optimize this function
1472 // memcpy is faster than me
1473 if(inw == outw && inh == outh)
1475 memcpy(out, in, 4 * inw * inh);
1479 // otherwise: a box filter
1480 area = (float)outw * (float)outh / (float)inw / (float)inh;
1481 for(y = 0; y < outh; ++y)
1483 float iny0 = y / (float)outh * inh; int iny0_i = floor(iny0);
1484 float iny1 = (y+1) / (float)outh * inh; int iny1_i = ceil(iny1);
1485 for(x = 0; x < outw; ++x)
1487 float inx0 = x / (float)outw * inw; int inx0_i = floor(inx0);
1488 float inx1 = (x+1) / (float)outw * inw; int inx1_i = ceil(inx1);
1489 float r = 0, g = 0, b = 0, alpha = 0;
1492 for(yy = iny0_i; yy < iny1_i; ++yy)
1494 float ya = min(yy+1, iny1) - max(iny0, yy);
1495 for(xx = inx0_i; xx < inx1_i; ++xx)
1497 float a = ya * (min(xx+1, inx1) - max(inx0, xx));
1498 r += a * in[4*(xx + inw * yy)+0];
1499 g += a * in[4*(xx + inw * yy)+1];
1500 b += a * in[4*(xx + inw * yy)+2];
1501 alpha += a * in[4*(xx + inw * yy)+3];
1505 out[4*(x + outw * y)+0] = r * area;
1506 out[4*(x + outw * y)+1] = g * area;
1507 out[4*(x + outw * y)+2] = b * area;
1508 out[4*(x + outw * y)+3] = alpha * area;
1513 qboolean SCR_CaptureVideo_VideoFrame(int newframenum)
1515 int x = 0, y = 0, width = cls.capturevideo.width, height = cls.capturevideo.height;
1516 unsigned char *in, *out;
1518 //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);
1519 // speed is critical here, so do saving as directly as possible
1520 switch (cls.capturevideo.format)
1522 case CAPTUREVIDEOFORMAT_AVI_I420:
1523 // if there's no videofile we have to just give up, and abort saving
1524 if (!cls.capturevideo.videofile)
1526 // FIXME: width/height must be multiple of 2, enforce this?
1527 qglReadPixels (x, y, vid.width, vid.height, GL_BGRA, GL_UNSIGNED_BYTE, cls.capturevideo.screenbuffer);CHECKGLERROR
1528 SCR_ScaleDownBGRA (cls.capturevideo.screenbuffer, vid.width, vid.height, cls.capturevideo.outbuffer, width, height);
1529 in = cls.capturevideo.outbuffer;
1530 out = cls.capturevideo.outbuffer + width*height*4;
1531 SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(width, height, in, out);
1532 x = width*height+(width/2)*(height/2)*2;
1533 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1534 for (;cls.capturevideo.frame < newframenum;cls.capturevideo.frame++)
1536 SCR_CaptureVideo_RIFF_IndexEntry("00dc", x, 0x10); // AVIIF_KEYFRAME
1537 SCR_CaptureVideo_RIFF_Push("00dc", NULL);
1538 SCR_CaptureVideo_RIFF_WriteBytes(out, x);
1539 SCR_CaptureVideo_RIFF_Pop();
1547 void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate)
1550 cls.capturevideo.soundrate = rate;
1551 cls.capturevideo.soundsampleframe += length;
1552 switch (cls.capturevideo.format)
1554 case CAPTUREVIDEOFORMAT_AVI_I420:
1556 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1557 SCR_CaptureVideo_RIFF_IndexEntry("01wb", x, 0x10); // AVIIF_KEYFRAME
1558 SCR_CaptureVideo_RIFF_Push("01wb", NULL);
1559 SCR_CaptureVideo_RIFF_WriteBytes(bufstereo16le, x);
1560 SCR_CaptureVideo_RIFF_Pop();
1567 void SCR_CaptureVideo(void)
1570 if (cl_capturevideo.integer && r_render.integer)
1572 if (!cls.capturevideo.active)
1573 SCR_CaptureVideo_BeginVideo();
1574 if (cls.capturevideo.framerate != cl_capturevideo_fps.value)
1576 Con_Printf("You can not change the video framerate while recording a video.\n");
1577 Cvar_SetValueQuick(&cl_capturevideo_fps, cls.capturevideo.framerate);
1579 // for AVI saving we have to make sure that sound is saved before video
1580 if (cls.capturevideo.soundrate && !cls.capturevideo.soundsampleframe)
1582 if (cls.capturevideo.realtime)
1584 // preserve sound sync by duplicating frames when running slow
1585 newframenum = (int)((realtime - cls.capturevideo.starttime) * cls.capturevideo.framerate);
1588 newframenum = cls.capturevideo.frame + 1;
1589 // if falling behind more than one second, stop
1590 if (newframenum - cls.capturevideo.frame > (int)ceil(cls.capturevideo.framerate))
1592 Cvar_SetValueQuick(&cl_capturevideo, 0);
1593 Con_Printf("video saving failed on frame %i, your machine is too slow for this capture speed.\n", cls.capturevideo.frame);
1594 SCR_CaptureVideo_EndVideo();
1598 SCR_CaptureVideo_VideoFrame(newframenum);
1599 if (cls.capturevideo.error)
1601 Cvar_SetValueQuick(&cl_capturevideo, 0);
1602 Con_Printf("video saving failed on frame %i, out of disk space? stopping video capture.\n", cls.capturevideo.frame);
1603 SCR_CaptureVideo_EndVideo();
1606 else if (cls.capturevideo.active)
1607 SCR_CaptureVideo_EndVideo();
1614 Grab six views for environment mapping tests
1621 qboolean flipx, flipy, flipdiagonaly;
1625 {{ 0, 0, 0}, "rt", false, false, false},
1626 {{ 0, 270, 0}, "ft", false, false, false},
1627 {{ 0, 180, 0}, "lf", false, false, false},
1628 {{ 0, 90, 0}, "bk", false, false, false},
1629 {{-90, 180, 0}, "up", true, true, false},
1630 {{ 90, 180, 0}, "dn", true, true, false},
1632 {{ 0, 0, 0}, "px", true, true, true},
1633 {{ 0, 90, 0}, "py", false, true, false},
1634 {{ 0, 180, 0}, "nx", false, false, true},
1635 {{ 0, 270, 0}, "ny", true, false, false},
1636 {{-90, 180, 0}, "pz", false, false, true},
1637 {{ 90, 180, 0}, "nz", false, false, true}
1640 static void R_Envmap_f (void)
1643 char filename[MAX_QPATH], basename[MAX_QPATH];
1644 unsigned char *buffer1;
1645 unsigned char *buffer2;
1646 unsigned char *buffer3;
1648 if (Cmd_Argc() != 3)
1650 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");
1654 strlcpy (basename, Cmd_Argv(1), sizeof (basename));
1655 size = atoi(Cmd_Argv(2));
1656 if (size != 128 && size != 256 && size != 512 && size != 1024)
1658 Con_Print("envmap: size must be one of 128, 256, 512, or 1024\n");
1661 if (size > vid.width || size > vid.height)
1663 Con_Print("envmap: your resolution is not big enough to render that size\n");
1667 r_refdef.envmap = true;
1669 R_UpdateVariables();
1674 r_view.width = size;
1675 r_view.height = size;
1677 r_view.useperspective = true;
1679 r_view.frustum_x = tan(90 * M_PI / 360.0);
1680 r_view.frustum_y = tan(90 * M_PI / 360.0);
1682 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1683 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1684 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3 + 18);
1686 for (j = 0;j < 12;j++)
1688 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1689 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);
1694 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);
1701 r_refdef.envmap = false;
1704 //=============================================================================
1706 void SHOWLMP_decodehide(void)
1710 lmplabel = MSG_ReadString();
1711 for (i = 0;i < cl.num_showlmps;i++)
1712 if (cl.showlmps[i].isactive && strcmp(cl.showlmps[i].label, lmplabel) == 0)
1714 cl.showlmps[i].isactive = false;
1719 void SHOWLMP_decodeshow(void)
1722 char lmplabel[256], picname[256];
1724 strlcpy (lmplabel,MSG_ReadString(), sizeof (lmplabel));
1725 strlcpy (picname, MSG_ReadString(), sizeof (picname));
1726 if (gamemode == GAME_NEHAHRA) // LordHavoc: nasty old legacy junk
1733 x = MSG_ReadShort();
1734 y = MSG_ReadShort();
1736 if (!cl.showlmps || cl.num_showlmps >= cl.max_showlmps)
1738 showlmp_t *oldshowlmps = cl.showlmps;
1739 cl.max_showlmps += 16;
1740 cl.showlmps = Mem_Alloc(cls.levelmempool, cl.max_showlmps * sizeof(showlmp_t));
1741 if (cl.num_showlmps)
1742 memcpy(cl.showlmps, oldshowlmps, cl.num_showlmps * sizeof(showlmp_t));
1744 Mem_Free(oldshowlmps);
1746 for (k = 0;k < cl.max_showlmps;k++)
1747 if (cl.showlmps[k].isactive && !strcmp(cl.showlmps[k].label, lmplabel))
1749 if (k == cl.max_showlmps)
1750 for (k = 0;k < cl.max_showlmps;k++)
1751 if (!cl.showlmps[k].isactive)
1753 cl.showlmps[k].isactive = true;
1754 strlcpy (cl.showlmps[k].label, lmplabel, sizeof (cl.showlmps[k].label));
1755 strlcpy (cl.showlmps[k].pic, picname, sizeof (cl.showlmps[k].pic));
1756 cl.showlmps[k].x = x;
1757 cl.showlmps[k].y = y;
1758 cl.num_showlmps = max(cl.num_showlmps, k + 1);
1761 void SHOWLMP_drawall(void)
1764 for (i = 0;i < cl.num_showlmps;i++)
1765 if (cl.showlmps[i].isactive)
1766 DrawQ_Pic(cl.showlmps[i].x, cl.showlmps[i].y, Draw_CachePic(cl.showlmps[i].pic, true), 0, 0, 1, 1, 1, 1, 0);
1770 ==============================================================================
1774 ==============================================================================
1777 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)
1779 int indices[3] = {0,1,2};
1782 if (!r_render.integer)
1786 qglReadPixels (x, y, width, height, jpeg ? GL_RGB : GL_BGR, GL_UNSIGNED_BYTE, buffer1);CHECKGLERROR
1788 if (scr_screenshot_gammaboost.value != 1 && gammacorrect)
1791 double igamma = 1.0 / scr_screenshot_gammaboost.value;
1792 unsigned char ramp[256];
1793 for (i = 0;i < 256;i++)
1794 ramp[i] = (unsigned char) (pow(i * (1.0 / 255.0), igamma) * 255.0);
1795 for (i = 0;i < width*height*3;i++)
1796 buffer1[i] = ramp[buffer1[i]];
1799 Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
1802 ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
1804 ret = Image_WriteTGABGR_preflipped (filename, width, height, buffer2, buffer3);
1809 //=============================================================================
1811 void R_ClearScreen(void)
1815 if (r_refdef.fogenabled)
1817 qglClearColor(r_refdef.fogcolor[0],r_refdef.fogcolor[1],r_refdef.fogcolor[2],0);CHECKGLERROR
1821 qglClearColor(0,0,0,0);CHECKGLERROR
1823 qglClearDepth(1);CHECKGLERROR
1826 // LordHavoc: we use a stencil centered around 128 instead of 0,
1827 // to avoid clamping interfering with strange shadow volume
1829 qglClearStencil(128);CHECKGLERROR
1832 if (r_render.integer)
1833 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));
1834 // set dithering mode
1835 if (gl_dither.integer)
1837 qglEnable(GL_DITHER);CHECKGLERROR
1841 qglDisable(GL_DITHER);CHECKGLERROR
1845 qboolean CL_VM_UpdateView (void);
1846 void SCR_DrawConsole (void);
1847 void R_Shadow_EditLights_DrawSelectedLightProperties(void);
1851 void SCR_DrawScreen (void)
1855 if (r_timereport_active)
1856 R_TimeReport("screensetup");
1858 R_UpdateVariables();
1860 // Quake uses clockwise winding, so these are swapped
1861 r_view.cullface_front = GL_BACK;
1862 r_view.cullface_back = GL_FRONT;
1864 if (cls.signon == SIGNONS)
1868 size = scr_viewsize.value * (1.0 / 100.0);
1869 size = min(size, 1);
1871 if (r_stereo_sidebyside.integer)
1873 r_view.width = (int)(vid.width * size / 2.5);
1874 r_view.height = (int)(vid.height * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100));
1876 r_view.x = (int)((vid.width - r_view.width * 2.5) * 0.5);
1877 r_view.y = (int)((vid.height - r_view.height)/2);
1880 r_view.x += (int)(r_view.width * 1.5);
1884 r_view.width = (int)(vid.width * size);
1885 r_view.height = (int)(vid.height * size * (1 - bound(0, r_letterbox.value, 100) / 100));
1887 r_view.x = (int)((vid.width - r_view.width)/2);
1888 r_view.y = (int)((vid.height - r_view.height)/2);
1892 // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
1893 // LordHavoc: this is designed to produce widescreen fov values
1894 // when the screen is wider than 4/3 width/height aspect, to do
1895 // this it simply assumes the requested fov is the vertical fov
1896 // for a 4x3 display, if the ratio is not 4x3 this makes the fov
1897 // higher/lower according to the ratio
1898 r_view.useperspective = true;
1899 r_view.frustum_y = tan(scr_fov.value * M_PI / 360.0) * (3.0/4.0) * cl.viewzoom;
1900 r_view.frustum_x = r_view.frustum_y * (float)r_view.width / (float)r_view.height / vid_pixelheight.value;
1902 r_view.frustum_x *= r_refdef.frustumscale_x;
1903 r_view.frustum_y *= r_refdef.frustumscale_y;
1905 if(!CL_VM_UpdateView())
1908 if (scr_zoomwindow.integer)
1910 float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
1911 float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
1912 r_view.width = (int)(vid.width * sizex);
1913 r_view.height = (int)(vid.height * sizey);
1915 r_view.x = (int)((vid.width - r_view.width)/2);
1919 r_view.useperspective = true;
1920 r_view.frustum_y = tan(scr_zoomwindow_fov.value * M_PI / 360.0) * (3.0/4.0) * cl.viewzoom;
1921 r_view.frustum_x = r_view.frustum_y * vid_pixelheight.value * (float)r_view.width / (float)r_view.height;
1923 r_view.frustum_x *= r_refdef.frustumscale_x;
1924 r_view.frustum_y *= r_refdef.frustumscale_y;
1926 if(!CL_VM_UpdateView())
1931 if (!r_stereo_sidebyside.integer)
1933 r_view.width = vid.width;
1934 r_view.height = vid.height;
1939 r_view.useperspective = false;
1944 if (cls.signon == SIGNONS)
1949 if (!r_letterbox.value)
1952 SCR_CheckDrawCenterString();
1954 SCR_DrawNetGraph ();
1957 R_Shadow_EditLights_DrawSelectedLightProperties();
1965 if (r_timereport_active)
1968 if (cls.signon == SIGNONS)
1969 R_TimeReport_Frame();
1977 if (r_timereport_active)
1978 R_TimeReport("meshfinish");
1981 void SCR_UpdateLoadingScreen (qboolean clear)
1986 float texcoord2f[8];
1987 // don't do anything if not initialized yet
1988 if (vid_hidden || !scr_refresh.integer)
1991 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
1992 //qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
1993 //qglDepthMask(1);CHECKGLERROR
1994 qglColorMask(1,1,1,1);CHECKGLERROR
1995 qglClearColor(0,0,0,0);CHECKGLERROR
1996 // when starting up a new video mode, make sure the screen is cleared to black
1999 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2001 //qglDisable(GL_CULL_FACE);CHECKGLERROR
2004 GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
2006 R_Mesh_Matrix(&identitymatrix);
2007 // draw the loading plaque
2008 pic = Draw_CachePic("gfx/loading", true);
2009 x = (vid_conwidth.integer - pic->width)/2;
2010 y = (vid_conheight.integer - pic->height)/2;
2012 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2013 GL_DepthRange(0, 1);
2014 GL_PolygonOffset(0, 0);
2015 GL_DepthTest(false);
2016 R_Mesh_VertexPointer(vertex3f, 0, 0);
2017 R_Mesh_ColorPointer(NULL, 0, 0);
2018 R_Mesh_ResetTextureState();
2019 R_Mesh_TexBind(0, R_GetTexture(pic->tex));
2020 R_Mesh_TexCoordPointer(0, 2, texcoord2f, 0, 0);
2021 vertex3f[2] = vertex3f[5] = vertex3f[8] = vertex3f[11] = 0;
2022 vertex3f[0] = vertex3f[9] = x;
2023 vertex3f[1] = vertex3f[4] = y;
2024 vertex3f[3] = vertex3f[6] = x + pic->width;
2025 vertex3f[7] = vertex3f[10] = y + pic->height;
2026 texcoord2f[0] = 0;texcoord2f[1] = 0;
2027 texcoord2f[2] = 1;texcoord2f[3] = 0;
2028 texcoord2f[4] = 1;texcoord2f[5] = 1;
2029 texcoord2f[6] = 0;texcoord2f[7] = 1;
2030 if (vid.stereobuffer)
2032 qglDrawBuffer(GL_FRONT_LEFT);
2033 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2034 qglDrawBuffer(GL_FRONT_RIGHT);
2035 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2039 qglDrawBuffer(GL_FRONT);
2040 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2044 // not necessary when rendering to GL_FRONT buffers
2045 //VID_Finish(false);
2046 // however this IS necessary on Windows Vista
2050 void CL_UpdateScreen(void)
2052 float conwidth, conheight;
2054 if (vid_hidden || !scr_refresh.integer)
2057 if (!scr_initialized || !con_initialized)
2058 return; // not initialized yet
2060 conwidth = bound(320, vid_conwidth.value, 2048);
2061 conheight = bound(200, vid_conheight.value, 1536);
2062 if (vid_conwidth.value != conwidth)
2063 Cvar_SetValue("vid_conwidth", conwidth);
2064 if (vid_conheight.value != conheight)
2065 Cvar_SetValue("vid_conheight", conheight);
2068 if (scr_viewsize.value < 30)
2069 Cvar_Set ("viewsize","30");
2070 if (scr_viewsize.value > 120)
2071 Cvar_Set ("viewsize","120");
2073 // bound field of view
2074 if (scr_fov.value < 1)
2075 Cvar_Set ("fov","1");
2076 if (scr_fov.value > 170)
2077 Cvar_Set ("fov","170");
2079 // validate r_textureunits cvar
2080 if (r_textureunits.integer > gl_textureunits)
2081 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
2082 if (r_textureunits.integer < 1)
2083 Cvar_SetValueQuick(&r_textureunits, 1);
2085 // validate gl_combine cvar
2086 if (gl_combine.integer && !gl_combine_extension)
2087 Cvar_SetValueQuick(&gl_combine, 0);
2089 // intermission is always full screen
2090 if (cl.intermission)
2094 if (scr_viewsize.value >= 120)
2095 sb_lines = 0; // no status bar at all
2096 else if (scr_viewsize.value >= 110)
2097 sb_lines = 24; // no inventory
2102 r_view.colormask[0] = 1;
2103 r_view.colormask[1] = 1;
2104 r_view.colormask[2] = 1;
2106 if (r_timereport_active)
2107 R_TimeReport("other");
2109 SCR_SetUpToDrawConsole();
2111 if (r_timereport_active)
2112 R_TimeReport("start");
2115 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
2116 qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
2117 qglDepthMask(1);CHECKGLERROR
2118 qglColorMask(1,1,1,1);CHECKGLERROR
2119 qglClearColor(0,0,0,0);CHECKGLERROR
2121 r_view.clear = false;
2123 if(scr_stipple.integer)
2125 GLubyte stipple[128];
2126 int i, s, width, parts;
2127 static int frame = 0;
2130 s = scr_stipple.integer;
2132 width = (s & 070) >> 3;
2134 qglEnable(GL_POLYGON_STIPPLE); // 0x0B42
2135 for(i = 0; i < 128; ++i)
2138 stipple[i] = (((line >> width) + frame) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
2140 qglPolygonStipple(stipple);
2143 qglDisable(GL_POLYGON_STIPPLE);
2145 if (r_timereport_active)
2146 R_TimeReport("screenclear");
2148 qglDrawBuffer(GL_BACK);
2150 if (vid.stereobuffer || r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
2152 matrix4x4_t originalmatrix = r_view.matrix;
2153 matrix4x4_t offsetmatrix;
2154 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * 0.5f, 0, 0, r_stereo_angle.value * 0.5f, 0, 1);
2155 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
2157 if (r_stereo_sidebyside.integer)
2160 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
2162 r_view.colormask[0] = 1;
2163 r_view.colormask[1] = 0;
2164 r_view.colormask[2] = 0;
2167 if (vid.stereobuffer)
2168 qglDrawBuffer(GL_BACK_RIGHT);
2172 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * -0.5f, 0, 0, r_stereo_angle.value * -0.5f, 0, 1);
2173 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
2175 if (r_stereo_sidebyside.integer)
2178 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
2180 r_view.colormask[0] = 0;
2181 r_view.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
2182 r_view.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
2185 if (vid.stereobuffer)
2186 qglDrawBuffer(GL_BACK_LEFT);
2190 r_view.matrix = originalmatrix;
2198 if (r_timereport_active)
2199 R_TimeReport("finish");
2202 void CL_Screen_NewMap(void)