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 std::string& filename)
71 if (stat(filename.c_str(), &filestat) == -1)
77 if(S_ISREG(filestat.st_mode))
84 /* Can we write to this location? */
85 int FileSystem::fwriteable(const std::string& filename)
88 fi = fopen(filename.c_str(), "wa");
97 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
98 int FileSystem::fcreatedir(const std::string& relative_dir)
100 std::string path = st_dir + "/" + relative_dir + "/";
101 if(mkdir(path.c_str(),0755) != 0)
103 path = datadir + "/" + relative_dir + "/";
104 if(mkdir(path.c_str(),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 std::string &rel_path,const std::string& expected_file)
125 struct dirent *direntp;
126 std::set<std::string> sdirs;
127 std::string filename;
128 std::string path = st_dir + "/" + rel_path;
130 if((dirStructP = opendir(path.c_str())) != NULL)
132 while((direntp = readdir(dirStructP)) != NULL)
134 std::string absolute_filename;
137 absolute_filename = path + "/" + direntp->d_name;
139 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
141 if(!expected_file.empty())
143 filename = path + "/" + direntp->d_name + "/" + expected_file;
144 if(!faccessible(filename))
148 sdirs.insert(direntp->d_name);
151 closedir(dirStructP);
154 path = datadir + "/" + rel_path;
155 if((dirStructP = opendir(path.c_str())) != NULL)
157 while((direntp = readdir(dirStructP)) != NULL)
159 std::string absolute_filename;
162 absolute_filename = path + "/" + direntp->d_name;
164 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
166 if(!expected_file.empty())
168 filename = path + "/" + direntp->d_name + "/" + expected_file;
169 if(!faccessible(filename.c_str()))
175 filename = st_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file;
176 if(faccessible(filename.c_str()))
181 sdirs.insert(direntp->d_name);
184 closedir(dirStructP);
190 std::set<std::string> FileSystem::dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str)
193 struct dirent *direntp;
194 std::set<std::string> sdirs;
195 std::string path = st_dir + "/" + rel_path;
197 if((dirStructP = opendir(path.c_str())) != NULL)
199 while((direntp = readdir(dirStructP)) != NULL)
201 std::string absolute_filename;
204 absolute_filename = path + "/" + direntp->d_name;
206 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
208 if(!exception_str.empty())
210 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
214 if(strstr(direntp->d_name,glob.c_str()) == NULL)
217 sdirs.insert(direntp->d_name);
220 closedir(dirStructP);
223 path = datadir + "/" + rel_path;
224 if((dirStructP = opendir(path.c_str())) != NULL)
226 while((direntp = readdir(dirStructP)) != NULL)
228 std::string absolute_filename;
231 absolute_filename = path + "/" + direntp->d_name;
233 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
235 if(!exception_str.empty())
237 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
241 if(strstr(direntp->d_name,glob.c_str()) == NULL)
244 sdirs.insert(direntp->d_name);
247 closedir(dirStructP);
253 void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
255 package_name = _package_name;
256 package_symbol_name = _package_symbol_name;
257 package_version = _package_version;
261 /* Set SuperTux configuration and save directories */
262 void Setup::directories(void)
265 /* Get home directory (from $HOME variable)... if we can't determine it,
266 use the current directory ("."): */
267 if (getenv("HOME") != NULL)
268 home = getenv("HOME");
272 st_dir = home + "/." + package_symbol_name;
274 /* Remove .supertux config-file from old SuperTux versions */
275 if(FileSystem::faccessible(st_dir))
277 remove(st_dir.c_str());
280 st_save_dir = st_dir + "/save";
282 /* Create them. In the case they exist they won't destroy anything. */
283 mkdir(st_dir.c_str(), 0755);
284 mkdir(st_save_dir.c_str(), 0755);
286 mkdir((st_dir + "/levels").c_str(), 0755);
288 // User has not that a datadir, so we try some magic
293 char exe_file[PATH_MAX];
294 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
296 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
297 datadir = DATA_PREFIX;
301 std::string exedir = std::string(dirname(exe_file)) + "/";
303 datadir = exedir + "../data"; // SuperTux run from source dir
304 if (access(datadir.c_str(), F_OK) != 0)
306 datadir = exedir + "../../data"; //SuperTux run from source dir (with libtool script)
308 if (access(datadir.c_str(), F_OK) != 0)
310 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
311 if (access(datadir.c_str(), F_OK) != 0)
312 { // If all fails, fall back to compiled path
313 datadir = DATA_PREFIX;
319 datadir = DATA_PREFIX;
322 printf("Datadir: %s\n", datadir.c_str());
325 void Setup::general(void)
327 /* Seed random number generator: */
329 srand(SDL_GetTicks());
331 /* Set icon image: */
335 /* Unicode needed for input handling: */
337 SDL_EnableUNICODE(1);
339 /* Load GUI/menu images: */
340 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
341 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
342 back = new Surface(datadir + "/images/status/back.png", true);
343 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
344 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
346 /* Load the mouse-cursor */
347 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
348 MouseCursor::set_current(mouse_cursor);
352 void Setup::general_free(void)
355 /* Free GUI/menu images: */
357 delete checkbox_checked;
362 /* Free mouse-cursor */
367 void Setup::video(unsigned int screen_w, unsigned int screen_h)
369 /* Init SDL Video: */
370 if (SDL_Init(SDL_INIT_VIDEO) < 0)
373 "\nError: I could not initialize video!\n"
374 "The Simple DirectMedia error that occured was:\n"
375 "%s\n\n", SDL_GetError());
381 video_gl(screen_w, screen_h);
383 video_sdl(screen_w, screen_h);
385 Surface::reload_all();
387 /* Set window manager stuff: */
388 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
391 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
395 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
399 "\nWarning: I could not set up fullscreen video for "
401 "The Simple DirectMedia error that occured was:\n"
402 "%s\n\n", SDL_GetError());
403 use_fullscreen = false;
408 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
413 "\nError: I could not set up video for 800x600 mode.\n"
414 "The Simple DirectMedia error that occured was:\n"
415 "%s\n\n", SDL_GetError());
421 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
425 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
426 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
427 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
428 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
429 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
433 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
437 "\nWarning: I could not set up fullscreen video for "
439 "The Simple DirectMedia error that occured was:\n"
440 "%s\n\n", SDL_GetError());
441 use_fullscreen = false;
446 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
451 "\nError: I could not set up video for 640x480 mode.\n"
452 "The Simple DirectMedia error that occured was:\n"
453 "%s\n\n", SDL_GetError());
459 * Set up OpenGL for 2D rendering.
461 glDisable(GL_DEPTH_TEST);
462 glDisable(GL_CULL_FACE);
464 glViewport(0, 0, screen->w, screen->h);
465 glMatrixMode(GL_PROJECTION);
467 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
469 glMatrixMode(GL_MODELVIEW);
471 glTranslatef(0.0f, 0.0f, 0.0f);
477 void Setup::joystick(void)
484 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
486 fprintf(stderr, "Warning: I could not initialize joystick!\n"
487 "The Simple DirectMedia error that occured was:\n"
488 "%s\n\n", SDL_GetError());
490 use_joystick = false;
495 if (SDL_NumJoysticks() <= 0)
497 fprintf(stderr, "Info: No joysticks were found.\n");
499 use_joystick = false;
503 js = SDL_JoystickOpen(joystick_num);
507 fprintf(stderr, "Warning: Could not open joystick %d.\n"
508 "The Simple DirectMedia error that occured was:\n"
509 "%s\n\n", joystick_num, SDL_GetError());
511 use_joystick = false;
515 if (SDL_JoystickNumAxes(js) < 2)
518 "Warning: Joystick does not have enough axes!\n");
520 use_joystick = false;
524 if (SDL_JoystickNumButtons(js) < 2)
528 "Joystick does not have enough buttons!\n");
530 use_joystick = false;
538 void Setup::audio(void)
541 /* Init SDL Audio silently even if --disable-sound : */
543 if (SoundManager::get()->audio_device_available())
545 if (SDL_Init(SDL_INIT_AUDIO) < 0)
547 /* only print out message if sound or music
548 was not disabled at command-line
550 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
553 "\nWarning: I could not initialize audio!\n"
554 "The Simple DirectMedia error that occured was:\n"
555 "%s\n\n", SDL_GetError());
557 /* keep the programming logic the same :-)
558 because in this case, use_sound & use_music' values are ignored
559 when there's no available audio device
561 SoundManager::get()->enable_sound(false);
562 SoundManager::get()->enable_music(false);
563 SoundManager::get()->set_audio_device_available(false);
568 /* Open sound silently regarless the value of "use_sound": */
570 if (SoundManager::get()->audio_device_available())
572 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
574 /* only print out message if sound or music
575 was not disabled at command-line
577 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
580 "\nWarning: I could not set up audio for 44100 Hz "
582 "The Simple DirectMedia error that occured was:\n"
583 "%s\n\n", SDL_GetError());
585 SoundManager::get()->enable_sound(false);
586 SoundManager::get()->enable_music(false);
587 SoundManager::get()->set_audio_device_available(false);
594 /* --- SHUTDOWN --- */
596 void Termination::shutdown(void)
599 SoundManager::get()->close_audio();
605 void Termination::abort(const std::string& reason, const std::string& details)
607 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
612 /* Set Icon (private) */
621 /* Load icon into a surface: */
623 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
627 "\nError: I could not load the icon image: %s%s\n"
628 "The Simple DirectMedia error that occured was:\n"
629 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
636 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
637 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
638 memset(mask, 0xFF, masklen);
643 SDL_WM_SetIcon(icon, NULL);//mask);
646 /* Free icon surface & mask: */
649 SDL_FreeSurface(icon);
653 /* Parse command-line arguments: */
655 void Setup::parseargs(int argc, char * argv[])
661 /* Parse arguments: */
663 for (i = 1; i < argc; i++)
665 if (strcmp(argv[i], "--fullscreen") == 0 ||
666 strcmp(argv[i], "-f") == 0)
668 use_fullscreen = true;
670 else if (strcmp(argv[i], "--window") == 0 ||
671 strcmp(argv[i], "-w") == 0)
673 use_fullscreen = false;
675 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
678 joystick_num = atoi(argv[++i]);
680 else if (strcmp(argv[i], "--joymap") == 0)
683 if (sscanf(argv[++i],
685 &joystick_keymap.x_axis,
686 &joystick_keymap.y_axis,
687 &joystick_keymap.a_button,
688 &joystick_keymap.b_button,
689 &joystick_keymap.start_button) != 5)
691 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
695 std::cout << "Using new joymap:\n"
696 << " X-Axis: " << joystick_keymap.x_axis << "\n"
697 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
698 << " A-Button: " << joystick_keymap.a_button << "\n"
699 << " B-Button: " << joystick_keymap.b_button << "\n"
700 << " Start-Button: " << joystick_keymap.start_button << std::endl;
703 else if (strcmp(argv[i], "--leveleditor") == 0)
705 launch_leveleditor_mode = true;
707 else if (strcmp(argv[i], "--worldmap") == 0)
709 launch_worldmap_mode = true;
711 else if (strcmp(argv[i], "--flip-levels") == 0)
713 flip_levels_mode = true;
715 else if (strcmp(argv[i], "--datadir") == 0
716 || strcmp(argv[i], "-d") == 0 )
721 else if (strcmp(argv[i], "--show-fps") == 0)
723 /* Use full screen: */
727 else if (strcmp(argv[i], "--opengl") == 0 ||
728 strcmp(argv[i], "-gl") == 0)
736 else if (strcmp(argv[i], "--sdl") == 0)
740 else if (strcmp(argv[i], "--usage") == 0)
746 else if (strcmp(argv[i], "--version") == 0)
749 printf((package_name + " " + package_version + "\n").c_str() );
752 else if (strcmp(argv[i], "--disable-sound") == 0)
754 /* Disable the compiled in sound feature */
755 printf("Sounds disabled \n");
756 SoundManager::get()->enable_sound(false);
758 else if (strcmp(argv[i], "--disable-music") == 0)
760 /* Disable the compiled in sound feature */
761 printf("Music disabled \n");
762 SoundManager::get()->enable_music(false);
764 else if (strcmp(argv[i], "--debug") == 0)
766 /* Enable the debug-mode */
770 else if (strcmp(argv[i], "--help") == 0)
772 puts(_((" SuperTux " + package_version + "\n"
773 " Please see the file \"README.txt\" for more details.\n").c_str()));
774 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
775 puts(_("Display Options:\n"
776 " -f, --fullscreen Run in fullscreen mode.\n"
777 " -w, --window Run in window mode.\n"
778 " --opengl If OpenGL support was compiled in, this will tell\n"
779 " SuperTux to make use of it.\n"
780 " --sdl Use the SDL software graphical renderer\n"
783 " --disable-sound If sound support was compiled in, this will\n"
784 " disable sound for this session of the game.\n"
785 " --disable-music Like above, but this will disable music.\n"
788 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
789 " --joymap XAXIS:YAXIS:A:B:START\n"
790 " Define how joystick buttons and axis should be mapped\n"
791 " --leveleditor Opens the leveleditor in a file.\n"
792 " --worldmap Opens the specified worldmap file.\n"
793 " --flip-levels Flip levels upside-down.\n"
794 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
795 " --debug Enables the debug mode, which is useful for developers.\n"
796 " --help Display a help message summarizing command-line\n"
797 " options, license and game controls.\n"
798 " --usage Display a brief message summarizing command-line options.\n"
799 " --version Display the version of SuperTux you're running.\n\n"
803 else if (argv[i][0] != '-')
805 level_startup_file = argv[i];
809 /* Unknown - complain! */
819 void usage(char * prog, int ret)
824 /* Determine which stream to write to: */
832 /* Display the usage message: */
834 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
843 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
845 std::set<std::string> dirnames;
847 DIR* dir = opendir(pathname.c_str());
850 struct dirent *direntp;
852 while((direntp = readdir(dir)))
854 dirnames.insert(direntp->d_name);