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