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