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