doom

a minimalistic implementation of doom
git clone git://ssnf.xyz/doom
Log | Files | Refs

d_main.c (6429B)


      1 #define	BGCOLOR		7
      2 #define	FGCOLOR		8
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <unistd.h>
      7 #include <sys/types.h>
      8 #include <sys/stat.h>
      9 #include <fcntl.h>
     10 
     11 #include "doomdef.h"
     12 #include "doomstat.h"
     13 #include "dstrings.h"
     14 #include "sounds.h"
     15 #include "z_zone.h"
     16 #include "w_wad.h"
     17 #include "s_sound.h"
     18 #include "v_video.h"
     19 #include "f_finale.h"
     20 #include "f_wipe.h"
     21 #include "m_argv.h"
     22 #include "m_misc.h"
     23 #include "m_menu.h"
     24 #include "i_system.h"
     25 #include "i_sound.h"
     26 #include "i_video.h"
     27 #include "g_game.h"
     28 #include "hu_stuff.h"
     29 #include "wi_stuff.h"
     30 #include "st_stuff.h"
     31 #include "am_map.h"
     32 #include "p_setup.h"
     33 #include "r_local.h"
     34 #include "d_main.h"
     35 
     36 void D_CheckNetGame();
     37 void D_ProcessEvents();
     38 void G_BuildTiccmd(ticcmd_t* cmd);
     39 void D_DoAdvanceDemo();
     40 
     41 extern boolean setsizeneeded;
     42 extern int     showMessages;
     43 extern boolean inhelpscreens;
     44 skill_t        startskill;
     45 FILE*          debugfile;
     46 char*          wadfiles[MAXWADFILES];
     47 int            startepisode;
     48 int            startmap;
     49 boolean        devparm;
     50 boolean        nomonsters;
     51 boolean        respawnparm;
     52 boolean        fastparm;
     53 boolean        drone;
     54 boolean        singletics = false;
     55 boolean        autostart;
     56 boolean        advancedemo;
     57 char           wadfile[1024];
     58 char           mapdir[1024];
     59 char           basedefault[1024];
     60 event_t        events[MAXEVENTS];
     61 int            eventhead;
     62 int            eventtail;
     63 gamestate_t    wipegamestate = GS_DEMOSCREEN;
     64 void           R_ExecuteSetViewSize();
     65 
     66 void
     67 D_PostEvent(event_t* ev)
     68 {
     69 	events[eventhead] = *ev;
     70 	eventhead =(eventhead + 1)&(MAXEVENTS-1);
     71 }
     72 
     73 void
     74 D_ProcessEvents()
     75 {
     76 	event_t*	ev;
     77 
     78 	for (; eventtail != eventhead; eventtail =(eventtail + 1)&(MAXEVENTS-1)) {
     79 		ev = &events[eventtail];
     80 		if (M_Responder(ev)) continue;
     81 		G_Responder(ev);
     82 	}
     83 }
     84 
     85 void
     86 D_Display()
     87 {
     88 	static boolean     viewactivestate = false;
     89 	static boolean     menuactivestate = false;
     90 	static boolean     inhelpscreensstate = false;
     91 	static boolean     fullscreen = false;
     92 	static gamestate_t oldgamestate = -1;
     93 	static int         borderdrawcount;
     94 	int                nowtime, tics, wipestart, y;
     95 	boolean            done, wipe, redrawsbar;
     96 
     97 	if (nodrawers)
     98 		return;
     99 	redrawsbar = false;
    100 	if (setsizeneeded) {
    101 		R_ExecuteSetViewSize();
    102 		oldgamestate = -1;
    103 		borderdrawcount = 3;
    104 	}
    105 	if (gamestate != wipegamestate) {
    106 		wipe = true;
    107 		wipe_StartScreen(0, 0, SCREENWIDTH, SCREENHEIGHT);
    108 	}
    109 	else
    110 		wipe = false;
    111 	if (gamestate == GS_LEVEL && gametic)
    112 		HU_Erase();
    113 	switch (gamestate) {
    114 		case GS_LEVEL:
    115 			if (!gametic)
    116 				break;
    117 			if (automapactive)
    118 				AM_Drawer();
    119 			if (wipe ||(viewheight != 200 && fullscreen))
    120 				redrawsbar = true;
    121 			if (inhelpscreensstate && !inhelpscreens)
    122 				redrawsbar = true;
    123 			ST_Drawer(viewheight == 200, redrawsbar);
    124 			fullscreen = viewheight == 200;
    125 			break;
    126 		case GS_INTERMISSION:
    127 			WI_Drawer();
    128 			break;
    129 		case GS_FINALE:
    130 			F_Drawer();
    131 			break;
    132 		case GS_DEMOSCREEN:
    133 			D_PageDrawer();
    134 			break;
    135 	}
    136 	I_UpdateNoBlit();
    137 	if (gamestate == GS_LEVEL && gametic && !automapactive)
    138 		R_RenderPlayerView(&players[displayplayer]);
    139 	if (gamestate == GS_LEVEL && gametic)
    140 		HU_Drawer();
    141 	if (gamestate != oldgamestate && gamestate != GS_LEVEL)
    142 		I_SetPalette(W_CacheLumpName("PLAYPAL", PU_CACHE));
    143 	if (gamestate == GS_LEVEL && oldgamestate != GS_LEVEL) {
    144 		viewactivestate = false;
    145 		R_FillBackScreen();
    146 	}
    147 	if (gamestate == GS_LEVEL && !automapactive && scaledviewwidth != 320) {
    148 		if (menuactive || menuactivestate || !viewactivestate) borderdrawcount = 3;
    149 		if (borderdrawcount) {
    150 			R_DrawViewBorder();
    151 			borderdrawcount--;
    152 		}
    153 	}
    154 	menuactivestate = menuactive;
    155 	viewactivestate = viewactive;
    156 	inhelpscreensstate = inhelpscreens;
    157 	oldgamestate = wipegamestate = gamestate;
    158 	if (paused) {
    159 		if (automapactive) y = 4;
    160 		else y = viewwindowy + 4;
    161 		V_DrawPatch(viewwindowx+(scaledviewwidth-68)/2, y, 0, W_CacheLumpName("M_PAUSE", PU_CACHE));
    162 	}
    163 	M_Drawer();
    164 	NetUpdate();
    165 	if (!wipe) {
    166 		I_FinishUpdate();
    167 		return;
    168 	}
    169 	wipe_EndScreen(0, 0, SCREENWIDTH, SCREENHEIGHT);
    170 	wipestart = I_GetTime() - 1;
    171 	do {
    172 		do {
    173 			nowtime = I_GetTime();
    174 			tics = nowtime - wipestart;
    175 		} while(!tics);
    176 		wipestart = nowtime;
    177 		done = wipe_ScreenWipe(wipe_Melt, 0, 0, SCREENWIDTH, SCREENHEIGHT, tics);
    178 		I_UpdateNoBlit();
    179 		M_Drawer();
    180 		I_FinishUpdate();
    181 	} while(!done);
    182 }
    183 
    184 int   demosequence;
    185 int   pagetic;
    186 char* pagename;
    187 
    188 void
    189 D_PageTicker()
    190 {
    191 	if (--pagetic < 0)
    192 		D_AdvanceDemo();
    193 }
    194 
    195 void
    196 D_PageDrawer()
    197 {
    198 	V_DrawPatch(0, 0, 0, W_CacheLumpName(pagename, PU_CACHE));
    199 }
    200 
    201 void
    202 D_AdvanceDemo()
    203 {
    204 	advancedemo = true;
    205 }
    206 
    207 void
    208 D_DoAdvanceDemo()
    209 {
    210 	players[consoleplayer].playerstate = PST_LIVE;
    211 	advancedemo = false;
    212 	usergame = false;
    213 	paused = false;
    214 	gameaction = ga_nothing;
    215 	demosequence =(demosequence+1)%6;
    216 	switch (demosequence) {
    217 		case 0:
    218 			pagetic = 170;
    219 			gamestate = GS_DEMOSCREEN;
    220 			pagename = "TITLEPIC";
    221 			S_StartMusic(mus_intro);
    222 			break;
    223 		case 1:
    224 			G_DeferedPlayDemo("DEMO1");
    225 			break;
    226 		case 2:
    227 			pagetic = 200;
    228 			gamestate = GS_DEMOSCREEN;
    229 			pagename = "CREDIT";
    230 			break;
    231 		case 3:
    232 			G_DeferedPlayDemo("DEMO2");
    233 			break;
    234 		case 4:
    235 			gamestate = GS_DEMOSCREEN;
    236 			pagetic = 200;
    237 			pagename = "HELP2";
    238 			break;
    239 		case 5:
    240 			G_DeferedPlayDemo("DEMO3");
    241 			break;
    242 		case 6:
    243 			G_DeferedPlayDemo("DEMO4");
    244 			break;
    245 	}
    246 }
    247 
    248 void
    249 D_StartTitle()
    250 {
    251 	gameaction = ga_nothing;
    252 	demosequence = -1;
    253 	D_AdvanceDemo();
    254 }
    255 
    256 void
    257 D_AddFile(char* file)
    258 {
    259 	char* newfile;
    260 	int   numwadfiles;
    261 
    262 	if (access("doom1.wad", R_OK))
    263 		I_Error("D_AddFile: no wad mmmbud");
    264 	for (numwadfiles = 0; wadfiles[numwadfiles]; ++numwadfiles);
    265 	newfile = malloc(strlen(file)+1);
    266 	strcpy(newfile, file);
    267 	wadfiles[numwadfiles] = newfile;
    268 }
    269 
    270 void
    271 init()
    272 {
    273 }
    274 
    275 void
    276 D_DoomMain()
    277 {
    278 	D_AddFile("doom1.wad");
    279 	setbuf(stdout, NULL);
    280 	modifiedgame = false;
    281 	startskill = sk_medium;
    282 	startepisode = 1;
    283 	startmap = 1;
    284 	autostart = false;
    285 	/*everything should be one big init function...*/
    286 	V_Init();
    287 	M_LoadDefaults();
    288 	Z_Init();
    289 	W_InitMultipleFiles(wadfiles);
    290 	M_Init();
    291 	R_Init();
    292 	P_Init();
    293 	I_Init();
    294 	D_CheckNetGame();
    295 	S_Init(snd_SfxVolume, snd_MusicVolume);
    296 	HU_Init();
    297 	ST_Init();
    298 	if (gameaction != ga_loadgame) {
    299 		if (autostart || netgame)
    300 			G_InitNew(startskill, startepisode, startmap);
    301 		else
    302 			D_StartTitle();
    303 	}
    304 	I_InitGraphics();
    305 	/*...everything*/
    306 	for (;;) {
    307 		TryRunTics();
    308 		S_UpdateSounds(players[consoleplayer].mo);
    309 		D_Display();
    310 		I_UpdateSound();
    311 		I_SubmitSound();
    312 	}
    313 }