convert line ends
[canaan.git] / prj / cam / src / framewrk / init.cpp
blob15225fc83e19f03879e669e4faf3939867647758
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 // $Header: r:/t2repos/thief2/src/framewrk/init.cpp,v 1.107 2000/02/25 22:58:18 mwhite Exp $
8 #include <float.h>
10 #include <comtools.h>
11 #include <recapi.h>
12 #include <gshelapi.h>
13 #include <dispapi.h>
14 #include <appapi.h>
15 #include <appagg.h>
16 #include <loopapi.h>
17 #include <config.h>
18 #include <cfg.h>
19 #include <fault.h>
20 #include <mprintf.h>
21 #include <kbcook.h>
22 #include <stdlib.h>
23 #include <res.h>
24 #include <resapi.h>
25 #include <inpapi.h>
26 #include <inpinit.h>
27 #include <tminit.h>
28 #include <timer.h>
29 #include <resagg.h>
30 #include <string.h>
31 #include <cfgtool.h>
32 #include <breakkey.h>
33 #include <hchkthrd.h>
34 #include <iobjsys.h>
35 #include <scrptapi.h>
36 #include <scrnmode.h>
38 #include <appname.h>
39 #include <contexts.h>
40 #include <init.h>
41 #include <2dapp.h>
42 #include <resapp.h>
43 #include <gameinfo.h>
44 #include <loopapp.h>
45 #include <uiapp.h>
46 #include <r3app.h>
47 #include <portapp.h>
48 #include <dispatch.h>
49 #include <dispbase.h>
50 #include <propman.h>
51 #include <pickgame.h>
52 #include <traitman.h>
53 #include <linkman.h>
54 #include <trcache.h>
55 #include <propag8n.h>
56 #include <stimuli.h>
57 #include <stimsens.h>
58 #include <reaction.h>
59 #include <stimul8r.h>
60 #include <stimsrc.h>
61 #include <dxload.h>
62 #include <simtime.h>
63 #include <gametool.h>
64 #include <aiapi.h>
65 #include <movieapi.h>
66 #include <contain.h>
67 #include <gen_bind.h>
68 #include <keysys.h>
69 #include <simman.h>
70 #include <questapi.h>
71 #include <gamestr.h>
72 #include <vocore.h>
73 #include <campaign.h>
74 #include <netman.h>
75 #include <iobjnet.h>
76 #include <movie.h>
77 #include <arqapi.h>
78 #include <diskfree.h>
79 #include <dxwrndlg.h>
80 #include <cdplayer.h>
81 #include <dyntex.h>
82 #include <scrptne_.h>
83 #include <random.h>
85 #include <md.h>
87 // must be last header
88 #include <dbmem.h>
90 // huh?
91 #ifdef __WATCOMC__
92 #pragma warning 555 9
93 #endif
95 EXTERN BOOL CheckForCD(void);
97 //------------------------------------------------------------
98 // CONFIG INITIALIZATION
101 #define CONFIG_FILE "cam.cfg"
102 #define GAME_CFG_VAR "game"
103 #define INCLUDE_PREFIX "include_"
104 void AppShutdownConfig(void);
106 //----------------------------------------
108 static uint app_read_cfg(char* )
110 return CONFIG_DFT_HI_PRI;
114 //------------------------------------------------------------
115 // MONOCHROME
119 static void init_monochrome()
121 static initialized = FALSE;
123 if (initialized)
124 return;
126 initialized = TRUE;
128 char monofname[256];
129 monofname[0] = '\0';
131 #ifdef WIN32
132 #ifndef SHIP
134 bool is_mono=!config_is_defined("mono_no_screen");
135 char *buf;
136 // for now, we know NT cant do Mono's
137 if ((buf=getenv("OS"))!=NULL)
138 if (stricmp(buf,"Windows_NT")==0)
139 is_mono=FALSE;
140 mono_win_init(is_mono);
142 #else
143 mono_win_init(FALSE);
144 #endif
145 #else
146 mono_init();
147 #endif
149 mono_clear();
150 mono_set_flags(MONO_FLG_WRAPCLEAR,NULL);
151 config_get_raw("monolog",monofname,256);
152 if (strcmp(monofname,"")==0)
153 strcpy(monofname,"monolog.txt");
154 if (config_is_defined("monolog"))
155 mono_logon(monofname, MONO_LOG_NEW, MONO_LOG_ALLWIN);
156 mono_to_debugger = !config_is_defined("nomonodebug");
159 //------------------------------------------------------------
160 // GAME SELECTION
163 static void pick_game(void)
165 char buf[80];
166 if (config_get_raw(GAME_CFG_VAR,buf,sizeof(buf)))
167 AppSelectGame(buf); // select the game we named
168 else
169 AppSelectGame(NULL); // select the default game
171 #ifndef SHIP
172 // for programmers only, load prog.cfg so we can find game.cfg and other
173 // stuff
174 config_load("prog.cfg");
175 #endif
178 // Include "game.cfg"
180 char game[128];
181 config_get_raw("game",game,sizeof(game));
183 // punt trailing space
184 char* s;
185 for (s = game + strlen(game) - 1; s >= game && isspace(*s); s--)
186 *s = '\0';
187 s++;
188 strcat(game,".cfg");
190 char path[256];
191 Verify(find_file_in_config_path(path,game,"include_path"));
192 config_load(path);
197 //------------------------------------------------------------
198 // CUSTOM PURE-VIRTUAL TRAP
200 #if defined(_MSC_VER)
201 EXTERN int __cdecl _purecall(void)
203 CriticalMsg("Pure-virtual function call!");
204 exit(1);
205 return 0;
207 #endif
209 //------------------------------------------------------------
210 // DEBUG/SPEW SUPPORT
213 #ifdef DBG_ON
214 int KeyGetch ()
216 ushort code;
218 kb_flush_bios();
220 while (TRUE)
222 if (kb_get_cooked (&code) && (code & KB_FLAG_DOWN))
224 return code ^ KB_FLAG_DOWN;
228 #endif
230 #ifndef SHIP
231 #define ScriptPrintString mprintf
232 #else
233 #define ScriptPrintString NULL
234 #endif
236 //------------------------------------------------------------
237 // MINIMUM DISK SPACE
240 #define MIN_STARTUP_DISK_MB 35
242 //------------------------------------------------------------
243 // CORE ENGINE AGGREGATE OBJECT CREATION
246 tResult LGAPI CoreEngineCreateObjects(int argc, const char *argv[])
248 char buffer[40];
250 g_argv = argv;
251 g_argc = argc;
254 // config sys initializes NOW so that it can be used to create objects
256 config_init();
257 config_read_file(CONFIG_FILE,app_read_cfg);
259 pick_game();
261 // must come after pick_game
262 process_config_includes(INCLUDE_PREFIX);
263 #ifdef EDITOR
264 process_config_includes("editor_"INCLUDE_PREFIX);
265 #endif
267 config_parse_commandline(g_argc,g_argv,NULL);
269 AtExit(AppShutdownConfig);
271 // now that we have config, lets go make sure we can run
273 if (!config_is_defined("skip_starting_checks"))
275 if (!CheckForDiskspaceAndMessage(NULL,MIN_STARTUP_DISK_MB))
276 Exit(1,NULL); // CheckFor does a message box
278 if (!LoadDirectX())
279 Exit(1,NULL); // message already printed in LoadDirectX itself
282 // umm, ship only or something?
283 init_monochrome();
285 #ifndef DONT_CHECK_CD
286 // Is the CD in the drive? - in ship, we always check this
287 #ifndef SHIP
288 if (!config_is_defined("skip_starting_checks"))
289 #endif
290 if (!CheckForCD())
291 Exit(1,NULL);
292 #endif
295 // Next let's do fault
298 #ifdef FAULT
299 if (!config_is_defined("nofault"))
300 ex_startup(EXM_DIVIDE_ERR);
301 #endif
303 #if defined(_MSC_VER) && !defined(SHIP)
304 int cw = _controlfp( 0, 0 );
306 if (config_is_defined("fp_fault_over"))
307 cw &= ~EM_OVERFLOW;
308 if (config_is_defined("fp_fault_under"))
309 cw &= ~EM_UNDERFLOW;
310 if (config_is_defined("fp_fault_divzero"))
311 cw &= ~EM_ZERODIVIDE;
312 if (config_is_defined("fp_fault_denorm"))
313 cw &= ~EM_DENORMAL;
315 _controlfp( cw, MCW_EM );
316 #endif
319 // From this point on, all we're doing is adding COM objects to the
320 // app aggregate. They initialize in constrained priority order.
324 // Get display device from config
325 eDisplayDeviceKind dispkind = kDispDebug;
326 config_get_int("display",&dispkind);
328 // Get other options from config
329 int opt = kGameShellDefault & ~(kLockFrame | kFlushOnEndFrame);
330 if (config_is_defined("multithread"))
331 opt |= kMultithreadedShell;
333 // Use the convenience macro to create all the basic game components
334 CoreGameLibrariesCreate(APPNAME, argc, argv, dispkind, opt);
335 ScriptManCreate(GetSimTime, ScriptPrintString);
336 Gr2dCreate();
337 ResSysCreate();
338 LoopManagerCreate();
339 LoopAppCreate();
340 uiSysCreate();
341 r3SysCreate();
342 PortalSysCreate();
343 InputManagerCreate();
344 InputCreate();
345 ResCreate();
346 Res2Create();
347 PropertyManagerCreate();
348 LinkManagerCreate();
349 TraitManagerCreate();
350 ObjectSystemCreate();
351 DonorCacheCreate();
352 PropagationCreate();
353 StimuliCreate();
354 StimSensorsCreate();
355 ReactionsCreate();
356 StimulatorCreate();
357 StimSourcesCreate();
358 GameToolsCreate();
359 AIManagerCreate();
360 MoviePlayer1Create();
361 SimManagerCreate();
362 QuestDataCreate();
363 GameStringsCreate();
364 VoiceOverCreate();
365 CampaignCreate();
366 InputBinderCreate (&g_pInputBinder);
367 AsyncReadQueueCreate();
368 CDPlayerCreate();
369 DynTextureCreate();
371 // @TODO: move these out to an "engine features" app object creation function,
372 // that is called by the systems that use engfeat?
373 ContainSysCreate();
374 KeySysCreate();
375 #ifdef NEW_NETWORK_ENABLED
376 NetManagerCreate();
377 ObjectNetworkingCreate();
378 ScriptNetworkingCreate();
379 #endif
381 ResSharedCacheCreate();
384 // start recording or playing back
385 if (config_get_raw("record",buffer,38)) {
386 RecorderCreate(kRecRecord, buffer);
387 } else if (config_get_raw("playback",buffer,38)) {
388 RecorderCreate(kRecPlayback, buffer);
391 // set the API switch
392 md_use_lgd3d();
394 return NOERROR;
398 //------------------------------------------------------------
399 // CORE INIT FUNCTION
401 tResult LGAPI CoreEngineAppInit()
403 pGameShell = AppGetObj(IGameShell);
405 init_monochrome();
407 #ifdef WE_CARED_ABOUT_DBG
408 DbgInit();
409 DbgInstallGetch (KeyGetch);
411 if (config_is_defined ("dbg"))
413 DbgMonoConfig ();
415 #endif
417 if (config_is_defined("breakkey"))
418 BreakKeyActivate(VK_F12,VK_F11);
420 if (config_is_defined("heapcheck"))
421 HeapCheckActivate(VK_F9);
423 tm_init();
425 // set default screen mode
426 sScrnMode mode = { 0 };
427 ScrnModeGetConfig(&mode,"");
428 ScrnModeSetDefault(&mode);
430 DispatchInit();
431 DispatchMsgAllClients(kMsgAppInit,NULL,kDispatchForward);
434 //input binding stuff. load all contexts from "default.bnd" and "<game>.bnd"
435 g_pInputBinder->Init (NULL, NULL);
436 InitIBVars ();
438 // pop dialog box about dxdrivers
439 DxWarnDlg();
441 // initialize the random number lib
442 // note fullwise: we retardedly could be calling this twice,
443 // since the net lib wants to init it as well
444 // if we didn't suck we would give this a better seed
445 RandInit(tm_get_millisec());
447 return NOERROR;
450 //------------------------------------------------------------
451 // CORE ENGINE APP EXIT
454 tResult LGAPI CoreEngineAppExit()
456 MovieOnExit();
457 g_pInputBinder->Term ();
458 DispatchMsgAllClients(kMsgAppTerm,NULL,kDispatchReverse);
459 DispatchShutdown();
460 SafeRelease(pGameShell);
461 return NOERROR;
465 //------------------------------------------------------------
466 // Config Sys Shutdown
469 static config_write_spec ConfigWritableTable[] =
471 { NULL, }
474 static bool write_func(char* filename, char* var)
476 if (config_write_to_same_file(filename,var))
477 return TRUE;
478 else
480 char buf[2]; // don't really need the whole filename
481 // did the var come from a file
482 config_get_origin(var,buf,sizeof(buf));
483 if (buf[0] == '\0')
484 return TRUE;
486 return config_default_writable(filename,var);
489 void AppShutdownConfig(void)
491 config_set_writable_table(ConfigWritableTable);
492 config_write_file(CONFIG_FILE,write_func);
493 config_shutdown();
496 //------------------------------------------------------------
497 // INITIALIZATION GLOBALS
500 const char ** g_argv;
501 int g_argc;
503 sLoopInstantiator* gPrimordialMode = NULL;
509 Local Variables:
510 typedefs:("config_write_spec" "uint" "ushort")
511 End: