2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 extern cvar_t cl_capturevideo;
26 void CL_FinishTimeDemo (void);
29 ==============================================================================
33 When a demo is playing back, all outgoing network messages are skipped, and
34 incoming messages are read from the demo file.
36 Whenever cl.time gets past the last received message, another message is
37 read from the demo file.
38 ==============================================================================
45 Called to play the next demo in the demo loop
48 void CL_NextDemo (void)
50 char str[MAX_INPUTLINE];
52 if (cls.demonum == -1)
53 return; // don't play demos
55 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
58 if (!cls.demos[cls.demonum][0])
60 Con_Print("No demos listed with startdemos\n");
66 dpsnprintf (str, sizeof(str), "playdemo %s\n", cls.demos[cls.demonum]);
67 Cbuf_InsertText (str);
75 Called when a demo file runs out, or the user starts a game
78 // LordHavoc: now called only by CL_Disconnect
79 void CL_StopPlayback (void)
81 if (!cls.demoplayback)
84 FS_Close (cls.demofile);
85 cls.demoplayback = false;
91 if (!cls.demostarting) // only quit if not starting another demo
92 if (COM_CheckParm("-demo") || COM_CheckParm("-capturedemo"))
101 Dumps the current net message, prefixed by the length and view angles
102 #====================
104 void CL_WriteDemoMessage (sizebuf_t *message)
110 if (cls.demopaused) // LordHavoc: pausedemo
113 len = LittleLong (message->cursize);
114 FS_Write (cls.demofile, &len, 4);
115 for (i=0 ; i<3 ; i++)
117 f = LittleFloat (cl.viewangles[i]);
118 FS_Write (cls.demofile, &f, 4);
120 FS_Write (cls.demofile, message->data, message->cursize);
127 Dumps the current demo to a buffer, and resets the demo to its starting point.
128 Used to insert csprogs.dat files as a download to the beginning of a demo file.
131 void CL_CutDemo (unsigned char **buf, fs_offset_t *filesize)
136 FS_Close(cls.demofile);
137 *buf = FS_LoadFile(cls.demoname, tempmempool, false, filesize);
139 // restart the demo recording
140 cls.demofile = FS_OpenRealFile(cls.demoname, "wb", false);
142 Host_Error("failed to reopen the demo file");
143 FS_Printf(cls.demofile, "%i\n", cls.forcetrack);
150 Adds the cut stuff back to the demo. Also frees the buffer.
151 Used to insert csprogs.dat files as a download to the beginning of a demo file.
154 void CL_PasteDemo (unsigned char **buf, fs_offset_t *filesize)
156 fs_offset_t startoffset = 0;
162 while(startoffset < *filesize && ((char *)(*buf))[startoffset] != '\n')
164 if(startoffset < *filesize)
167 FS_Write(cls.demofile, *buf + startoffset, *filesize - startoffset);
178 Handles playback of demos
181 void CL_ReadDemoMessage(void)
186 if (!cls.demoplayback)
189 // LordHavoc: pausedemo
195 // decide if it is time to grab the next message
196 // always grab until fully connected
197 if (cls.signon == SIGNONS)
202 cls.td_onesecondframes++;
203 // if this is the first official frame we can now grab the real
204 // td_starttime so the bogus time on the first frame doesn't
205 // count against the final report
206 if (cls.td_frames == 0)
208 cls.td_starttime = realtime;
209 cls.td_onesecondnexttime = cl.time + 1;
210 cls.td_onesecondrealtime = realtime;
211 cls.td_onesecondframes = 0;
212 cls.td_onesecondminfps = 0;
213 cls.td_onesecondmaxfps = 0;
214 cls.td_onesecondavgfps = 0;
215 cls.td_onesecondavgcount = 0;
217 if (cl.time >= cls.td_onesecondnexttime)
219 double fps = cls.td_onesecondframes / (realtime - cls.td_onesecondrealtime);
220 if (cls.td_onesecondavgcount == 0)
222 cls.td_onesecondminfps = fps;
223 cls.td_onesecondmaxfps = fps;
225 cls.td_onesecondrealtime = realtime;
226 cls.td_onesecondminfps = min(cls.td_onesecondminfps, fps);
227 cls.td_onesecondmaxfps = max(cls.td_onesecondmaxfps, fps);
228 cls.td_onesecondavgfps += fps;
229 cls.td_onesecondavgcount++;
230 cls.td_onesecondframes = 0;
231 cls.td_onesecondnexttime++;
234 else if (cl.time <= cl.mtime[0])
236 // don't need another message yet
241 // get the next message
242 FS_Read(cls.demofile, &net_message.cursize, 4);
243 net_message.cursize = LittleLong(net_message.cursize);
244 if(net_message.cursize & DEMOMSG_CLIENT_TO_SERVER) // This is a client->server message! Ignore for now!
246 // skip over demo packet
247 FS_Seek(cls.demofile, 12 + (net_message.cursize & (~DEMOMSG_CLIENT_TO_SERVER)), SEEK_CUR);
250 if (net_message.cursize > net_message.maxsize)
251 Host_Error("Demo message (%i) > net_message.maxsize (%i)", net_message.cursize, net_message.maxsize);
252 VectorCopy(cl.mviewangles[0], cl.mviewangles[1]);
253 for (i = 0;i < 3;i++)
255 FS_Read(cls.demofile, &f, 4);
256 cl.mviewangles[0][i] = LittleFloat(f);
259 if (FS_Read(cls.demofile, net_message.data, net_message.cursize) == net_message.cursize)
262 CL_ParseServerMessage();
264 if (cls.signon != SIGNONS)
265 Cbuf_Execute(); // immediately execute svc_stufftext if in the demo before connect!
267 // In case the demo contains a "svc_disconnect" message
268 if (!cls.demoplayback)
287 stop recording a demo
290 void CL_Stop_f (void)
293 unsigned char bufdata[64];
295 if (!cls.demorecording)
297 Con_Print("Not recording a demo.\n");
301 // write a disconnect message to the demo file
302 // LordHavoc: don't replace the net_message when doing this
304 buf.maxsize = sizeof(bufdata);
306 MSG_WriteByte(&buf, svc_disconnect);
307 CL_WriteDemoMessage(&buf);
310 if(cl_autodemo.integer && (cl_autodemo_delete.integer & 1))
312 FS_RemoveOnClose(cls.demofile);
313 Con_Print("Completed and deleted demo\n");
316 Con_Print("Completed demo\n");
317 FS_Close (cls.demofile);
319 cls.demorecording = false;
326 record <demoname> <map> [cd track]
329 void CL_Record_f (void)
332 char name[MAX_OSPATH];
335 if (c != 2 && c != 3 && c != 4)
337 Con_Print("record <demoname> [<map> [cd track]]\n");
341 if (strstr(Cmd_Argv(1), ".."))
343 Con_Print("Relative pathnames are not allowed.\n");
347 if (c == 2 && cls.state == ca_connected)
349 Con_Print("Can not record - already connected to server\nClient demo recording must be started before connecting\n");
353 if (cls.state == ca_connected)
356 // write the forced cd track number, or -1
359 track = atoi(Cmd_Argv(3));
360 Con_Printf("Forcing CD track to %i\n", cls.forcetrack);
366 strlcpy (name, Cmd_Argv(1), sizeof (name));
367 FS_DefaultExtension (name, ".dem", sizeof (name));
371 Cmd_ExecuteString ( va("map %s", Cmd_Argv(2)), src_command);
373 // open the demo file
374 Con_Printf("recording to %s.\n", name);
375 cls.demofile = FS_OpenRealFile(name, "wb", false);
378 Con_Print("ERROR: couldn't open.\n");
381 strlcpy(cls.demoname, name, sizeof(cls.demoname));
383 cls.forcetrack = track;
384 FS_Printf(cls.demofile, "%i\n", cls.forcetrack);
386 cls.demorecording = true;
387 cls.demo_lastcsprogssize = -1;
388 cls.demo_lastcsprogscrc = -1;
399 void CL_PlayDemo_f (void)
401 char name[MAX_QPATH];
403 qboolean neg = false;
408 Con_Print("play <demoname> : plays a demo\n");
412 // open the demo file
413 strlcpy (name, Cmd_Argv(1), sizeof (name));
414 FS_DefaultExtension (name, ".dem", sizeof (name));
415 f = FS_OpenVirtualFile(name, false);
418 Con_Printf("ERROR: couldn't open %s.\n", name);
419 cls.demonum = -1; // stop demo loop
423 cls.demostarting = true;
425 // disconnect from server
427 Host_ShutdownServer ();
429 // update networking ports (this is mainly just needed at startup)
430 NetConn_UpdateSockets();
432 cls.protocol = PROTOCOL_QUAKE;
434 Con_Printf("Playing demo %s.\n", name);
436 strlcpy(cls.demoname, name, sizeof(cls.demoname));
438 cls.demoplayback = true;
439 cls.state = ca_connected;
442 while ((c = FS_Getc (cls.demofile)) != '\n')
446 cls.forcetrack = cls.forcetrack * 10 + (c - '0');
449 cls.forcetrack = -cls.forcetrack;
451 cls.demostarting = false;
460 void CL_FinishTimeDemo (void)
464 double time, totalfpsavg;
465 double fpsmin, fpsavg, fpsmax; // report min/avg/max fps
466 static int benchmark_runs = 0;
468 cls.timedemo = false;
470 frames = cls.td_frames;
471 time = realtime - cls.td_starttime;
472 totalfpsavg = time > 0 ? frames / time : 0;
473 fpsmin = cls.td_onesecondminfps;
474 fpsavg = cls.td_onesecondavgcount ? cls.td_onesecondavgfps / cls.td_onesecondavgcount : 0;
475 fpsmax = cls.td_onesecondmaxfps;
476 // LordHavoc: timedemo now prints out 7 digits of fraction, and min/avg/max
477 Con_Printf("%i frames %5.7f seconds %5.7f fps, one-second fps min/avg/max: %.0f %.0f %.0f (%i seconds)\n", frames, time, totalfpsavg, fpsmin, fpsavg, fpsmax, cls.td_onesecondavgcount);
478 Log_Printf("benchmark.log", "date %s | enginedate %s | demo %s | commandline %s | run %d | result %i frames %5.7f seconds %5.7f fps, one-second fps min/avg/max: %.0f %.0f %.0f (%i seconds)\n", Sys_TimeString("%Y-%m-%d %H:%M:%S"), buildstring, cls.demoname, cmdline.string, benchmark_runs + 1, frames, time, totalfpsavg, fpsmin, fpsavg, fpsmax, cls.td_onesecondavgcount);
479 if (COM_CheckParm("-benchmark"))
482 i = COM_CheckParm("-benchmarkruns");
483 if(i && i + 1 < com_argc)
485 if(atoi(com_argv[i + 1]) > benchmark_runs)
487 // restart the benchmark
488 Cbuf_AddText(va("timedemo %s\n", cls.demoname));
489 // cannot execute here
506 void CL_TimeDemo_f (void)
510 Con_Print("timedemo <demoname> : gets demo speeds\n");
514 srand(0); // predictable random sequence for benchmarking
518 // cls.td_starttime will be grabbed at the second frame of the demo, so
519 // all the loading time doesn't get counted
521 // instantly hide console and deactivate it
523 key_consoleactive = 0;
527 cls.td_frames = -2; // skip the first frame
528 cls.demonum = -1; // stop demo loop
529 cls.demonum = -1; // stop demo loop