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"}; // 1 - 170
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 (not used)"};
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","8", "speed of intermission printing (episode end texts)"};
25 cvar_t vid_conwidth = {CVAR_SAVE, "vid_conwidth", "640", "virtual width of 2D graphics system"};
26 cvar_t vid_conheight = {CVAR_SAVE, "vid_conheight", "480", "virtual height of 2D graphics system"};
27 cvar_t vid_pixelheight = {CVAR_SAVE, "vid_pixelheight", "1", "adjusts vertical field of vision to account for non-square pixels (1280x1024 on a CRT monitor for example)"};
28 cvar_t scr_screenshot_jpeg = {CVAR_SAVE, "scr_screenshot_jpeg","1", "save jpeg instead of targa"};
29 cvar_t scr_screenshot_jpeg_quality = {CVAR_SAVE, "scr_screenshot_jpeg_quality","0.9", "image quality of saved jpeg"};
30 cvar_t scr_screenshot_gammaboost = {CVAR_SAVE, "scr_screenshot_gammaboost","1", "gamma correction on saved screenshots and videos, 1.0 saves unmodified images"};
31 // scr_screenshot_name is defined in fs.c
32 cvar_t cl_capturevideo = {0, "cl_capturevideo", "0", "enables saving of video to a .avi file using uncompressed I420 colorspace and PCM audio, note that scr_screenshot_gammaboost affects the brightness of the output)"};
33 cvar_t cl_capturevideo_realtime = {0, "cl_capturevideo_realtime", "0", "causes video saving to operate in realtime (mostly useful while playing, not while capturing demos), this can produce a much lower quality video due to poor sound/video sync and will abort saving if your machine stalls for over 1 second"};
34 cvar_t cl_capturevideo_fps = {0, "cl_capturevideo_fps", "30", "how many frames per second to save (29.97 for NTSC, 30 for typical PC video, 15 can be useful)"};
35 cvar_t cl_capturevideo_number = {CVAR_SAVE, "cl_capturevideo_number", "1", "number to append to video filename, incremented each time a capture begins"};
36 cvar_t r_letterbox = {0, "r_letterbox", "0", "reduces vertical height of view to simulate a letterboxed movie effect (can be used by mods for cutscenes)"};
37 cvar_t r_stereo_separation = {0, "r_stereo_separation", "4", "separation of eyes in the world (try negative values too)"};
38 cvar_t r_stereo_sidebyside = {0, "r_stereo_sidebyside", "0", "side by side views (for those who can't afford glasses but can afford eye strain)"};
39 cvar_t r_stereo_redblue = {0, "r_stereo_redblue", "0", "red/blue anaglyph stereo glasses (note: most of these glasses are actually red/cyan, try that one too)"};
40 cvar_t r_stereo_redcyan = {0, "r_stereo_redcyan", "0", "red/cyan anaglyph stereo glasses, the kind given away at drive-in movies like Creature From The Black Lagoon In 3D"};
41 cvar_t r_stereo_redgreen = {0, "r_stereo_redgreen", "0", "red/green anaglyph stereo glasses (for those who don't mind yellow)"};
42 cvar_t scr_zoomwindow = {CVAR_SAVE, "scr_zoomwindow", "0", "displays a zoomed in overlay window"};
43 cvar_t scr_zoomwindow_viewsizex = {CVAR_SAVE, "scr_zoomwindow_viewsizex", "20", "horizontal viewsize of zoom window"};
44 cvar_t scr_zoomwindow_viewsizey = {CVAR_SAVE, "scr_zoomwindow_viewsizey", "20", "vertical viewsize of zoom window"};
45 cvar_t scr_zoomwindow_fov = {CVAR_SAVE, "scr_zoomwindow_fov", "20", "fov of zoom window"};
46 cvar_t scr_stipple = {0, "scr_stipple", "0", "interlacing-like stippling of the display"};
49 int jpeg_supported = false;
51 qboolean scr_initialized; // ready to draw
53 float scr_con_current;
55 extern int con_vislines;
57 static void SCR_ScreenShot_f (void);
58 static void R_Envmap_f (void);
61 void R_ClearScreen(void);
64 ===============================================================================
68 ===============================================================================
71 char scr_centerstring[MAX_INPUTLINE];
72 float scr_centertime_start; // for slow victory printing
73 float scr_centertime_off;
82 Called for important messages that should stay in the center of the screen
86 void SCR_CenterPrint(char *str)
88 strlcpy (scr_centerstring, str, sizeof (scr_centerstring));
89 scr_centertime_off = scr_centertime.value;
90 scr_centertime_start = cl.time;
92 // count the number of lines for centering
103 void SCR_DrawCenterString (void)
111 // the finale prints the characters one at a time
113 remaining = (int)(scr_printspeed.value * (cl.time - scr_centertime_start));
117 scr_erase_center = 0;
118 start = scr_centerstring;
123 if (scr_center_lines <= 4)
124 y = (int)(vid_conheight.integer*0.35);
131 // scan the number of characters on the line, not counting color codes
133 for (l=0 ; l<vid_conwidth.integer/8 ; l++)
135 if (start[l] == '\n' || !start[l])
137 // color codes add no visible characters, so don't count them
138 if (start[l] == STRING_COLOR_TAG && (start[l+1] >= '0' && start[l+1] <= '9'))
143 x = (vid_conwidth.integer - chars*8)/2;
148 DrawQ_ColoredString(x, y, start, l, 8, 8, 1, 1, 1, 1, 0, &color);
156 while (*start && *start != '\n')
161 start++; // skip the \n
165 void SCR_CheckDrawCenterString (void)
167 if (scr_center_lines > scr_erase_lines)
168 scr_erase_lines = scr_center_lines;
170 scr_centertime_off -= cl.realframetime;
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 ();
189 void SCR_DrawTurtle (void)
193 if (cls.state != ca_connected)
196 if (!scr_showturtle.integer)
199 if (cl.realframetime < 0.1)
209 DrawQ_Pic (0, 0, Draw_CachePic("gfx/turtle", true), 0, 0, 1, 1, 1, 1, 0);
217 void SCR_DrawNet (void)
219 if (cls.state != ca_connected)
221 if (realtime - cl.last_received_message < 0.3)
223 if (cls.demoplayback)
226 DrawQ_Pic (64, 0, Draw_CachePic("gfx/net", true), 0, 0, 1, 1, 1, 1, 0);
234 void SCR_DrawPause (void)
238 if (cls.state != ca_connected)
241 if (!scr_showpause.integer) // turn off for screenshots
247 pic = Draw_CachePic ("gfx/pause", true);
248 DrawQ_Pic ((vid_conwidth.integer - pic->width)/2, (vid_conheight.integer - pic->height)/2, pic, 0, 0, 1, 1, 1, 1, 0);
256 void SCR_DrawBrand (void)
261 if (!scr_showbrand.value)
264 pic = Draw_CachePic ("gfx/brand", true);
266 switch ((int)scr_showbrand.value)
268 case 1: // bottom left
270 y = vid_conheight.integer - pic->height;
272 case 2: // bottom centre
273 x = (vid_conwidth.integer - pic->width) / 2;
274 y = vid_conheight.integer - pic->height;
276 case 3: // bottom right
277 x = vid_conwidth.integer - pic->width;
278 y = vid_conheight.integer - pic->height;
280 case 4: // centre right
281 x = vid_conwidth.integer - pic->width;
282 y = (vid_conheight.integer - pic->height) / 2;
285 x = vid_conwidth.integer - pic->width;
288 case 6: // top centre
289 x = (vid_conwidth.integer - pic->width) / 2;
296 case 8: // centre left
298 y = (vid_conheight.integer - pic->height) / 2;
304 DrawQ_Pic (x, y, pic, 0, 0, 1, 1, 1, 1, 0);
312 static int SCR_DrawQWDownload(int offset)
318 if (!cls.qw_downloadname[0])
320 dpsnprintf(temp, sizeof(temp), "Downloading %s ... %3i%%\n", cls.qw_downloadname, cls.qw_downloadpercent);
321 len = (int)strlen(temp);
322 x = (vid_conwidth.integer - len*size) / 2;
323 y = vid_conheight.integer - size - offset;
324 DrawQ_Pic(0, y, NULL, vid_conwidth.integer, size, 0, 0, 0, 0.5, 0);
325 DrawQ_String(x, y, temp, len, size, size, 1, 1, 1, 1, 0);
334 static int SCR_DrawCurlDownload(int offset)
341 Curl_downloadinfo_t *downinfo;
345 downinfo = Curl_GetDownloadInfo(&nDownloads, &addinfo);
349 y = vid_conheight.integer - size * nDownloads - offset;
353 len = (int)strlen(addinfo);
354 x = (vid_conwidth.integer - len*size) / 2;
355 DrawQ_Pic(0, y - size, NULL, vid_conwidth.integer, size, 1, 1, 1, 0.8, 0);
356 DrawQ_String(x, y - size, addinfo, len, size, size, 0, 0, 0, 1, 0);
359 for(i = 0; i != nDownloads; ++i)
361 if(downinfo[i].queued)
362 dpsnprintf(temp, sizeof(temp), "Still in queue: %s\n", downinfo[i].filename);
363 else if(downinfo[i].progress <= 0)
364 dpsnprintf(temp, sizeof(temp), "Downloading %s ... ???.?%% @ %.1f KiB/s\n", downinfo[i].filename, downinfo[i].speed / 1024.0);
366 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);
367 len = (int)strlen(temp);
368 x = (vid_conwidth.integer - len*size) / 2;
369 DrawQ_Pic(0, y + i * size, NULL, vid_conwidth.integer, size, 0, 0, 0, 0.8, 0);
370 DrawQ_String(x, y + i * size, temp, len, size, size, 1, 1, 1, 1, 0);
375 return 8 * (nDownloads + (addinfo ? 1 : 0));
383 static void SCR_DrawDownload()
386 offset += SCR_DrawQWDownload(offset);
387 offset += SCR_DrawCurlDownload(offset);
390 //=============================================================================
394 SCR_SetUpToDrawConsole
397 void SCR_SetUpToDrawConsole (void)
399 // lines of console to display
401 static int framecounter = 0;
405 if (scr_menuforcewhiledisconnected.integer && key_dest == key_game && cls.state == ca_disconnected)
407 if (framecounter >= 2)
415 if (scr_conforcewhiledisconnected.integer && key_dest == key_game && cls.signon != SIGNONS)
416 key_consoleactive |= KEY_CONSOLEACTIVE_FORCED;
418 key_consoleactive &= ~KEY_CONSOLEACTIVE_FORCED;
420 // decide on the height of the console
421 if (key_consoleactive & KEY_CONSOLEACTIVE_USER)
422 conlines = vid_conheight.integer/2; // half screen
424 conlines = 0; // none visible
426 scr_con_current = conlines;
434 void SCR_DrawConsole (void)
436 if (key_consoleactive & KEY_CONSOLEACTIVE_FORCED)
439 Con_DrawConsole (vid_conheight.integer);
441 else if (scr_con_current)
442 Con_DrawConsole ((int)scr_con_current);
446 if ((key_dest == key_game || key_dest == key_message) && !r_letterbox.value)
447 Con_DrawNotify (); // only draw notify in game
453 SCR_BeginLoadingPlaque
457 void SCR_BeginLoadingPlaque (void)
459 // save console log up to this point to log_file if it was set by configs
464 SCR_UpdateLoadingScreen(false);
467 //=============================================================================
469 char r_speeds_string[1024];
470 int speedstringcount, r_timereport_active;
471 double r_timereport_temp = 0, r_timereport_current = 0, r_timereport_start = 0;
473 void R_TimeReport(char *desc)
479 if (r_speeds.integer < 2 || !r_timereport_active)
483 qglFinish();CHECKGLERROR
484 r_timereport_temp = r_timereport_current;
485 r_timereport_current = Sys_DoubleTime();
486 t = (int) ((r_timereport_current - r_timereport_temp) * 1000000.0 + 0.5);
488 dpsnprintf(tempbuf, sizeof(tempbuf), "%8i %-11s", t, desc);
489 length = (int)strlen(tempbuf);
490 if (speedstringcount + length > (vid_conwidth.integer / 8))
492 strlcat(r_speeds_string, "\n", sizeof(r_speeds_string));
493 speedstringcount = 0;
495 strlcat(r_speeds_string, tempbuf, sizeof(r_speeds_string));
496 speedstringcount += length;
499 void R_TimeReport_Frame(void)
503 if (r_speeds_string[0])
505 if (r_timereport_active)
507 r_timereport_current = r_timereport_start;
508 R_TimeReport("total");
511 if (r_speeds_string[strlen(r_speeds_string)-1] == '\n')
512 r_speeds_string[strlen(r_speeds_string)-1] = 0;
514 for (i = 0;r_speeds_string[i];i++)
515 if (r_speeds_string[i] == '\n')
517 y = vid_conheight.integer - sb_lines - lines * 8;
519 DrawQ_Pic(0, y, NULL, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
520 while (r_speeds_string[i])
523 while (r_speeds_string[i] && r_speeds_string[i] != '\n')
526 DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0);
527 if (r_speeds_string[i] == '\n')
531 r_speeds_string[0] = 0;
532 r_timereport_active = false;
534 if (r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected)
536 speedstringcount = 0;
537 r_speeds_string[0] = 0;
538 r_timereport_active = false;
539 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]);
540 sprintf(r_speeds_string + strlen(r_speeds_string), "%5i entities%6i surfaces%6i triangles%5i leafs%5i portals%6i particles\n", r_refdef.stats.entities, r_refdef.stats.entities_surfaces, r_refdef.stats.entities_triangles, r_refdef.stats.world_leafs, r_refdef.stats.world_portals, r_refdef.stats.particles);
541 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);
542 if (r_refdef.stats.bloom)
543 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);
545 sprintf(r_speeds_string + strlen(r_speeds_string), "rendered%6i meshes%8i triangles\n", r_refdef.stats.meshes, r_refdef.stats.meshes_elements / 3);
547 memset(&r_refdef.stats, 0, sizeof(r_refdef.stats));
549 if (r_speeds.integer >= 2)
551 r_timereport_active = true;
552 r_timereport_start = r_timereport_current = Sys_DoubleTime();
564 void SCR_SizeUp_f (void)
566 Cvar_SetValue ("viewsize",scr_viewsize.value+10);
577 void SCR_SizeDown_f (void)
579 Cvar_SetValue ("viewsize",scr_viewsize.value-10);
582 void CL_Screen_Init(void)
584 Cvar_RegisterVariable (&scr_fov);
585 Cvar_RegisterVariable (&scr_viewsize);
586 Cvar_RegisterVariable (&scr_conalpha);
587 Cvar_RegisterVariable (&scr_conbrightness);
588 Cvar_RegisterVariable (&scr_conforcewhiledisconnected);
589 Cvar_RegisterVariable (&scr_menuforcewhiledisconnected);
590 Cvar_RegisterVariable (&scr_showram);
591 Cvar_RegisterVariable (&scr_showturtle);
592 Cvar_RegisterVariable (&scr_showpause);
593 Cvar_RegisterVariable (&scr_showbrand);
594 Cvar_RegisterVariable (&scr_centertime);
595 Cvar_RegisterVariable (&scr_printspeed);
596 Cvar_RegisterVariable (&vid_conwidth);
597 Cvar_RegisterVariable (&vid_conheight);
598 Cvar_RegisterVariable (&vid_pixelheight);
599 Cvar_RegisterVariable (&scr_screenshot_jpeg);
600 Cvar_RegisterVariable (&scr_screenshot_jpeg_quality);
601 Cvar_RegisterVariable (&scr_screenshot_gammaboost);
602 Cvar_RegisterVariable (&cl_capturevideo);
603 Cvar_RegisterVariable (&cl_capturevideo_realtime);
604 Cvar_RegisterVariable (&cl_capturevideo_fps);
605 Cvar_RegisterVariable (&cl_capturevideo_number);
606 Cvar_RegisterVariable (&r_letterbox);
607 Cvar_RegisterVariable(&r_stereo_separation);
608 Cvar_RegisterVariable(&r_stereo_sidebyside);
609 Cvar_RegisterVariable(&r_stereo_redblue);
610 Cvar_RegisterVariable(&r_stereo_redcyan);
611 Cvar_RegisterVariable(&r_stereo_redgreen);
612 Cvar_RegisterVariable(&scr_zoomwindow);
613 Cvar_RegisterVariable(&scr_zoomwindow_viewsizex);
614 Cvar_RegisterVariable(&scr_zoomwindow_viewsizey);
615 Cvar_RegisterVariable(&scr_zoomwindow_fov);
616 Cvar_RegisterVariable(&scr_stipple);
618 Cmd_AddCommand ("sizeup",SCR_SizeUp_f, "increase view size (increases viewsize cvar)");
619 Cmd_AddCommand ("sizedown",SCR_SizeDown_f, "decrease view size (decreases viewsize cvar)");
620 Cmd_AddCommand ("screenshot",SCR_ScreenShot_f, "takes a screenshot of the next rendered frame");
621 Cmd_AddCommand ("envmap", R_Envmap_f, "render a cubemap (skybox) of the current scene");
623 scr_initialized = true;
631 void SCR_ScreenShot_f (void)
633 static int shotnumber;
634 static char oldname[MAX_QPATH];
635 char base[MAX_QPATH];
636 char filename[MAX_QPATH];
637 unsigned char *buffer1;
638 unsigned char *buffer2;
639 unsigned char *buffer3;
640 qboolean jpeg = (scr_screenshot_jpeg.integer != 0);
642 sprintf (base, "screenshots/%s", scr_screenshot_name.string);
644 if (strcmp (oldname, scr_screenshot_name.string))
646 sprintf(oldname, "%s", scr_screenshot_name.string);
650 // find a file name to save it to
651 for (;shotnumber < 1000000;shotnumber++)
652 if (!FS_SysFileExists(va("%s/%s%06d.tga", fs_gamedir, base, shotnumber)) && !FS_SysFileExists(va("%s/%s%06d.jpg", fs_gamedir, base, shotnumber)))
654 if (shotnumber >= 1000000)
656 Con_Print("SCR_ScreenShot_f: Couldn't create the image file\n");
660 sprintf(filename, "%s%06d.%s", base, shotnumber, jpeg ? "jpg" : "tga");
662 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
663 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3);
664 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * 3 + 18);
666 if (SCR_ScreenShot (filename, buffer1, buffer2, buffer3, 0, 0, vid.width, vid.height, false, false, false, jpeg, true))
667 Con_Printf("Wrote %s\n", filename);
669 Con_Printf("unable to write %s\n", filename);
678 static void SCR_CaptureVideo_RIFF_Start(void)
680 memset(&cls.capturevideo.riffbuffer, 0, sizeof(sizebuf_t));
681 cls.capturevideo.riffbuffer.maxsize = sizeof(cls.capturevideo.riffbufferdata);
682 cls.capturevideo.riffbuffer.data = cls.capturevideo.riffbufferdata;
685 static void SCR_CaptureVideo_RIFF_Flush(void)
687 if (cls.capturevideo.riffbuffer.cursize > 0)
689 if (!FS_Write(cls.capturevideo.videofile, cls.capturevideo.riffbuffer.data, cls.capturevideo.riffbuffer.cursize))
690 cls.capturevideo.error = true;
691 cls.capturevideo.riffbuffer.cursize = 0;
692 cls.capturevideo.riffbuffer.overflowed = false;
696 static void SCR_CaptureVideo_RIFF_WriteBytes(const unsigned char *data, size_t size)
698 SCR_CaptureVideo_RIFF_Flush();
699 if (!FS_Write(cls.capturevideo.videofile, data, size))
700 cls.capturevideo.error = true;
703 static void SCR_CaptureVideo_RIFF_Write32(int n)
705 if (cls.capturevideo.riffbuffer.cursize + 4 > cls.capturevideo.riffbuffer.maxsize)
706 SCR_CaptureVideo_RIFF_Flush();
707 MSG_WriteLong(&cls.capturevideo.riffbuffer, n);
710 static void SCR_CaptureVideo_RIFF_Write16(int n)
712 if (cls.capturevideo.riffbuffer.cursize + 2 > cls.capturevideo.riffbuffer.maxsize)
713 SCR_CaptureVideo_RIFF_Flush();
714 MSG_WriteShort(&cls.capturevideo.riffbuffer, n);
717 static void SCR_CaptureVideo_RIFF_WriteFourCC(const char *chunkfourcc)
719 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(chunkfourcc) > cls.capturevideo.riffbuffer.maxsize)
720 SCR_CaptureVideo_RIFF_Flush();
721 MSG_WriteUnterminatedString(&cls.capturevideo.riffbuffer, chunkfourcc);
724 static void SCR_CaptureVideo_RIFF_WriteTerminatedString(const char *string)
726 if (cls.capturevideo.riffbuffer.cursize + (int)strlen(string) > cls.capturevideo.riffbuffer.maxsize)
727 SCR_CaptureVideo_RIFF_Flush();
728 MSG_WriteString(&cls.capturevideo.riffbuffer, string);
731 static fs_offset_t SCR_CaptureVideo_RIFF_GetPosition(void)
733 SCR_CaptureVideo_RIFF_Flush();
734 return FS_Tell(cls.capturevideo.videofile);
737 static void SCR_CaptureVideo_RIFF_Push(const char *chunkfourcc, const char *listtypefourcc)
739 SCR_CaptureVideo_RIFF_WriteFourCC(chunkfourcc);
740 SCR_CaptureVideo_RIFF_Write32(0);
741 SCR_CaptureVideo_RIFF_Flush();
742 cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel++] = SCR_CaptureVideo_RIFF_GetPosition();
744 SCR_CaptureVideo_RIFF_WriteFourCC(listtypefourcc);
747 static void SCR_CaptureVideo_RIFF_Pop(void)
751 unsigned char sizebytes[4];
752 // write out the chunk size and then return to the current file position
753 cls.capturevideo.riffstacklevel--;
754 offset = SCR_CaptureVideo_RIFF_GetPosition();
755 x = (int)(offset - (cls.capturevideo.riffstackstartoffset[cls.capturevideo.riffstacklevel]));
756 sizebytes[0] = (x) & 0xff;sizebytes[1] = (x >> 8) & 0xff;sizebytes[2] = (x >> 16) & 0xff;sizebytes[3] = (x >> 24) & 0xff;
757 FS_Seek(cls.capturevideo.videofile, -(x + 4), SEEK_END);
758 FS_Write(cls.capturevideo.videofile, sizebytes, 4);
759 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
763 FS_Write(cls.capturevideo.videofile, &c, 1);
767 static void SCR_CaptureVideo_RIFF_IndexEntry(const char *chunkfourcc, int chunksize, int flags)
769 if (cls.capturevideo.riffstacklevel != 2)
770 Sys_Error("SCR_Capturevideo_RIFF_IndexEntry: RIFF stack level is %i (should be 2)\n", cls.capturevideo.riffstacklevel);
771 if (cls.capturevideo.riffindexbuffer.cursize + 16 > cls.capturevideo.riffindexbuffer.maxsize)
773 int oldsize = cls.capturevideo.riffindexbuffer.maxsize;
774 unsigned char *olddata;
775 olddata = cls.capturevideo.riffindexbuffer.data;
776 cls.capturevideo.riffindexbuffer.maxsize = max(cls.capturevideo.riffindexbuffer.maxsize * 2, 4096);
777 cls.capturevideo.riffindexbuffer.data = Mem_Alloc(tempmempool, cls.capturevideo.riffindexbuffer.maxsize);
780 memcpy(cls.capturevideo.riffindexbuffer.data, olddata, oldsize);
784 MSG_WriteUnterminatedString(&cls.capturevideo.riffindexbuffer, chunkfourcc);
785 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, flags);
786 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, (int)FS_Tell(cls.capturevideo.videofile) - cls.capturevideo.riffstackstartoffset[1]);
787 MSG_WriteLong(&cls.capturevideo.riffindexbuffer, chunksize);
790 static void SCR_CaptureVideo_RIFF_Finish(void)
792 // close the "movi" list
793 SCR_CaptureVideo_RIFF_Pop();
794 // write the idx1 chunk that we've been building while saving the frames
795 SCR_CaptureVideo_RIFF_Push("idx1", NULL);
796 SCR_CaptureVideo_RIFF_WriteBytes(cls.capturevideo.riffindexbuffer.data, cls.capturevideo.riffindexbuffer.cursize);
797 SCR_CaptureVideo_RIFF_Pop();
798 cls.capturevideo.riffindexbuffer.cursize = 0;
799 // pop the RIFF chunk itself
800 while (cls.capturevideo.riffstacklevel > 0)
801 SCR_CaptureVideo_RIFF_Pop();
802 SCR_CaptureVideo_RIFF_Flush();
805 static void SCR_CaptureVideo_RIFF_OverflowCheck(int framesize)
808 if (cls.capturevideo.riffstacklevel != 2)
809 Sys_Error("SCR_CaptureVideo_RIFF_OverflowCheck: chunk stack leakage!\n");
810 // check where we are in the file
811 SCR_CaptureVideo_RIFF_Flush();
812 cursize = SCR_CaptureVideo_RIFF_GetPosition() - cls.capturevideo.riffstackstartoffset[0];
813 // if this would overflow the windows limit of 1GB per RIFF chunk, we need
814 // to close the current RIFF chunk and open another for future frames
815 if (8 + cursize + framesize > 1<<30)
817 SCR_CaptureVideo_RIFF_Finish();
818 while (cls.capturevideo.riffstacklevel > 0)
819 SCR_CaptureVideo_RIFF_Pop();
820 // begin a new 1GB extended section of the AVI
821 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX");
822 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
826 void SCR_CaptureVideo_BeginVideo(void)
829 int width = vid.width, height = vid.height, x;
831 if (cls.capturevideo.active)
833 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
834 // soundrate is figured out on the first SoundFrame
835 cls.capturevideo.active = true;
836 cls.capturevideo.starttime = Sys_DoubleTime();
837 cls.capturevideo.framerate = bound(1, cl_capturevideo_fps.value, 1000);
838 cls.capturevideo.soundrate = S_GetSoundRate();
839 cls.capturevideo.frame = 0;
840 cls.capturevideo.soundsampleframe = 0;
841 cls.capturevideo.realtime = cl_capturevideo_realtime.integer != 0;
842 cls.capturevideo.buffer = (unsigned char *)Mem_Alloc(tempmempool, vid.width * vid.height * (3+3+3) + 18);
843 gamma = 1.0/scr_screenshot_gammaboost.value;
844 dpsnprintf(cls.capturevideo.basename, sizeof(cls.capturevideo.basename), "video/dpvideo%03i", cl_capturevideo_number.integer);
845 Cvar_SetValueQuick(&cl_capturevideo_number, cl_capturevideo_number.integer + 1);
848 for (i = 0;i < 256;i++)
850 unsigned char j = (unsigned char)bound(0, 255*pow(i/255.0, gamma), 255);
851 cls.capturevideo.rgbgammatable[0][i] = j;
852 cls.capturevideo.rgbgammatable[1][i] = j;
853 cls.capturevideo.rgbgammatable[2][i] = j;
857 R = Y + 1.4075 * (Cr - 128);
858 G = Y + -0.3455 * (Cb - 128) + -0.7169 * (Cr - 128);
859 B = Y + 1.7790 * (Cb - 128);
860 Y = R * .299 + G * .587 + B * .114;
861 Cb = R * -.169 + G * -.332 + B * .500 + 128.;
862 Cr = R * .500 + G * -.419 + B * -.0813 + 128.;
864 for (i = 0;i < 256;i++)
866 g = 255*pow(i/255.0, gamma);
867 // Y weights from RGB
868 cls.capturevideo.rgbtoyuvscaletable[0][0][i] = (short)(g * 0.299);
869 cls.capturevideo.rgbtoyuvscaletable[0][1][i] = (short)(g * 0.587);
870 cls.capturevideo.rgbtoyuvscaletable[0][2][i] = (short)(g * 0.114);
871 // Cb weights from RGB
872 cls.capturevideo.rgbtoyuvscaletable[1][0][i] = (short)(g * -0.169);
873 cls.capturevideo.rgbtoyuvscaletable[1][1][i] = (short)(g * -0.332);
874 cls.capturevideo.rgbtoyuvscaletable[1][2][i] = (short)(g * 0.500);
875 // Cr weights from RGB
876 cls.capturevideo.rgbtoyuvscaletable[2][0][i] = (short)(g * 0.500);
877 cls.capturevideo.rgbtoyuvscaletable[2][1][i] = (short)(g * -0.419);
878 cls.capturevideo.rgbtoyuvscaletable[2][2][i] = (short)(g * -0.0813);
879 // range reduction of YCbCr to valid signal range
880 cls.capturevideo.yuvnormalizetable[0][i] = 16 + i * (236-16) / 256;
881 cls.capturevideo.yuvnormalizetable[1][i] = 16 + i * (240-16) / 256;
882 cls.capturevideo.yuvnormalizetable[2][i] = 16 + i * (240-16) / 256;
885 //if (cl_capturevideo_)
890 cls.capturevideo.format = CAPTUREVIDEOFORMAT_AVI_I420;
891 cls.capturevideo.videofile = FS_Open (va("%s.avi", cls.capturevideo.basename), "wb", false, true);
892 SCR_CaptureVideo_RIFF_Start();
893 // 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)
894 SCR_CaptureVideo_RIFF_Push("RIFF", "AVI ");
896 SCR_CaptureVideo_RIFF_Push("LIST", "hdrl");
897 SCR_CaptureVideo_RIFF_Push("avih", NULL);
898 SCR_CaptureVideo_RIFF_Write32((int)(1000000.0 / cls.capturevideo.framerate)); // microseconds per frame
899 SCR_CaptureVideo_RIFF_Write32(0); // max bytes per second
900 SCR_CaptureVideo_RIFF_Write32(0); // padding granularity
901 SCR_CaptureVideo_RIFF_Write32(0x910); // flags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
902 cls.capturevideo.videofile_totalframes_offset1 = SCR_CaptureVideo_RIFF_GetPosition();
903 SCR_CaptureVideo_RIFF_Write32(0); // total frames
904 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
905 if (cls.capturevideo.soundrate)
906 SCR_CaptureVideo_RIFF_Write32(2); // number of streams
908 SCR_CaptureVideo_RIFF_Write32(1); // number of streams
909 SCR_CaptureVideo_RIFF_Write32(0); // suggested buffer size
910 SCR_CaptureVideo_RIFF_Write32(width); // width
911 SCR_CaptureVideo_RIFF_Write32(height); // height
912 SCR_CaptureVideo_RIFF_Write32(0); // reserved[0]
913 SCR_CaptureVideo_RIFF_Write32(0); // reserved[1]
914 SCR_CaptureVideo_RIFF_Write32(0); // reserved[2]
915 SCR_CaptureVideo_RIFF_Write32(0); // reserved[3]
916 SCR_CaptureVideo_RIFF_Pop();
918 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
919 SCR_CaptureVideo_RIFF_Push("strh", "vids");
920 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // stream fourcc (I420 colorspace, uncompressed)
921 SCR_CaptureVideo_RIFF_Write32(0); // flags
922 SCR_CaptureVideo_RIFF_Write16(0); // priority
923 SCR_CaptureVideo_RIFF_Write16(0); // language
924 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
925 // find an ideal divisor for the framerate
926 for (x = 1;x < 1000;x++)
927 if (cls.capturevideo.framerate * x == floor(cls.capturevideo.framerate * x))
929 SCR_CaptureVideo_RIFF_Write32(x); // samples/second divisor
930 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.framerate * x)); // samples/second multiplied by divisor
931 SCR_CaptureVideo_RIFF_Write32(0); // start
932 cls.capturevideo.videofile_totalframes_offset2 = SCR_CaptureVideo_RIFF_GetPosition();
933 SCR_CaptureVideo_RIFF_Write32(0); // length
934 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // suggested buffer size
935 SCR_CaptureVideo_RIFF_Write32(0); // quality
936 SCR_CaptureVideo_RIFF_Write32(0); // sample size
937 SCR_CaptureVideo_RIFF_Write16(0); // frame left
938 SCR_CaptureVideo_RIFF_Write16(0); // frame top
939 SCR_CaptureVideo_RIFF_Write16(width); // frame right
940 SCR_CaptureVideo_RIFF_Write16(height); // frame bottom
941 SCR_CaptureVideo_RIFF_Pop();
942 // video stream format
943 SCR_CaptureVideo_RIFF_Push("strf", NULL);
944 SCR_CaptureVideo_RIFF_Write32(40); // BITMAPINFO struct size
945 SCR_CaptureVideo_RIFF_Write32(width); // width
946 SCR_CaptureVideo_RIFF_Write32(height); // height
947 SCR_CaptureVideo_RIFF_Write16(3); // planes
948 SCR_CaptureVideo_RIFF_Write16(12); // bitcount
949 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // compression
950 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // size of image
951 SCR_CaptureVideo_RIFF_Write32(0); // x pixels per meter
952 SCR_CaptureVideo_RIFF_Write32(0); // y pixels per meter
953 SCR_CaptureVideo_RIFF_Write32(0); // color used
954 SCR_CaptureVideo_RIFF_Write32(0); // color important
955 SCR_CaptureVideo_RIFF_Pop();
956 SCR_CaptureVideo_RIFF_Pop();
957 if (cls.capturevideo.soundrate)
960 SCR_CaptureVideo_RIFF_Push("LIST", "strl");
961 SCR_CaptureVideo_RIFF_Push("strh", "auds");
962 SCR_CaptureVideo_RIFF_Write32(1); // stream fourcc (PCM audio, uncompressed)
963 SCR_CaptureVideo_RIFF_Write32(0); // flags
964 SCR_CaptureVideo_RIFF_Write16(0); // priority
965 SCR_CaptureVideo_RIFF_Write16(0); // language
966 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
967 SCR_CaptureVideo_RIFF_Write32(1); // samples/second divisor
968 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.soundrate)); // samples/second multiplied by divisor
969 SCR_CaptureVideo_RIFF_Write32(0); // start
970 cls.capturevideo.videofile_totalsampleframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
971 SCR_CaptureVideo_RIFF_Write32(0); // length
972 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 2); // suggested buffer size (this is a half second)
973 SCR_CaptureVideo_RIFF_Write32(0); // quality
974 SCR_CaptureVideo_RIFF_Write32(4); // sample size
975 SCR_CaptureVideo_RIFF_Write16(0); // frame left
976 SCR_CaptureVideo_RIFF_Write16(0); // frame top
977 SCR_CaptureVideo_RIFF_Write16(0); // frame right
978 SCR_CaptureVideo_RIFF_Write16(0); // frame bottom
979 SCR_CaptureVideo_RIFF_Pop();
980 // audio stream format
981 SCR_CaptureVideo_RIFF_Push("strf", NULL);
982 SCR_CaptureVideo_RIFF_Write16(1); // format (uncompressed PCM?)
983 SCR_CaptureVideo_RIFF_Write16(2); // channels (stereo)
984 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate); // sampleframes per second
985 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 4); // average bytes per second
986 SCR_CaptureVideo_RIFF_Write16(4); // block align
987 SCR_CaptureVideo_RIFF_Write16(16); // bits per sample
988 SCR_CaptureVideo_RIFF_Write16(0); // size
989 SCR_CaptureVideo_RIFF_Pop();
990 SCR_CaptureVideo_RIFF_Pop();
992 // close the AVI header list
993 SCR_CaptureVideo_RIFF_Pop();
994 // software that produced this AVI video file
995 SCR_CaptureVideo_RIFF_Push("LIST", "INFO");
996 SCR_CaptureVideo_RIFF_Push("ISFT", NULL);
997 SCR_CaptureVideo_RIFF_WriteTerminatedString(engineversion);
998 SCR_CaptureVideo_RIFF_Pop();
999 // enable this junk filler if you like the LIST movi to always begin at 4KB in the file (why?)
1001 SCR_CaptureVideo_RIFF_Push("JUNK", NULL);
1002 x = 4096 - SCR_CaptureVideo_RIFF_GetPosition();
1005 const char *junkfiller = "[ DarkPlaces junk data ]";
1006 int i = min(x, (int)strlen(junkfiller));
1007 SCR_CaptureVideo_RIFF_WriteBytes((const unsigned char *)junkfiller, i);
1010 SCR_CaptureVideo_RIFF_Pop();
1012 SCR_CaptureVideo_RIFF_Pop();
1013 // begin the actual video section now
1014 SCR_CaptureVideo_RIFF_Push("LIST", "movi");
1015 // we're done with the headers now...
1016 SCR_CaptureVideo_RIFF_Flush();
1017 if (cls.capturevideo.riffstacklevel != 2)
1018 Sys_Error("SCR_CaptureVideo_BeginVideo: broken AVI writing code (stack level is %i (should be 2) at end of headers)\n", cls.capturevideo.riffstacklevel);
1021 switch(cls.capturevideo.format)
1023 case CAPTUREVIDEOFORMAT_AVI_I420:
1030 void SCR_CaptureVideo_EndVideo(void)
1032 if (!cls.capturevideo.active)
1034 cls.capturevideo.active = false;
1035 if (cls.capturevideo.videofile)
1037 switch(cls.capturevideo.format)
1039 case CAPTUREVIDEOFORMAT_AVI_I420:
1040 // close any open chunks
1041 SCR_CaptureVideo_RIFF_Finish();
1042 // go back and fix the video frames and audio samples fields
1043 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset1, SEEK_SET);
1044 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1045 SCR_CaptureVideo_RIFF_Flush();
1046 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalframes_offset2, SEEK_SET);
1047 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
1048 SCR_CaptureVideo_RIFF_Flush();
1049 if (cls.capturevideo.soundrate)
1051 FS_Seek(cls.capturevideo.videofile, cls.capturevideo.videofile_totalsampleframes_offset, SEEK_SET);
1052 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundsampleframe);
1053 SCR_CaptureVideo_RIFF_Flush();
1059 FS_Close(cls.capturevideo.videofile);
1060 cls.capturevideo.videofile = NULL;
1063 if (cls.capturevideo.buffer)
1065 Mem_Free (cls.capturevideo.buffer);
1066 cls.capturevideo.buffer = NULL;
1069 if (cls.capturevideo.riffindexbuffer.data)
1071 Mem_Free(cls.capturevideo.riffindexbuffer.data);
1072 cls.capturevideo.riffindexbuffer.data = NULL;
1075 memset(&cls.capturevideo, 0, sizeof(cls.capturevideo));
1078 // converts from RGB24 to I420 colorspace (identical to YV12 except chroma plane order is reversed), this colorspace is handled by the Intel(r) 4:2:0 codec on Windows
1079 void SCR_CaptureVideo_ConvertFrame_RGB_to_I420_flip(int width, int height, unsigned char *instart, unsigned char *outstart)
1082 int outoffset = (width/2)*(height/2);
1083 unsigned char *b, *out;
1084 // process one line at a time, and CbCr every other line at 2 pixel intervals
1085 for (y = 0;y < height;y++)
1088 for (b = instart + (height-1-y)*width*3, out = outstart + y*width, x = 0;x < width;x++, b += 3, out++)
1089 *out = cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[0][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[0][2][b[2]]];
1092 // 2x2 Cr and Cb planes
1094 // low quality, no averaging
1095 for (b = instart + (height-2-y)*width*3, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 6, out++)
1098 out[0 ] = cls.capturevideo.yuvnormalizetable[1][cls.capturevideo.rgbtoyuvscaletable[1][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[1][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[1][2][b[2]] + 128];
1100 out[outoffset] = cls.capturevideo.yuvnormalizetable[2][cls.capturevideo.rgbtoyuvscaletable[2][0][b[0]] + cls.capturevideo.rgbtoyuvscaletable[2][1][b[1]] + cls.capturevideo.rgbtoyuvscaletable[2][2][b[2]] + 128];
1103 // high quality, averaging
1104 int inpitch = width*3;
1105 for (b = instart + (height-2-y)*width*3, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 6, out++)
1107 int blockr, blockg, blockb;
1108 blockr = (b[0] + b[3] + b[inpitch+0] + b[inpitch+3]) >> 2;
1109 blockg = (b[1] + b[4] + b[inpitch+1] + b[inpitch+4]) >> 2;
1110 blockb = (b[2] + b[5] + b[inpitch+2] + b[inpitch+5]) >> 2;
1112 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];
1114 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];
1121 qboolean SCR_CaptureVideo_VideoFrame(int newframenum)
1123 int x = 0, y = 0, width = vid.width, height = vid.height;
1124 unsigned char *in, *out;
1126 //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);
1127 // speed is critical here, so do saving as directly as possible
1128 switch (cls.capturevideo.format)
1130 case CAPTUREVIDEOFORMAT_AVI_I420:
1131 // if there's no videofile we have to just give up, and abort saving
1132 if (!cls.capturevideo.videofile)
1134 // FIXME: width/height must be multiple of 2, enforce this?
1135 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, cls.capturevideo.buffer);CHECKGLERROR
1136 in = cls.capturevideo.buffer;
1137 out = cls.capturevideo.buffer + width*height*3;
1138 SCR_CaptureVideo_ConvertFrame_RGB_to_I420_flip(width, height, in, out);
1139 x = width*height+(width/2)*(height/2)*2;
1140 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1141 for (;cls.capturevideo.frame < newframenum;cls.capturevideo.frame++)
1143 SCR_CaptureVideo_RIFF_IndexEntry("00dc", x, 0x10); // AVIIF_KEYFRAME
1144 SCR_CaptureVideo_RIFF_Push("00dc", NULL);
1145 SCR_CaptureVideo_RIFF_WriteBytes(out, x);
1146 SCR_CaptureVideo_RIFF_Pop();
1154 void SCR_CaptureVideo_SoundFrame(unsigned char *bufstereo16le, size_t length, int rate)
1157 cls.capturevideo.soundrate = rate;
1158 cls.capturevideo.soundsampleframe += length;
1159 switch (cls.capturevideo.format)
1161 case CAPTUREVIDEOFORMAT_AVI_I420:
1163 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
1164 SCR_CaptureVideo_RIFF_IndexEntry("01wb", x, 0x10); // AVIIF_KEYFRAME
1165 SCR_CaptureVideo_RIFF_Push("01wb", NULL);
1166 SCR_CaptureVideo_RIFF_WriteBytes(bufstereo16le, x);
1167 SCR_CaptureVideo_RIFF_Pop();
1174 void SCR_CaptureVideo(void)
1177 if (cl_capturevideo.integer && r_render.integer)
1179 if (!cls.capturevideo.active)
1180 SCR_CaptureVideo_BeginVideo();
1181 if (cls.capturevideo.framerate != cl_capturevideo_fps.value)
1183 Con_Printf("You can not change the video framerate while recording a video.\n");
1184 Cvar_SetValueQuick(&cl_capturevideo_fps, cls.capturevideo.framerate);
1186 // for AVI saving we have to make sure that sound is saved before video
1187 if (cls.capturevideo.soundrate && !cls.capturevideo.soundsampleframe)
1189 if (cls.capturevideo.realtime)
1191 // preserve sound sync by duplicating frames when running slow
1192 newframenum = (int)((Sys_DoubleTime() - cls.capturevideo.starttime) * cls.capturevideo.framerate);
1195 newframenum = cls.capturevideo.frame + 1;
1196 // if falling behind more than one second, stop
1197 if (newframenum - cls.capturevideo.frame > (int)ceil(cls.capturevideo.framerate))
1199 Cvar_SetValueQuick(&cl_capturevideo, 0);
1200 Con_Printf("video saving failed on frame %i, your machine is too slow for this capture speed.\n", cls.capturevideo.frame);
1201 SCR_CaptureVideo_EndVideo();
1205 SCR_CaptureVideo_VideoFrame(newframenum);
1206 if (cls.capturevideo.error)
1208 Cvar_SetValueQuick(&cl_capturevideo, 0);
1209 Con_Printf("video saving failed on frame %i, out of disk space? stopping video capture.\n", cls.capturevideo.frame);
1210 SCR_CaptureVideo_EndVideo();
1213 else if (cls.capturevideo.active)
1214 SCR_CaptureVideo_EndVideo();
1221 Grab six views for environment mapping tests
1228 qboolean flipx, flipy, flipdiagonaly;
1232 {{ 0, 0, 0}, "rt", false, false, false},
1233 {{ 0, 270, 0}, "ft", false, false, false},
1234 {{ 0, 180, 0}, "lf", false, false, false},
1235 {{ 0, 90, 0}, "bk", false, false, false},
1236 {{-90, 180, 0}, "up", true, true, false},
1237 {{ 90, 180, 0}, "dn", true, true, false},
1239 {{ 0, 0, 0}, "px", true, true, true},
1240 {{ 0, 90, 0}, "py", false, true, false},
1241 {{ 0, 180, 0}, "nx", false, false, true},
1242 {{ 0, 270, 0}, "ny", true, false, false},
1243 {{-90, 180, 0}, "pz", false, false, true},
1244 {{ 90, 180, 0}, "nz", false, false, true}
1247 static void R_Envmap_f (void)
1250 char filename[MAX_QPATH], basename[MAX_QPATH];
1251 unsigned char *buffer1;
1252 unsigned char *buffer2;
1253 unsigned char *buffer3;
1255 if (Cmd_Argc() != 3)
1257 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");
1261 strlcpy (basename, Cmd_Argv(1), sizeof (basename));
1262 size = atoi(Cmd_Argv(2));
1263 if (size != 128 && size != 256 && size != 512 && size != 1024)
1265 Con_Print("envmap: size must be one of 128, 256, 512, or 1024\n");
1268 if (size > vid.width || size > vid.height)
1270 Con_Print("envmap: your resolution is not big enough to render that size\n");
1274 r_refdef.envmap = true;
1276 R_UpdateVariables();
1281 r_view.width = size;
1282 r_view.height = size;
1285 r_view.frustum_x = tan(90 * M_PI / 360.0);
1286 r_view.frustum_y = tan(90 * M_PI / 360.0);
1288 buffer1 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1289 buffer2 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3);
1290 buffer3 = (unsigned char *)Mem_Alloc(tempmempool, size * size * 3 + 18);
1292 for (j = 0;j < 12;j++)
1294 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1295 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);
1300 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);
1307 r_refdef.envmap = false;
1310 //=============================================================================
1312 // LordHavoc: SHOWLMP stuff
1313 #define SHOWLMP_MAXLABELS 256
1314 typedef struct showlmp_s
1324 showlmp_t showlmp[SHOWLMP_MAXLABELS];
1326 void SHOWLMP_decodehide(void)
1330 lmplabel = MSG_ReadString();
1331 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1332 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
1334 showlmp[i].isactive = false;
1339 void SHOWLMP_decodeshow(void)
1342 char lmplabel[256], picname[256];
1344 strlcpy (lmplabel,MSG_ReadString(), sizeof (lmplabel));
1345 strlcpy (picname, MSG_ReadString(), sizeof (picname));
1346 if (gamemode == GAME_NEHAHRA) // LordHavoc: nasty old legacy junk
1353 x = MSG_ReadShort();
1354 y = MSG_ReadShort();
1357 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1358 if (showlmp[i].isactive)
1360 if (strcmp(showlmp[i].label, lmplabel) == 0)
1363 break; // drop out to replace it
1366 else if (k < 0) // find first empty one to replace
1369 return; // none found to replace
1370 // change existing one
1371 showlmp[k].isactive = true;
1372 strlcpy (showlmp[k].label, lmplabel, sizeof (showlmp[k].label));
1373 strlcpy (showlmp[k].pic, picname, sizeof (showlmp[k].pic));
1378 void SHOWLMP_drawall(void)
1381 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1382 if (showlmp[i].isactive)
1383 DrawQ_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic, true), 0, 0, 1, 1, 1, 1, 0);
1386 void SHOWLMP_clear(void)
1389 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
1390 showlmp[i].isactive = false;
1394 ==============================================================================
1398 ==============================================================================
1401 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)
1403 int indices[3] = {0,1,2};
1406 if (!r_render.integer)
1410 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer1);CHECKGLERROR
1412 if (scr_screenshot_gammaboost.value != 1 && gammacorrect)
1415 double igamma = 1.0 / scr_screenshot_gammaboost.value;
1416 unsigned char ramp[256];
1417 for (i = 0;i < 256;i++)
1418 ramp[i] = (unsigned char) (pow(i * (1.0 / 255.0), igamma) * 255.0);
1419 for (i = 0;i < width*height*3;i++)
1420 buffer1[i] = ramp[buffer1[i]];
1423 Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
1426 ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
1428 ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer2, buffer3);
1433 //=============================================================================
1435 void R_ClearScreen(void)
1439 if (r_refdef.fogenabled)
1441 qglClearColor(r_refdef.fogcolor[0],r_refdef.fogcolor[1],r_refdef.fogcolor[2],0);CHECKGLERROR
1445 qglClearColor(0,0,0,0);CHECKGLERROR
1447 qglClearDepth(1);CHECKGLERROR
1450 // LordHavoc: we use a stencil centered around 128 instead of 0,
1451 // to avoid clamping interfering with strange shadow volume
1453 qglClearStencil(128);CHECKGLERROR
1456 if (r_render.integer)
1457 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));
1458 // set dithering mode
1459 if (gl_dither.integer)
1461 qglEnable(GL_DITHER);CHECKGLERROR
1465 qglDisable(GL_DITHER);CHECKGLERROR
1469 qboolean CL_VM_UpdateView (void);
1470 void SCR_DrawConsole (void);
1471 void R_Shadow_EditLights_DrawSelectedLightProperties(void);
1475 void SCR_DrawScreen (void)
1479 if (r_timereport_active)
1480 R_TimeReport("setup");
1482 R_UpdateVariables();
1484 if (cls.signon == SIGNONS)
1488 size = scr_viewsize.value * (1.0 / 100.0);
1489 size = min(size, 1);
1491 if (r_stereo_sidebyside.integer)
1493 r_view.width = (int)(vid.width * size / 2.5);
1494 r_view.height = (int)(vid.height * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100));
1496 r_view.x = (int)((vid.width - r_view.width * 2.5) * 0.5);
1497 r_view.y = (int)((vid.height - r_view.height)/2);
1500 r_view.x += (int)(r_view.width * 1.5);
1504 r_view.width = (int)(vid.width * size);
1505 r_view.height = (int)(vid.height * size * (1 - bound(0, r_letterbox.value, 100) / 100));
1507 r_view.x = (int)((vid.width - r_view.width)/2);
1508 r_view.y = (int)((vid.height - r_view.height)/2);
1512 // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
1513 // LordHavoc: this is designed to produce widescreen fov values
1514 // when the screen is wider than 4/3 width/height aspect, to do
1515 // this it simply assumes the requested fov is the vertical fov
1516 // for a 4x3 display, if the ratio is not 4x3 this makes the fov
1517 // higher/lower according to the ratio
1518 r_view.frustum_y = tan(scr_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
1519 r_view.frustum_x = r_view.frustum_y * (float)r_view.width / (float)r_view.height / vid_pixelheight.value;
1521 r_view.frustum_x *= r_refdef.frustumscale_x;
1522 r_view.frustum_y *= r_refdef.frustumscale_y;
1524 if(!CL_VM_UpdateView())
1529 if (scr_zoomwindow.integer)
1531 float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
1532 float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
1533 r_view.width = (int)(vid.width * sizex);
1534 r_view.height = (int)(vid.height * sizey);
1536 r_view.x = (int)((vid.width - r_view.width)/2);
1540 r_view.frustum_y = tan(scr_zoomwindow_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
1541 r_view.frustum_x = r_view.frustum_y * vid_pixelheight.value * (float)r_view.width / (float)r_view.height;
1543 r_view.frustum_x *= r_refdef.frustumscale_x;
1544 r_view.frustum_y *= r_refdef.frustumscale_y;
1546 if(!CL_VM_UpdateView())
1551 if (!r_stereo_sidebyside.integer)
1553 r_view.width = vid.width;
1554 r_view.height = vid.height;
1563 //FIXME: force menu if nothing else to look at?
1564 //if (key_dest == key_game && cls.signon != SIGNONS && cls.state == ca_disconnected)
1566 if (cls.signon == SIGNONS)
1571 if (!r_letterbox.value)
1574 SCR_CheckDrawCenterString();
1578 R_Shadow_EditLights_DrawSelectedLightProperties();
1587 if (r_timereport_active)
1590 if (cls.signon == SIGNONS)
1591 R_TimeReport_Frame();
1599 if (r_timereport_active)
1600 R_TimeReport("meshfinish");
1603 void SCR_UpdateLoadingScreen (qboolean clear)
1608 float texcoord2f[8];
1609 // don't do anything if not initialized yet
1613 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
1614 //qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
1615 //qglDepthMask(1);CHECKGLERROR
1616 qglColorMask(1,1,1,1);CHECKGLERROR
1617 qglClearColor(0,0,0,0);CHECKGLERROR
1618 // when starting up a new video mode, make sure the screen is cleared to black
1621 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
1623 //qglDisable(GL_CULL_FACE);CHECKGLERROR
1626 GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
1628 R_Mesh_Matrix(&identitymatrix);
1629 // draw the loading plaque
1630 pic = Draw_CachePic("gfx/loading", true);
1631 x = (vid_conwidth.integer - pic->width)/2;
1632 y = (vid_conheight.integer - pic->height)/2;
1634 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1635 GL_DepthTest(false);
1636 R_Mesh_VertexPointer(vertex3f);
1637 R_Mesh_ColorPointer(NULL);
1638 R_Mesh_ResetTextureState();
1639 R_Mesh_TexBind(0, R_GetTexture(pic->tex));
1640 R_Mesh_TexCoordPointer(0, 2, texcoord2f);
1641 vertex3f[2] = vertex3f[5] = vertex3f[8] = vertex3f[11] = 0;
1642 vertex3f[0] = vertex3f[9] = x;
1643 vertex3f[1] = vertex3f[4] = y;
1644 vertex3f[3] = vertex3f[6] = x + pic->width;
1645 vertex3f[7] = vertex3f[10] = y + pic->height;
1646 texcoord2f[0] = 0;texcoord2f[1] = 0;
1647 texcoord2f[2] = 1;texcoord2f[3] = 0;
1648 texcoord2f[4] = 1;texcoord2f[5] = 1;
1649 texcoord2f[6] = 0;texcoord2f[7] = 1;
1650 if (vid.stereobuffer)
1652 qglDrawBuffer(GL_FRONT_LEFT);
1653 R_Mesh_Draw(0, 4, 2, polygonelements);
1654 qglDrawBuffer(GL_FRONT_RIGHT);
1655 R_Mesh_Draw(0, 4, 2, polygonelements);
1659 qglDrawBuffer(GL_FRONT);
1660 R_Mesh_Draw(0, 4, 2, polygonelements);
1664 // not necessary when rendering to GL_FRONT buffers
1665 //VID_Finish(false);
1668 void CL_UpdateScreen(void)
1670 float conwidth, conheight;
1675 if (!scr_initialized || !con_initialized || vid_hidden)
1676 return; // not initialized yet
1678 // don't allow cheats in multiplayer
1679 if (!cl.islocalgame && cl.worldmodel)
1681 if (r_fullbright.integer != 0)
1682 Cvar_Set ("r_fullbright", "0");
1683 if (r_ambient.value != 0)
1684 Cvar_Set ("r_ambient", "0");
1687 conwidth = bound(320, vid_conwidth.value, 2048);
1688 conheight = bound(200, vid_conheight.value, 1536);
1689 if (vid_conwidth.value != conwidth)
1690 Cvar_SetValue("vid_conwidth", conwidth);
1691 if (vid_conheight.value != conheight)
1692 Cvar_SetValue("vid_conheight", conheight);
1695 if (scr_viewsize.value < 30)
1696 Cvar_Set ("viewsize","30");
1697 if (scr_viewsize.value > 120)
1698 Cvar_Set ("viewsize","120");
1700 // bound field of view
1701 if (scr_fov.value < 1)
1702 Cvar_Set ("fov","1");
1703 if (scr_fov.value > 170)
1704 Cvar_Set ("fov","170");
1706 // validate r_textureunits cvar
1707 if (r_textureunits.integer > gl_textureunits)
1708 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
1709 if (r_textureunits.integer < 1)
1710 Cvar_SetValueQuick(&r_textureunits, 1);
1712 // validate gl_combine cvar
1713 if (gl_combine.integer && !gl_combine_extension)
1714 Cvar_SetValueQuick(&gl_combine, 0);
1716 // intermission is always full screen
1717 if (cl.intermission)
1721 if (scr_viewsize.value >= 120)
1722 sb_lines = 0; // no status bar at all
1723 else if (scr_viewsize.value >= 110)
1724 sb_lines = 24; // no inventory
1729 r_view.colormask[0] = 1;
1730 r_view.colormask[1] = 1;
1731 r_view.colormask[2] = 1;
1733 if (r_timereport_active)
1734 R_TimeReport("other");
1736 SCR_SetUpToDrawConsole();
1738 if (r_timereport_active)
1739 R_TimeReport("start");
1742 qglViewport(0, 0, vid.width, vid.height);CHECKGLERROR
1743 qglDisable(GL_SCISSOR_TEST);CHECKGLERROR
1744 qglDepthMask(1);CHECKGLERROR
1745 qglColorMask(1,1,1,1);CHECKGLERROR
1746 qglClearColor(0,0,0,0);CHECKGLERROR
1747 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
1749 if(scr_stipple.integer)
1751 GLubyte stipple[128];
1752 int i, s, width, parts;
1753 static int frame = 0;
1756 s = scr_stipple.integer;
1758 width = (s & 070) >> 3;
1760 qglEnable(GL_POLYGON_STIPPLE); // 0x0B42
1761 for(i = 0; i < 128; ++i)
1764 stipple[i] = (((line >> width) + frame) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
1766 qglPolygonStipple(stipple);
1769 qglDisable(GL_POLYGON_STIPPLE);
1771 if (r_timereport_active)
1772 R_TimeReport("clear");
1774 if (vid.stereobuffer || r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
1776 matrix4x4_t originalmatrix = r_view.matrix;
1777 matrix4x4_t offsetmatrix;
1778 Matrix4x4_CreateTranslate(&offsetmatrix, 0, r_stereo_separation.value * -0.5f, 0);
1779 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
1781 if (r_stereo_sidebyside.integer)
1784 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
1786 r_view.colormask[0] = 1;
1787 r_view.colormask[1] = 0;
1788 r_view.colormask[2] = 0;
1791 if (vid.stereobuffer)
1792 qglDrawBuffer(GL_BACK_RIGHT);
1796 Matrix4x4_CreateTranslate(&offsetmatrix, 0, r_stereo_separation.value * 0.5f, 0);
1797 Matrix4x4_Concat(&r_view.matrix, &originalmatrix, &offsetmatrix);
1799 if (r_stereo_sidebyside.integer)
1802 if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
1804 r_view.colormask[0] = 0;
1805 r_view.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
1806 r_view.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
1809 if (vid.stereobuffer)
1810 qglDrawBuffer(GL_BACK_LEFT);
1814 r_view.matrix = originalmatrix;
1818 qglDrawBuffer(GL_BACK);
1825 if (r_timereport_active)
1826 R_TimeReport("finish");
1829 void CL_Screen_NewMap(void)