-Fix bugs where resources from source directory weren't always found
[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/intro.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, screen->w, screen->h, 0, -1.0, 1.0);
492
493   glMatrixMode(GL_MODELVIEW);
494   glLoadIdentity();
495   glTranslatef(0.0f, 0.0f, 0.0f);
496
497 #endif
498
499 }
500
501 void Setup::joystick(void)
502 {
503
504   /* Init Joystick: */
505
506   use_joystick = true;
507
508   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
509     {
510       fprintf(stderr, "Warning: I could not initialize joystick!\n"
511               "The Simple DirectMedia error that occured was:\n"
512               "%s\n\n", SDL_GetError());
513
514       use_joystick = false;
515     }
516   else
517     {
518       /* Open joystick: */
519       if (SDL_NumJoysticks() <= 0)
520         {
521           fprintf(stderr, "Info: No joysticks were found.\n");
522
523           use_joystick = false;
524         }
525       else
526         {
527           js = SDL_JoystickOpen(joystick_num);
528
529           if (js == NULL)
530             {
531               fprintf(stderr, "Warning: Could not open joystick %d.\n"
532                       "The Simple DirectMedia error that occured was:\n"
533                       "%s\n\n", joystick_num, SDL_GetError());
534
535               use_joystick = false;
536             }
537           else
538             {
539               if (SDL_JoystickNumAxes(js) < 2)
540                 {
541                   fprintf(stderr,
542                           "Warning: Joystick does not have enough axes!\n");
543
544                   use_joystick = false;
545                 }
546               else
547                 {
548                   if (SDL_JoystickNumButtons(js) < 2)
549                     {
550                       fprintf(stderr,
551                               "Warning: "
552                               "Joystick does not have enough buttons!\n");
553
554                       use_joystick = false;
555                     }
556                 }
557             }
558         }
559     }
560 }
561
562 void Setup::audio(void)
563 {
564
565   /* Init SDL Audio silently even if --disable-sound : */
566
567   if (SoundManager::get()->audio_device_available())
568     {
569       if (SDL_Init(SDL_INIT_AUDIO) < 0)
570         {
571           /* only print out message if sound or music
572              was not disabled at command-line
573            */
574           if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
575             {
576               fprintf(stderr,
577                       "\nWarning: I could not initialize audio!\n"
578                       "The Simple DirectMedia error that occured was:\n"
579                       "%s\n\n", SDL_GetError());
580             }
581           /* keep the programming logic the same :-)
582              because in this case, use_sound & use_music' values are ignored
583              when there's no available audio device
584           */
585           SoundManager::get()->enable_sound(false);
586           SoundManager::get()->enable_music(false);
587           SoundManager::get()->set_audio_device_available(false);
588         }
589     }
590
591
592   /* Open sound silently regarless the value of "use_sound": */
593
594   if (SoundManager::get()->audio_device_available())
595     {
596       if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
597         {
598           /* only print out message if sound or music
599              was not disabled at command-line
600            */
601           if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
602             {
603               fprintf(stderr,
604                       "\nWarning: I could not set up audio for 44100 Hz "
605                       "16-bit stereo.\n"
606                       "The Simple DirectMedia error that occured was:\n"
607                       "%s\n\n", SDL_GetError());
608             }
609           SoundManager::get()->enable_sound(false);
610           SoundManager::get()->enable_music(false);
611           SoundManager::get()->set_audio_device_available(false);
612         }
613     }
614
615 }
616
617
618 /* --- SHUTDOWN --- */
619
620 void Termination::shutdown(void)
621 {
622   config->save();
623   SoundManager::get()->close_audio();
624   SDL_Quit();
625 }
626
627 /* --- ABORT! --- */
628
629 void Termination::abort(const std::string& reason, const std::string& details)
630 {
631   fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
632   shutdown();
633   ::abort();
634 }
635
636 /* Set Icon (private) */
637
638 void seticon(void)
639 {
640 //  int masklen;
641 //  Uint8 * mask;
642   SDL_Surface * icon;
643
644
645   /* Load icon into a surface: */
646
647   icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
648   if (icon == NULL)
649     {
650       fprintf(stderr,
651               "\nError: I could not load the icon image: %s%s\n"
652               "The Simple DirectMedia error that occured was:\n"
653               "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
654       exit(1);
655     }
656
657
658   /* Create mask: */
659 /*
660   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
661   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
662   memset(mask, 0xFF, masklen);
663 */
664
665   /* Set icon: */
666
667   SDL_WM_SetIcon(icon, NULL);//mask);
668
669
670   /* Free icon surface & mask: */
671
672 //  free(mask);
673   SDL_FreeSurface(icon);
674 }
675
676
677 /* Parse command-line arguments: */
678
679 void Setup::parseargs(int argc, char * argv[])
680 {
681   int i;
682
683   config->load();
684
685   /* Parse arguments: */
686
687   for (i = 1; i < argc; i++)
688     {
689       if (strcmp(argv[i], "--fullscreen") == 0 ||
690           strcmp(argv[i], "-f") == 0)
691         {
692           use_fullscreen = true;
693         }
694       else if (strcmp(argv[i], "--window") == 0 ||
695                strcmp(argv[i], "-w") == 0)
696         {
697           use_fullscreen = false;
698         }
699       else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
700         {
701           assert(i+1 < argc);
702           joystick_num = atoi(argv[++i]);
703         }
704       else if (strcmp(argv[i], "--joymap") == 0)
705         {
706           assert(i+1 < argc);
707           if (sscanf(argv[++i],
708                      "%d:%d:%d:%d:%d", 
709                      &joystick_keymap.x_axis, 
710                      &joystick_keymap.y_axis, 
711                      &joystick_keymap.a_button, 
712                      &joystick_keymap.b_button, 
713                      &joystick_keymap.start_button) != 5)
714             {
715               puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
716             }
717           else
718             {
719               std::cout << "Using new joymap:\n"
720                         << "  X-Axis:       " << joystick_keymap.x_axis << "\n"
721                         << "  Y-Axis:       " << joystick_keymap.y_axis << "\n"
722                         << "  A-Button:     " << joystick_keymap.a_button << "\n"
723                         << "  B-Button:     " << joystick_keymap.b_button << "\n"
724                         << "  Start-Button: " << joystick_keymap.start_button << std::endl;
725             }
726         }
727       else if (strcmp(argv[i], "--leveleditor") == 0)
728         {
729           launch_leveleditor_mode = true;
730         }
731       else if (strcmp(argv[i], "--worldmap") == 0)
732         {
733           launch_worldmap_mode = true;
734         }
735       else if (strcmp(argv[i], "--flip-levels") == 0)
736         {
737           flip_levels_mode = true;
738         }
739       else if (strcmp(argv[i], "--datadir") == 0 
740                || strcmp(argv[i], "-d") == 0 )
741         {
742           assert(i+1 < argc);
743           datadir = argv[++i];
744         }
745       else if (strcmp(argv[i], "--show-fps") == 0)
746         {
747           /* Use full screen: */
748
749           show_fps = true;
750         }
751       else if (strcmp(argv[i], "--opengl") == 0 ||
752                strcmp(argv[i], "-gl") == 0)
753         {
754 #ifndef NOOPENGL
755           /* Use OpengGL: */
756
757           use_gl = true;
758 #endif
759         }
760       else if (strcmp(argv[i], "--sdl") == 0)
761           {
762             use_gl = false;
763           }
764       else if (strcmp(argv[i], "--usage") == 0)
765         {
766           /* Show usage: */
767
768           usage(argv[0], 0);
769         }
770       else if (strcmp(argv[i], "--version") == 0)
771         {
772           /* Show version: */
773           printf((package_name + " " + package_version + "\n").c_str() );
774           exit(0);
775         }
776       else if (strcmp(argv[i], "--disable-sound") == 0)
777         {
778           /* Disable the compiled in sound feature */
779           printf("Sounds disabled \n");
780           SoundManager::get()->enable_sound(false); 
781         }
782       else if (strcmp(argv[i], "--disable-music") == 0)
783         {
784           /* Disable the compiled in sound feature */
785           printf("Music disabled \n");
786           SoundManager::get()->enable_music(false); 
787         }
788       else if (strcmp(argv[i], "--debug") == 0)
789         {
790           /* Enable the debug-mode */
791           debug_mode = true;
792
793         }
794       else if (strcmp(argv[i], "--help") == 0)
795         {     /* Show help: */
796           puts(_(("  SuperTux  " + package_version + "\n"
797                "  Please see the file \"README.txt\" for more details.\n").c_str()));
798           printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
799           puts(_("Display Options:\n"
800                "  -f, --fullscreen    Run in fullscreen mode.\n"
801                "  -w, --window        Run in window mode.\n"
802                "  --opengl            If OpenGL support was compiled in, this will tell\n"
803                "                      SuperTux to make use of it.\n"
804                "  --sdl               Use the SDL software graphical renderer\n"
805                "\n"
806                "Sound Options:\n"
807                "  --disable-sound     If sound support was compiled in,  this will\n"
808                "                      disable sound for this session of the game.\n"
809                "  --disable-music     Like above, but this will disable music.\n"
810                "\n"
811                "Misc Options:\n"
812                "  -j, --joystick NUM  Use joystick NUM (default: 0)\n" 
813                "  --joymap XAXIS:YAXIS:A:B:START\n"
814                "                      Define how joystick buttons and axis should be mapped\n"
815                "  --leveleditor       Opens the leveleditor in a file.\n"
816                "  --worldmap          Opens the specified worldmap file.\n"
817                "  --flip-levels       Flip levels upside-down.\n"
818                "  -d, --datadir DIR   Load Game data from DIR (default: automatic)\n"
819                "  --debug             Enables the debug mode, which is useful for developers.\n"
820                "  --help              Display a help message summarizing command-line\n"
821                "                      options, license and game controls.\n"
822                "  --usage             Display a brief message summarizing command-line options.\n"
823                "  --version           Display the version of SuperTux you're running.\n\n"
824                ));
825           exit(0);
826         }
827       else if (argv[i][0] != '-')
828         {
829           level_startup_file = argv[i];
830         }
831       else
832         {
833           /* Unknown - complain! */
834
835           usage(argv[0], 1);
836         }
837     }
838 }
839
840
841 /* Display usage: */
842
843 void usage(char * prog, int ret)
844 {
845   FILE * fi;
846
847
848   /* Determine which stream to write to: */
849
850   if (ret == 0)
851     fi = stdout;
852   else
853     fi = stderr;
854
855
856   /* Display the usage message: */
857
858   fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
859           prog);
860
861
862   /* Quit! */
863
864   exit(ret);
865 }
866
867 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
868 {
869   std::set<std::string> dirnames;
870   
871   DIR* dir = opendir(pathname.c_str());
872   if (dir)
873     {
874       struct dirent *direntp;
875       
876       while((direntp = readdir(dir)))
877         {
878           dirnames.insert(direntp->d_name);
879         }
880       
881       closedir(dir);
882     }
883
884   return dirnames;
885 }
886
887 /* EOF */