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 bool 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 bool 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 bool 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 std::string FileSystem::dirname(const std::string& filename)
257 std::string::size_type p = filename.find_last_of('/');
258 if(p == std::string::npos)
261 return filename.substr(0, p+1);
264 void Setup::init(const std::string& _package_name,
265 const std::string& _package_symbol_name,
266 const std::string& _package_version)
268 package_name = _package_name;
269 package_symbol_name = _package_symbol_name;
270 package_version = _package_version;
273 dictionary_manager.add_directory(datadir + "/locale");
274 dictionary_manager.set_charset("iso8859-1");
278 /* Set SuperTux configuration and save directories */
279 void Setup::directories()
282 /* Get home directory (from $HOME variable)... if we can't determine it,
283 use the current directory ("."): */
284 if (getenv("HOME") != NULL)
285 home = getenv("HOME");
289 st_dir = home + "/." + package_symbol_name;
291 /* Remove .supertux config-file from old SuperTux versions */
292 if(FileSystem::faccessible(st_dir)) {
293 remove(st_dir.c_str());
296 st_save_dir = st_dir + "/save";
298 /* Create them. In the case they exist they won't destroy anything. */
299 mkdir(st_dir.c_str(), 0755);
300 mkdir(st_save_dir.c_str(), 0755);
302 mkdir((st_dir + "/levels").c_str(), 0755);
304 // try current directory as datadir
305 if(datadir.empty()) {
306 if(FileSystem::faccessible("./data/intro.txt"))
310 // User has not that a datadir, so we try some magic
315 char exe_file[PATH_MAX];
316 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
318 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
319 datadir = DATA_PREFIX;
323 std::string exedir = std::string(dirname(exe_file)) + "/";
325 datadir = exedir + "./data/"; // SuperTux run from source dir
326 if (access(datadir.c_str(), F_OK) != 0)
328 datadir = exedir + "../../data/"; //SuperTux run from source dir (with libtool script)
330 if (access(datadir.c_str(), F_OK) != 0)
332 datadir = exedir + "../share/" + package_symbol_name + "/"; // SuperTux run from PATH
333 if (access(datadir.c_str(), F_OK) != 0)
334 { // If all fails, fall back to compiled path
335 datadir = DATA_PREFIX;
342 datadir = DATA_PREFIX;
346 printf("Datadir: %s\n", datadir.c_str());
349 void Setup::general(void)
351 /* Seed random number generator: */
353 srand(SDL_GetTicks());
355 /* Set icon image: */
359 /* Unicode needed for input handling: */
361 SDL_EnableUNICODE(1);
363 /* Load GUI/menu images: */
364 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
365 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
366 back = new Surface(datadir + "/images/status/back.png", true);
367 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
368 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
370 /* Load the mouse-cursor */
371 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
372 MouseCursor::set_current(mouse_cursor);
376 void Setup::general_free(void)
379 /* Free GUI/menu images: */
381 delete checkbox_checked;
386 /* Free mouse-cursor */
391 void Setup::video(unsigned int screen_w, unsigned int screen_h)
393 /* Init SDL Video: */
394 if (SDL_Init(SDL_INIT_VIDEO) < 0)
397 "\nError: I could not initialize video!\n"
398 "The Simple DirectMedia error that occured was:\n"
399 "%s\n\n", SDL_GetError());
405 video_gl(screen_w, screen_h);
407 video_sdl(screen_w, screen_h);
409 Surface::reload_all();
411 /* Set window manager stuff: */
412 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
415 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
419 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
423 "\nWarning: I could not set up fullscreen video for "
425 "The Simple DirectMedia error that occured was:\n"
426 "%s\n\n", SDL_GetError());
427 use_fullscreen = false;
432 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
437 "\nError: I could not set up video for 800x600 mode.\n"
438 "The Simple DirectMedia error that occured was:\n"
439 "%s\n\n", SDL_GetError());
445 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
449 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
450 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
451 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
452 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
453 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
457 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
461 "\nWarning: I could not set up fullscreen video for "
463 "The Simple DirectMedia error that occured was:\n"
464 "%s\n\n", SDL_GetError());
465 use_fullscreen = false;
470 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
475 "\nError: I could not set up video for 640x480 mode.\n"
476 "The Simple DirectMedia error that occured was:\n"
477 "%s\n\n", SDL_GetError());
483 * Set up OpenGL for 2D rendering.
485 glDisable(GL_DEPTH_TEST);
486 glDisable(GL_CULL_FACE);
488 glViewport(0, 0, screen->w, screen->h);
489 glMatrixMode(GL_PROJECTION);
491 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
493 glMatrixMode(GL_MODELVIEW);
495 glTranslatef(0.0f, 0.0f, 0.0f);
501 void Setup::joystick(void)
508 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
510 fprintf(stderr, "Warning: I could not initialize joystick!\n"
511 "The Simple DirectMedia error that occured was:\n"
512 "%s\n\n", SDL_GetError());
514 use_joystick = false;
519 if (SDL_NumJoysticks() <= 0)
521 fprintf(stderr, "Info: No joysticks were found.\n");
523 use_joystick = false;
527 js = SDL_JoystickOpen(joystick_num);
531 fprintf(stderr, "Warning: Could not open joystick %d.\n"
532 "The Simple DirectMedia error that occured was:\n"
533 "%s\n\n", joystick_num, SDL_GetError());
535 use_joystick = false;
539 if (SDL_JoystickNumAxes(js) < 2)
542 "Warning: Joystick does not have enough axes!\n");
544 use_joystick = false;
548 if (SDL_JoystickNumButtons(js) < 2)
552 "Joystick does not have enough buttons!\n");
554 use_joystick = false;
562 void Setup::audio(void)
565 /* Init SDL Audio silently even if --disable-sound : */
567 if (SoundManager::get()->audio_device_available())
569 if (SDL_Init(SDL_INIT_AUDIO) < 0)
571 /* only print out message if sound or music
572 was not disabled at command-line
574 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
577 "\nWarning: I could not initialize audio!\n"
578 "The Simple DirectMedia error that occured was:\n"
579 "%s\n\n", SDL_GetError());
581 /* keep the programming logic the same :-)
582 because in this case, use_sound & use_music' values are ignored
583 when there's no available audio device
585 SoundManager::get()->enable_sound(false);
586 SoundManager::get()->enable_music(false);
587 SoundManager::get()->set_audio_device_available(false);
592 /* Open sound silently regarless the value of "use_sound": */
594 if (SoundManager::get()->audio_device_available())
596 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
598 /* only print out message if sound or music
599 was not disabled at command-line
601 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
604 "\nWarning: I could not set up audio for 44100 Hz "
606 "The Simple DirectMedia error that occured was:\n"
607 "%s\n\n", SDL_GetError());
609 SoundManager::get()->enable_sound(false);
610 SoundManager::get()->enable_music(false);
611 SoundManager::get()->set_audio_device_available(false);
618 /* --- SHUTDOWN --- */
620 void Termination::shutdown(void)
623 SoundManager::get()->close_audio();
629 void Termination::abort(const std::string& reason, const std::string& details)
631 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
636 /* Set Icon (private) */
645 /* Load icon into a surface: */
647 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
651 "\nError: I could not load the icon image: %s%s\n"
652 "The Simple DirectMedia error that occured was:\n"
653 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
660 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
661 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
662 memset(mask, 0xFF, masklen);
667 SDL_WM_SetIcon(icon, NULL);//mask);
670 /* Free icon surface & mask: */
673 SDL_FreeSurface(icon);
677 /* Parse command-line arguments: */
679 void Setup::parseargs(int argc, char * argv[])
685 /* Parse arguments: */
687 for (i = 1; i < argc; i++)
689 if (strcmp(argv[i], "--fullscreen") == 0 ||
690 strcmp(argv[i], "-f") == 0)
692 use_fullscreen = true;
694 else if (strcmp(argv[i], "--window") == 0 ||
695 strcmp(argv[i], "-w") == 0)
697 use_fullscreen = false;
699 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
702 joystick_num = atoi(argv[++i]);
704 else if (strcmp(argv[i], "--joymap") == 0)
707 if (sscanf(argv[++i],
709 &joystick_keymap.x_axis,
710 &joystick_keymap.y_axis,
711 &joystick_keymap.a_button,
712 &joystick_keymap.b_button,
713 &joystick_keymap.start_button) != 5)
715 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
719 std::cout << "Using new joymap:\n"
720 << " X-Axis: " << joystick_keymap.x_axis << "\n"
721 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
722 << " A-Button: " << joystick_keymap.a_button << "\n"
723 << " B-Button: " << joystick_keymap.b_button << "\n"
724 << " Start-Button: " << joystick_keymap.start_button << std::endl;
727 else if (strcmp(argv[i], "--leveleditor") == 0)
729 launch_leveleditor_mode = true;
731 else if (strcmp(argv[i], "--worldmap") == 0)
733 launch_worldmap_mode = true;
735 else if (strcmp(argv[i], "--flip-levels") == 0)
737 flip_levels_mode = true;
739 else if (strcmp(argv[i], "--datadir") == 0
740 || strcmp(argv[i], "-d") == 0 )
745 else if (strcmp(argv[i], "--show-fps") == 0)
747 /* Use full screen: */
751 else if (strcmp(argv[i], "--opengl") == 0 ||
752 strcmp(argv[i], "-gl") == 0)
760 else if (strcmp(argv[i], "--sdl") == 0)
764 else if (strcmp(argv[i], "--usage") == 0)
770 else if (strcmp(argv[i], "--version") == 0)
773 printf((package_name + " " + package_version + "\n").c_str() );
776 else if (strcmp(argv[i], "--disable-sound") == 0)
778 /* Disable the compiled in sound feature */
779 printf("Sounds disabled \n");
780 SoundManager::get()->enable_sound(false);
782 else if (strcmp(argv[i], "--disable-music") == 0)
784 /* Disable the compiled in sound feature */
785 printf("Music disabled \n");
786 SoundManager::get()->enable_music(false);
788 else if (strcmp(argv[i], "--debug") == 0)
790 /* Enable the debug-mode */
794 else if (strcmp(argv[i], "--help") == 0)
796 puts(_((" SuperTux " + package_version + "\n"
797 " Please see the file \"README.txt\" for more details.\n").c_str()));
798 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
799 puts(_("Display Options:\n"
800 " -f, --fullscreen Run in fullscreen mode.\n"
801 " -w, --window Run in window mode.\n"
802 " --opengl If OpenGL support was compiled in, this will tell\n"
803 " SuperTux to make use of it.\n"
804 " --sdl Use the SDL software graphical renderer\n"
807 " --disable-sound If sound support was compiled in, this will\n"
808 " disable sound for this session of the game.\n"
809 " --disable-music Like above, but this will disable music.\n"
812 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
813 " --joymap XAXIS:YAXIS:A:B:START\n"
814 " Define how joystick buttons and axis should be mapped\n"
815 " --leveleditor Opens the leveleditor in a file.\n"
816 " --worldmap Opens the specified worldmap file.\n"
817 " --flip-levels Flip levels upside-down.\n"
818 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
819 " --debug Enables the debug mode, which is useful for developers.\n"
820 " --help Display a help message summarizing command-line\n"
821 " options, license and game controls.\n"
822 " --usage Display a brief message summarizing command-line options.\n"
823 " --version Display the version of SuperTux you're running.\n\n"
827 else if (argv[i][0] != '-')
829 level_startup_file = argv[i];
833 /* Unknown - complain! */
843 void usage(char * prog, int ret)
848 /* Determine which stream to write to: */
856 /* Display the usage message: */
858 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
867 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
869 std::set<std::string> dirnames;
871 DIR* dir = opendir(pathname.c_str());
874 struct dirent *direntp;
876 while((direntp = readdir(dir)))
878 dirnames.insert(direntp->d_name);