- added draw_part()
[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,"InGame Menu",0,0);
426   game_menu->additem(MN_HL,"",0,0);
427   game_menu->additem(MN_ACTION,"Return To Game",0,0);
428   game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
429   game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
430   game_menu->additem(MN_GOTO,"Options",0,options_menu);
431   game_menu->additem(MN_HL,"",0,0);
432   game_menu->additem(MN_ACTION,"Quit Game",0,0);
433
434   worldmap_menu->additem(MN_LABEL,"Worldmap Menu",0,0);
435   worldmap_menu->additem(MN_HL,"",0,0);
436   worldmap_menu->additem(MN_ACTION,"Return To Game",0,0);
437   worldmap_menu->additem(MN_GOTO,"Options",0,options_menu);
438   worldmap_menu->additem(MN_HL,"",0,0);
439   worldmap_menu->additem(MN_ACTION,"Quit Game",0,0);
440
441   highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
442 }
443
444 void update_load_save_game_menu(Menu* pmenu)
445 {
446   for(int i = 2; i < 7; ++i)
447     {
448       // FIXME: Insert a real savegame struct/class here instead of
449       // doing string vodoo
450       std::string tmp = slotinfo(i - 1);
451       pmenu->item[i].kind = MN_ACTION;
452       pmenu->item[i].change_text(tmp.c_str());
453     }
454 }
455
456 bool process_load_game_menu()
457 {
458   int slot = load_game_menu->check();
459
460   if(slot != -1)
461     {
462       WorldMapNS::WorldMap worldmap;
463
464       char slotfile[1024];
465       snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot-1);
466       
467       worldmap.loadgame(slotfile);
468
469       worldmap.display();
470       
471       show_menu = true;
472       Menu::set_current(main_menu);
473
474       st_pause_ticks_stop();
475       return true;
476     }
477   else
478     {
479       return false;
480     }
481 }
482
483 /* Handle changes made to global settings in the options menu. */
484 void process_options_menu(void)
485 {
486   switch (options_menu->check())
487     {
488     case 2:
489 #ifndef NOOPENGL
490       if(use_gl != options_menu->item[2].toggled)
491         {
492           use_gl = !use_gl;
493           st_video_setup();
494         }
495 #endif
496       break;
497     case 3:
498       if(use_fullscreen != options_menu->item[3].toggled)
499         {
500           use_fullscreen = !use_fullscreen;
501           st_video_setup();
502         }
503       break;
504     case 4:
505       if(use_sound != options_menu->item[4].toggled)
506         use_sound = !use_sound;
507       break;
508     case 5:
509       if(use_music != options_menu->item[5].toggled)
510         {
511           if(use_music)
512             {
513               if(playing_music())
514                 {
515                   halt_music();
516                 }
517               use_music = false;
518             }
519           else
520             {
521               use_music = true;
522               if (!playing_music())
523                 {
524                   play_current_music();
525                 }
526             }
527         }
528       break;
529     case 6:
530       if(show_fps != options_menu->item[6].toggled)
531         show_fps = !show_fps;
532       break;
533     }
534 }
535
536 void st_general_setup(void)
537 {
538   /* Seed random number generator: */
539
540   srand(SDL_GetTicks());
541
542   /* Set icon image: */
543
544   seticon();
545
546   /* Unicode needed for input handling: */
547
548   SDL_EnableUNICODE(1);
549
550   /* Load global images: */
551
552   black_text  = new Text(datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
553   gold_text   = new Text(datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
554   blue_text   = new Text(datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
555   red_text    = new Text(datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
556   white_text  = new Text(datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
557   white_small_text = new Text(datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
558   white_big_text   = new Text(datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
559   yellow_nums = new Text(datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
560
561   /* Load GUI/menu images: */
562   checkbox = new Surface(datadir + "/images/status/checkbox.png", USE_ALPHA);
563   checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
564   back = new Surface(datadir + "/images/status/back.png", USE_ALPHA);
565   arrow_left = new Surface(datadir + "/images/icons/left.png", USE_ALPHA);
566   arrow_right = new Surface(datadir + "/images/icons/right.png", USE_ALPHA);
567
568   /* Load the mouse-cursor */
569   mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
570   
571 }
572
573 void st_general_free(void)
574 {
575
576   /* Free global images: */
577
578   delete black_text;
579   delete gold_text;
580   delete white_text;
581   delete blue_text;
582   delete red_text;
583   delete white_small_text;
584   delete white_big_text;
585
586   /* Free GUI/menu images: */
587   delete checkbox;
588   delete checkbox_checked;
589   delete back;
590   delete arrow_left;
591   delete arrow_right;
592
593   /* Free mouse-cursor */
594   delete mouse_cursor;
595   
596   /* Free menus */
597   delete main_menu;
598   delete game_menu;
599   delete options_menu;
600   delete highscore_menu;
601   delete save_game_menu;
602   delete load_game_menu;
603 }
604
605 void st_video_setup(void)
606 {
607   if(screen != NULL)
608     SDL_FreeSurface(screen);
609
610   /* Init SDL Video: */
611
612   if (SDL_Init(SDL_INIT_VIDEO) < 0)
613     {
614       fprintf(stderr,
615               "\nError: I could not initialize video!\n"
616               "The Simple DirectMedia error that occured was:\n"
617               "%s\n\n", SDL_GetError());
618       exit(1);
619     }
620
621   /* Open display: */
622   if(use_gl)
623     st_video_setup_gl();
624   else
625     st_video_setup_sdl();
626
627   Surface::reload_all();
628
629   /* Set window manager stuff: */
630   SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
631 }
632
633 void st_video_setup_sdl(void)
634 {
635   SDL_FreeSurface(screen);
636
637   if (use_fullscreen)
638     {
639       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
640       if (screen == NULL)
641         {
642           fprintf(stderr,
643                   "\nWarning: I could not set up fullscreen video for "
644                   "640x480 mode.\n"
645                   "The Simple DirectMedia error that occured was:\n"
646                   "%s\n\n", SDL_GetError());
647           use_fullscreen = false;
648         }
649     }
650   else
651     {
652       screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
653
654       if (screen == NULL)
655         {
656           fprintf(stderr,
657                   "\nError: I could not set up video for 640x480 mode.\n"
658                   "The Simple DirectMedia error that occured was:\n"
659                   "%s\n\n", SDL_GetError());
660           exit(1);
661         }
662     }
663 }
664
665 void st_video_setup_gl(void)
666 {
667 #ifndef NOOPENGL
668
669   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
670   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
671   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
672   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
673   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
674
675   if (use_fullscreen)
676     {
677       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
678       if (screen == NULL)
679         {
680           fprintf(stderr,
681                   "\nWarning: I could not set up fullscreen video for "
682                   "640x480 mode.\n"
683                   "The Simple DirectMedia error that occured was:\n"
684                   "%s\n\n", SDL_GetError());
685           use_fullscreen = false;
686         }
687     }
688   else
689     {
690       screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
691
692       if (screen == NULL)
693         {
694           fprintf(stderr,
695                   "\nError: I could not set up video for 640x480 mode.\n"
696                   "The Simple DirectMedia error that occured was:\n"
697                   "%s\n\n", SDL_GetError());
698           exit(1);
699         }
700     }
701
702   /*
703    * Set up OpenGL for 2D rendering.
704    */
705   glDisable(GL_DEPTH_TEST);
706   glDisable(GL_CULL_FACE);
707
708   glViewport(0, 0, screen->w, screen->h);
709   glMatrixMode(GL_PROJECTION);
710   glLoadIdentity();
711   glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
712
713   glMatrixMode(GL_MODELVIEW);
714   glLoadIdentity();
715   glTranslatef(0.0f, 0.0f, 0.0f);
716
717 #endif
718
719 }
720
721 void st_joystick_setup(void)
722 {
723
724   /* Init Joystick: */
725
726   use_joystick = true;
727
728   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
729     {
730       fprintf(stderr, "Warning: I could not initialize joystick!\n"
731               "The Simple DirectMedia error that occured was:\n"
732               "%s\n\n", SDL_GetError());
733
734       use_joystick = false;
735     }
736   else
737     {
738       /* Open joystick: */
739       if (SDL_NumJoysticks() <= 0)
740         {
741           fprintf(stderr, "Warning: No joysticks are available.\n");
742
743           use_joystick = false;
744         }
745       else
746         {
747           js = SDL_JoystickOpen(joystick_num);
748
749           if (js == NULL)
750             {
751               fprintf(stderr, "Warning: Could not open joystick %d.\n"
752                       "The Simple DirectMedia error that occured was:\n"
753                       "%s\n\n", joystick_num, SDL_GetError());
754
755               use_joystick = false;
756             }
757           else
758             {
759               /* Check for proper joystick configuration: */
760
761               if (SDL_JoystickNumAxes(js) < 2)
762                 {
763                   fprintf(stderr,
764                           "Warning: Joystick does not have enough axes!\n");
765
766                   use_joystick = false;
767                 }
768               else
769                 {
770                   if (SDL_JoystickNumButtons(js) < 2)
771                     {
772                       fprintf(stderr,
773                               "Warning: "
774                               "Joystick does not have enough buttons!\n");
775
776                       use_joystick = false;
777                     }
778                 }
779             }
780         }
781     }
782 }
783
784 void st_audio_setup(void)
785 {
786
787   /* Init SDL Audio silently even if --disable-sound : */
788
789   if (audio_device)
790     {
791       if (SDL_Init(SDL_INIT_AUDIO) < 0)
792         {
793           /* only print out message if sound or music
794              was not disabled at command-line
795            */
796           if (use_sound || use_music)
797             {
798               fprintf(stderr,
799                       "\nWarning: I could not initialize audio!\n"
800                       "The Simple DirectMedia error that occured was:\n"
801                       "%s\n\n", SDL_GetError());
802             }
803           /* keep the programming logic the same :-)
804              because in this case, use_sound & use_music' values are ignored
805              when there's no available audio device
806           */
807           use_sound = false;
808           use_music = false;
809           audio_device = false;
810         }
811     }
812
813
814   /* Open sound silently regarless the value of "use_sound": */
815
816   if (audio_device)
817     {
818       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
819         {
820           /* only print out message if sound or music
821              was not disabled at command-line
822            */
823           if (use_sound || use_music)
824             {
825               fprintf(stderr,
826                       "\nWarning: I could not set up audio for 44100 Hz "
827                       "16-bit stereo.\n"
828                       "The Simple DirectMedia error that occured was:\n"
829                       "%s\n\n", SDL_GetError());
830             }
831           use_sound = false;
832           use_music = false;
833           audio_device = false;
834         }
835     }
836
837 }
838
839
840 /* --- SHUTDOWN --- */
841
842 void st_shutdown(void)
843 {
844   close_audio();
845   SDL_Quit();
846   saveconfig();
847 }
848
849 /* --- ABORT! --- */
850
851 void st_abort(const std::string& reason, const std::string& details)
852 {
853   fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
854   st_shutdown();
855   abort();
856 }
857
858
859 /* Set Icon (private) */
860
861 void seticon(void)
862 {
863   int masklen;
864   Uint8 * mask;
865   SDL_Surface * icon;
866
867
868   /* Load icon into a surface: */
869
870   icon = IMG_Load((datadir + "/images/icon.png").c_str());
871   if (icon == NULL)
872     {
873       fprintf(stderr,
874               "\nError: I could not load the icon image: %s%s\n"
875               "The Simple DirectMedia error that occured was:\n"
876               "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
877       exit(1);
878     }
879
880
881   /* Create mask: */
882
883   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
884   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
885   memset(mask, 0xFF, masklen);
886
887
888   /* Set icon: */
889
890   SDL_WM_SetIcon(icon, mask);
891
892
893   /* Free icon surface & mask: */
894
895   free(mask);
896   SDL_FreeSurface(icon);
897 }
898
899
900 /* Parse command-line arguments: */
901
902 void parseargs(int argc, char * argv[])
903 {
904   int i;
905
906   loadconfig();
907
908   /* Parse arguments: */
909
910   for (i = 1; i < argc; i++)
911     {
912       if (strcmp(argv[i], "--fullscreen") == 0 ||
913           strcmp(argv[i], "-f") == 0)
914         {
915           /* Use full screen: */
916
917           use_fullscreen = true;
918         }
919       else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
920         {
921           assert(i+1 < argc);
922           joystick_num = atoi(argv[++i]);
923         }
924       else if (strcmp(argv[i], "--worldmap") == 0)
925         {
926           launch_worldmap_mode = true;
927         }
928       else if (strcmp(argv[i], "--datadir") == 0 
929                || strcmp(argv[i], "-d") == 0 )
930         {
931           assert(i+1 < argc);
932           datadir = argv[++i];
933         }
934       else if (strcmp(argv[i], "--show-fps") == 0)
935         {
936           /* Use full screen: */
937
938           show_fps = true;
939         }
940       else if (strcmp(argv[i], "--opengl") == 0 ||
941                strcmp(argv[i], "-gl") == 0)
942         {
943 #ifndef NOOPENGL
944           /* Use OpengGL: */
945
946           use_gl = true;
947 #endif
948         }
949       else if (strcmp(argv[i], "--sdl") == 0)
950           {
951             use_gl = false;
952           }
953       else if (strcmp(argv[i], "--usage") == 0)
954         {
955           /* Show usage: */
956
957           usage(argv[0], 0);
958         }
959       else if (strcmp(argv[i], "--version") == 0)
960         {
961           /* Show version: */
962           printf("SuperTux " VERSION "\n");
963           exit(0);
964         }
965       else if (strcmp(argv[i], "--disable-sound") == 0)
966         {
967           /* Disable the compiled in sound feature */
968           printf("Sounds disabled \n");
969           use_sound = false;
970         }
971       else if (strcmp(argv[i], "--disable-music") == 0)
972         {
973           /* Disable the compiled in sound feature */
974           printf("Music disabled \n");
975           use_music = false;
976         }
977       else if (strcmp(argv[i], "--debug-mode") == 0)
978         {
979           /* Enable the debug-mode */
980           debug_mode = true;
981
982         }
983       else if (strcmp(argv[i], "--help") == 0)
984         {     /* Show help: */
985           puts("Super Tux " VERSION "\n"
986                "  Please see the file \"README.txt\" for more details.\n");
987           printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
988           puts("Display Options:\n"
989                "  --fullscreen        Run in fullscreen mode.\n"
990                "  --opengl            If opengl support was compiled in, this will enable\n"
991                "                      the EXPERIMENTAL OpenGL mode.\n"
992                "  --sdl               Use non-opengl renderer\n"
993                "\n"
994                "Sound Options:\n"
995                "  --disable-sound     If sound support was compiled in,  this will\n"
996                "                      disable sound for this session of the game.\n"
997                "  --disable-music     Like above, but this will disable music.\n"
998                "\n"
999                "Misc Options:\n"
1000                "  -j, --joystick NUM  Use joystick NUM (default: 0)\n" 
1001                "  --worldmap          Start in worldmap-mode (EXPERIMENTAL)\n"          
1002                "  -d, --datadir DIR   Load Game data from DIR (default: automatic)\n"
1003                "  --debug-mode        Enables the debug-mode, which is useful for developers.\n"
1004                "  --help              Display a help message summarizing command-line\n"
1005                "                      options, license and game controls.\n"
1006                "  --usage             Display a brief message summarizing command-line options.\n"
1007                "  --version           Display the version of SuperTux you're running.\n\n"
1008                );
1009           exit(0);
1010         }
1011       else if (argv[i][0] != '-')
1012         {
1013           level_startup_file = argv[i];
1014         }
1015       else
1016         {
1017           /* Unknown - complain! */
1018
1019           usage(argv[0], 1);
1020         }
1021     }
1022 }
1023
1024
1025 /* Display usage: */
1026
1027 void usage(char * prog, int ret)
1028 {
1029   FILE * fi;
1030
1031
1032   /* Determine which stream to write to: */
1033
1034   if (ret == 0)
1035     fi = stdout;
1036   else
1037     fi = stderr;
1038
1039
1040   /* Display the usage message: */
1041
1042   fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",
1043           prog);
1044
1045
1046   /* Quit! */
1047
1048   exit(ret);
1049 }
1050