1 | /************************ |
---|
2 | * setup.c |
---|
3 | * SDL port for Abuse |
---|
4 | * by Anthony Kruize |
---|
5 | ************************/ |
---|
6 | |
---|
7 | #include "config.h" |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <sys/stat.h> |
---|
12 | #include <signal.h> |
---|
13 | #include <SDL.h> |
---|
14 | #ifdef HAVE_OPENGL |
---|
15 | #ifdef __APPLE__ |
---|
16 | #include <OpenGL/gl.h> |
---|
17 | #include <OpenGL/glu.h> |
---|
18 | #else |
---|
19 | #include <GL/gl.h> |
---|
20 | #include <GL/glu.h> |
---|
21 | #endif /* __APPLE__ */ |
---|
22 | #endif /* HAVE_OPENGL */ |
---|
23 | |
---|
24 | #include "specs.hpp" |
---|
25 | #include "keys.hpp" |
---|
26 | #include "setup.h" |
---|
27 | |
---|
28 | flags_struct flags; |
---|
29 | keys_struct keys; |
---|
30 | |
---|
31 | extern unsigned int xres, yres; |
---|
32 | static unsigned int scale; |
---|
33 | |
---|
34 | // |
---|
35 | // Display help |
---|
36 | // |
---|
37 | void showHelp() |
---|
38 | { |
---|
39 | printf( "\n" ); |
---|
40 | printf( "Usage: abuse.sdl [options]\n" ); |
---|
41 | printf( "Options:\n\n" ); |
---|
42 | printf( "** Abuse Options **\n" ); |
---|
43 | printf( " -size <arg> Set the size of the screen\n" ); |
---|
44 | printf( " -edit Startup in editor mode\n" ); |
---|
45 | printf( " -a <arg> Use addon named <arg>\n" ); |
---|
46 | printf( " -f <arg> Load map file named <arg>\n" ); |
---|
47 | printf( " -lisp Startup in lisp interpreter mode\n" ); |
---|
48 | printf( " -nodelay Run at maximum speed\n" ); |
---|
49 | printf( "\n" ); |
---|
50 | printf( "** Abuse-SDL Options **\n" ); |
---|
51 | printf( " -datadir <arg> Set the location of the game data to <arg>\n" ); |
---|
52 | printf( " -doublebuf Enable double buffering\n" ); |
---|
53 | printf( " -fullscreen Enable fullscreen mode\n" ); |
---|
54 | #ifdef HAVE_OPENGL |
---|
55 | printf( " -gl Enable OpenGL\n" ); |
---|
56 | printf( " -antialias Enable anti-aliasing (with -gl only)\n" ); |
---|
57 | #endif |
---|
58 | printf( " -h, --help Display this text\n" ); |
---|
59 | printf( " -mono Disable stereo sound\n" ); |
---|
60 | printf( " -nosound Disable sound\n" ); |
---|
61 | printf( " -scale <arg> Scale to <arg>\n" ); |
---|
62 | // printf( " -x <arg> Set the width to <arg>\n" ); |
---|
63 | // printf( " -y <arg> Set the height to <arg>\n" ); |
---|
64 | printf( "\n" ); |
---|
65 | printf( "Anthony Kruize <trandor@labyrinth.net.au>\n" ); |
---|
66 | printf( "\n" ); |
---|
67 | } |
---|
68 | |
---|
69 | // |
---|
70 | // Create a default 'abuserc' file |
---|
71 | // |
---|
72 | void createRCFile( char *rcfile ) |
---|
73 | { |
---|
74 | FILE *fd = NULL; |
---|
75 | |
---|
76 | if( (fd = fopen( rcfile, "w" )) != NULL ) |
---|
77 | { |
---|
78 | fputs( "; Abuse-SDL Configuration file\n\n", fd ); |
---|
79 | fputs( "; Location of the datafiles\ndatadir=/var/games/abuse\n\n", fd ); |
---|
80 | fputs( "; Startup fullscreen\nfullscreen=0\n\n", fd ); |
---|
81 | fputs( "; Use DoubleBuffering\ndoublebuf=0\n\n", fd ); |
---|
82 | fputs( "; Use mono audio only\nmono=0\n\n", fd ); |
---|
83 | fputs( "; Grab the mouse to the window\ngrabmouse=0\n\n", fd ); |
---|
84 | fputs( "; Set the scale factor\nscale=2\n\n", fd ); |
---|
85 | fputs( "; Use OpenGL\ngl=0\n\n", fd ); |
---|
86 | fputs( "; Use anti-aliasing (with gl=1 only)\nantialias=1\n\n", fd ); |
---|
87 | // fputs( "; Set the width of the window\nx=320\n\n", fd ); |
---|
88 | // fputs( "; Set the height of the window\ny=200\n\n", fd ); |
---|
89 | fputs( "; Disable the SDL parachute in the case of a crash\nnosdlparachute=0\n\n", fd ); |
---|
90 | fputs( "; Key mappings\n", fd ); |
---|
91 | fputs( "left=LEFT\nright=RIGHT\nup=UP\ndown=DOWN\n", fd ); |
---|
92 | fputs( "fire=SPACE\nweapprev=CTRL_R\nweapnext=INSERT\n", fd ); |
---|
93 | fclose( fd ); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | printf( "Unable to create 'abuserc' file.\n" ); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | // |
---|
102 | // Read in the 'abuserc' file |
---|
103 | // |
---|
104 | void readRCFile() |
---|
105 | { |
---|
106 | FILE *fd = NULL; |
---|
107 | char *rcfile; |
---|
108 | char buf[255]; |
---|
109 | char *result; |
---|
110 | |
---|
111 | rcfile = (char *)jmalloc( strlen( get_save_filename_prefix() ) + 9, "rcfile" ); |
---|
112 | sprintf( rcfile, "%s/abuserc", get_save_filename_prefix() ); |
---|
113 | if( (fd = fopen( rcfile, "r" )) != NULL ) |
---|
114 | { |
---|
115 | while( fgets( buf, sizeof( buf ), fd ) != NULL ) |
---|
116 | { |
---|
117 | result = strtok( buf, "=" ); |
---|
118 | if( strcasecmp( result, "fullscreen" ) == 0 ) |
---|
119 | { |
---|
120 | result = strtok( NULL, "\n" ); |
---|
121 | flags.fullscreen = atoi( result ); |
---|
122 | } |
---|
123 | else if( strcasecmp( result, "doublebuf" ) == 0 ) |
---|
124 | { |
---|
125 | result = strtok( NULL, "\n" ); |
---|
126 | flags.doublebuf = atoi( result ); |
---|
127 | } |
---|
128 | else if( strcasecmp( result, "mono" ) == 0 ) |
---|
129 | { |
---|
130 | result = strtok( NULL, "\n" ); |
---|
131 | flags.mono = atoi( result ); |
---|
132 | } |
---|
133 | else if( strcasecmp( result, "grabmouse" ) == 0 ) |
---|
134 | { |
---|
135 | result = strtok( NULL, "\n" ); |
---|
136 | flags.grabmouse = atoi( result ); |
---|
137 | } |
---|
138 | else if( strcasecmp( result, "scale" ) == 0 ) |
---|
139 | { |
---|
140 | result = strtok( NULL, "\n" ); |
---|
141 | scale = atoi( result ); |
---|
142 | // flags.xres = xres * atoi( result ); |
---|
143 | // flags.yres = yres * atoi( result ); |
---|
144 | } |
---|
145 | /* else if( strcasecmp( result, "x" ) == 0 ) |
---|
146 | { |
---|
147 | result = strtok( NULL, "\n" ); |
---|
148 | flags.xres = atoi( result ); |
---|
149 | } |
---|
150 | else if( strcasecmp( result, "y" ) == 0 ) |
---|
151 | { |
---|
152 | result = strtok( NULL, "\n" ); |
---|
153 | flags.yres = atoi( result ); |
---|
154 | }*/ |
---|
155 | else if( strcasecmp( result, "gl" ) == 0 ) |
---|
156 | { |
---|
157 | // We leave this in even if we don't have OpenGL so we can |
---|
158 | // at least inform the user. |
---|
159 | result = strtok( NULL, "\n" ); |
---|
160 | flags.gl = atoi( result ); |
---|
161 | } |
---|
162 | #ifdef HAVE_OPENGL |
---|
163 | else if( strcasecmp( result, "antialias" ) == 0 ) |
---|
164 | { |
---|
165 | result = strtok( NULL, "\n" ); |
---|
166 | if( atoi( result ) ) |
---|
167 | { |
---|
168 | flags.antialias = GL_LINEAR; |
---|
169 | } |
---|
170 | } |
---|
171 | #endif |
---|
172 | else if( strcasecmp( result, "nosdlparachute" ) == 0 ) |
---|
173 | { |
---|
174 | result = strtok( NULL, "\n" ); |
---|
175 | flags.nosdlparachute = atoi( result ); |
---|
176 | } |
---|
177 | else if( strcasecmp( result, "datadir" ) == 0 ) |
---|
178 | { |
---|
179 | result = strtok( NULL, "\n" ); |
---|
180 | set_filename_prefix( result ); |
---|
181 | } |
---|
182 | else if( strcasecmp( result, "left" ) == 0 ) |
---|
183 | { |
---|
184 | result = strtok( NULL,"\n" ); |
---|
185 | keys.left = key_value( result ); |
---|
186 | } |
---|
187 | else if( strcasecmp( result, "right" ) == 0 ) |
---|
188 | { |
---|
189 | result = strtok( NULL,"\n" ); |
---|
190 | keys.right = key_value( result ); |
---|
191 | } |
---|
192 | else if( strcasecmp( result, "up" ) == 0 ) |
---|
193 | { |
---|
194 | result = strtok( NULL,"\n" ); |
---|
195 | keys.up = key_value( result ); |
---|
196 | } |
---|
197 | else if( strcasecmp( result, "down" ) == 0 ) |
---|
198 | { |
---|
199 | result = strtok( NULL,"\n" ); |
---|
200 | keys.down = key_value( result ); |
---|
201 | } |
---|
202 | else if( strcasecmp( result, "fire" ) == 0 ) |
---|
203 | { |
---|
204 | result = strtok( NULL,"\n" ); |
---|
205 | keys.b2 = key_value( result ); |
---|
206 | } |
---|
207 | else if( strcasecmp( result, "special" ) == 0 ) |
---|
208 | { |
---|
209 | result = strtok( NULL,"\n" ); |
---|
210 | keys.b1 = key_value( result ); |
---|
211 | } |
---|
212 | else if( strcasecmp( result, "weapprev" ) == 0 ) |
---|
213 | { |
---|
214 | result = strtok( NULL,"\n" ); |
---|
215 | keys.b3 = key_value( result ); |
---|
216 | } |
---|
217 | else if( strcasecmp( result, "weapnext" ) == 0 ) |
---|
218 | { |
---|
219 | result = strtok( NULL,"\n" ); |
---|
220 | keys.b4 = key_value( result ); |
---|
221 | } |
---|
222 | } |
---|
223 | fclose( fd ); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | // Couldn't open the abuserc file so let's create a default one |
---|
228 | createRCFile( rcfile ); |
---|
229 | } |
---|
230 | jfree( rcfile ); |
---|
231 | } |
---|
232 | |
---|
233 | // |
---|
234 | // Parse the command-line parameters |
---|
235 | // |
---|
236 | void parseCommandLine( int argc, char **argv ) |
---|
237 | { |
---|
238 | for( int ii = 1; ii < argc; ii++ ) |
---|
239 | { |
---|
240 | if( !strcasecmp( argv[ii], "-fullscreen" ) ) |
---|
241 | { |
---|
242 | flags.fullscreen = 1; |
---|
243 | } |
---|
244 | else if( !strcasecmp( argv[ii], "-doublebuf" ) ) |
---|
245 | { |
---|
246 | flags.doublebuf = 1; |
---|
247 | } |
---|
248 | else if( !strcasecmp( argv[ii], "-size" ) ) |
---|
249 | { |
---|
250 | if( !sscanf( argv[++ii], "%d", &xres ) ) |
---|
251 | { |
---|
252 | xres = 320; |
---|
253 | } |
---|
254 | if( !sscanf( argv[++ii], "%d", &yres ) ) |
---|
255 | { |
---|
256 | yres = 200; |
---|
257 | } |
---|
258 | } |
---|
259 | else if( !strcasecmp( argv[ii], "-scale" ) ) |
---|
260 | { |
---|
261 | int result; |
---|
262 | if( sscanf( argv[++ii], "%d", &result ) ) |
---|
263 | { |
---|
264 | scale = result; |
---|
265 | /* flags.xres = xres * scale; |
---|
266 | flags.yres = yres * scale;*/ |
---|
267 | } |
---|
268 | } |
---|
269 | /* else if( !strcasecmp( argv[ii], "-x" ) ) |
---|
270 | { |
---|
271 | int x; |
---|
272 | if( sscanf( argv[++ii], "%d", &x ) ) |
---|
273 | { |
---|
274 | flags.xres = x; |
---|
275 | } |
---|
276 | } |
---|
277 | else if( !strcasecmp( argv[ii], "-y" ) ) |
---|
278 | { |
---|
279 | int y; |
---|
280 | if( sscanf( argv[++ii], "%d", &y ) ) |
---|
281 | { |
---|
282 | flags.yres = y; |
---|
283 | } |
---|
284 | }*/ |
---|
285 | else if( !strcasecmp( argv[ii], "-nosound" ) ) |
---|
286 | { |
---|
287 | flags.nosound = 1; |
---|
288 | } |
---|
289 | else if( !strcasecmp( argv[ii], "-gl" ) ) |
---|
290 | { |
---|
291 | // We leave this in even if we don't have OpenGL so we can |
---|
292 | // at least inform the user. |
---|
293 | flags.gl = 1; |
---|
294 | } |
---|
295 | #ifdef HAVE_OPENGL |
---|
296 | else if( !strcasecmp( argv[ii], "-antialias" ) ) |
---|
297 | { |
---|
298 | flags.antialias = GL_LINEAR; |
---|
299 | } |
---|
300 | #endif |
---|
301 | else if( !strcasecmp( argv[ii], "-mono" ) ) |
---|
302 | { |
---|
303 | flags.mono = 1; |
---|
304 | } |
---|
305 | else if( !strcasecmp( argv[ii], "-datadir" ) ) |
---|
306 | { |
---|
307 | char datadir[255]; |
---|
308 | if( sscanf( argv[++ii], "%s", datadir ) ) |
---|
309 | { |
---|
310 | set_filename_prefix( datadir ); |
---|
311 | } |
---|
312 | } |
---|
313 | else if( !strcasecmp( argv[ii], "-h" ) || !strcasecmp( argv[ii], "--help" ) ) |
---|
314 | { |
---|
315 | showHelp(); |
---|
316 | exit( 0 ); |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | // |
---|
322 | // Setup SDL and configuration |
---|
323 | // |
---|
324 | void setup( int argc, char **argv ) |
---|
325 | { |
---|
326 | // Initialise default settings |
---|
327 | flags.fullscreen = 0; // Start in a window |
---|
328 | flags.doublebuf = 0; // No double buffering |
---|
329 | flags.mono = 0; // Enable stereo sound |
---|
330 | flags.nosound = 0; // Enable sound |
---|
331 | flags.grabmouse = 0; // Don't grab the mouse |
---|
332 | flags.nosdlparachute = 0; // SDL error handling |
---|
333 | flags.xres = xres = 320; // Default window width |
---|
334 | flags.yres = yres = 200; // Default window height |
---|
335 | flags.gl = 0; // Don't use opengl |
---|
336 | #ifdef HAVE_OPENGL |
---|
337 | flags.antialias = GL_NEAREST; // Don't anti-alias |
---|
338 | #endif |
---|
339 | keys.up = key_value( "UP" ); |
---|
340 | keys.down = key_value( "DOWN" ); |
---|
341 | keys.left = key_value( "LEFT" ); |
---|
342 | keys.right = key_value( "RIGHT" ); |
---|
343 | keys.b3 = key_value( "CTRL_R" ); |
---|
344 | keys.b4 = key_value( "INSERT" ); |
---|
345 | scale = 2; // Default scale amount |
---|
346 | |
---|
347 | // Display our name and version |
---|
348 | printf( "%s %s\n", PACKAGE, VERSION ); |
---|
349 | |
---|
350 | // Initialize SDL with video and audio support |
---|
351 | if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 ) |
---|
352 | { |
---|
353 | printf( "Unable to initialise SDL : %s\n", SDL_GetError() ); |
---|
354 | exit( 1 ); |
---|
355 | } |
---|
356 | atexit( SDL_Quit ); |
---|
357 | |
---|
358 | // Set the savegame directory |
---|
359 | char *homedir; |
---|
360 | char *savedir; |
---|
361 | FILE *fd = NULL; |
---|
362 | |
---|
363 | if( (homedir = getenv( "HOME" )) != NULL ) |
---|
364 | { |
---|
365 | savedir = (char *)jmalloc( strlen( homedir ) + 9, "savedir" ); |
---|
366 | sprintf( savedir, "%s/.abuse/", homedir ); |
---|
367 | // Check if we already have a savegame directory |
---|
368 | if( (fd = fopen( savedir, "r" )) == NULL ) |
---|
369 | { |
---|
370 | // FIXME: Add some error checking here |
---|
371 | mkdir( savedir, S_IRUSR | S_IWUSR | S_IXUSR ); |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | fclose( fd ); |
---|
376 | } |
---|
377 | set_save_filename_prefix( savedir ); |
---|
378 | jfree( savedir ); |
---|
379 | } |
---|
380 | else |
---|
381 | { |
---|
382 | // Warn the user that we couldn't set the savename prefix |
---|
383 | printf( "WARNING: Unable to get $HOME environment variable.\n" ); |
---|
384 | printf( " Savegames will probably fail.\n" ); |
---|
385 | // Just use the working directory. |
---|
386 | // Hopefully they have write permissions.... |
---|
387 | set_save_filename_prefix( "" ); |
---|
388 | } |
---|
389 | |
---|
390 | // Set the datadir to a default value |
---|
391 | // (The current directory) |
---|
392 | set_filename_prefix( EXPDATADIR ); |
---|
393 | |
---|
394 | // Load the users configuration |
---|
395 | readRCFile(); |
---|
396 | |
---|
397 | // Handle command-line parameters |
---|
398 | parseCommandLine( argc, argv ); |
---|
399 | |
---|
400 | // Calculate the scaled window size. |
---|
401 | flags.xres = xres * scale; |
---|
402 | flags.yres = yres * scale; |
---|
403 | |
---|
404 | // Stop SDL handling some errors |
---|
405 | if( flags.nosdlparachute ) |
---|
406 | { |
---|
407 | // segmentation faults |
---|
408 | signal( SIGSEGV, SIG_DFL ); |
---|
409 | // floating point errors |
---|
410 | signal( SIGFPE, SIG_DFL ); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | // |
---|
415 | // Get the key binding for the requested function |
---|
416 | // |
---|
417 | int get_key_binding(char const *dir, int i) |
---|
418 | { |
---|
419 | if( strcasecmp( dir, "left" ) == 0 ) |
---|
420 | return keys.left; |
---|
421 | else if( strcasecmp( dir, "right" ) == 0 ) |
---|
422 | return keys.right; |
---|
423 | else if( strcasecmp( dir, "up" ) == 0 ) |
---|
424 | return keys.up; |
---|
425 | else if( strcasecmp( dir, "down" ) == 0 ) |
---|
426 | return keys.down; |
---|
427 | else if( strcasecmp( dir, "b1" ) == 0 ) |
---|
428 | return keys.b1; |
---|
429 | else if( strcasecmp( dir, "b2" ) == 0 ) |
---|
430 | return keys.b2; |
---|
431 | else if( strcasecmp( dir, "b3" ) == 0 ) |
---|
432 | return keys.b3; |
---|
433 | else if( strcasecmp( dir, "b4" ) == 0 ) |
---|
434 | return keys.b4; |
---|
435 | |
---|
436 | return 0; |
---|
437 | } |
---|