/
dd_loop.cpp
424 lines (353 loc) · 11.6 KB
/
dd_loop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/** @file dd_loop.cpp Main loop and the core timer.
* @ingroup base
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_platform.h"
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_render.h"
#include "de_play.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_ui.h"
#include "de_misc.h"
#include <de/App>
#ifdef __SERVER__
# include <de/TextApp>
#endif
#ifdef __CLIENT__
# include "ui/busyvisual.h"
# include "ui/clientwindow.h"
#endif
/// Development utility: on sharp tics, print player 0 movement state.
//#define LIBDENG_PLAYER0_MOVEMENT_ANALYSIS
/**
* There needs to be at least this many tics per second. A smaller value
* is likely to cause unpredictable changes in playsim.
*/
#define MIN_TIC_RATE 35
/**
* The length of one tic can be at most this.
*/
#define MAX_FRAME_TIME (1.0/MIN_TIC_RATE)
int maxFrameRate = 120; // Zero means 'unlimited'.
// Refresh frame count (independant of the viewport-specific frameCount).
int rFrameCount = 0;
byte devShowFrameTimeDeltas = false;
byte processSharpEventsAfterTickers = true;
timespan_t sysTime, gameTime, demoTime, ddMapTime;
//timespan_t frameStartTime;
boolean stopTime = false; // If true the time counters won't be incremented
boolean tickUI = false; // If true the UI will be tick'd
boolean tickFrame = true; // If false frame tickers won't be tick'd (unless netGame)
static int gameLoopExitCode = 0;
static double lastRunTicsTime;
static boolean firstTic = true;
static boolean tickIsSharp = false;
#define NUM_FRAMETIME_DELTAS 200
static int timeDeltas[NUM_FRAMETIME_DELTAS];
static int timeDeltasIndex = 0;
static float realFrameTimePos = 0;
void DD_RegisterLoop(void)
{
C_VAR_BYTE("input-sharp-lateprocessing", &processSharpEventsAfterTickers, 0, 0, 1);
C_VAR_INT ("refresh-rate-maximum", &maxFrameRate, 0, 35, 1000);
C_VAR_INT ("rend-dev-framecount", &rFrameCount, CVF_NO_ARCHIVE | CVF_PROTECTED, 0, 0);
C_VAR_BYTE("rend-info-deltas-frametime", &devShowFrameTimeDeltas, CVF_NO_ARCHIVE, 0, 1);
}
void DD_SetGameLoopExitCode(int code)
{
gameLoopExitCode = code;
}
int DD_GameLoopExitCode(void)
{
return gameLoopExitCode;
}
float DD_GetFrameRate()
{
#ifdef __CLIENT__
return ClientWindow::main().frameRate();
#else
return 0;
#endif
}
#undef DD_IsSharpTick
DENG_EXTERN_C boolean DD_IsSharpTick(void)
{
return tickIsSharp;
}
boolean DD_IsFrameTimeAdvancing(void)
{
if(BusyMode_Active()) return false;
return tickFrame || netGame;
}
void DD_CheckSharpTick(timespan_t time)
{
// Sharp ticks are the ones that occur 35 per second. The rest are
// interpolated (smoothed) somewhere in between.
tickIsSharp = false;
if(DD_IsFrameTimeAdvancing())
{
/**
* realFrameTimePos will be reduced when new sharp world positions are
* calculated, so that frametime always stays within the range 0..1.
*/
realFrameTimePos += time * TICSPERSEC;
// When one full tick has passed, it is time to do a sharp tick.
if(realFrameTimePos >= 1)
{
tickIsSharp = true;
}
}
}
/**
* This is the main ticker of the engine. We'll call all the other tickers
* from here.
*
* @param time Duration of the tick. This will never be longer than 1.0/TICSPERSEC.
*/
static void baseTicker(timespan_t time)
{
if(DD_IsFrameTimeAdvancing())
{
#ifdef __CLIENT__
// Demo ticker. Does stuff like smoothing of view angles.
Demo_Ticker(time);
#endif
P_Ticker(time);
UI2_Ticker(time);
// InFine ticks whenever it's active.
FI_Ticker();
// Game logic.
if(App_GameLoaded() && gx.Ticker)
{
gx.Ticker(time);
}
#ifdef __CLIENT__
// Windowing system ticks.
R_Ticker(time);
if(isClient)
{
Cl_Ticker(time);
}
#elif __SERVER__
Sv_Ticker(time);
#endif
if(DD_IsSharpTick())
{
// Set frametime back by one tick (to stay in the 0..1 range).
realFrameTimePos -= 1;
// Camera smoothing: now that the world tic has occurred, the next sharp
// position can be processed.
R_NewSharpWorld();
#ifdef LIBDENG_PLAYER0_MOVEMENT_ANALYSIS
if(ddPlayers[0].shared.inGame && ddPlayers[0].shared.mo)
{
mobj_t* mo = ddPlayers[0].shared.mo;
static coord_t prevPos[3] = { 0, 0, 0 };
static coord_t prevSpeed = 0;
coord_t speed = V2d_Length(mo->mom);
coord_t actualMom[2] = { mo->origin[0] - prevPos[0], mo->origin[1] - prevPos[1] };
coord_t actualSpeed = V2d_Length(actualMom);
Con_Message("%i,%f,%f,%f,%f", SECONDS_TO_TICKS(sysTime + time),
ddPlayers[0].shared.forwardMove, speed, actualSpeed, speed - prevSpeed);
V3d_Copy(prevPos, mo->origin);
prevSpeed = speed;
}
#endif
}
#ifdef __CLIENT__
// While paused, don't modify frametime so things keep still.
if(!clientPaused)
#endif
{
frameTimePos = realFrameTimePos;
}
}
// Console is always ticking.
Con_Ticker(time);
// User interface ticks.
if(tickUI)
{
UI_Ticker(time);
}
// Plugins tick always.
DD_CallHooks(HOOK_TICKER, 0, &time);
// The netcode gets to tick, too.
Net_Ticker(time);
}
/**
* Advance time counters.
*/
static void advanceTime(timespan_t time)
{
int oldGameTic = 0;
sysTime += time;
if(!stopTime || netGame)
{
oldGameTic = SECONDS_TO_TICKS(gameTime);
// The difference between gametic and demotic is that demotic
// is not altered at any point. Gametic changes at handshakes.
gameTime += time;
demoTime += time;
if(DD_IsSharpTick())
{
// When a new sharp tick begins, we want that the 35 Hz tick
// calculated from gameTime also changes. If this is not the
// case, we will adjust gameTime slightly so that it syncs again.
if(oldGameTic == SECONDS_TO_TICKS(gameTime))
{
DEBUG_VERBOSE2_Message(("DD_AdvanceTime: Syncing gameTime with sharp ticks (tic=%i pos=%f)\n",
oldGameTic, frameTimePos));
// Realign.
gameTime = (SECONDS_TO_TICKS(gameTime) + 1) / 35.f;
}
}
// Leveltic is reset to zero at every map change.
// The map time only advances when the game is not paused.
#ifdef __CLIENT__
if(!clientPaused)
#endif
{
ddMapTime += time;
}
}
}
void DD_ResetTimer(void)
{
firstTic = true;
Net_ResetTimer();
}
static void timeDeltaStatistics(int deltaMs)
{
timeDeltas[timeDeltasIndex++] = deltaMs;
if(timeDeltasIndex == NUM_FRAMETIME_DELTAS)
{
timeDeltasIndex = 0;
if(devShowFrameTimeDeltas)
{
int maxDelta = timeDeltas[0], minDelta = timeDeltas[0];
float average = 0, variance = 0;
int lateCount = 0;
int i;
for(i = 0; i < NUM_FRAMETIME_DELTAS; ++i)
{
maxDelta = MAX_OF(timeDeltas[i], maxDelta);
minDelta = MIN_OF(timeDeltas[i], minDelta);
average += timeDeltas[i];
variance += timeDeltas[i] * timeDeltas[i];
if(timeDeltas[i] > 0) lateCount++;
}
average /= NUM_FRAMETIME_DELTAS;
variance /= NUM_FRAMETIME_DELTAS;
Con_Message("Time deltas [%i frames]: min=%-6i max=%-6i avg=%-11.7f late=%5.1f%% var=%12.10f",
NUM_FRAMETIME_DELTAS, minDelta, maxDelta, average,
lateCount/(float)NUM_FRAMETIME_DELTAS*100,
variance);
}
}
}
void DD_WaitForOptimalUpdateTime(void)
{
// All times are in milliseconds.
static uint prevUpdateTime = 0;
uint nowTime, elapsed = 0;
uint targetUpdateTime;
// optimalDelta is integer on purpose: we're measuring time at a 1 ms
// accuracy, so we can't use fractions of a millisecond.
const uint optimalDelta = (maxFrameRate > 0? 1000/maxFrameRate : 1);
if(Sys_IsShuttingDown()) return; // No need for finesse.
// This is when we would ideally like to make the update.
targetUpdateTime = prevUpdateTime + optimalDelta;
// Check the current time.
nowTime = Timer_RealMilliseconds();
elapsed = nowTime - prevUpdateTime;
if(elapsed < optimalDelta)
{
uint needSleepMs = optimalDelta - elapsed;
// We need to wait until the optimal time has passed.
if(needSleepMs > 5)
{
// Longer sleep, yield to other threads.
Sys_Sleep(needSleepMs - 3); // Leave some room for inaccuracies.
}
// Attempt to make sure we really wait until the optimal time.
Sys_BlockUntilRealTime(targetUpdateTime);
nowTime = Timer_RealMilliseconds();
elapsed = nowTime - prevUpdateTime;
}
// The time for this update.
prevUpdateTime = nowTime;
timeDeltaStatistics((int)elapsed - (int)optimalDelta);
}
timespan_t DD_LatestRunTicsStartTime(void)
{
if(BusyMode_Active()) return Timer_Seconds();
return lastRunTicsTime;
}
void Loop_RunTics(void)
{
double elapsedTime, ticLength, nowTime;
// Do a network update first.
N_Update();
Net_Update();
// Check the clock.
if(firstTic)
{
// On the first tic, no time actually passes.
firstTic = false;
lastRunTicsTime = Timer_Seconds();
return;
}
// Let's see how much time has passed. This is affected by "settics".
nowTime = Timer_Seconds();
elapsedTime = nowTime - lastRunTicsTime;
// Remember when this frame started.
lastRunTicsTime = nowTime;
// Tic until all the elapsed time has been processed.
while(elapsedTime > 0)
{
ticLength = MIN_OF(MAX_FRAME_TIME, elapsedTime);
elapsedTime -= ticLength;
// Will this be a sharp tick?
DD_CheckSharpTick(ticLength);
#ifdef __CLIENT__
// Process input events.
DD_ProcessEvents(ticLength);
if(!processSharpEventsAfterTickers)
{
// We are allowed to process sharp events before tickers.
DD_ProcessSharpEvents(ticLength);
}
#endif
// Call all the tickers.
baseTicker(ticLength);
#ifdef __CLIENT__
if(processSharpEventsAfterTickers)
{
// This is done after tickers for compatibility with ye olde game logic.
DD_ProcessSharpEvents(ticLength);
}
#endif
// Various global variables are used for counting time.
advanceTime(ticLength);
}
}