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