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