File: main.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (418 lines) | stat: -rw-r--r-- 8,464 bytes parent folder | download
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
/*
 *  Fenix - Videogame compiler/interpreter
 *  Current release       : FENIX - PROJECT 1.0 - R 0.84
 *  Last stable release   :
 *  Project documentation : http://fenix.divsite.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  Copyright � 1999 Jos� Luis Cebri�n Pag�e
 *  Copyright � 2002 Fenix Team
 *
 */

/*
 * FILE        : main.c
 * DESCRIPTION : Main entry point for FXI
 *
 * HISTORY:	0.81 - Removed -w option
 *
 */

/*
 * INCLUDES
 */

#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef USE_GETTEXT
#include <libintl.h>
#include <locale.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include "fxi.h"
#include "font.h"
#include "dcb.h"

/* os versions */
#ifdef WIN32
	#define OS_ID 0
#endif
#ifdef TARGET_Linux
	#define OS_ID 1
#endif
#ifdef TARGET_BEOS
	#define OS_ID 2
#endif
#ifdef TARGET_MAC
	#define OS_ID 3
#endif
#ifdef TARGET_GP32
	#define OS_ID 4
#endif
#ifdef TARGET_DC
	#define OS_ID 5
#endif
#ifdef TARGET_BSD
	#define OS_ID 6
#endif
#ifdef WIN32
#include <windows.h>
#endif

#define DCB_MAGIC	"DCB Stub\x1A\x0D\x0A"
/*
 *  GLOBAL VARIABLES
 */

int debug	 = 0;		/* 1 if running in debug mode					 */
int fxi		 = 0;		/* 1 only if this is an standalone interpreter   */
int embedded = 0;		/* 1 only if this is a stub with an embedded DCB */

#ifdef TARGET_MAC
    int current_file = 0;
    char files[][256];
#endif

extern int full_screen, double_buffer ;
extern char *apptitle;


/*
 *  FUNCTION : do_exit
 *
 *  Exits from the program cleanly ending operations
 *
 *  PARAMS:
 *      INT n: ERROR LEVEL to return to OS
 *
 *  RETURN VALUE:
 *      No value
 *
 */

void
do_exit(int n)
{
    int i;

	if (keytab_initialized) keytab_free() ;

	for (i=0;i<MAX_JOYS;i++)
	    if (_joysticks[i]!=NULL) SDL_JoystickClose(_joysticks[i]) ;

	SDL_Quit() ;
	exit(n) ;
}

/*
 *  FUNCTION : main
 *
 *  Main function for FXI
 *
 *  PARAMS:
 *      INT n: ERROR LEVEL to return to OS
 *
 *  RETURN VALUE:
 *      No value
 *
 */

int main (int argc, char **argv)
{
	char *				filename = 0 ;
	char				dcbname[256] ;
	INSTANCE *			mainproc_running ;
	int					i, j ;
	int					norun = 0 ;
	const SDL_version * sdl_version ;
	char *				ptr ;
	file *				fp = NULL;

	struct
	{
		char	magic[12];
		int		dcb_offset;
	}
	dcb_signature;

#ifdef USE_GETTEXT
	setlocale (LC_MESSAGES, "");
	setlocale (LC_CTYPE, "");
	setlocale (LC_COLLATE, "");
	textdomain ("fenix-fxi");
	bindtextdomain ("fenix-fxi", NULL);
#endif

	/* Find out if we are calling fxi.exe or whatever.exe */

 	ptr = argv[0] + strlen(argv[0]) ;
 	while (ptr > argv[0] && ptr[-1] != '\\' && ptr[-1] != '/')
 		ptr-- ;
 	fxi = (strncasecmp(ptr,"fenix-fxi",9) == 0) \
		|| (strncasecmp(ptr,"fxi",3) == 0) ;

	/* Init RAND generator */

	srand (time(NULL)) ;

	/* Init SDL */

	if ( SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_CDROM) < 0 )
	{
		printf (_("SDL Init Error: %s\n"), SDL_GetError()) ;
		do_exit(1) ;
	}

	SDL_JoystickEventState (SDL_ENABLE) ;

    gr_con_printf ("�15" FXI_VERSION) ;

	if (fxi)
	{
		/* Standalone interpreter, dump some info */

		sdl_version = SDL_Linked_Version();
		gr_con_printf ("�14SDL: %d.%d.%d (DLL loaded: %d.%d.%d)",
				SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
				sdl_version->major, sdl_version->minor, sdl_version->patch) ;
	}
	else
	{
		/* Hand-made interpreter: search for DCB at EOF */

		if (!file_exists(argv[0]))
		{
#ifdef WIN32
			char   exepath[MAX_PATH];

			GetModuleFileName(NULL, exepath, sizeof(exepath));
			fp = file_open(exepath, "rb0");
#endif
		}
		else
			fp = file_open(argv[0], "rb0");

		if (fp != NULL)
		{
			file_seek (fp, -(int)sizeof(dcb_signature), SEEK_END);
			file_read (fp, &dcb_signature, sizeof(dcb_signature));

			if (strcmp(dcb_signature.magic, DCB_MAGIC) == 0)
			{
				ARRANGE_DWORD (&dcb_signature.dcb_offset);

				filename = argv[0];
				embedded = 1;
			}
		}

		if (!embedded)
		{
			/* No embedded DCB; search for a DCB with similar name */

			filename = ptr ;
#ifdef WIN32
	 		while (*ptr && *ptr != '.') ptr++ ;
	 		*ptr = 0 ;
#endif
		}
	}

 	if (fxi)
	{
		/* Calling FXI.EXE so we must get all command line params */

		for (i = 1 ; i < argc ; i++)
		{
			if (argv[i][0] == '-' && argv[i][1])
			{
				j = 1 ;
				while (argv[i][j])
				{
					if (argv[i][j] == 'd') debug = 1 ;
					if (argv[i][j] == 'r') norun = 1 ;
					//if (argv[i][j] == 'b') double_buffer = 1 ;
					if (argv[i][j] == 'f') enable_filtering = 1 ;

					if (argv[i][j] == 't')
					{
						if (apptitle) free(apptitle);
						if (argv[i][j+1] == 0)
						{
							if (i == argc-1)
								gr_error (_("You must provide a title")) ;
							apptitle = strdup(argv[i+1]);
							i++ ;
							break ;
						}
						apptitle = strdup(argv[i]+j+1);
						break;
					}

					if (argv[i][j] == 'i')
					{
						if (argv[i][j+1] == 0)
						{
							if (i == argc-1)
								gr_error (_("You must provide a directory")) ;
							file_addp (argv[i+1]);
							i++ ;
							break ;
						}
						file_addp (argv[i]+j+1) ;
						break ;
					}
					j++ ;
				}
			}
			else
			{
				if (!filename)
				{
					filename = argv[i] ;
					if (i < argc-1)
						memmove (&argv[i], &argv[i+1],
							sizeof(char*) * (argc-i-1)) ;
					argc-- ;
					i-- ;
				}
			}
		}

		if (!filename)
		{
			gr_error ( _("Fenix 0.92 FXI\nCopyright (C) 2002 Fenix Team\n"
				"Copyright (C) 1999 Jose Luis Cebrian Pague\n"
				"Fenix comes with ABSOLUTELY NO WARRANTY; see COPYING for details\n\n"
				"Usage: %s [options] file.dcb\n\n"
				"   -d       Activate DEBUG mode\n"
				"   -f       16bpp Filter ON (only 16bpp color mode)\n"
				"This program is free software dsitributed under.\n\n"
				"GNU General Public License published by Free Software Foundation.\n"
				"Permission granted to distribute and/or modify as stated in the license\n"
				"agreement (GNU GPL version 2 or later).\n"
				"See COPYING for license details.\n"),
				argv[0]) ;
			return -1 ;
		}
	}

	/* Initialization (modules needed before dcb_load) */

	gprof_init () ;
	string_init () ;
	init_c_type() ;

	/* Init application title for windowed modes */

	strcpy (dcbname, filename) ;
	if (!apptitle)
	{
		if (strcmp(filename, "-") != 0)
			apptitle = strdup(filename) ;
		else
			apptitle = strdup("Fenix") ;
	}

#ifdef TARGET_MAC
        strcpy (files[current_file], filename);
#endif

	if (!embedded)
	{
		/* First try to load directly (we expect fxi myfile.dcb) */
		if (!dcb_load(dcbname))
		{
			/* not successful... try for DCB */

			strcat (dcbname, ".dcb") ;

			if (!dcb_load(dcbname))
			{
				/* not successful... try for DAT */

				strcpy (dcbname, filename) ;
 				strcat (dcbname, ".dat") ;

				if (!dcb_load(dcbname))
				{
					gr_error (_("'%s': file does not exist or it's not a DCB version %d file or compatible"), filename, DCB_VERSION >> 8) ;
					return -2 ;
				}
			}
		}
	}
	else
	{
		dcb_load_from(fp, dcb_signature.dcb_offset);
/*		file_close (fp); */
	}

	/* If the dcb is not in debug mode, switch off the profiler */

	if (dcb.data.NID == 0)
	{
		gprof_toggle();
		debug = 0;
	}

	/* Initialization (modules needed after dcb_load) */

	fnc_init();
	#ifdef MMX_FUNCTIONS
		MMX_init();
	#endif

	grlib_init () ;
	sysproc_init () ;
	gr_font_systemfont (default_font);

	if (argc > 32) argc = 32 ;
	for (i = 0 ; i < argc ; i++)
	{
		int * ptr = &GLODWORD(ARGV_TABLE+i*4) ;
		*ptr = string_new(argv[i]) ;
		string_use (*ptr) ;
	}
	GLODWORD(ARGC) = argc-1 ;
	GLODWORD(FXI_OS) = OS_ID ;
	if (mainproc)
	{
		mainproc_running = instance_new (mainproc, 0) ;
		instance_go_all () ;
	}

	//if (cd_playing()) cd_stop() ;
	//string_dump() ;
	//gprof_dump ("profile.txt");

	do_exit(0);
	return 0;
}