source: abuse/branches/lol/src/sdlport/setup.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 9 years ago

build: SDL2 compilation fixes.

File size: 11.1 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-2013 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#if HAVE_CONFIG_H
22#   include "config.h"
23#endif
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <sys/stat.h>
28#include <signal.h>
29
30#if defined HAVE_SDL_SDL_H
31#   include <SDL/SDL.h>
32#else
33#   include <SDL.h>
34#endif
35
36#include "common.h"
37
38#include "imlib/specs.h"
39#include "imlib/keys.h"
40
41#include "setup.h"
42
43flags_struct flags;
44keys_struct keys;
45
46extern int xres, yres;
47
48//
49// Display help
50//
51void showHelp()
52{
53    printf( "\n" );
54    printf( "Usage: abuse.sdl [options]\n" );
55    printf( "Options:\n\n" );
56    printf( "** Abuse Options **\n" );
57    printf( "  -size <arg>       Set the size of the screen\n" );
58    printf( "  -edit             Startup in editor mode\n" );
59    printf( "  -a <arg>          Use addon named <arg>\n" );
60    printf( "  -f <arg>          Load map file named <arg>\n" );
61    printf( "  -lisp             Startup in lisp interpreter mode\n" );
62    printf( "  -nodelay          Run at maximum speed\n" );
63    printf( "\n" );
64    printf( "** Abuse-SDL Options **\n" );
65    printf( "  -datadir <arg>    Set the location of the game data to <arg>\n" );
66    printf( "  -fullscreen       Enable fullscreen mode\n" );
67    printf( "  -h, --help        Display this text\n" );
68    printf( "  -mono             Disable stereo sound\n" );
69    printf( "  -nosound          Disable sound\n" );
70    printf( "\n" );
71    printf( "Anthony Kruize <trandor@labyrinth.net.au>\n" );
72    printf( "\n" );
73}
74
75//
76// Create a default 'abuserc' file
77//
78void createRCFile( char *rcfile )
79{
80    FILE *fd = NULL;
81
82    if( (fd = fopen( rcfile, "w" )) != NULL )
83    {
84        fputs( "; Abuse-SDL Configuration file\n\n", fd );
85        fputs( "; Startup fullscreen\nfullscreen=0\n\n", fd );
86        #ifdef __APPLE__
87        #else
88        fputs( "; Location of the datafiles\ndatadir=", fd );
89        fputs( ASSETDIR "\n\n", fd );
90        #endif
91        fputs( "; Use mono audio only\nmono=0\n\n", fd );
92        fputs( "; Grab the mouse to the window\ngrabmouse=0\n\n", fd );
93        fputs( "; Disable the SDL parachute in the case of a crash\nnosdlparachute=0\n\n", fd );
94        fputs( "; Key mappings\n", fd );
95        fputs( "left=LEFT\nright=RIGHT\nup=UP\ndown=DOWN\n", fd );
96        fputs( "fire=SPACE\nweapprev=CTRL_R\nweapnext=INSERT\n", fd );
97        fclose( fd );
98    }
99    else
100    {
101        printf( "Unable to create 'abuserc' file.\n" );
102    }
103}
104
105//
106// Read in the 'abuserc' file
107//
108void readRCFile()
109{
110    FILE *fd = NULL;
111    char *rcfile;
112    char buf[255];
113    char *result;
114
115    rcfile = (char *)malloc( strlen( get_save_filename_prefix() ) + 9 );
116    sprintf( rcfile, "%s/abuserc", get_save_filename_prefix() );
117    if( (fd = fopen( rcfile, "r" )) != NULL )
118    {
119        while( fgets( buf, sizeof( buf ), fd ) != NULL )
120        {
121            result = strtok( buf, "=" );
122            if( strcasecmp( result, "fullscreen" ) == 0 )
123            {
124                result = strtok( NULL, "\n" );
125                flags.fullscreen = atoi( result );
126            }
127            else if( strcasecmp( result, "mono" ) == 0 )
128            {
129                result = strtok( NULL, "\n" );
130                flags.mono = atoi( result );
131            }
132            else if( strcasecmp( result, "grabmouse" ) == 0 )
133            {
134                result = strtok( NULL, "\n" );
135                flags.grabmouse = atoi( result );
136            }
137            else if( strcasecmp( result, "nosdlparachute" ) == 0 )
138            {
139                result = strtok( NULL, "\n" );
140                flags.nosdlparachute = atoi( result );
141            }
142            else if( strcasecmp( result, "datadir" ) == 0 )
143            {
144                result = strtok( NULL, "\n" );
145                set_filename_prefix( result );
146            }
147            else if( strcasecmp( result, "left" ) == 0 )
148            {
149                result = strtok( NULL,"\n" );
150                keys.left = key_value( result );
151            }
152            else if( strcasecmp( result, "right" ) == 0 )
153            {
154                result = strtok( NULL,"\n" );
155                keys.right = key_value( result );
156            }
157            else if( strcasecmp( result, "up" ) == 0 )
158            {
159                result = strtok( NULL,"\n" );
160                keys.up = key_value( result );
161            }
162            else if( strcasecmp( result, "down" ) == 0 )
163            {
164                result = strtok( NULL,"\n" );
165                keys.down = key_value( result );
166            }
167            else if( strcasecmp( result, "fire" ) == 0 )
168            {
169                result = strtok( NULL,"\n" );
170                keys.b2 = key_value( result );
171            }
172            else if( strcasecmp( result, "special" ) == 0 )
173            {
174                result = strtok( NULL,"\n" );
175                keys.b1 = key_value( result );
176            }
177            else if( strcasecmp( result, "weapprev" ) == 0 )
178            {
179                result = strtok( NULL,"\n" );
180                keys.b3 = key_value( result );
181            }
182            else if( strcasecmp( result, "weapnext" ) == 0 )
183            {
184                result = strtok( NULL,"\n" );
185                keys.b4 = key_value( result );
186            }
187        }
188        fclose( fd );
189    }
190    else
191    {
192        // Couldn't open the abuserc file so let's create a default one
193        createRCFile( rcfile );
194    }
195    free( rcfile );
196}
197
198//
199// Parse the command-line parameters
200//
201void parseCommandLine( int argc, char **argv )
202{
203    for( int ii = 1; ii < argc; ii++ )
204    {
205        if( !strcasecmp( argv[ii], "-fullscreen" ) )
206        {
207            flags.fullscreen = 1;
208        }
209        else if( !strcasecmp( argv[ii], "-size" ) )
210        {
211            if( ii + 1 < argc && !sscanf( argv[++ii], "%d", &xres ) )
212            {
213                xres = 320;
214            }
215            if( ii + 1 < argc && !sscanf( argv[++ii], "%d", &yres ) )
216            {
217                yres = 200;
218            }
219        }
220        else if( !strcasecmp( argv[ii], "-nosound" ) )
221        {
222            flags.nosound = 1;
223        }
224        else if( !strcasecmp( argv[ii], "-mono" ) )
225        {
226            flags.mono = 1;
227        }
228        else if( !strcasecmp( argv[ii], "-datadir" ) )
229        {
230            char datadir[255];
231            if( ii + 1 < argc && sscanf( argv[++ii], "%s", datadir ) )
232            {
233                set_filename_prefix( datadir );
234            }
235        }
236        else if( !strcasecmp( argv[ii], "-h" ) || !strcasecmp( argv[ii], "--help" ) )
237        {
238            showHelp();
239            exit( 0 );
240        }
241    }
242}
243
244//
245// Setup SDL and configuration
246//
247void setup( int argc, char **argv )
248{
249    // Initialise default settings
250    flags.fullscreen        = 0;            // Start in a window
251    flags.mono                = 0;            // Enable stereo sound
252    flags.nosound            = 0;            // Enable sound
253    flags.grabmouse            = 0;            // Don't grab the mouse
254    flags.nosdlparachute    = 0;            // SDL error handling
255    flags.xres = xres        = 320;            // Default window width
256    flags.yres = yres        = 200;            // Default window height
257    keys.up                    = key_value( "UP" );
258    keys.down                = key_value( "DOWN" );
259    keys.left                = key_value( "LEFT" );
260    keys.right                = key_value( "RIGHT" );
261    keys.b3                    = key_value( "CTRL_R" );
262    keys.b4                    = key_value( "INSERT" );
263
264    // Display our name and version
265    printf( "%s %s\n", PACKAGE_NAME, PACKAGE_VERSION );
266
267    // Initialize SDL with video and audio support
268    if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
269    {
270        printf( "Unable to initialise SDL : %s\n", SDL_GetError() );
271        exit( 1 );
272    }
273    atexit( SDL_Quit );
274
275    // Set the savegame directory
276    char *homedir;
277    char *savedir;
278    FILE *fd = NULL;
279
280    if( (homedir = getenv( "HOME" )) != NULL )
281    {
282        savedir = (char *)malloc( strlen( homedir ) + 9 );
283        sprintf( savedir, "%s/.abuse/", homedir );
284        // Check if we already have a savegame directory
285        if( (fd = fopen( savedir, "r" )) == NULL )
286        {
287            // FIXME: Add some error checking here
288#if defined S_IRUSR && defined S_IRGRP && defined S_IROTH
289            mkdir(savedir, S_IRUSR | S_IWUSR | S_IXUSR);
290#else
291            mkdir(savedir);
292#endif
293        }
294        else
295        {
296            fclose( fd );
297        }
298        set_save_filename_prefix( savedir );
299        free( savedir );
300    }
301    else
302    {
303        // Warn the user that we couldn't set the savename prefix
304        printf( "WARNING: Unable to get $HOME environment variable.\n" );
305        printf( "         Savegames will probably fail.\n" );
306        // Just use the working directory.
307        // Hopefully they have write permissions....
308        set_save_filename_prefix( "" );
309    }
310
311    // Set the datadir to a default value
312    // (The current directory)
313    #ifdef __APPLE__
314    UInt8 buffer[255];
315    CFURLRef bundleurl = CFBundleCopyBundleURL(CFBundleGetMainBundle());
316    CFURLRef url = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, bundleurl, CFSTR("Contents/Resources/data"), true);
317
318    if (!CFURLGetFileSystemRepresentation(url, true, buffer, 255))
319    {
320        exit(1);
321    }
322    else
323        set_filename_prefix( (const char*)buffer );
324    #else
325    set_filename_prefix( ASSETDIR );
326    #endif
327
328    // Load the users configuration
329    readRCFile();
330
331    // Handle command-line parameters
332    parseCommandLine( argc, argv );
333
334    // Calculate the scaled window size.
335    flags.xres = xres;
336    flags.yres = yres;
337
338    // Stop SDL handling some errors
339    if( flags.nosdlparachute )
340    {
341        // segmentation faults
342        signal( SIGSEGV, SIG_DFL );
343        // floating point errors
344        signal( SIGFPE, SIG_DFL );
345    }
346}
347
348//
349// Get the key binding for the requested function
350//
351int get_key_binding(char const *dir, int i)
352{
353    if( strcasecmp( dir, "left" ) == 0 )
354        return keys.left;
355    else if( strcasecmp( dir, "right" ) == 0 )
356        return keys.right;
357    else if( strcasecmp( dir, "up" ) == 0 )
358        return keys.up;
359    else if( strcasecmp( dir, "down" ) == 0 )
360        return keys.down;
361    else if( strcasecmp( dir, "b1" ) == 0 )
362        return keys.b1;
363    else if( strcasecmp( dir, "b2" ) == 0 )
364        return keys.b2;
365    else if( strcasecmp( dir, "b3" ) == 0 )
366        return keys.b3;
367    else if( strcasecmp( dir, "b4" ) == 0 )
368        return keys.b4;
369
370    return 0;
371}
Note: See TracBrowser for help on using the repository browser.