source: abuse/trunk/src/sdlport/setup.cpp @ 28

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