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.
32 #include "SDL_image.h"
34 #include "SDL_opengl.h"
38 #include <sys/types.h>
48 #include "video/screen.h"
49 #include "video/surface.h"
51 #include "utils/configfile.h"
52 #include "audio/sound_manager.h"
55 using namespace SuperTux;
58 #define mkdir(dir, mode) mkdir(dir)
59 // on win32 we typically don't want LFS paths
61 #define DATA_PREFIX "./data/"
64 /* Local function prototypes: */
67 void usage(char * prog, int ret);
69 /* Does the given file exist and is it accessible? */
70 int FileSystem::faccessible(const std::string& filename)
73 if (stat(filename.c_str(), &filestat) == -1)
79 if(S_ISREG(filestat.st_mode))
86 /* Can we write to this location? */
87 int FileSystem::fwriteable(const std::string& filename)
90 fi = fopen(filename.c_str(), "wa");
99 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
100 int FileSystem::fcreatedir(const std::string& relative_dir)
102 std::string path = st_dir + "/" + relative_dir + "/";
103 if(mkdir(path.c_str(),0755) != 0)
105 path = datadir + "/" + relative_dir + "/";
106 if(mkdir(path.c_str(),0755) != 0)
121 /* Get all names of sub-directories in a certain directory. */
122 /* Returns the number of sub-directories found. */
123 /* Note: The user has to free the allocated space. */
124 std::set<std::string> FileSystem::dsubdirs(const std::string &rel_path,const std::string& expected_file)
127 struct dirent *direntp;
128 std::set<std::string> sdirs;
129 std::string filename;
130 std::string path = st_dir + "/" + rel_path;
132 if((dirStructP = opendir(path.c_str())) != NULL)
134 while((direntp = readdir(dirStructP)) != NULL)
136 std::string absolute_filename;
139 absolute_filename = path + "/" + direntp->d_name;
141 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
143 if(!expected_file.empty())
145 filename = path + "/" + direntp->d_name + "/" + expected_file;
146 if(!faccessible(filename))
150 sdirs.insert(direntp->d_name);
153 closedir(dirStructP);
156 path = datadir + "/" + rel_path;
157 if((dirStructP = opendir(path.c_str())) != NULL)
159 while((direntp = readdir(dirStructP)) != NULL)
161 std::string absolute_filename;
164 absolute_filename = path + "/" + direntp->d_name;
166 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
168 if(!expected_file.empty())
170 filename = path + "/" + direntp->d_name + "/" + expected_file;
171 if(!faccessible(filename.c_str()))
177 filename = st_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file;
178 if(faccessible(filename.c_str()))
183 sdirs.insert(direntp->d_name);
186 closedir(dirStructP);
192 std::set<std::string> FileSystem::dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str)
195 struct dirent *direntp;
196 std::set<std::string> sdirs;
197 std::string path = st_dir + "/" + rel_path;
199 if((dirStructP = opendir(path.c_str())) != NULL)
201 while((direntp = readdir(dirStructP)) != NULL)
203 std::string absolute_filename;
206 absolute_filename = path + "/" + direntp->d_name;
208 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
210 if(!exception_str.empty())
212 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
216 if(strstr(direntp->d_name,glob.c_str()) == NULL)
219 sdirs.insert(direntp->d_name);
222 closedir(dirStructP);
225 path = datadir + "/" + rel_path;
226 if((dirStructP = opendir(path.c_str())) != NULL)
228 while((direntp = readdir(dirStructP)) != NULL)
230 std::string absolute_filename;
233 absolute_filename = path + "/" + direntp->d_name;
235 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
237 if(!exception_str.empty())
239 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
243 if(strstr(direntp->d_name,glob.c_str()) == NULL)
246 sdirs.insert(direntp->d_name);
249 closedir(dirStructP);
255 void Setup::init(const std::string& _package_name,
256 const std::string& _package_symbol_name,
257 const std::string& _package_version)
259 package_name = _package_name;
260 package_symbol_name = _package_symbol_name;
261 package_version = _package_version;
264 dictionary_manager.add_directory(datadir + "/locale");
265 dictionary_manager.set_charset("iso8859-1");
269 /* Set SuperTux configuration and save directories */
270 void Setup::directories()
273 /* Get home directory (from $HOME variable)... if we can't determine it,
274 use the current directory ("."): */
275 if (getenv("HOME") != NULL)
276 home = getenv("HOME");
280 st_dir = home + "/." + package_symbol_name;
282 /* Remove .supertux config-file from old SuperTux versions */
283 if(FileSystem::faccessible(st_dir))
285 remove(st_dir.c_str());
288 st_save_dir = st_dir + "/save";
290 /* Create them. In the case they exist they won't destroy anything. */
291 mkdir(st_dir.c_str(), 0755);
292 mkdir(st_save_dir.c_str(), 0755);
294 mkdir((st_dir + "/levels").c_str(), 0755);
296 // try current directory as datadir
297 if(datadir.empty()) {
298 if(FileSystem::faccessible("./data/intro.txt"))
302 // User has not that a datadir, so we try some magic
307 char exe_file[PATH_MAX];
308 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
310 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
311 datadir = DATA_PREFIX;
315 std::string exedir = std::string(dirname(exe_file)) + "/";
317 datadir = exedir + "../data"; // SuperTux run from source dir
318 if (access(datadir.c_str(), F_OK) != 0)
320 datadir = exedir + "../../data"; //SuperTux run from source dir (with libtool script)
322 if (access(datadir.c_str(), F_OK) != 0)
324 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
325 if (access(datadir.c_str(), F_OK) != 0)
326 { // If all fails, fall back to compiled path
327 datadir = DATA_PREFIX;
333 datadir = DATA_PREFIX;
336 printf("Datadir: %s\n", datadir.c_str());
339 void Setup::general(void)
341 /* Seed random number generator: */
343 srand(SDL_GetTicks());
345 /* Set icon image: */
349 /* Unicode needed for input handling: */
351 SDL_EnableUNICODE(1);
353 /* Load GUI/menu images: */
354 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
355 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
356 back = new Surface(datadir + "/images/status/back.png", true);
357 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
358 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
360 /* Load the mouse-cursor */
361 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
362 MouseCursor::set_current(mouse_cursor);
366 void Setup::general_free(void)
369 /* Free GUI/menu images: */
371 delete checkbox_checked;
376 /* Free mouse-cursor */
381 void Setup::video(unsigned int screen_w, unsigned int screen_h)
383 /* Init SDL Video: */
384 if (SDL_Init(SDL_INIT_VIDEO) < 0)
387 "\nError: I could not initialize video!\n"
388 "The Simple DirectMedia error that occured was:\n"
389 "%s\n\n", SDL_GetError());
395 video_gl(screen_w, screen_h);
397 video_sdl(screen_w, screen_h);
399 Surface::reload_all();
401 /* Set window manager stuff: */
402 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
405 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
409 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
413 "\nWarning: I could not set up fullscreen video for "
415 "The Simple DirectMedia error that occured was:\n"
416 "%s\n\n", SDL_GetError());
417 use_fullscreen = false;
422 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
427 "\nError: I could not set up video for 800x600 mode.\n"
428 "The Simple DirectMedia error that occured was:\n"
429 "%s\n\n", SDL_GetError());
435 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
439 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
440 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
441 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
442 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
443 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
447 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
451 "\nWarning: I could not set up fullscreen video for "
453 "The Simple DirectMedia error that occured was:\n"
454 "%s\n\n", SDL_GetError());
455 use_fullscreen = false;
460 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
465 "\nError: I could not set up video for 640x480 mode.\n"
466 "The Simple DirectMedia error that occured was:\n"
467 "%s\n\n", SDL_GetError());
473 * Set up OpenGL for 2D rendering.
475 glDisable(GL_DEPTH_TEST);
476 glDisable(GL_CULL_FACE);
478 glViewport(0, 0, screen->w, screen->h);
479 glMatrixMode(GL_PROJECTION);
481 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
483 glMatrixMode(GL_MODELVIEW);
485 glTranslatef(0.0f, 0.0f, 0.0f);
491 void Setup::joystick(void)
498 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
500 fprintf(stderr, "Warning: I could not initialize joystick!\n"
501 "The Simple DirectMedia error that occured was:\n"
502 "%s\n\n", SDL_GetError());
504 use_joystick = false;
509 if (SDL_NumJoysticks() <= 0)
511 fprintf(stderr, "Info: No joysticks were found.\n");
513 use_joystick = false;
517 js = SDL_JoystickOpen(joystick_num);
521 fprintf(stderr, "Warning: Could not open joystick %d.\n"
522 "The Simple DirectMedia error that occured was:\n"
523 "%s\n\n", joystick_num, SDL_GetError());
525 use_joystick = false;
529 if (SDL_JoystickNumAxes(js) < 2)
532 "Warning: Joystick does not have enough axes!\n");
534 use_joystick = false;
538 if (SDL_JoystickNumButtons(js) < 2)
542 "Joystick does not have enough buttons!\n");
544 use_joystick = false;
552 void Setup::audio(void)
555 /* Init SDL Audio silently even if --disable-sound : */
557 if (SoundManager::get()->audio_device_available())
559 if (SDL_Init(SDL_INIT_AUDIO) < 0)
561 /* only print out message if sound or music
562 was not disabled at command-line
564 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
567 "\nWarning: I could not initialize audio!\n"
568 "The Simple DirectMedia error that occured was:\n"
569 "%s\n\n", SDL_GetError());
571 /* keep the programming logic the same :-)
572 because in this case, use_sound & use_music' values are ignored
573 when there's no available audio device
575 SoundManager::get()->enable_sound(false);
576 SoundManager::get()->enable_music(false);
577 SoundManager::get()->set_audio_device_available(false);
582 /* Open sound silently regarless the value of "use_sound": */
584 if (SoundManager::get()->audio_device_available())
586 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
588 /* only print out message if sound or music
589 was not disabled at command-line
591 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
594 "\nWarning: I could not set up audio for 44100 Hz "
596 "The Simple DirectMedia error that occured was:\n"
597 "%s\n\n", SDL_GetError());
599 SoundManager::get()->enable_sound(false);
600 SoundManager::get()->enable_music(false);
601 SoundManager::get()->set_audio_device_available(false);
608 /* --- SHUTDOWN --- */
610 void Termination::shutdown(void)
613 SoundManager::get()->close_audio();
619 void Termination::abort(const std::string& reason, const std::string& details)
621 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
626 /* Set Icon (private) */
635 /* Load icon into a surface: */
637 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
641 "\nError: I could not load the icon image: %s%s\n"
642 "The Simple DirectMedia error that occured was:\n"
643 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
650 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
651 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
652 memset(mask, 0xFF, masklen);
657 SDL_WM_SetIcon(icon, NULL);//mask);
660 /* Free icon surface & mask: */
663 SDL_FreeSurface(icon);
667 /* Parse command-line arguments: */
669 void Setup::parseargs(int argc, char * argv[])
675 /* Parse arguments: */
677 for (i = 1; i < argc; i++)
679 if (strcmp(argv[i], "--fullscreen") == 0 ||
680 strcmp(argv[i], "-f") == 0)
682 use_fullscreen = true;
684 else if (strcmp(argv[i], "--window") == 0 ||
685 strcmp(argv[i], "-w") == 0)
687 use_fullscreen = false;
689 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
692 joystick_num = atoi(argv[++i]);
694 else if (strcmp(argv[i], "--joymap") == 0)
697 if (sscanf(argv[++i],
699 &joystick_keymap.x_axis,
700 &joystick_keymap.y_axis,
701 &joystick_keymap.a_button,
702 &joystick_keymap.b_button,
703 &joystick_keymap.start_button) != 5)
705 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
709 std::cout << "Using new joymap:\n"
710 << " X-Axis: " << joystick_keymap.x_axis << "\n"
711 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
712 << " A-Button: " << joystick_keymap.a_button << "\n"
713 << " B-Button: " << joystick_keymap.b_button << "\n"
714 << " Start-Button: " << joystick_keymap.start_button << std::endl;
717 else if (strcmp(argv[i], "--leveleditor") == 0)
719 launch_leveleditor_mode = true;
721 else if (strcmp(argv[i], "--worldmap") == 0)
723 launch_worldmap_mode = true;
725 else if (strcmp(argv[i], "--flip-levels") == 0)
727 flip_levels_mode = true;
729 else if (strcmp(argv[i], "--datadir") == 0
730 || strcmp(argv[i], "-d") == 0 )
735 else if (strcmp(argv[i], "--show-fps") == 0)
737 /* Use full screen: */
741 else if (strcmp(argv[i], "--opengl") == 0 ||
742 strcmp(argv[i], "-gl") == 0)
750 else if (strcmp(argv[i], "--sdl") == 0)
754 else if (strcmp(argv[i], "--usage") == 0)
760 else if (strcmp(argv[i], "--version") == 0)
763 printf((package_name + " " + package_version + "\n").c_str() );
766 else if (strcmp(argv[i], "--disable-sound") == 0)
768 /* Disable the compiled in sound feature */
769 printf("Sounds disabled \n");
770 SoundManager::get()->enable_sound(false);
772 else if (strcmp(argv[i], "--disable-music") == 0)
774 /* Disable the compiled in sound feature */
775 printf("Music disabled \n");
776 SoundManager::get()->enable_music(false);
778 else if (strcmp(argv[i], "--debug") == 0)
780 /* Enable the debug-mode */
784 else if (strcmp(argv[i], "--help") == 0)
786 puts(_((" SuperTux " + package_version + "\n"
787 " Please see the file \"README.txt\" for more details.\n").c_str()));
788 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
789 puts(_("Display Options:\n"
790 " -f, --fullscreen Run in fullscreen mode.\n"
791 " -w, --window Run in window mode.\n"
792 " --opengl If OpenGL support was compiled in, this will tell\n"
793 " SuperTux to make use of it.\n"
794 " --sdl Use the SDL software graphical renderer\n"
797 " --disable-sound If sound support was compiled in, this will\n"
798 " disable sound for this session of the game.\n"
799 " --disable-music Like above, but this will disable music.\n"
802 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
803 " --joymap XAXIS:YAXIS:A:B:START\n"
804 " Define how joystick buttons and axis should be mapped\n"
805 " --leveleditor Opens the leveleditor in a file.\n"
806 " --worldmap Opens the specified worldmap file.\n"
807 " --flip-levels Flip levels upside-down.\n"
808 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
809 " --debug Enables the debug mode, which is useful for developers.\n"
810 " --help Display a help message summarizing command-line\n"
811 " options, license and game controls.\n"
812 " --usage Display a brief message summarizing command-line options.\n"
813 " --version Display the version of SuperTux you're running.\n\n"
817 else if (argv[i][0] != '-')
819 level_startup_file = argv[i];
823 /* Unknown - complain! */
833 void usage(char * prog, int ret)
838 /* Determine which stream to write to: */
846 /* Display the usage message: */
848 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
857 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
859 std::set<std::string> dirnames;
861 DIR* dir = opendir(pathname.c_str());
864 struct dirent *direntp;
866 while((direntp = readdir(dir)))
868 dirnames.insert(direntp->d_name);