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