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