applied a patch by Duong-Khang NGUYEN <neoneurone@users.sf.net> and fixed it. ;)...
[supertux.git] / src / setup.c
1 /*
2   setup.c
3   
4   Super Tux - Setup
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - November 7, 2001
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <ctype.h>
26 #endif
27
28 #include "defines.h"
29 #include "globals.h"
30 #include "setup.h"
31 #include "screen.h"
32 #include "sound.h"
33
34 /* Local function prototypes: */
35
36 void seticon(void);
37 void usage(char * prog, int ret);
38
39
40 /* --- SETUP --- */
41
42 void st_setup(void)
43 {
44
45   /* Set SuperTux configuration and save directories */
46
47   /* Get home directory (from $HOME variable)... if we can't determine it,
48      use the current directory ("."): */
49   char *home;
50   if (getenv("HOME") != NULL)
51     home = getenv("HOME");
52   else
53     home = ".";
54
55   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
56                            strlen("/.supertux") + 1));
57   strcpy(st_dir, home);
58   strcat(st_dir, "/.supertux");
59
60   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
61
62   strcpy(st_save_dir,st_dir);
63   strcat(st_save_dir,"/save");
64
65   /* Create them. In the case they exist it won't destroy anything. */
66   mkdir(st_dir, 0755);
67   mkdir(st_save_dir, 0755);
68
69   /* Seed random number generator: */
70
71   srand(SDL_GetTicks());
72
73
74   /* Init SDL Video: */
75
76   if (SDL_Init(SDL_INIT_VIDEO) < 0)
77     {
78       fprintf(stderr,
79               "\nError: I could not initialize video!\n"
80               "The Simple DirectMedia error that occured was:\n"
81               "%s\n\n", SDL_GetError());
82       exit(1);
83     }
84
85
86   /* Init Joystick: */
87
88 #ifdef JOY_YES
89   use_joystick = YES;
90
91   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
92     {
93       fprintf(stderr, "Warning: I could not initialize joystick!\n"
94               "The Simple DirectMedia error that occured was:\n"
95               "%s\n\n", SDL_GetError());
96
97       use_joystick = NO;
98     }
99   else
100     {
101       /* Open joystick: */
102
103       if (SDL_NumJoysticks() <= 0)
104         {
105           fprintf(stderr, "Warning: No joysticks are available.\n");
106
107           use_joystick = NO;
108         }
109       else
110         {
111           js = SDL_JoystickOpen(0);
112
113           if (js == NULL)
114             {
115               fprintf(stderr, "Warning: Could not open joystick 1.\n"
116                       "The Simple DirectMedia error that occured was:\n"
117                       "%s\n\n", SDL_GetError());
118
119               use_joystick = NO;
120             }
121           else
122             {
123               /* Check for proper joystick configuration: */
124
125               if (SDL_JoystickNumAxes(js) < 2)
126                 {
127                   fprintf(stderr,
128                           "Warning: Joystick does not have enough axes!\n");
129
130                   use_joystick = NO;
131                 }
132               else
133                 {
134                   if (SDL_JoystickNumButtons(js) < 2)
135                     {
136                       fprintf(stderr,
137                               "Warning: "
138                               "Joystick does not have enough buttons!\n");
139
140                       use_joystick = NO;
141                     }
142                 }
143             }
144         }
145     }
146 #endif
147
148
149
150
151   /* Init SDL Audio silently even if --disable-sound : */
152
153   if (audio_device == YES)
154     {
155       if (SDL_Init(SDL_INIT_AUDIO) < 0)
156         {
157           /* only print out message if sound or music
158              was not disabled at command-line
159            */
160           if (use_sound == YES || use_music == YES)
161             {
162               fprintf(stderr,
163                       "\nWarning: I could not initialize audio!\n"
164                       "The Simple DirectMedia error that occured was:\n"
165                       "%s\n\n", SDL_GetError());
166             }
167           /* keep the programming logic the same :-)
168              because in this case, use_sound & use_music' values are ignored
169              when there's no available audio device
170           */
171           use_sound = NO;
172           use_music = NO;
173           audio_device = NO;
174         }
175     }
176
177
178   /* Open sound silently regarless the value of "use_sound": */
179
180   if (audio_device == YES)
181     {
182       if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
183         {
184           /* only print out message if sound or music
185              was not disabled at command-line
186            */
187           if ((use_sound == YES) || (use_music == YES))
188             {
189               fprintf(stderr,
190                       "\nWarning: I could not set up audio for 44100 Hz "
191                       "16-bit stereo.\n"
192                       "The Simple DirectMedia error that occured was:\n"
193                       "%s\n\n", SDL_GetError());
194             }
195           use_sound = NO;
196           use_music = NO;
197           audio_device = NO;
198         }
199     }
200
201
202   /* Open display: */
203
204   if (use_fullscreen == YES)
205     {
206       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
207       if (screen == NULL)
208         {
209           fprintf(stderr,
210                   "\nWarning: I could not set up fullscreen video for "
211                   "640x480 mode.\n"
212                   "The Simple DirectMedia error that occured was:\n"
213                   "%s\n\n", SDL_GetError());
214           use_fullscreen = NO;
215         }
216     }
217
218   if (use_fullscreen == NO)
219     {
220       screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
221
222       if (screen == NULL)
223         {
224           fprintf(stderr,
225                   "\nError: I could not set up video for 640x480 mode.\n"
226                   "The Simple DirectMedia error that occured was:\n"
227                   "%s\n\n", SDL_GetError());
228           exit(1);
229         }
230     }
231
232
233   /* Load global images: */
234
235   letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
236                              USE_ALPHA);
237
238   letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
239                             USE_ALPHA);
240
241   letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
242                             USE_ALPHA);
243
244   letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
245                            USE_ALPHA);
246
247
248   /* Set icon image: */
249
250   seticon();
251
252
253   /* Set window manager stuff: */
254
255   SDL_WM_SetCaption("Super Tux", "Super Tux");
256 }
257
258
259 /* --- SHUTDOWN --- */
260
261 void st_shutdown(void)
262 {
263   SDL_Quit();
264 }
265
266
267 /* --- ABORT! --- */
268
269 void st_abort(char * reason, char * details)
270 {
271   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
272   st_shutdown();
273   exit(1);
274 }
275
276
277 /* Set Icon (private) */
278
279 void seticon(void)
280 {
281   int masklen;
282   Uint8 * mask;
283   SDL_Surface * icon;
284
285
286   /* Load icon into a surface: */
287
288   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
289   if (icon == NULL)
290     {
291       fprintf(stderr,
292               "\nError: I could not load the icon image: %s\n"
293               "The Simple DirectMedia error that occured was:\n"
294               "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
295       exit(1);
296     }
297
298
299   /* Create mask: */
300
301   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
302   mask = malloc(masklen * sizeof(Uint8));
303   memset(mask, 0xFF, masklen);
304
305
306   /* Set icon: */
307
308   SDL_WM_SetIcon(icon, mask);
309
310
311   /* Free icon surface & mask: */
312
313   free(mask);
314   SDL_FreeSurface(icon);
315 }
316
317
318 /* Parse command-line arguments: */
319
320 void parseargs(int argc, char * argv[])
321 {
322   int i;
323
324
325   /* Set defaults: */
326
327   use_fullscreen = NO;
328 #ifndef NOSOUND
329
330   use_sound = YES;
331   use_music = YES;
332   audio_device = YES;
333 #else
334
335   use_sound = NO;
336   use_music = NO;
337   audio_device = NO;
338 #endif
339
340   /* Parse arguments: */
341
342   for (i = 1; i < argc; i++)
343     {
344       if (strcmp(argv[i], "--fullscreen") == 0 ||
345           strcmp(argv[i], "-f") == 0)
346         {
347           /* Use full screen: */
348
349           use_fullscreen = YES;
350         }
351       else if (strcmp(argv[i], "--usage") == 0)
352         {
353           /* Show usage: */
354
355           usage(argv[0], 0);
356         }
357       else if (strcmp(argv[i], "--version") == 0)
358         {
359           /* Show version: */
360
361           printf("Super Tux - version " VERSION "\n");
362           exit(0);
363         }
364       else if (strcmp(argv[i], "--disable-sound") == 0)
365         {
366           /* Disable the compiled in sound feature */
367 #ifndef NOSOUND
368           printf("Sounds disabled \n");
369           use_sound = NO;
370 #else
371
372           printf("Warning: Sounds feature is not compiled in \n");
373           printf("Warning: Sounds feature is not compiled in \n");
374 #endif
375         }
376       else if (strcmp(argv[i], "--disable-music") == 0)
377         {
378           /* Disable the compiled in sound feature */
379 #ifndef NOSOUND
380           printf("Music disabled \n");
381           use_music = NO;
382 #else
383           printf("Warning: Music feature is not compiled in \n");
384 #endif
385
386         }
387       else if (strcmp(argv[i], "--help") == 0)
388         {         /* Show help: */
389
390           printf("Super Tux " VERSION "\n\n");
391
392           printf("----------  Command-line options  ----------\n\n");
393
394           printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable sound for this session of the game.\n\n");
395
396           printf("  --disable-music     - Like above, but this will disable music.\n\n");
397
398           printf("  --fullscreen        - Run in fullscreen mode.\n\n");
399
400           printf("  --help              - Display a help message summarizing command-line\n                        options, license and game controls.\n\n");
401
402           printf("  --usage             - Display a brief message summarizing command-line options.\n\n");
403
404           printf("  --version           - Display the version of SuperTux you're running.\n\n\n");
405
406
407           printf("----------          License       ----------\n\n");
408           printf("  This program comes with ABSOLUTELY NO WARRANTY.\n");
409           printf("  This is free software, and you are welcome to redistribute\n");
410           printf("  or modify it under certain conditions. See the file \n");
411           printf("  \"COPYING.txt\" for more details.\n\n\n");
412
413           printf("----------      Game controls     ----------\n\n");
414           printf("  Please see the file \"README.txt\"\n\n");
415
416           exit(0);
417         }
418       else
419         {
420           /* Unknown - complain! */
421
422           usage(argv[0], 1);
423         }
424     }
425 }
426
427
428 /* Display usage: */
429
430 void usage(char * prog, int ret)
431 {
432   FILE * fi;
433
434
435   /* Determine which stream to write to: */
436
437   if (ret == 0)
438     fi = stdout;
439   else
440     fi = stderr;
441
442
443   /* Display the usage message: */
444
445   fprintf(fi, "Usage: %s [--fullscreen] [--disable-sound] [--disable-music] | [--usage | --help | --version]\n",
446            prog);
447
448
449   /* Quit! */
450
451   exit(ret);
452 }