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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 12 years ago

style: remove trailing spaces, fix copyright statements.

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