- added detection magic for the datadir
[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 <pwd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <libgen.h>
31 #include <ctype.h>
32 #endif
33
34 #include "defines.h"
35 #include "globals.h"
36 #include "setup.h"
37 #include "screen.h"
38 #include "texture.h"
39 #include "menu.h"
40 #include "gameloop.h"
41
42 /* Local function prototypes: */
43
44 void seticon(void);
45 void usage(char * prog, int ret);
46
47 /* Does the given file exist and is it accessible? */
48 int faccessible(const char *filename)
49 {
50   struct stat filestat;
51   if (stat(filename, &filestat) == -1)
52     {
53       return NO;
54     }
55   else
56     {
57       if(S_ISREG(filestat.st_mode))
58         return YES;
59       else
60         return NO;
61     }
62 }
63
64 /* Can we write to this location? */
65 int fwriteable(const char *filename)
66 {
67   FILE* fi;
68   fi = fopen(filename, "wa");
69   if (fi == NULL)
70     {
71       return NO;
72     }
73   return YES;
74 }
75
76 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
77 int fcreatedir(const char* relative_dir)
78 {
79   char path[1024];
80   snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
81   if(mkdir(path,0755) != 0)
82     {
83       snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
84       if(mkdir(path,0755) != 0)
85         {
86           return NO;
87         }
88       else
89         {
90           return YES;
91         }
92     }
93   else
94     {
95       return YES;
96     }
97 }
98
99 /* Get all names of sub-directories in a certain directory. */
100 /* Returns the number of sub-directories found. */
101 /* Note: The user has to free the allocated space. */
102 string_list_type dsubdirs(const char *rel_path,const  char* expected_file)
103 {
104   DIR *dirStructP;
105   struct dirent *direntp;
106   string_list_type sdirs;
107   char filename[1024];
108   char path[1024];
109
110   string_list_init(&sdirs);
111   sprintf(path,"%s/%s",st_dir,rel_path);
112   if((dirStructP = opendir(path)) != NULL)
113     {
114       while((direntp = readdir(dirStructP)) != NULL)
115         {
116           char absolute_filename[1024];
117           struct stat buf;
118
119           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
120
121           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
122             {
123               if(expected_file != NULL)
124                 {
125                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
126                   if(!faccessible(filename))
127                     continue;
128                 }
129
130               string_list_add_item(&sdirs,direntp->d_name);
131             }
132         }
133       closedir(dirStructP);
134     }
135
136   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
137   if((dirStructP = opendir(path)) != NULL)
138     {
139       while((direntp = readdir(dirStructP)) != NULL)
140         {
141           char absolute_filename[1024];
142           struct stat buf;
143
144           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
145
146           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
147             {
148               if(expected_file != NULL)
149                 {
150                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
151                   if(!faccessible(filename))
152                     {
153                       continue;
154                     }
155                   else
156                     {
157                       sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
158                       if(faccessible(filename))
159                         continue;
160                     }
161                 }
162
163               string_list_add_item(&sdirs,direntp->d_name);
164             }
165         }
166       closedir(dirStructP);
167     }
168
169   return sdirs;
170 }
171
172 string_list_type dfiles(const char *rel_path, const  char* glob, const  char* exception_str)
173 {
174   DIR *dirStructP;
175   struct dirent *direntp;
176   string_list_type sdirs;
177   char path[1024];
178
179   string_list_init(&sdirs);
180   sprintf(path,"%s/%s",st_dir,rel_path);
181   if((dirStructP = opendir(path)) != NULL)
182     {
183       while((direntp = readdir(dirStructP)) != NULL)
184         {
185           char absolute_filename[1024];
186           struct stat buf;
187
188           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
189
190           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
191             {
192               if(exception_str != NULL)
193                 {
194                   if(strstr(direntp->d_name,exception_str) != NULL)
195                     continue;
196                 }
197               if(glob != NULL)
198                 if(strstr(direntp->d_name,glob) == NULL)
199                   continue;
200
201               string_list_add_item(&sdirs,direntp->d_name);
202             }
203         }
204       closedir(dirStructP);
205     }
206
207   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
208   if((dirStructP = opendir(path)) != NULL)
209     {
210       while((direntp = readdir(dirStructP)) != NULL)
211         {
212           char absolute_filename[1024];
213           struct stat buf;
214
215           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
216
217           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
218             {
219               if(exception_str != NULL)
220                 {
221                   if(strstr(direntp->d_name,exception_str) != NULL)
222                     continue;
223                 }
224               if(glob != NULL)
225                 if(strstr(direntp->d_name,glob) == NULL)
226                   continue;
227
228               string_list_add_item(&sdirs,direntp->d_name);
229             }
230         }
231       closedir(dirStructP);
232     }
233
234   return sdirs;
235 }
236
237 void free_strings(char **strings, int num)
238 {
239   int i;
240   for(i=0; i < num; ++i)
241     free(strings[i]);
242 }
243
244 /* --- SETUP --- */
245 /* Set SuperTux configuration and save directories */
246 void st_directory_setup(void)
247 {
248   char *home;
249   char str[1024];
250   /* Get home directory (from $HOME variable)... if we can't determine it,
251      use the current directory ("."): */
252   if (getenv("HOME") != NULL)
253     home = getenv("HOME");
254   else
255     home = ".";
256
257   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
258                                            strlen("/.supertux") + 1));
259   strcpy(st_dir, home);
260   strcat(st_dir, "/.supertux");
261
262   /* Remove .supertux config-file from old SuperTux versions */
263   if(faccessible(st_dir))
264     {
265       remove
266         (st_dir);
267     }
268
269   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
270
271   strcpy(st_save_dir,st_dir);
272   strcat(st_save_dir,"/save");
273
274   /* Create them. In the case they exist they won't destroy anything. */
275 #ifndef WIN32
276   mkdir(st_dir, 0755);
277   mkdir(st_save_dir, 0755);
278
279   sprintf(str, "%s/levels", st_dir);
280   mkdir(str, 0755);
281 #else
282   mkdir(st_dir);
283   mkdir(st_save_dir);
284   sprintf(str, "%s/levels", st_dir);
285   mkdir(str);
286 #endif
287
288   // User has not that a datadir, so we try some magic
289   if (datadir.empty())
290     {
291       // Detect datadir
292       char exe_file[PATH_MAX];
293       if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
294         {
295           puts("Couldn't read /proc/self/exe, using default path: " 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,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
491   text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
492   text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
493   text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
494   text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
495   text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
496   text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
497   text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
498
499   /* Load GUI/menu images: */
500   texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
501   texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
502   texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
503   texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
504   texture_load(&arrow_right, DATA_PREFIX "/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 #ifdef JOY_YES
665   use_joystick = YES;
666
667   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
668     {
669       fprintf(stderr, "Warning: I could not initialize joystick!\n"
670               "The Simple DirectMedia error that occured was:\n"
671               "%s\n\n", SDL_GetError());
672
673       use_joystick = NO;
674     }
675   else
676     {
677       /* Open joystick: */
678
679       if (SDL_NumJoysticks() <= 0)
680         {
681           fprintf(stderr, "Warning: No joysticks are available.\n");
682
683           use_joystick = NO;
684         }
685       else
686         {
687           js = SDL_JoystickOpen(0);
688
689           if (js == NULL)
690             {
691               fprintf(stderr, "Warning: Could not open joystick 1.\n"
692                       "The Simple DirectMedia error that occured was:\n"
693                       "%s\n\n", SDL_GetError());
694
695               use_joystick = NO;
696             }
697           else
698             {
699               /* Check for proper joystick configuration: */
700
701               if (SDL_JoystickNumAxes(js) < 2)
702                 {
703                   fprintf(stderr,
704                           "Warning: Joystick does not have enough axes!\n");
705
706                   use_joystick = NO;
707                 }
708               else
709                 {
710                   if (SDL_JoystickNumButtons(js) < 2)
711                     {
712                       fprintf(stderr,
713                               "Warning: "
714                               "Joystick does not have enough buttons!\n");
715
716                       use_joystick = NO;
717                     }
718                 }
719             }
720         }
721     }
722 #endif
723
724 }
725
726 void st_audio_setup(void)
727 {
728
729   /* Init SDL Audio silently even if --disable-sound : */
730
731   if (audio_device == YES)
732     {
733       if (SDL_Init(SDL_INIT_AUDIO) < 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 initialize audio!\n"
742                       "The Simple DirectMedia error that occured was:\n"
743                       "%s\n\n", SDL_GetError());
744             }
745           /* keep the programming logic the same :-)
746              because in this case, use_sound & use_music' values are ignored
747              when there's no available audio device
748           */
749           use_sound = NO;
750           use_music = NO;
751           audio_device = NO;
752         }
753     }
754
755
756   /* Open sound silently regarless the value of "use_sound": */
757
758   if (audio_device == YES)
759     {
760       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
761         {
762           /* only print out message if sound or music
763              was not disabled at command-line
764            */
765           if ((use_sound == YES) || (use_music == YES))
766             {
767               fprintf(stderr,
768                       "\nWarning: I could not set up audio for 44100 Hz "
769                       "16-bit stereo.\n"
770                       "The Simple DirectMedia error that occured was:\n"
771                       "%s\n\n", SDL_GetError());
772             }
773           use_sound = NO;
774           use_music = NO;
775           audio_device = NO;
776         }
777     }
778
779 }
780
781
782 /* --- SHUTDOWN --- */
783
784 void st_shutdown(void)
785 {
786   close_audio();
787   SDL_Quit();
788 }
789
790
791 /* --- ABORT! --- */
792
793 void st_abort(const char * reason,const  char * details)
794 {
795   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
796   st_shutdown();
797   exit(1);
798 }
799
800
801 /* Set Icon (private) */
802
803 void seticon(void)
804 {
805   int masklen;
806   Uint8 * mask;
807   SDL_Surface * icon;
808
809
810   /* Load icon into a surface: */
811
812   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
813   if (icon == NULL)
814     {
815       fprintf(stderr,
816               "\nError: I could not load the icon image: %s\n"
817               "The Simple DirectMedia error that occured was:\n"
818               "%s\n\n", DATA_PREFIX "/images/icon.png", SDL_GetError());
819       exit(1);
820     }
821
822
823   /* Create mask: */
824
825   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
826   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
827   memset(mask, 0xFF, masklen);
828
829
830   /* Set icon: */
831
832   SDL_WM_SetIcon(icon, mask);
833
834
835   /* Free icon surface & mask: */
836
837   free(mask);
838   SDL_FreeSurface(icon);
839 }
840
841
842 /* Parse command-line arguments: */
843
844 void parseargs(int argc, char * argv[])
845 {
846   int i;
847
848   /* Set defaults: */
849
850
851   debug_mode = NO;
852   use_fullscreen = NO;
853   show_fps = NO;
854   use_gl = NO;
855
856 #ifndef NOSOUND
857
858   use_sound = YES;
859   use_music = YES;
860   audio_device = YES;
861 #else
862
863   use_sound = NO;
864   use_music = NO;
865   audio_device = NO;
866 #endif
867
868   /* Parse arguments: */
869
870   for (i = 1; i < argc; i++)
871     {
872       if (strcmp(argv[i], "--fullscreen") == 0 ||
873           strcmp(argv[i], "-f") == 0)
874         {
875           /* Use full screen: */
876
877           use_fullscreen = YES;
878         }
879       else if (strcmp(argv[i], "--worldmap") == 0)
880         {
881           launch_worldmap_mode = true;
882         }
883       else if (strcmp(argv[i], "--datadir") == 0 
884                || strcmp(argv[i], "-d") == 0 )
885         {
886           assert(i+1 < argc);
887           datadir = argv[i+1];
888         }
889       else if (strcmp(argv[i], "--show-fps") == 0)
890         {
891           /* Use full screen: */
892
893           show_fps = YES;
894         }
895       else if (strcmp(argv[i], "--opengl") == 0 ||
896                strcmp(argv[i], "-gl") == 0)
897         {
898 #ifndef NOOPENGL
899           /* Use OpengGL: */
900
901           use_gl = YES;
902 #endif
903
904         }
905       else if (strcmp(argv[i], "--usage") == 0)
906         {
907           /* Show usage: */
908
909           usage(argv[0], 0);
910         }
911       else if (strcmp(argv[i], "--version") == 0)
912         {
913           /* Show version: */
914
915           printf("Super Tux - version " VERSION "\n");
916           exit(0);
917         }
918       else if (strcmp(argv[i], "--disable-sound") == 0)
919         {
920           /* Disable the compiled in sound feature */
921 #ifndef NOSOUND
922           printf("Sounds disabled \n");
923           use_sound = NO;
924 #else
925
926           printf("Warning: Sound capability has not been compiled into this build.\n");
927 #endif
928
929         }
930       else if (strcmp(argv[i], "--disable-music") == 0)
931         {
932           /* Disable the compiled in sound feature */
933 #ifndef NOSOUND
934           printf("Music disabled \n");
935           use_music = NO;
936 #else
937
938           printf("Warning: Music feature is not compiled in \n");
939 #endif
940
941         }
942       else if (strcmp(argv[i], "--debug-mode") == 0)
943         {
944           /* Enable the debug-mode */
945           debug_mode = YES;
946
947         }
948       else if (strcmp(argv[i], "--help") == 0)
949         {         /* Show help: */
950           puts("Super Tux " VERSION "\n"
951                "  Please see the file \"README.txt\" for more details.\n");
952           printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
953           puts("Display Options:\n"
954                "  --fullscreen      Run in fullscreen mode.\n"
955                "  --opengl          If opengl support was compiled in, this will enable\n"
956                "                    the EXPERIMENTAL OpenGL mode.\n"
957                "\n"
958                "Sound Options:\n"
959                "  --disable-sound   If sound support was compiled in,  this will\n"
960                "                    disable sound for this session of the game.\n"
961                "  --disable-music   Like above, but this will disable music.\n"
962                "\n"
963                "Misc Options:\n"
964                "  --worldmap        Start in worldmap-mode (EXPERIMENTAL)\n"          
965                "  -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
966                "  --debug-mode      Enables the debug-mode, which is useful for developers.\n"
967                "  --help            Display a help message summarizing command-line\n"
968                "                    options, license and game controls.\n"
969                "  --usage           Display a brief message summarizing command-line options.\n"
970                "  --version         Display the version of SuperTux you're running.\n\n"
971                );
972           exit(0);
973         }
974       else if (argv[i][0] != '-')
975         {
976           level_startup_file = argv[i];
977         }
978       else
979         {
980           /* Unknown - complain! */
981
982           usage(argv[0], 1);
983         }
984     }
985 }
986
987
988 /* Display usage: */
989
990 void usage(char * prog, int ret)
991 {
992   FILE * fi;
993
994
995   /* Determine which stream to write to: */
996
997   if (ret == 0)
998     fi = stdout;
999   else
1000     fi = stderr;
1001
1002
1003   /* Display the usage message: */
1004
1005   fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",
1006           prog);
1007
1008
1009   /* Quit! */
1010
1011   exit(ret);
1012 }
1013