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