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