- replaced #ifdef LINUX with #ifndef WIN32, should be easier to handle for other...
[supertux.git] / src / setup.cpp
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 - March 15, 2004
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 #ifndef NOOPENGL
21 #include <SDL_opengl.h>
22 #endif
23
24 #ifndef WIN32
25 #include <pwd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <ctype.h>
30 #endif
31
32 #include "defines.h"
33 #include "globals.h"
34 #include "setup.h"
35 #include "screen.h"
36 #include "texture.h"
37 #include "menu.h"
38 #include "gameloop.h"
39
40 /* Local function prototypes: */
41
42 void seticon(void);
43 void usage(char * prog, int ret);
44
45 /* Does the given file exist and is it accessible? */
46 int faccessible(char *filename)
47 {
48   struct stat filestat;
49   if (stat(filename, &filestat) == -1)
50     {
51       return NO;
52     }
53   else
54     {
55       if(S_ISREG(filestat.st_mode))
56         return YES;
57       else
58         return NO;
59     }
60 }
61
62 /* Can we write to this location? */
63 int fwriteable(char *filename)
64 {
65   FILE* fi;
66   fi = fopen(filename, "wa");
67   if (fi == NULL)
68     {
69       return NO;
70     }
71   return YES;
72 }
73
74 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
75 int fcreatedir(char* relative_dir)
76 {
77   char path[1024];
78   snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
79   if(mkdir(path,0755) != 0)
80     {
81       snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
82       if(mkdir(path,0755) != 0)
83         {
84           return NO;
85         }
86       else
87         {
88           return YES;
89         }
90     }
91   else
92     {
93       return YES;
94     }
95 }
96
97 /* Get all names of sub-directories in a certain directory. */
98 /* Returns the number of sub-directories found. */
99 /* Note: The user has to free the allocated space. */
100 string_list_type dsubdirs(char *rel_path, char* expected_file)
101 {
102   DIR *dirStructP;
103   struct dirent *direntp;
104   int i = 0;
105   string_list_type sdirs;
106   char filename[1024];
107   char path[1024];
108
109   string_list_init(&sdirs);
110   sprintf(path,"%s/%s",st_dir,rel_path);
111   if((dirStructP = opendir(path)) != NULL)
112     {
113       while((direntp = readdir(dirStructP)) != NULL)
114         {
115           char absolute_filename[1024];
116           struct stat buf;
117
118           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
119
120           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
121             {
122               if(expected_file != NULL)
123                 {
124                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
125                   if(!faccessible(filename))
126                     continue;
127                 }
128
129               string_list_add_item(&sdirs,direntp->d_name);
130             }
131         }
132       closedir(dirStructP);
133     }
134
135   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
136   if((dirStructP = opendir(path)) != NULL)
137     {
138       while((direntp = readdir(dirStructP)) != NULL)
139         {
140           char absolute_filename[1024];
141           struct stat buf;
142
143           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
144
145           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
146             {
147               if(expected_file != NULL)
148                 {
149                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
150                   if(!faccessible(filename))
151                     {
152                       continue;
153                     }
154                   else
155                     {
156                       sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
157                       if(faccessible(filename))
158                         continue;
159                     }
160                 }
161
162               string_list_add_item(&sdirs,direntp->d_name);
163             }
164         }
165       closedir(dirStructP);
166     }
167
168   return sdirs;
169 }
170
171 string_list_type dfiles(char *rel_path, char* glob, char* exception_str)
172 {
173   DIR *dirStructP;
174   struct dirent *direntp;
175   int i = 0;
176   string_list_type sdirs;
177   char filename[1024];
178   char path[1024];
179
180   string_list_init(&sdirs);
181   sprintf(path,"%s/%s",st_dir,rel_path);
182   if((dirStructP = opendir(path)) != NULL)
183     {
184       while((direntp = readdir(dirStructP)) != NULL)
185         {
186           char absolute_filename[1024];
187           struct stat buf;
188
189           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
190
191           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
192             {
193               if(exception_str != NULL)
194                 {
195                   if(strstr(direntp->d_name,exception_str) != NULL)
196                     continue;
197                 }
198               if(glob != NULL)
199                 if(strstr(direntp->d_name,glob) == NULL)
200                   continue;
201
202               string_list_add_item(&sdirs,direntp->d_name);
203             }
204         }
205       closedir(dirStructP);
206     }
207
208   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
209   if((dirStructP = opendir(path)) != NULL)
210     {
211       while((direntp = readdir(dirStructP)) != NULL)
212         {
213           char absolute_filename[1024];
214           struct stat buf;
215
216           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
217
218           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
219             {
220               if(exception_str != NULL)
221                 {
222                   if(strstr(direntp->d_name,exception_str) != NULL)
223                     continue;
224                 }
225               if(glob != NULL)
226                 if(strstr(direntp->d_name,glob) == NULL)
227                   continue;
228
229               string_list_add_item(&sdirs,direntp->d_name);
230             }
231         }
232       closedir(dirStructP);
233     }
234
235   return sdirs;
236 }
237
238 void free_strings(char **strings, int num)
239 {
240   int i;
241   for(i=0; i < num; ++i)
242     free(strings[i]);
243 }
244
245 /* --- SETUP --- */
246 /* Set SuperTux configuration and save directories */
247 void st_directory_setup(void)
248 {
249   char *home;
250   char str[1024];
251   /* Get home directory (from $HOME variable)... if we can't determine it,
252      use the current directory ("."): */
253   if (getenv("HOME") != NULL)
254     home = getenv("HOME");
255   else
256     home = ".";
257
258   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
259                            strlen("/.supertux") + 1));
260   strcpy(st_dir, home);
261   strcat(st_dir, "/.supertux");
262
263   /* Remove .supertux config-file from old SuperTux versions */
264   if(faccessible(st_dir))
265     {
266       remove
267         (st_dir);
268     }
269
270   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
271
272   strcpy(st_save_dir,st_dir);
273   strcat(st_save_dir,"/save");
274
275   /* Create them. In the case they exist they won't destroy anything. */
276 #ifndef WIN32
277   mkdir(st_dir, 0755);
278   mkdir(st_save_dir, 0755);
279
280   sprintf(str, "%s/levels", st_dir);
281   mkdir(str, 0755);
282 #else
283   mkdir(st_dir);
284   mkdir(st_save_dir);
285   sprintf(str, "%s/levels", st_dir);
286   mkdir(str);
287 #endif
288
289 }
290
291 /* Create and setup menus. */
292 void st_menu(void)
293 {
294   menu_init(&main_menu);
295   menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
296   menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
297   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
298   menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
299   menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
300   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
301   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
302   menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
303   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
304
305   menu_init(&options_menu);
306   menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
307   menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
308   menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
309   if(audio_device == YES)
310     {
311       menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound     ",use_sound,0));
312       menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music     ",use_music,0));
313     }
314   else
315     {
316       menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound     ",use_sound,0));
317       menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music     ",use_music,0));
318     }
319   menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS  ",show_fps,0));
320   menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
321   menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
322
323   menu_init(&load_game_menu);
324   menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
325   menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
326   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
327   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
328   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
329   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
330   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
331   menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
332   menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
333
334   menu_init(&save_game_menu);
335   menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
336   menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
337   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
338   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
339   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
340   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
341   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
342   menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
343   menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
344
345   menu_init(&game_menu);
346   menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
347   menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
348   menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
349   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
350   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
351   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
352   menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
353   menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
354
355   menu_init(&highscore_menu);
356   menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
357
358 }
359
360 void update_load_save_game_menu(menu_type* pmenu, int load)
361 {
362   int i;
363
364   for(i = 2; i < 7; ++i)
365     {
366       char *tmp;
367       slotinfo(&tmp,i-1);
368       if(load && strlen(tmp) == strlen("Slot X - Free") )
369         pmenu->item[i].kind = MN_DEACTIVE;
370       else
371         pmenu->item[i].kind = MN_ACTION;
372       menu_item_change_text(&pmenu->item[i],tmp);
373       free(tmp);
374     }
375 }
376
377 void process_save_load_game_menu(int save)
378 {
379   int slot;
380   switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
381     {
382     default:
383       if(slot != -1)
384         {
385           if(save == YES)
386             {
387               savegame(slot - 1);
388             }
389           else
390             {
391               if(game_started == NO)
392                 {
393                   gameloop("default",slot - 1,ST_GL_LOAD_GAME);
394                   show_menu = YES;
395                   menu_set_current(&main_menu);
396                 }
397               else
398                 loadgame(slot - 1);
399             }
400           st_pause_ticks_stop();
401         }
402       break;
403     }
404 }
405
406 /* Handle changes made to global settings in the options menu. */
407 void process_options_menu(void)
408 {
409   switch (menu_check(&options_menu))
410     {
411     case 2:
412       if(use_fullscreen != options_menu.item[2].toggled)
413         {
414           use_fullscreen = !use_fullscreen;
415           st_video_setup();
416         }
417       break;
418     case 3:
419       if(use_sound != options_menu.item[3].toggled)
420         use_sound = !use_sound;
421       break;
422     case 4:
423       if(use_music != options_menu.item[4].toggled)
424         {
425           if(use_music == YES)
426             {
427               if(playing_music())
428                 {
429                   halt_music();
430                 }
431               use_music = NO;
432             }
433           else
434             {
435               use_music = YES;
436               if (!playing_music())
437                 {
438                   play_current_music();
439                 }
440             }
441         }
442       break;
443     case 5:
444       if(show_fps != options_menu.item[5].toggled)
445         show_fps = !show_fps;
446       break;
447     }
448 }
449
450 void st_general_setup(void)
451 {
452   /* Seed random number generator: */
453
454   srand(SDL_GetTicks());
455
456   /* Set icon image: */
457
458   seticon();
459
460   /* Unicode needed for input handling: */
461
462   SDL_EnableUNICODE(1);
463
464   /* Load global images: */
465
466   text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
467   text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
468   text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
469   text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
470   text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
471   text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
472   text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
473   text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
474
475   /* Load GUI/menu images: */
476   texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
477   texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
478   texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
479   texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
480   texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
481
482 }
483
484 void st_general_free(void)
485 {
486
487   /* Free global images: */
488
489   text_free(&black_text);
490   text_free(&gold_text);
491   text_free(&white_text);
492   text_free(&blue_text);
493   text_free(&red_text);
494   text_free(&white_small_text);
495   text_free(&white_big_text);
496
497   /* Free GUI/menu images: */
498   texture_free(&checkbox);
499   texture_free(&checkbox_checked);
500   texture_free(&back);
501   texture_free(&arrow_left);
502   texture_free(&arrow_right);
503
504   /* Free menus */
505
506   menu_free(&main_menu);
507   menu_free(&game_menu);
508   menu_free(&options_menu);
509   menu_free(&highscore_menu);
510   menu_free(&save_game_menu);
511   menu_free(&load_game_menu);
512
513 }
514
515 void st_video_setup(void)
516 {
517
518   if(screen != NULL)
519     SDL_FreeSurface(screen);
520
521   /* Init SDL Video: */
522
523   if (SDL_Init(SDL_INIT_VIDEO) < 0)
524     {
525       fprintf(stderr,
526               "\nError: I could not initialize video!\n"
527               "The Simple DirectMedia error that occured was:\n"
528               "%s\n\n", SDL_GetError());
529       exit(1);
530     }
531
532   /* Open display: */
533
534   if(use_gl)
535     st_video_setup_gl();
536   else
537     st_video_setup_sdl();
538
539   texture_setup();
540
541   /* Set window manager stuff: */
542
543   SDL_WM_SetCaption("Super Tux", "Super Tux");
544
545 }
546
547 void st_video_setup_sdl(void)
548 {
549   SDL_FreeSurface(screen);
550
551   if (use_fullscreen == YES)
552     {
553       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
554       if (screen == NULL)
555         {
556           fprintf(stderr,
557                   "\nWarning: I could not set up fullscreen video for "
558                   "640x480 mode.\n"
559                   "The Simple DirectMedia error that occured was:\n"
560                   "%s\n\n", SDL_GetError());
561           use_fullscreen = NO;
562         }
563     }
564   else
565     {
566       screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
567
568       if (screen == NULL)
569         {
570           fprintf(stderr,
571                   "\nError: I could not set up video for 640x480 mode.\n"
572                   "The Simple DirectMedia error that occured was:\n"
573                   "%s\n\n", SDL_GetError());
574           exit(1);
575         }
576     }
577 }
578
579 void st_video_setup_gl(void)
580 {
581 #ifndef NOOPENGL
582
583   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
584   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
585   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
586   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
587   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
588
589   if (use_fullscreen == YES)
590     {
591       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
592       if (screen == NULL)
593         {
594           fprintf(stderr,
595                   "\nWarning: I could not set up fullscreen video for "
596                   "640x480 mode.\n"
597                   "The Simple DirectMedia error that occured was:\n"
598                   "%s\n\n", SDL_GetError());
599           use_fullscreen = NO;
600         }
601     }
602   else
603     {
604       screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
605
606       if (screen == NULL)
607         {
608           fprintf(stderr,
609                   "\nError: I could not set up video for 640x480 mode.\n"
610                   "The Simple DirectMedia error that occured was:\n"
611                   "%s\n\n", SDL_GetError());
612           exit(1);
613         }
614     }
615
616   /*
617    * Set up OpenGL for 2D rendering.
618    */
619   glDisable(GL_DEPTH_TEST);
620   glDisable(GL_CULL_FACE);
621
622   glViewport(0, 0, screen->w, screen->h);
623   glMatrixMode(GL_PROJECTION);
624   glLoadIdentity();
625   glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
626
627   glMatrixMode(GL_MODELVIEW);
628   glLoadIdentity();
629   glTranslatef(0.0f, 0.0f, 0.0f);
630
631 #endif
632
633 }
634
635 void st_joystick_setup(void)
636 {
637
638   /* Init Joystick: */
639
640 #ifdef JOY_YES
641   use_joystick = YES;
642
643   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
644     {
645       fprintf(stderr, "Warning: I could not initialize joystick!\n"
646               "The Simple DirectMedia error that occured was:\n"
647               "%s\n\n", SDL_GetError());
648
649       use_joystick = NO;
650     }
651   else
652     {
653       /* Open joystick: */
654
655       if (SDL_NumJoysticks() <= 0)
656         {
657           fprintf(stderr, "Warning: No joysticks are available.\n");
658
659           use_joystick = NO;
660         }
661       else
662         {
663           js = SDL_JoystickOpen(0);
664
665           if (js == NULL)
666             {
667               fprintf(stderr, "Warning: Could not open joystick 1.\n"
668                       "The Simple DirectMedia error that occured was:\n"
669                       "%s\n\n", SDL_GetError());
670
671               use_joystick = NO;
672             }
673           else
674             {
675               /* Check for proper joystick configuration: */
676
677               if (SDL_JoystickNumAxes(js) < 2)
678                 {
679                   fprintf(stderr,
680                           "Warning: Joystick does not have enough axes!\n");
681
682                   use_joystick = NO;
683                 }
684               else
685                 {
686                   if (SDL_JoystickNumButtons(js) < 2)
687                     {
688                       fprintf(stderr,
689                               "Warning: "
690                               "Joystick does not have enough buttons!\n");
691
692                       use_joystick = NO;
693                     }
694                 }
695             }
696         }
697     }
698 #endif
699
700 }
701
702 void st_audio_setup(void)
703 {
704
705   /* Init SDL Audio silently even if --disable-sound : */
706
707   if (audio_device == YES)
708     {
709       if (SDL_Init(SDL_INIT_AUDIO) < 0)
710         {
711           /* only print out message if sound or music
712              was not disabled at command-line
713            */
714           if (use_sound == YES || use_music == YES)
715             {
716               fprintf(stderr,
717                       "\nWarning: I could not initialize audio!\n"
718                       "The Simple DirectMedia error that occured was:\n"
719                       "%s\n\n", SDL_GetError());
720             }
721           /* keep the programming logic the same :-)
722              because in this case, use_sound & use_music' values are ignored
723              when there's no available audio device
724           */
725           use_sound = NO;
726           use_music = NO;
727           audio_device = NO;
728         }
729     }
730
731
732   /* Open sound silently regarless the value of "use_sound": */
733
734   if (audio_device == YES)
735     {
736       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
737         {
738           /* only print out message if sound or music
739              was not disabled at command-line
740            */
741           if ((use_sound == YES) || (use_music == YES))
742             {
743               fprintf(stderr,
744                       "\nWarning: I could not set up audio for 44100 Hz "
745                       "16-bit stereo.\n"
746                       "The Simple DirectMedia error that occured was:\n"
747                       "%s\n\n", SDL_GetError());
748             }
749           use_sound = NO;
750           use_music = NO;
751           audio_device = NO;
752         }
753     }
754
755 }
756
757
758 /* --- SHUTDOWN --- */
759
760 void st_shutdown(void)
761 {
762   close_audio();
763   SDL_Quit();
764 }
765
766
767 /* --- ABORT! --- */
768
769 void st_abort(char * reason, char * details)
770 {
771   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
772   st_shutdown();
773   exit(1);
774 }
775
776
777 /* Set Icon (private) */
778
779 void seticon(void)
780 {
781   int masklen;
782   Uint8 * mask;
783   SDL_Surface * icon;
784
785
786   /* Load icon into a surface: */
787
788   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
789   if (icon == NULL)
790     {
791       fprintf(stderr,
792               "\nError: I could not load the icon image: %s\n"
793               "The Simple DirectMedia error that occured was:\n"
794               "%s\n\n", DATA_PREFIX "/images/icon.png", SDL_GetError());
795       exit(1);
796     }
797
798
799   /* Create mask: */
800
801   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
802   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
803   memset(mask, 0xFF, masklen);
804
805
806   /* Set icon: */
807
808   SDL_WM_SetIcon(icon, mask);
809
810
811   /* Free icon surface & mask: */
812
813   free(mask);
814   SDL_FreeSurface(icon);
815 }
816
817
818 /* Parse command-line arguments: */
819
820 void parseargs(int argc, char * argv[])
821 {
822   int i;
823
824   /* Set defaults: */
825
826
827   debug_mode = NO;
828   use_fullscreen = NO;
829   show_fps = NO;
830   use_gl = NO;
831
832 #ifndef NOSOUND
833
834   use_sound = YES;
835   use_music = YES;
836   audio_device = YES;
837 #else
838
839   use_sound = NO;
840   use_music = NO;
841   audio_device = NO;
842 #endif
843
844   /* Parse arguments: */
845
846   for (i = 1; i < argc; i++)
847     {
848       if (strcmp(argv[i], "--fullscreen") == 0 ||
849           strcmp(argv[i], "-f") == 0)
850         {
851           /* Use full screen: */
852
853           use_fullscreen = YES;
854         }
855       else if (strcmp(argv[i], "--worldmap") == 0)
856         {
857           launch_worldmap_mode = true;
858         }
859       else if (strcmp(argv[i], "--show-fps") == 0)
860         {
861           /* Use full screen: */
862
863           show_fps = YES;
864         }
865       else if (strcmp(argv[i], "--opengl") == 0 ||
866                strcmp(argv[i], "-gl") == 0)
867         {
868 #ifndef NOOPENGL
869           /* Use OpengGL: */
870
871           use_gl = YES;
872 #endif
873
874         }
875       else if (strcmp(argv[i], "--usage") == 0)
876         {
877           /* Show usage: */
878
879           usage(argv[0], 0);
880         }
881       else if (strcmp(argv[i], "--version") == 0)
882         {
883           /* Show version: */
884
885           printf("Super Tux - version " VERSION "\n");
886           exit(0);
887         }
888       else if (strcmp(argv[i], "--disable-sound") == 0)
889         {
890           /* Disable the compiled in sound feature */
891 #ifndef NOSOUND
892           printf("Sounds disabled \n");
893           use_sound = NO;
894 #else
895
896           printf("Warning: Sound capability has not been compiled into this build.\n");
897 #endif
898
899         }
900       else if (strcmp(argv[i], "--disable-music") == 0)
901         {
902           /* Disable the compiled in sound feature */
903 #ifndef NOSOUND
904           printf("Music disabled \n");
905           use_music = NO;
906 #else
907
908           printf("Warning: Music feature is not compiled in \n");
909 #endif
910
911         }
912       else if (strcmp(argv[i], "--debug-mode") == 0)
913         {
914           /* Enable the debug-mode */
915           debug_mode = YES;
916
917         }
918       else if (strcmp(argv[i], "--help") == 0)
919         {         /* Show help: */
920           puts("Super Tux " VERSION "\n"
921                "  Please see the file \"README.txt\" for more details.\n");
922           printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
923           puts("Display Options:\n"
924                "  --fullscreen      Run in fullscreen mode.\n"
925                "  --opengl          If opengl support was compiled in, this will enable\n"
926                "                    the EXPERIMENTAL OpenGL mode.\n"
927                "\n"
928                "Sound Options:\n"
929                "  --disable-sound   If sound support was compiled in,  this will\n"
930                "                    disable sound for this session of the game.\n"
931                "  --disable-music   Like above, but this will disable music.\n"
932                "\n"
933                "Misc Options:\n"
934                "  --worldmap        Start in worldmap-mode (EXPERIMENTAL)\n"          
935                "  --debug-mode      Enables the debug-mode, which is useful for developers.\n"
936                "  --help            Display a help message summarizing command-line\n"
937                "                    options, license and game controls.\n"
938                "  --usage           Display a brief message summarizing command-line options.\n"
939                "  --version         Display the version of SuperTux you're running.\n\n"
940                );
941           exit(0);
942         }
943       else if (argv[i][0] != '-')
944         {
945           level_startup_file = argv[i];
946         }
947       else
948         {
949           /* Unknown - complain! */
950
951           usage(argv[0], 1);
952         }
953     }
954 }
955
956
957 /* Display usage: */
958
959 void usage(char * prog, int ret)
960 {
961   FILE * fi;
962
963
964   /* Determine which stream to write to: */
965
966   if (ret == 0)
967     fi = stdout;
968   else
969     fi = stderr;
970
971
972   /* Display the usage message: */
973
974   fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",
975           prog);
976
977
978   /* Quit! */
979
980   exit(ret);
981 }
982