-
Notifications
You must be signed in to change notification settings - Fork 5
/
d_main.c
521 lines (407 loc) · 10.8 KB
/
d_main.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2004 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* 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., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION:
* DOOM main program (D_DoomMain) and game loop (D_DoomLoop),
* plus functions to
* parse command line parameters, configure game parameters (turbo),
* and call the startup functions.
*
*-----------------------------------------------------------------------------
*/
#include <stdint.h>
#include "doomdef.h"
#include "doomtype.h"
#include "d_player.h"
#include "d_englsh.h"
#include "sounds.h"
#include "z_zone.h"
#include "w_wad.h"
#include "s_sound.h"
#include "v_video.h"
#include "f_finale.h"
#include "f_wipe.h"
#include "m_menu.h"
#include "i_system.h"
#include "i_sound.h"
#include "g_game.h"
#include "hu_stuff.h"
#include "wi_stuff.h"
#include "st_stuff.h"
#include "am_map.h"
#include "p_setup.h"
#include "r_main.h"
#include "d_main.h"
#include "am_map.h"
#include "m_cheat.h"
#include "globdata.h"
static void D_DoAdvanceDemo(void);
static void D_PageDrawer(void);
static void D_UpdateFPS(void);
// CPhipps - removed wadfiles[] stuff
//jff 1/22/98 parms for disabling music and sound
const boolean nosfxparm = false;
const boolean nomusicparm = true;
const boolean nodrawers = false;
static int32_t maketic;
static int16_t pagetic;
static boolean singletics; // debug flag to cancel adaptiveness
static boolean advancedemo;
boolean _g_fps_show;
//fps counter stuff
int16_t _g_fps_framerate;
static int16_t titlepicnum;
/*
* D_PostEvent - Event handling
*
* Called by I/O functions when an event is received.
* Try event handlers for each code area in turn.
* cph - in the true spirit of the Boom source, let the
* short ciruit operator madness begin!
*/
void D_PostEvent(event_t *ev)
{
/* cph - suppress all input events at game start
* FIXME: This is a lousy kludge */
if (_g_gametic < 3)
return;
if (!M_Responder(ev))
if (!(_g_gamestate == GS_LEVEL && (C_Responder(ev) || AM_Responder(ev))))
G_Responder(ev);
}
static void D_BuildNewTiccmds(void)
{
static int32_t lastmadetic = 0;
int16_t newtics = I_GetTime() - lastmadetic;
lastmadetic += newtics;
while (newtics--)
{
I_StartTic();
if ((int16_t)(maketic - _g_gametic) > 3)
break;
G_BuildTiccmd();
maketic++;
}
}
//
// D_Display
// draw current display, possibly wiping it from the previous
//
gamestate_t wipegamestate = GS_DEMOSCREEN; // wipegamestate can be set to -1 to force a wipe on the next draw
static void D_Display (void)
{
static gamestate_t oldgamestate = GS_LEVEL;
if (nodrawers) // for comparative timing / profiling
return;
// save the current screen if about to wipe
boolean wipe = (_g_gamestate != wipegamestate);
if (wipe)
wipe_StartScreen();
if (_g_gamestate != GS_LEVEL) { // Not a level
if (oldgamestate == GS_LEVEL)
I_SetPalette(0); // cph - use default (basic) palette
switch (_g_gamestate)
{
case GS_INTERMISSION:
WI_Drawer();
break;
case GS_FINALE:
F_Drawer();
break;
case GS_DEMOSCREEN:
D_PageDrawer();
break;
default:
break;
}
}
else if (_g_gametic != _g_basetic)
{ // In a level
// Work out if the player view is visible, and if there is a border
boolean viewactive = (!(automapmode & am_active) || (automapmode & am_overlay));
// Now do the drawing
if (viewactive)
R_RenderPlayerView (&_g_player);
if (automapmode & am_active)
AM_Drawer();
ST_doPaletteStuff(); // Do red-/gold-shifts from damage/items
ST_Drawer();
HU_Drawer();
}
oldgamestate = wipegamestate = _g_gamestate;
// menus go directly to the screen
M_Drawer(); // menu is drawn even on top of everything
D_BuildNewTiccmds();
if (wipe)
// wipe update
D_Wipe();
else
// normal update
I_FinishUpdate (); // page flip or blit buffer
}
//? how many ticks to run?
static void TryRunTics (void)
{
int16_t runtics;
int32_t entertime = I_GetTime();
// Wait for tics to run
while (1)
{
D_BuildNewTiccmds();
runtics = maketic - _g_gametic;
if (runtics <= 0)
{
if ((int16_t)(I_GetTime() - entertime) > 10)
{
M_Ticker();
return;
}
}
else
break;
}
while (runtics-- > 0)
{
if (advancedemo)
D_DoAdvanceDemo ();
M_Ticker ();
G_Ticker ();
_g_gametic++;
}
}
//
// D_DoomLoop()
//
// Not a globally visible function,
// just included for source reference,
// called by D_DoomMain, never exits.
// Manages timing and IO,
// calls all ?_Responder, ?_Ticker, and ?_Drawer,
// calls I_GetTime and I_StartTic
//
static void _Noreturn D_DoomLoop(void)
{
for (;;)
{
// frame syncronous IO operations
// process one or more tics
if (singletics)
{
I_StartTic ();
G_BuildTiccmd ();
if (advancedemo)
D_DoAdvanceDemo ();
M_Ticker ();
G_Ticker ();
_g_gametic++;
maketic++;
}
else
TryRunTics (); // will run at least one tic
// killough 3/16/98: change consoleplayer to displayplayer
if (_g_player.mo) // cph 2002/08/10
S_UpdateSounds();// move positional sounds
// Update display, next frame, with current state.
D_Display();
if(_g_fps_show)
{
D_UpdateFPS();
}
}
}
static void D_UpdateFPS()
{
static uint32_t fps_frames = 0;
static uint32_t fps_timebefore = 0;
fps_frames++;
uint32_t timenow = I_GetTime();
if(timenow >= (fps_timebefore + TICRATE))
{
uint32_t tics_elapsed = timenow - fps_timebefore;
fixed_t f_realfps = FixedApproxDiv((fps_frames*(TICRATE*10)) << FRACBITS, tics_elapsed <<FRACBITS);
_g_fps_framerate = (f_realfps >> FRACBITS);
fps_frames = 0;
fps_timebefore = timenow;
}
else if(timenow < fps_timebefore)
{
//timer overflow.
fps_timebefore = timenow;
fps_frames = 0;
}
}
//
// DEMO LOOP
//
//
// D_PageTicker
// Handles timing for warped projection
//
void D_PageTicker(void)
{
if (--pagetic < 0)
D_AdvanceDemo();
}
//
// D_PageDrawer
//
static void D_PageDrawer(void)
{
V_DrawRawFullScreen(titlepicnum);
}
//
// D_AdvanceDemo
// Called after each demo or intro demosequence finishes
//
void D_AdvanceDemo (void)
{
advancedemo = true;
}
static void D_DrawTitle1(const char *name)
{
UNUSED(name);
S_StartMusic(mus_intro);
pagetic = (TICRATE*30);
}
static struct
{
void (*func)(const char *);
const char *name;
}
const demostates[] =
{
{D_DrawTitle1, NULL},
{G_DeferedPlayDemo, "demo3"},
{NULL, NULL},
};
static int16_t demosequence;
/*
* This cycles through the demo sequences.
*/
void D_DoAdvanceDemo(void)
{
_g_player.playerstate = PST_LIVE; /* not reborn */
advancedemo = _g_usergame = false;
_g_gameaction = ga_nothing;
pagetic = TICRATE * 11;
_g_gamestate = GS_DEMOSCREEN;
if (!demostates[++demosequence].func)
demosequence = 0;
demostates[demosequence].func(demostates[demosequence].name);
}
//
// D_StartTitle
//
void D_StartTitle (void)
{
_g_gameaction = ga_nothing;
demosequence = -1;
D_AdvanceDemo();
}
/*
=================
=
= M_CheckParm
=
= Checks for the given parameter in the program's command line arguments
=
= Returns the argument number (1 to argc - 1) or 0 if not present
=
=================
*/
static int myargc;
static const char * const * myargv;
int16_t M_CheckParm(char *check)
{
for (int16_t i = 1; i < myargc; i++)
if (!stricmp(check, myargv[i]))
return i;
return 0;
}
static void D_Init(void)
{
titlepicnum = W_GetNumForName("TITLEPIC");
}
//
// D_DoomMainSetup
//
// CPhipps - the old contents of D_DoomMain, but moved out of the main
// line of execution so its stack space can be freed
static void D_DoomMainSetup(void)
{
// init subsystems
I_InitKeyboard();
I_InitTimer();
I_InitSound();
printf("Z_Init: Init zone memory allocation daemon.\n");
Z_Init();
G_ReloadDefaults(); // killough 3/4/98: set defaults just loaded.
printf("W_Init: Init WADfiles.\n");
W_Init(); // CPhipps - handling of wadfiles init changed
D_Init();
F_Init();
WI_Init();
printf("M_Init: Init miscellaneous info.\n");
M_Init();
printf("R_Init: DOOM refresh daemon - [...................]\n");
R_Init();
printf("P_Init: Init Playloop state.\n");
P_Init();
S_Init(snd_SfxVolume /* *8 */, snd_MusicVolume /* *8*/ );
printf("HU_Init: Setting up heads up display.\n");
HU_Init();
printf("ST_Init: Init status bar.\n");
ST_Init();
G_LoadSettings();
_g_fps_show = false;
I_InitGraphics();
int16_t p = M_CheckParm("-timedemo");
if (p && p < myargc - 1)
{
singletics = true;
_g_timingdemo = true; // show stats after quit
G_DeferedPlayDemo(myargv[p + 1]);
_g_singledemo = true; // quit after one demo
}
else
{
D_StartTitle(); // start up intro loop
}
}
//
// D_DoomMain
//
void D_DoomMain(int argc, const char * const * argv)
{
myargc = argc;
myargv = argv;
D_DoomMainSetup();
D_DoomLoop (); // never returns
}