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