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