forked from ForsakenX/forsaken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
665 lines (524 loc) · 12.8 KB
/
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "main.h"
#include "main_sdl.h"
#include <stdio.h>
#include <string.h>
#include <search.h>
#include "version.h"
#include "render.h"
#include "util.h"
#include "file.h"
#include "net.h"
#include "title.h"
#include "lua_common.h"
#include "sfx.h"
#include <SDL.h>
#include "input.h"
#include "sound.h"
#ifndef WIN32
#include <unistd.h>
#endif
#ifdef __WINE__
#define LR_VGACOLOR LR_VGA_COLOR
#endif
//
// GLOBAL VARIABLES
//
bool Debug = true;
bool ShowFrameRate = false;
bool ShowInfo = false;
int cliSleep = 0;
render_info_t render_info;
//
// Parses the directory to change to from the command line options
//
static bool parse_chdir( char *cli )
{
char * option;
char cmdline[256];
size_t size;
size = strlen(cli)+1;
if ( size > sizeof(cmdline) )
{
Msg("Command line to long!");
return false;
}
strcpy(cmdline,cli);
option = strtok(cmdline, " -+'\"");
while(option != NULL )
{
// WARNING: chdir can only be the LAST option !
// For running the exe outside the root folder
// -chdir c:\\Program Files\\ProjectX
if (!strcasecmp(option,"chdir"))
{
// get option enclosed in quotes
// this does not consider space as separator
option = strtok(NULL, "\"'");
if (!option)
{
Msg("Error using chdir");
return false;
break;
}
// change to root directory
// the rest of the command line will be used as the path
if( chdir( option ) != 0 )
{
// error
Msg("Could not change to directory: %s", option);
return false;
}
// dont loop anymore were done
break;
}
// get the next token
option = strtok(NULL, " -+'\"");
}
return true;
}
//
// checks for all critical folders
//
#define CRITICAL_FOLDERS 4
static bool missing_folders( void )
{
int x = 0;
char* folders[CRITICAL_FOLDERS] = {"Configs","Data","Pilots","Scripts"};
for( x = 0; x < CRITICAL_FOLDERS; x++ )
if( ! is_folder(folders[x]) )
{
Msg("Could not locate the '%s' folder...\n%s\n%s", folders[x],
"exe is most likely in the wrong directory.",
"or you just need to create the folder.");
return true;
}
return false;
}
//
// Parse the Command Line
//
extern bool NoSFX;
extern float normal_fov;
extern float UV_Fix;
extern int NetUpdateIntervalCmdLine;
extern char *config_name;
extern int cliSleep;
extern TEXT local_port_str;
extern bool SpaceOrbSetup;
extern TEXT TCPAddress;
extern TEXT local_port_str;
extern TEXT host_port_str;
extern bool DebugLog;
extern u_int8_t QuickStart;
extern bool IpOnCLI;
static bool ParseCommandLine(char* lpCmdLine)
{
//
// Locals
//
char * option;
char cmdline[256];
size_t size;
//
// Set Global Defaults
//
NoSFX = false; // turns off sound
Debug = false; // turns it off now
NetUpdateIntervalCmdLine = 0;
//
// Get the command line string
//
size = strlen(lpCmdLine)+1;
if ( size > sizeof(cmdline) )
{
Msg("Command line to long!");
return false;
}
strcpy(cmdline,lpCmdLine);
//
// extract and process tokens from command line
//
option = strtok(cmdline, " -+'\"");
// loop over every option
while(option != NULL )
{
// last option
if (!strcasecmp(option,"chdir"))
{
// dont loop anymore were done
break;
}
// sdl + opengl setting
else if (!strcasecmp(option,"ForceAccel")){
render_info.force_accel = true;
}
// off only works in full screen...
// turn on vertical syncing
else if (!strcasecmp(option,"VSync")){
render_info.vsync = true;
}
// debugging information send to Log...
else if (!strcasecmp(option, "log"))
{
DebugLog = true;
}
// debugging information
else if (!strcasecmp(option, "Debug"))
{
Debug = true;
}
// start in window mode
else if (!strcasecmp(option,"Fullscreen"))
{
render_info.fullscreen = true;
}
// start in window mode
else if (!strcasecmp(option,"Window"))
{
render_info.fullscreen = false;
}
// turn off sound
else if (!strcasecmp(option, "NoSFX"))
{
NoSFX = true;
}
// jump to the host screen
else if ( !strcasecmp( option, "QuickHost" ) )
{
QuickStart = QUICKSTART_Start;
}
// jump to the join game screen
else if ( !strcasecmp( option, "QuickJoin" ) )
{
QuickStart = QUICKSTART_Join;
}
// set the ip address for game to join
else if ( !strcasecmp( option, "TCP" ) )
{
char * port;
char address[255];
IpOnCLI = true;
// extract the address
option = strtok(NULL, " ");
strcpy( address, option );
// try to find a port in the address
port = strchr(address,':');
// if port found assign it
if( port )
{
*port = 0; // separate hostname from port
strcpy( (char*) host_port_str.text, ++port );
}
// other wise set default port
else
{
sprintf( (char*) host_port_str.text, "%d", NETWORK_DEFAULT_PORT );
}
// copy in the hostname
strcpy( (char*)TCPAddress.text, address );
}
// supposedly to set wire mode for mxv's...
else if (!strcasecmp(option, "wireframe"))
{
render_info.wireframe = true;
}
// special override to allow setting up of spaceorb
else if ( !strcasecmp( option, "SetupSpaceOrb" ) )
{
SpaceOrbSetup = true;
}
// use sscanf
else
{
int w,h;
// override local port
if ( sscanf( option, "port:%s", (char*)&local_port_str.text[0] ) )
{
DebugPrintf("Command Line: local port set to %s\n", local_port_str.text);
}
// sleep time for every loop
else if ( sscanf( option, "sleep:%d", &cliSleep )){}
// select the pilot
else if ( sscanf( option , "pilot:%s", config_name )){}
// set the packets per second
else if ( sscanf( option, "PPS:%d", &NetUpdateIntervalCmdLine ) ){}
// resolution mode
// must be a valid resolution list in the resolution list in game
// other wise you will end up with the default highest possible resolution
// note: if you pick a value not in the list then your window will be 1 size and your resolution another
else if ( sscanf( option, "mode:%d:%d", &render_info.default_mode.w, &render_info.default_mode.h ) ){}
// let user fix up aspect
else if ( sscanf( option, "aspect:%d:%d", &w, &h ) )
{
DebugPrintf("cli: aspect ratio set to %d:%d\n",w,h);
render_info.aspect_ratio = (float) w / (float) h;
}
// modifies texture dimentions.. don't now what uv stands for..
else if ( sscanf( option, "UVFix:%f", &UV_Fix ) ){}
// set the horizontal frame of view
// this is the screen stretching when you go into nitro
// default is 90... max is 120...
else if ( sscanf( option, "fov:%f", &normal_fov ) ){}
//
else {
DebugPrintf("cli: unknown option: %s\n",option);
}
}
// get the next token
option = strtok(NULL, " -+'\"");
}
return true;
}
//
// Cleans up the application before quiting
//
extern void ReleaseView(void);
extern void DestroySound( int flags );
extern void render_cleanup( render_info_t * info );
extern void ReleaseScene(void);
bool QuitRequested = false;
void CleanUpAndPostQuit(void)
{
// check if this function was ran already
if (QuitRequested)
return;
// kill stuff
ReleaseView();
// stop rendering and destroy objects
render_cleanup( &render_info );
// destroy the sound
DestroySound( DESTROYSOUND_All );
// destroy direct input
joysticks_cleanup();
// release the scene
ReleaseScene();
// set flag
QuitRequested = true;
// we dont control the cursor anymore
input_grab( false );
// close up lua
lua_shutdown();
// cleanup networking
network_cleanup();
#ifdef SOUND_SUPPORT
// cleanup sound system
sound_destroy();
#endif
// should come last
SDL_Quit();
}
//
// Initializes the application
//
#ifdef BREAKPAD
// breakpad running through wine, built for windows doens't work well..
#ifndef __WINE__
extern bool breakpad_init( void );
#endif
#endif
extern bool InitView( void );
extern void GetGamePrefs( void );
extern void SetSoundLevels( int *dummy );
extern void GetDefaultPilot(void);
extern bool InitScene(void);
extern BYTE MyGameStatus;
#include "mload.h"
extern RENDEROBJECT Portal_Execs[ MAXGROUPS ];
extern RENDEROBJECT Skin_Execs[ MAXGROUPS ];
extern RENDEROBJECT RenderBufs[4];
static bool AppInit( char * lpCmdLine )
{
#if defined(DEBUG_ON) && defined(_SVID_)
_LIB_VERSION = _SVID_; // enable matherr
#endif
ZERO_STACK_MEM(render_info);
ZERO_STACK_MEM(RenderBufs);
ZERO_STACK_MEM(Portal_Execs);
ZERO_STACK_MEM(Skin_Execs);
render_info.vsync = false;
#ifdef DXMOUSE
if(!dx_init_mouse())
{
DebugPrintf("Could not init dx mouse\n");
return false;
}
#endif
#ifdef BREAKPAD
// breakpad running through wine, built for windows doens't work well..
#ifndef __WINE__
// initialize google breakpad crash reporting
if(!breakpad_init())
return false;
// test breakpad by uncommenting this
//{ *(int*)0=0; }
#endif
#endif
#ifdef DEBUG_ON
// special debuggin routines
XMem_Init();
#endif
//
if(!sdl_init())
return false;
// parse chdir from command line first
if(!parse_chdir(lpCmdLine))
return false;
// we are now in the skeleton folder
// now we need to see if we are in right place
// check for missing folders
if(missing_folders())
return false;
// startup lua
if( lua_init() != 0 )
return false;
// copy game settings from config
GetGamePrefs();
// our configs are now loaded
// now we can check the command line for overrides
// parse the command line
if(!ParseCommandLine(lpCmdLine))
return false;
//
// create and show the window
//
if(!sdl_init_video())
{
Msg("sdl_init_video() returned false");
return false;
}
// appears dinput has to be after init window
// initialize direct input
// This requires an application and window handle
// so it most not come earlier than here
if (!joysticks_init())
{
Msg("Failed to initialized joysticks!");
return false;
}
// this needs to come after joysticks_init
// because joysticks_init will wipe the joystick settings
GetDefaultPilot();
// this is where it starts to take so long cause it scans directory for dynamic sound files...
// start the title scene
MyGameStatus = STATUS_Title;
if (!InitScene())
return false;
// load the view
if (!InitView() )
{
Msg("InitView failed.\n");
//CleanUpAndPostQuit();
return false;
}
// exclusively grab input in fullscreen mode
input_grab( render_info.fullscreen );
//
SetSoundLevels( NULL );
// done
DebugPrintf("AppInit finished...\n");
return true;
}
//
// Render the next frame and update the window
//
extern bool RenderScene( void );
static bool RenderLoop()
{
if ( !render_info.ok_to_render || render_info.minimized || render_info.bPaused || QuitRequested )
return true;
// Call the sample's RenderScene to render this frame
if (!RenderScene())
{
Msg("RenderScene failed.\n");
return false;
}
if ( quitting )
{
quitting = false;
CleanUpAndPostQuit();
}
// Blt or flip the back buffer to the front buffer
if( !QuitRequested )
{
#ifdef DEMO_SUPPORT
if ((!PlayDemo || ( MyGameStatus != STATUS_PlayingDemo ) || DemoShipInit[ Current_Camera_View ] ))
#endif
{
// this is the actual call to render a frame...
if (!render_flip(&render_info))
{
Msg("RenderLoop: render_flip() failed\n");
return false;
}
}
}
//
return true;
}
//
// The main routine
//
extern int DebugMathErrors( void );
extern void network_cleanup( void );
extern bool SeriousError;
extern void CleanUpAndPostQuit(void);
int main( int argc, char* argv[] )
{
int i;
char cli[500];
int failcount = 0; // number of times RenderLoop has failed
// build cli string
// TODO - cli parsing should be updated
strncpy(cli, " ", 500);
for ( i=1; i<argc; i++ )
{
if( strlen(cli) + strlen(argv[i]) -1 > 500 )
{
DebugPrintf("Stopped parsing cli options at argc='%s' because greator than 500 characters\n",argv[i]);
break;
}
strcat( cli, " " );
strcat( cli, argv[i] );
}
DebugPrintf("cli: %s\n",cli);
// Create the window and initialize all objects needed to begin rendering
if(!AppInit(cli))
goto FAILURE;
while( !QuitRequested )
{
// process system events
if(!handle_events())
goto FAILURE;
// Attempt to render a frame, if it fails, take a note. If
// rendering fails more than twice, abort execution.
if( !RenderLoop() )
{
++failcount;
if( SeriousError )
{
CleanUpAndPostQuit();
break;
}
if (failcount == 3) {
DebugPrintf("Rendering has failed too many times. Aborting execution.\n");
CleanUpAndPostQuit();
break;
}
}
// command line asks us to sleep and free up sys resources a bit...
if ( cliSleep )
SDL_Delay( cliSleep );
}
DebugPrintf("exit(0)\n");
return 0;
FAILURE:
//
CleanUpAndPostQuit();
#ifdef DEBUG_ON
DebugMathErrors();
if ( UnMallocedBlocks() )
DebugPrintf( "Un-malloced blocks found!\n" );
#endif
DebugPrintf("exit(1)\n");
return 1;
}