-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
dd_loop.c
474 lines (385 loc) · 11.7 KB
/
dd_loop.c
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2011 Daniel Swanson <danij@dengine.net>
*
* 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
*/
/**
* dd_loop.c: Main Loop
*/
// HEADER FILES ------------------------------------------------------------
#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_refresh.h"
#include "de_play.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_ui.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
/**
* 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)
/**
* Maximum number of milliseconds spent uploading textures at the beginning
* of a frame. Note that non-uploaded textures will appear as pure white
* until their content gets uploaded (you should precache them).
*/
#define FRAME_DEFERRED_UPLOAD_TIMEOUT 20
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
void Net_ResetTimer(void);
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
void DD_RunTics(void);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
boolean appShutdown = false; // Set to true when we should exit (normally).
#ifdef WIN32
boolean suspendMsgPump = false; // Set to true to disable checking windows msgs.
#endif
int maxFrameRate = 200; // Zero means 'unlimited'.
// Refresh frame count (independant of the viewport-specific frameCount).
int rFrameCount = 0;
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)
boolean drawGame = true; // If false the game viewport won't be rendered
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static double lastFrameTime;
static float fps;
static int lastFrameCount;
static boolean firstTic = true;
static boolean tickIsSharp = false;
// CODE --------------------------------------------------------------------
/**
* Register console variables for main loop.
*/
void DD_RegisterLoop(void)
{
C_VAR_INT("refresh-rate-maximum", &maxFrameRate, 0, 35, 1000);
C_VAR_INT("rend-dev-framecount", &rFrameCount,
CVF_NO_ARCHIVE | CVF_PROTECTED, 0, 0);
}
/**
* This is the refresh thread (the main thread).
*/
int DD_GameLoop(void)
{
int exitCode = 0;
#ifdef WIN32
MSG msg;
#endif
// Limit the frame rate to 35 when running in dedicated mode.
if(isDedicated)
{
maxFrameRate = 35;
}
while(!appShutdown)
{
#ifdef WIN32
/**
* Start by checking Windows messages.
* \note Must be in the same thread as that which registered the
* window it is handling messages for - DJS.
*/
while(!suspendMsgPump &&
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
{
if(msg.message == WM_QUIT)
{
appShutdown = true;
suspendMsgPump = true;
exitCode = msg.wParam;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(appShutdown)
continue;
#endif
// Frame syncronous I/O operations.
DD_StartFrame();
// Run at least one tic. If no tics are available (maxfps interval
// not reached yet), the function blocks.
DD_RunTics();
// Update clients.
Sv_TransmitFrame();
// Finish the refresh frame.
DD_EndFrame();
// Send out new accumulation. Drawing will take the longest.
//Net_Update();
DD_DrawAndBlit();
//Net_Update();
// After the first frame, start timedemo.
DD_CheckTimeDemo();
}
return exitCode;
}
/**
* Drawing anything outside this routine is frowned upon.
* Seriously frowned!
*/
void DD_DrawAndBlit(void)
{
if(novideo)
return;
if(Con_IsBusy())
{
Con_Error("DD_DrawAndBlit: Console is busy, can't draw!\n");
}
if(renderWireframe)
{
// When rendering is wireframe mode, we must clear the screen
// before rendering a frame.
glClear(GL_COLOR_BUFFER_BIT);
}
if(drawGame)
{
// Interpolate the world ready for drawing view(s) of it.
R_BeginWorldFrame();
// Set up the basic 320x200 legacy projection for the game.
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 320, 200, 0, -1, 1);
R_RenderViewPorts();
// Draw any over/outside view window game graphics (e.g. fullscreen
// menus and other displays).
if(gx.G_Drawer2 && !(UI_IsActive() && UI_Alpha() >= 1.0))
{
gx.G_Drawer2();
}
// Restore the projection mode that was previously in effect.
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
if(Con_TransitionInProgress())
Con_DrawTransition();
if(drawGame)
{
// Debug information.
Net_Drawer();
S_Drawer();
// Finish up any tasks that must be completed after view(s) have
// been drawn.
R_EndWorldFrame();
}
if(UI_IsActive())
{
// Draw user interface.
UI_Drawer();
}
// Draw console.
Rend_Console();
// End any open DGL sequence.
DGL_End();
// Flush buffered stuff to screen (blits everything).
GL_DoUpdate();
}
void DD_StartFrame(void)
{
if(!isDedicated)
GL_UploadDeferredContent(FRAME_DEFERRED_UPLOAD_TIMEOUT);
frameStartTime = Sys_GetTimef();
S_StartFrame();
if(gx.BeginFrame)
{
gx.BeginFrame();
}
}
void DD_EndFrame(void)
{
static uint lastFpsTime = 0;
uint nowTime = Sys_GetRealTime();
// Increment the (local) frame counter.
rFrameCount++;
// Count the frames every other second.
if(nowTime - 2000 >= lastFpsTime)
{
fps = (rFrameCount - lastFrameCount) /
((nowTime - lastFpsTime)/1000.0f);
lastFpsTime = nowTime;
lastFrameCount = rFrameCount;
}
if(gx.EndFrame)
gx.EndFrame();
S_EndFrame();
}
float DD_GetFrameRate(void)
{
return fps;
}
/**
* Determines whether it is time for tickers to run their 35 Hz actions.
* Set at the beginning of a tick by DD_Ticker.
*/
boolean DD_IsSharpTick(void)
{
return tickIsSharp;
}
/**
* 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.
*/
void DD_Ticker(timespan_t time)
{
static float realFrameTimePos = 0;
// Sharp ticks are the ones that occur 35 per second. The rest are interpolated
// (smoothed) somewhere in between.
tickIsSharp = false;
if(!Con_TransitionInProgress() && (tickFrame || netGame)) // Advance frametime?
{
/**
* 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;
}
// Demo ticker. Does stuff like smoothing of view angles.
Demo_Ticker(time);
P_Ticker(time);
// Game logic.
gx.Ticker(time);
if(isClient)
Cl_Ticker(time);
else
Sv_Ticker(time);
if(DD_IsSharpTick())
{
// A new 35 Hz tick begins.
// Set frametime back by one tick (to stay in the 0..1 range).
realFrameTimePos -= 1;
//assert(realFrameTimePos < 1);
// Camera smoothing: now that the world tic has occurred, the next sharp
// position can be processed.
R_NewSharpWorld();
}
// While paused, don't modify frametime so things keep still.
if(!clientPaused)
frameTimePos = realFrameTimePos;
}
// Console is always ticking.
Con_Ticker(time);
if(tickUI)
{ // User interface ticks.
UI_Ticker(time);
}
// Plugins tick always.
Plug_DoHook(HOOK_TICKER, 0, &time);
// The netcode gets to tick, too.
Net_Ticker(time);
}
/**
* Advance time counters.
*/
void DD_AdvanceTime(timespan_t time)
{
sysTime += time;
if(!stopTime || netGame)
{
// The difference between gametic and demotic is that demotic
// is not altered at any point. Gametic changes at handshakes.
gameTime += time;
demoTime += time;
// Leveltic is reset to zero at every map change.
// The map time only advances when the game is not paused.
if(!clientPaused)
{
ddMapTime += time;
}
}
}
/**
* Reset the game time so that on the next frame, the effect will be
* that no time has passed.
*/
void DD_ResetTimer(void)
{
firstTic = true;
Net_ResetTimer();
}
/**
* Run at least one tic.
*/
void DD_RunTics(void)
{
double frameTime, ticLength;
double nowTime = Sys_GetSeconds();
// Do a network update first.
N_Update();
Net_Update();
// Check the clock.
if(firstTic)
{
// On the first tic, no time actually passes.
firstTic = false;
lastFrameTime = nowTime;
return;
}
// We'll sleep until we go past the maxfps interval (the shortest
// allowed interval between tics).
if(maxFrameRate > 0)
{
while((nowTime = Sys_GetSeconds()) - lastFrameTime < 1.0 / maxFrameRate)
{
// Wait for a short while.
Sys_Sleep(3);
N_Update();
Net_Update();
}
}
// How much time do we have for this frame?
frameTime = nowTime - lastFrameTime;
lastFrameTime = nowTime;
// Tic length is determined by the minfps rate.
while(frameTime > 0)
{
ticLength = MIN_OF(MAX_FRAME_TIME, frameTime);
frameTime -= ticLength;
// Process input events.
DD_ProcessEvents(ticLength);
// Call all the tickers.
DD_Ticker(ticLength);
// Various global variables are used for counting time.
DD_AdvanceTime(ticLength);
}
}