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