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>
46 #include "../app/globals.h"
47 #include "../app/setup.h"
48 #include "../video/screen.h"
49 #include "../video/surface.h"
50 #include "../gui/menu.h"
51 #include "../utils/configfile.h"
52 #include "../audio/sound_manager.h"
53 #include "../app/gettext.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::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)
267 /* Get home directory (from $HOME variable)... if we can't determine it,
268 use the current directory ("."): */
269 if (getenv("HOME") != NULL)
270 home = getenv("HOME");
274 st_dir = home + "/." + package_symbol_name;
276 /* Remove .supertux config-file from old SuperTux versions */
277 if(FileSystem::faccessible(st_dir))
279 remove(st_dir.c_str());
282 st_save_dir = st_dir + "/save";
284 /* Create them. In the case they exist they won't destroy anything. */
285 mkdir(st_dir.c_str(), 0755);
286 mkdir(st_save_dir.c_str(), 0755);
288 mkdir((st_dir + "/levels").c_str(), 0755);
290 // try current directory as datadir
291 if(datadir.empty()) {
292 if(FileSystem::faccessible("./data/intro.txt"))
296 // User has not that a datadir, so we try some magic
301 char exe_file[PATH_MAX];
302 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
304 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
305 datadir = DATA_PREFIX;
309 std::string exedir = std::string(dirname(exe_file)) + "/";
311 datadir = exedir + "../data"; // SuperTux run from source dir
312 if (access(datadir.c_str(), F_OK) != 0)
314 datadir = exedir + "../../data"; //SuperTux run from source dir (with libtool script)
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;
327 datadir = DATA_PREFIX;
330 printf("Datadir: %s\n", datadir.c_str());
333 void Setup::general(void)
335 /* Seed random number generator: */
337 srand(SDL_GetTicks());
339 /* Set icon image: */
343 /* Unicode needed for input handling: */
345 SDL_EnableUNICODE(1);
347 /* Load GUI/menu images: */
348 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
349 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
350 back = new Surface(datadir + "/images/status/back.png", true);
351 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
352 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
354 /* Load the mouse-cursor */
355 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
356 MouseCursor::set_current(mouse_cursor);
360 void Setup::general_free(void)
363 /* Free GUI/menu images: */
365 delete checkbox_checked;
370 /* Free mouse-cursor */
375 void Setup::video(unsigned int screen_w, unsigned int screen_h)
377 /* Init SDL Video: */
378 if (SDL_Init(SDL_INIT_VIDEO) < 0)
381 "\nError: I could not initialize video!\n"
382 "The Simple DirectMedia error that occured was:\n"
383 "%s\n\n", SDL_GetError());
389 video_gl(screen_w, screen_h);
391 video_sdl(screen_w, screen_h);
393 Surface::reload_all();
395 /* Set window manager stuff: */
396 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
399 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
403 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
407 "\nWarning: I could not set up fullscreen video for "
409 "The Simple DirectMedia error that occured was:\n"
410 "%s\n\n", SDL_GetError());
411 use_fullscreen = false;
416 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
421 "\nError: I could not set up video for 800x600 mode.\n"
422 "The Simple DirectMedia error that occured was:\n"
423 "%s\n\n", SDL_GetError());
429 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
433 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
434 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
435 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
436 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
437 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
441 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
445 "\nWarning: I could not set up fullscreen video for "
447 "The Simple DirectMedia error that occured was:\n"
448 "%s\n\n", SDL_GetError());
449 use_fullscreen = false;
454 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
459 "\nError: I could not set up video for 640x480 mode.\n"
460 "The Simple DirectMedia error that occured was:\n"
461 "%s\n\n", SDL_GetError());
467 * Set up OpenGL for 2D rendering.
469 glDisable(GL_DEPTH_TEST);
470 glDisable(GL_CULL_FACE);
472 glViewport(0, 0, screen->w, screen->h);
473 glMatrixMode(GL_PROJECTION);
475 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
477 glMatrixMode(GL_MODELVIEW);
479 glTranslatef(0.0f, 0.0f, 0.0f);
485 void Setup::joystick(void)
492 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
494 fprintf(stderr, "Warning: I could not initialize joystick!\n"
495 "The Simple DirectMedia error that occured was:\n"
496 "%s\n\n", SDL_GetError());
498 use_joystick = false;
503 if (SDL_NumJoysticks() <= 0)
505 fprintf(stderr, "Info: No joysticks were found.\n");
507 use_joystick = false;
511 js = SDL_JoystickOpen(joystick_num);
515 fprintf(stderr, "Warning: Could not open joystick %d.\n"
516 "The Simple DirectMedia error that occured was:\n"
517 "%s\n\n", joystick_num, SDL_GetError());
519 use_joystick = false;
523 if (SDL_JoystickNumAxes(js) < 2)
526 "Warning: Joystick does not have enough axes!\n");
528 use_joystick = false;
532 if (SDL_JoystickNumButtons(js) < 2)
536 "Joystick does not have enough buttons!\n");
538 use_joystick = false;
546 void Setup::audio(void)
549 /* Init SDL Audio silently even if --disable-sound : */
551 if (SoundManager::get()->audio_device_available())
553 if (SDL_Init(SDL_INIT_AUDIO) < 0)
555 /* only print out message if sound or music
556 was not disabled at command-line
558 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
561 "\nWarning: I could not initialize audio!\n"
562 "The Simple DirectMedia error that occured was:\n"
563 "%s\n\n", SDL_GetError());
565 /* keep the programming logic the same :-)
566 because in this case, use_sound & use_music' values are ignored
567 when there's no available audio device
569 SoundManager::get()->enable_sound(false);
570 SoundManager::get()->enable_music(false);
571 SoundManager::get()->set_audio_device_available(false);
576 /* Open sound silently regarless the value of "use_sound": */
578 if (SoundManager::get()->audio_device_available())
580 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
582 /* only print out message if sound or music
583 was not disabled at command-line
585 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
588 "\nWarning: I could not set up audio for 44100 Hz "
590 "The Simple DirectMedia error that occured was:\n"
591 "%s\n\n", SDL_GetError());
593 SoundManager::get()->enable_sound(false);
594 SoundManager::get()->enable_music(false);
595 SoundManager::get()->set_audio_device_available(false);
602 /* --- SHUTDOWN --- */
604 void Termination::shutdown(void)
607 SoundManager::get()->close_audio();
613 void Termination::abort(const std::string& reason, const std::string& details)
615 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
620 /* Set Icon (private) */
629 /* Load icon into a surface: */
631 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
635 "\nError: I could not load the icon image: %s%s\n"
636 "The Simple DirectMedia error that occured was:\n"
637 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
644 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
645 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
646 memset(mask, 0xFF, masklen);
651 SDL_WM_SetIcon(icon, NULL);//mask);
654 /* Free icon surface & mask: */
657 SDL_FreeSurface(icon);
661 /* Parse command-line arguments: */
663 void Setup::parseargs(int argc, char * argv[])
669 /* Parse arguments: */
671 for (i = 1; i < argc; i++)
673 if (strcmp(argv[i], "--fullscreen") == 0 ||
674 strcmp(argv[i], "-f") == 0)
676 use_fullscreen = true;
678 else if (strcmp(argv[i], "--window") == 0 ||
679 strcmp(argv[i], "-w") == 0)
681 use_fullscreen = false;
683 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
686 joystick_num = atoi(argv[++i]);
688 else if (strcmp(argv[i], "--joymap") == 0)
691 if (sscanf(argv[++i],
693 &joystick_keymap.x_axis,
694 &joystick_keymap.y_axis,
695 &joystick_keymap.a_button,
696 &joystick_keymap.b_button,
697 &joystick_keymap.start_button) != 5)
699 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
703 std::cout << "Using new joymap:\n"
704 << " X-Axis: " << joystick_keymap.x_axis << "\n"
705 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
706 << " A-Button: " << joystick_keymap.a_button << "\n"
707 << " B-Button: " << joystick_keymap.b_button << "\n"
708 << " Start-Button: " << joystick_keymap.start_button << std::endl;
711 else if (strcmp(argv[i], "--leveleditor") == 0)
713 launch_leveleditor_mode = true;
715 else if (strcmp(argv[i], "--worldmap") == 0)
717 launch_worldmap_mode = true;
719 else if (strcmp(argv[i], "--flip-levels") == 0)
721 flip_levels_mode = true;
723 else if (strcmp(argv[i], "--datadir") == 0
724 || strcmp(argv[i], "-d") == 0 )
729 else if (strcmp(argv[i], "--show-fps") == 0)
731 /* Use full screen: */
735 else if (strcmp(argv[i], "--opengl") == 0 ||
736 strcmp(argv[i], "-gl") == 0)
744 else if (strcmp(argv[i], "--sdl") == 0)
748 else if (strcmp(argv[i], "--usage") == 0)
754 else if (strcmp(argv[i], "--version") == 0)
757 printf((package_name + " " + package_version + "\n").c_str() );
760 else if (strcmp(argv[i], "--disable-sound") == 0)
762 /* Disable the compiled in sound feature */
763 printf("Sounds disabled \n");
764 SoundManager::get()->enable_sound(false);
766 else if (strcmp(argv[i], "--disable-music") == 0)
768 /* Disable the compiled in sound feature */
769 printf("Music disabled \n");
770 SoundManager::get()->enable_music(false);
772 else if (strcmp(argv[i], "--debug") == 0)
774 /* Enable the debug-mode */
778 else if (strcmp(argv[i], "--help") == 0)
780 puts(_((" SuperTux " + package_version + "\n"
781 " Please see the file \"README.txt\" for more details.\n").c_str()));
782 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
783 puts(_("Display Options:\n"
784 " -f, --fullscreen Run in fullscreen mode.\n"
785 " -w, --window Run in window mode.\n"
786 " --opengl If OpenGL support was compiled in, this will tell\n"
787 " SuperTux to make use of it.\n"
788 " --sdl Use the SDL software graphical renderer\n"
791 " --disable-sound If sound support was compiled in, this will\n"
792 " disable sound for this session of the game.\n"
793 " --disable-music Like above, but this will disable music.\n"
796 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
797 " --joymap XAXIS:YAXIS:A:B:START\n"
798 " Define how joystick buttons and axis should be mapped\n"
799 " --leveleditor Opens the leveleditor in a file.\n"
800 " --worldmap Opens the specified worldmap file.\n"
801 " --flip-levels Flip levels upside-down.\n"
802 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
803 " --debug Enables the debug mode, which is useful for developers.\n"
804 " --help Display a help message summarizing command-line\n"
805 " options, license and game controls.\n"
806 " --usage Display a brief message summarizing command-line options.\n"
807 " --version Display the version of SuperTux you're running.\n\n"
811 else if (argv[i][0] != '-')
813 level_startup_file = argv[i];
817 /* Unknown - complain! */
827 void usage(char * prog, int ret)
832 /* Determine which stream to write to: */
840 /* Display the usage message: */
842 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
851 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
853 std::set<std::string> dirnames;
855 DIR* dir = opendir(pathname.c_str());
858 struct dirent *direntp;
860 while((direntp = readdir(dir)))
862 dirnames.insert(direntp->d_name);