3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
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.
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.
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.
30 #include "SDL_image.h"
32 #include "SDL_opengl.h"
36 #include <sys/types.h>
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"
53 using namespace SuperTux;
56 #define mkdir(dir, mode) mkdir(dir)
57 // on win32 we typically don't want LFS paths
59 #define DATA_PREFIX "./data/"
62 /* Local function prototypes: */
65 void usage(char * prog, int ret);
67 /* Does the given file exist and is it accessible? */
68 int FileSystem::faccessible(const char *filename)
71 if (stat(filename, &filestat) == -1)
77 if(S_ISREG(filestat.st_mode))
84 /* Can we write to this location? */
85 int FileSystem::fwriteable(const char *filename)
88 fi = fopen(filename, "wa");
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)
100 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
101 if(mkdir(path,0755) != 0)
103 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
104 if(mkdir(path,0755) != 0)
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)
125 struct dirent *direntp;
126 std::set<std::string> sdirs;
130 sprintf(path,"%s/%s",st_dir,rel_path);
131 if((dirStructP = opendir(path)) != NULL)
133 while((direntp = readdir(dirStructP)) != NULL)
135 char absolute_filename[1024];
138 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
140 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
142 if(expected_file != NULL)
144 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
145 if(!faccessible(filename))
149 sdirs.insert(direntp->d_name);
152 closedir(dirStructP);
155 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
156 if((dirStructP = opendir(path)) != NULL)
158 while((direntp = readdir(dirStructP)) != NULL)
160 char absolute_filename[1024];
163 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
165 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
167 if(expected_file != NULL)
169 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
170 if(!faccessible(filename))
176 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
177 if(faccessible(filename))
182 sdirs.insert(direntp->d_name);
185 closedir(dirStructP);
191 std::set<std::string> FileSystem::dfiles(const char *rel_path, const char* glob, const char* exception_str)
194 struct dirent *direntp;
195 std::set<std::string> sdirs;
198 sprintf(path,"%s/%s",st_dir,rel_path);
199 if((dirStructP = opendir(path)) != NULL)
201 while((direntp = readdir(dirStructP)) != NULL)
203 char absolute_filename[1024];
206 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
208 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
210 if(exception_str != NULL)
212 if(strstr(direntp->d_name,exception_str) != NULL)
216 if(strstr(direntp->d_name,glob) == NULL)
219 sdirs.insert(direntp->d_name);
222 closedir(dirStructP);
225 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
226 if((dirStructP = opendir(path)) != NULL)
228 while((direntp = readdir(dirStructP)) != NULL)
230 char absolute_filename[1024];
233 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
235 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
237 if(exception_str != NULL)
239 if(strstr(direntp->d_name,exception_str) != NULL)
243 if(strstr(direntp->d_name,glob) == NULL)
246 sdirs.insert(direntp->d_name);
249 closedir(dirStructP);
255 void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
257 package_name = _package_name;
258 package_symbol_name = _package_symbol_name;
259 package_version = _package_version;
263 /* Set SuperTux configuration and save directories */
264 void Setup::directories(void)
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");
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());
281 /* Remove .supertux config-file from old SuperTux versions */
282 if(FileSystem::faccessible(st_dir))
288 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
290 strcpy(st_save_dir,st_dir);
291 strcat(st_save_dir,"/save");
293 /* Create them. In the case they exist they won't destroy anything. */
295 mkdir(st_save_dir, 0755);
297 sprintf(str, "%s/levels", st_dir);
300 // User has not that a datadir, so we try some magic
305 char exe_file[PATH_MAX];
306 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
308 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
309 datadir = DATA_PREFIX;
313 std::string exedir = std::string(dirname(exe_file)) + "/";
315 datadir = exedir + "../data"; // SuperTux run from source dir
316 if (access(datadir.c_str(), F_OK) != 0)
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;
326 datadir = DATA_PREFIX;
329 printf("Datadir: %s\n", datadir.c_str());
332 void Setup::general(void)
334 /* Seed random number generator: */
336 srand(SDL_GetTicks());
338 /* Set icon image: */
342 /* Unicode needed for input handling: */
344 SDL_EnableUNICODE(1);
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);
353 /* Load the mouse-cursor */
354 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
355 MouseCursor::set_current(mouse_cursor);
359 void Setup::general_free(void)
362 /* Free GUI/menu images: */
364 delete checkbox_checked;
369 /* Free mouse-cursor */
374 void Setup::video(unsigned int screen_w, unsigned int screen_h)
376 /* Init SDL Video: */
377 if (SDL_Init(SDL_INIT_VIDEO) < 0)
380 "\nError: I could not initialize video!\n"
381 "The Simple DirectMedia error that occured was:\n"
382 "%s\n\n", SDL_GetError());
388 video_gl(screen_w, screen_h);
390 video_sdl(screen_w, screen_h);
392 Surface::reload_all();
394 /* Set window manager stuff: */
395 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
398 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
402 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
406 "\nWarning: I could not set up fullscreen video for "
408 "The Simple DirectMedia error that occured was:\n"
409 "%s\n\n", SDL_GetError());
410 use_fullscreen = false;
415 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
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());
428 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
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);
440 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
444 "\nWarning: I could not set up fullscreen video for "
446 "The Simple DirectMedia error that occured was:\n"
447 "%s\n\n", SDL_GetError());
448 use_fullscreen = false;
453 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
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());
466 * Set up OpenGL for 2D rendering.
468 glDisable(GL_DEPTH_TEST);
469 glDisable(GL_CULL_FACE);
471 glViewport(0, 0, screen->w, screen->h);
472 glMatrixMode(GL_PROJECTION);
474 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
476 glMatrixMode(GL_MODELVIEW);
478 glTranslatef(0.0f, 0.0f, 0.0f);
484 void Setup::joystick(void)
491 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
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());
497 use_joystick = false;
502 if (SDL_NumJoysticks() <= 0)
504 fprintf(stderr, "Info: No joysticks were found.\n");
506 use_joystick = false;
510 js = SDL_JoystickOpen(joystick_num);
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());
518 use_joystick = false;
522 if (SDL_JoystickNumAxes(js) < 2)
525 "Warning: Joystick does not have enough axes!\n");
527 use_joystick = false;
531 if (SDL_JoystickNumButtons(js) < 2)
535 "Joystick does not have enough buttons!\n");
537 use_joystick = false;
545 void Setup::audio(void)
548 /* Init SDL Audio silently even if --disable-sound : */
550 if (SoundManager::get()->audio_device_available())
552 if (SDL_Init(SDL_INIT_AUDIO) < 0)
554 /* only print out message if sound or music
555 was not disabled at command-line
557 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
560 "\nWarning: I could not initialize audio!\n"
561 "The Simple DirectMedia error that occured was:\n"
562 "%s\n\n", SDL_GetError());
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
568 SoundManager::get()->enable_sound(false);
569 SoundManager::get()->enable_music(false);
570 SoundManager::get()->set_audio_device_available(false);
575 /* Open sound silently regarless the value of "use_sound": */
577 if (SoundManager::get()->audio_device_available())
579 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
581 /* only print out message if sound or music
582 was not disabled at command-line
584 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
587 "\nWarning: I could not set up audio for 44100 Hz "
589 "The Simple DirectMedia error that occured was:\n"
590 "%s\n\n", SDL_GetError());
592 SoundManager::get()->enable_sound(false);
593 SoundManager::get()->enable_music(false);
594 SoundManager::get()->set_audio_device_available(false);
601 /* --- SHUTDOWN --- */
603 void Termination::shutdown(void)
606 SoundManager::get()->close_audio();
612 void Termination::abort(const std::string& reason, const std::string& details)
614 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
619 /* Set Icon (private) */
628 /* Load icon into a surface: */
630 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
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());
643 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
644 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
645 memset(mask, 0xFF, masklen);
650 SDL_WM_SetIcon(icon, NULL);//mask);
653 /* Free icon surface & mask: */
656 SDL_FreeSurface(icon);
660 /* Parse command-line arguments: */
662 void Setup::parseargs(int argc, char * argv[])
668 /* Parse arguments: */
670 for (i = 1; i < argc; i++)
672 if (strcmp(argv[i], "--fullscreen") == 0 ||
673 strcmp(argv[i], "-f") == 0)
675 use_fullscreen = true;
677 else if (strcmp(argv[i], "--window") == 0 ||
678 strcmp(argv[i], "-w") == 0)
680 use_fullscreen = false;
682 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
685 joystick_num = atoi(argv[++i]);
687 else if (strcmp(argv[i], "--joymap") == 0)
690 if (sscanf(argv[++i],
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)
698 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
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;
710 else if (strcmp(argv[i], "--leveleditor") == 0)
712 launch_leveleditor_mode = true;
714 else if (strcmp(argv[i], "--worldmap") == 0)
716 launch_worldmap_mode = true;
718 else if (strcmp(argv[i], "--flip-levels") == 0)
720 flip_levels_mode = true;
722 else if (strcmp(argv[i], "--datadir") == 0
723 || strcmp(argv[i], "-d") == 0 )
728 else if (strcmp(argv[i], "--show-fps") == 0)
730 /* Use full screen: */
734 else if (strcmp(argv[i], "--opengl") == 0 ||
735 strcmp(argv[i], "-gl") == 0)
743 else if (strcmp(argv[i], "--sdl") == 0)
747 else if (strcmp(argv[i], "--usage") == 0)
753 else if (strcmp(argv[i], "--version") == 0)
756 printf((package_name + package_version + "\n").c_str() );
759 else if (strcmp(argv[i], "--disable-sound") == 0)
761 /* Disable the compiled in sound feature */
762 printf("Sounds disabled \n");
763 SoundManager::get()->enable_sound(false);
765 else if (strcmp(argv[i], "--disable-music") == 0)
767 /* Disable the compiled in sound feature */
768 printf("Music disabled \n");
769 SoundManager::get()->enable_music(false);
771 else if (strcmp(argv[i], "--debug") == 0)
773 /* Enable the debug-mode */
777 else if (strcmp(argv[i], "--help") == 0)
779 puts(_((" SuperTux " + package_version + "\n"
780 " Please see the file \"README.txt\" for more details.\n").c_str()));
781 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
782 puts(_("Display Options:\n"
783 " -f, --fullscreen Run in fullscreen mode.\n"
784 " -w, --window Run in window mode.\n"
785 " --opengl If OpenGL support was compiled in, this will tell\n"
786 " SuperTux to make use of it.\n"
787 " --sdl Use the SDL software graphical renderer\n"
790 " --disable-sound If sound support was compiled in, this will\n"
791 " disable sound for this session of the game.\n"
792 " --disable-music Like above, but this will disable music.\n"
795 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
796 " --joymap XAXIS:YAXIS:A:B:START\n"
797 " Define how joystick buttons and axis should be mapped\n"
798 " --leveleditor Opens the leveleditor in a file.\n"
799 " --worldmap Opens the specified worldmap file.\n"
800 " --flip-levels Flip levels upside-down.\n"
801 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
802 " --debug Enables the debug mode, which is useful for developers.\n"
803 " --help Display a help message summarizing command-line\n"
804 " options, license and game controls.\n"
805 " --usage Display a brief message summarizing command-line options.\n"
806 " --version Display the version of SuperTux you're running.\n\n"
810 else if (argv[i][0] != '-')
812 level_startup_file = argv[i];
816 /* Unknown - complain! */
826 void usage(char * prog, int ret)
831 /* Determine which stream to write to: */
839 /* Display the usage message: */
841 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
850 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
852 std::set<std::string> dirnames;
854 DIR* dir = opendir(pathname.c_str());
857 struct dirent *direntp;
859 while((direntp = readdir(dir)))
861 dirnames.insert(direntp->d_name);