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");
96 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
97 int FileSystem::fcreatedir(const std::string& relative_dir)
99 std::string path = st_dir + "/" + relative_dir + "/";
100 if(mkdir(path.c_str(),0755) != 0)
102 path = datadir + "/" + relative_dir + "/";
103 if(mkdir(path.c_str(),0755) != 0)
118 /* Get all names of sub-directories in a certain directory. */
119 /* Returns the number of sub-directories found. */
120 /* Note: The user has to free the allocated space. */
121 std::set<std::string> FileSystem::dsubdirs(const std::string &rel_path,const std::string& expected_file)
124 struct dirent *direntp;
125 std::set<std::string> sdirs;
126 std::string filename;
127 std::string path = st_dir + "/" + rel_path;
129 if((dirStructP = opendir(path.c_str())) != NULL)
131 while((direntp = readdir(dirStructP)) != NULL)
133 std::string absolute_filename;
136 absolute_filename = path + "/" + direntp->d_name;
138 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
140 if(!expected_file.empty())
142 filename = path + "/" + direntp->d_name + "/" + expected_file;
143 if(!faccessible(filename))
147 sdirs.insert(direntp->d_name);
150 closedir(dirStructP);
153 path = datadir + "/" + rel_path;
154 if((dirStructP = opendir(path.c_str())) != NULL)
156 while((direntp = readdir(dirStructP)) != NULL)
158 std::string absolute_filename;
161 absolute_filename = path + "/" + direntp->d_name;
163 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
165 if(!expected_file.empty())
167 filename = path + "/" + direntp->d_name + "/" + expected_file;
168 if(!faccessible(filename.c_str()))
174 filename = st_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file;
175 if(faccessible(filename.c_str()))
180 sdirs.insert(direntp->d_name);
183 closedir(dirStructP);
189 std::set<std::string> FileSystem::dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str)
192 struct dirent *direntp;
193 std::set<std::string> sdirs;
194 std::string path = st_dir + "/" + rel_path;
196 if((dirStructP = opendir(path.c_str())) != NULL)
198 while((direntp = readdir(dirStructP)) != NULL)
200 std::string absolute_filename;
203 absolute_filename = path + "/" + direntp->d_name;
205 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
207 if(!exception_str.empty())
209 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
213 if(strstr(direntp->d_name,glob.c_str()) == NULL)
216 sdirs.insert(direntp->d_name);
219 closedir(dirStructP);
222 path = datadir + "/" + rel_path;
223 if((dirStructP = opendir(path.c_str())) != NULL)
225 while((direntp = readdir(dirStructP)) != NULL)
227 std::string absolute_filename;
230 absolute_filename = path + "/" + direntp->d_name;
232 if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode))
234 if(!exception_str.empty())
236 if(strstr(direntp->d_name,exception_str.c_str()) != NULL)
240 if(strstr(direntp->d_name,glob.c_str()) == NULL)
243 sdirs.insert(direntp->d_name);
246 closedir(dirStructP);
252 void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
254 package_name = _package_name;
255 package_symbol_name = _package_symbol_name;
256 package_version = _package_version;
260 /* Set SuperTux configuration and save directories */
261 void Setup::directories(void)
264 /* Get home directory (from $HOME variable)... if we can't determine it,
265 use the current directory ("."): */
266 if (getenv("HOME") != NULL)
267 home = getenv("HOME");
271 st_dir = home + "/." + package_symbol_name;
273 /* Remove .supertux config-file from old SuperTux versions */
274 if(FileSystem::faccessible(st_dir))
276 remove(st_dir.c_str());
279 st_save_dir = st_dir + "/save";
281 /* Create them. In the case they exist they won't destroy anything. */
282 mkdir(st_dir.c_str(), 0755);
283 mkdir(st_save_dir.c_str(), 0755);
285 mkdir((st_dir + "/levels").c_str(), 0755);
287 // User has not that a datadir, so we try some magic
292 char exe_file[PATH_MAX];
293 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
295 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
296 datadir = DATA_PREFIX;
300 std::string exedir = std::string(dirname(exe_file)) + "/";
302 datadir = exedir + "../../data"; // SuperTux run from source dir
303 if (access(datadir.c_str(), F_OK) != 0)
305 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
306 if (access(datadir.c_str(), F_OK) != 0)
307 { // If all fails, fall back to compiled path
308 datadir = DATA_PREFIX;
313 datadir = DATA_PREFIX;
316 printf("Datadir: %s\n", datadir.c_str());
319 void Setup::general(void)
321 /* Seed random number generator: */
323 srand(SDL_GetTicks());
325 /* Set icon image: */
329 /* Unicode needed for input handling: */
331 SDL_EnableUNICODE(1);
333 /* Load GUI/menu images: */
334 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
335 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
336 back = new Surface(datadir + "/images/status/back.png", true);
337 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
338 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
340 /* Load the mouse-cursor */
341 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
342 MouseCursor::set_current(mouse_cursor);
346 void Setup::general_free(void)
349 /* Free GUI/menu images: */
351 delete checkbox_checked;
356 /* Free mouse-cursor */
361 void Setup::video(unsigned int screen_w, unsigned int screen_h)
363 /* Init SDL Video: */
364 if (SDL_Init(SDL_INIT_VIDEO) < 0)
367 "\nError: I could not initialize video!\n"
368 "The Simple DirectMedia error that occured was:\n"
369 "%s\n\n", SDL_GetError());
375 video_gl(screen_w, screen_h);
377 video_sdl(screen_w, screen_h);
379 Surface::reload_all();
381 /* Set window manager stuff: */
382 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
385 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
389 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
393 "\nWarning: I could not set up fullscreen video for "
395 "The Simple DirectMedia error that occured was:\n"
396 "%s\n\n", SDL_GetError());
397 use_fullscreen = false;
402 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
407 "\nError: I could not set up video for 800x600 mode.\n"
408 "The Simple DirectMedia error that occured was:\n"
409 "%s\n\n", SDL_GetError());
415 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
419 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
420 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
421 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
422 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
423 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
427 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
431 "\nWarning: I could not set up fullscreen video for "
433 "The Simple DirectMedia error that occured was:\n"
434 "%s\n\n", SDL_GetError());
435 use_fullscreen = false;
440 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
445 "\nError: I could not set up video for 640x480 mode.\n"
446 "The Simple DirectMedia error that occured was:\n"
447 "%s\n\n", SDL_GetError());
453 * Set up OpenGL for 2D rendering.
455 glDisable(GL_DEPTH_TEST);
456 glDisable(GL_CULL_FACE);
458 glViewport(0, 0, screen->w, screen->h);
459 glMatrixMode(GL_PROJECTION);
461 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
463 glMatrixMode(GL_MODELVIEW);
465 glTranslatef(0.0f, 0.0f, 0.0f);
471 void Setup::joystick(void)
478 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
480 fprintf(stderr, "Warning: I could not initialize joystick!\n"
481 "The Simple DirectMedia error that occured was:\n"
482 "%s\n\n", SDL_GetError());
484 use_joystick = false;
489 if (SDL_NumJoysticks() <= 0)
491 fprintf(stderr, "Info: No joysticks were found.\n");
493 use_joystick = false;
497 js = SDL_JoystickOpen(joystick_num);
501 fprintf(stderr, "Warning: Could not open joystick %d.\n"
502 "The Simple DirectMedia error that occured was:\n"
503 "%s\n\n", joystick_num, SDL_GetError());
505 use_joystick = false;
509 if (SDL_JoystickNumAxes(js) < 2)
512 "Warning: Joystick does not have enough axes!\n");
514 use_joystick = false;
518 if (SDL_JoystickNumButtons(js) < 2)
522 "Joystick does not have enough buttons!\n");
524 use_joystick = false;
532 void Setup::audio(void)
535 /* Init SDL Audio silently even if --disable-sound : */
537 if (SoundManager::get()->audio_device_available())
539 if (SDL_Init(SDL_INIT_AUDIO) < 0)
541 /* only print out message if sound or music
542 was not disabled at command-line
544 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
547 "\nWarning: I could not initialize audio!\n"
548 "The Simple DirectMedia error that occured was:\n"
549 "%s\n\n", SDL_GetError());
551 /* keep the programming logic the same :-)
552 because in this case, use_sound & use_music' values are ignored
553 when there's no available audio device
555 SoundManager::get()->enable_sound(false);
556 SoundManager::get()->enable_music(false);
557 SoundManager::get()->set_audio_device_available(false);
562 /* Open sound silently regarless the value of "use_sound": */
564 if (SoundManager::get()->audio_device_available())
566 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
568 /* only print out message if sound or music
569 was not disabled at command-line
571 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
574 "\nWarning: I could not set up audio for 44100 Hz "
576 "The Simple DirectMedia error that occured was:\n"
577 "%s\n\n", SDL_GetError());
579 SoundManager::get()->enable_sound(false);
580 SoundManager::get()->enable_music(false);
581 SoundManager::get()->set_audio_device_available(false);
588 /* --- SHUTDOWN --- */
590 void Termination::shutdown(void)
593 SoundManager::get()->close_audio();
599 void Termination::abort(const std::string& reason, const std::string& details)
601 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
606 /* Set Icon (private) */
615 /* Load icon into a surface: */
617 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
621 "\nError: I could not load the icon image: %s%s\n"
622 "The Simple DirectMedia error that occured was:\n"
623 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
630 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
631 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
632 memset(mask, 0xFF, masklen);
637 SDL_WM_SetIcon(icon, NULL);//mask);
640 /* Free icon surface & mask: */
643 SDL_FreeSurface(icon);
647 /* Parse command-line arguments: */
649 void Setup::parseargs(int argc, char * argv[])
655 /* Parse arguments: */
657 for (i = 1; i < argc; i++)
659 if (strcmp(argv[i], "--fullscreen") == 0 ||
660 strcmp(argv[i], "-f") == 0)
662 use_fullscreen = true;
664 else if (strcmp(argv[i], "--window") == 0 ||
665 strcmp(argv[i], "-w") == 0)
667 use_fullscreen = false;
669 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
672 joystick_num = atoi(argv[++i]);
674 else if (strcmp(argv[i], "--joymap") == 0)
677 if (sscanf(argv[++i],
679 &joystick_keymap.x_axis,
680 &joystick_keymap.y_axis,
681 &joystick_keymap.a_button,
682 &joystick_keymap.b_button,
683 &joystick_keymap.start_button) != 5)
685 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
689 std::cout << "Using new joymap:\n"
690 << " X-Axis: " << joystick_keymap.x_axis << "\n"
691 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
692 << " A-Button: " << joystick_keymap.a_button << "\n"
693 << " B-Button: " << joystick_keymap.b_button << "\n"
694 << " Start-Button: " << joystick_keymap.start_button << std::endl;
697 else if (strcmp(argv[i], "--leveleditor") == 0)
699 launch_leveleditor_mode = true;
701 else if (strcmp(argv[i], "--worldmap") == 0)
703 launch_worldmap_mode = true;
705 else if (strcmp(argv[i], "--flip-levels") == 0)
707 flip_levels_mode = true;
709 else if (strcmp(argv[i], "--datadir") == 0
710 || strcmp(argv[i], "-d") == 0 )
715 else if (strcmp(argv[i], "--show-fps") == 0)
717 /* Use full screen: */
721 else if (strcmp(argv[i], "--opengl") == 0 ||
722 strcmp(argv[i], "-gl") == 0)
730 else if (strcmp(argv[i], "--sdl") == 0)
734 else if (strcmp(argv[i], "--usage") == 0)
740 else if (strcmp(argv[i], "--version") == 0)
743 printf((package_name + package_version + "\n").c_str() );
746 else if (strcmp(argv[i], "--disable-sound") == 0)
748 /* Disable the compiled in sound feature */
749 printf("Sounds disabled \n");
750 SoundManager::get()->enable_sound(false);
752 else if (strcmp(argv[i], "--disable-music") == 0)
754 /* Disable the compiled in sound feature */
755 printf("Music disabled \n");
756 SoundManager::get()->enable_music(false);
758 else if (strcmp(argv[i], "--debug") == 0)
760 /* Enable the debug-mode */
764 else if (strcmp(argv[i], "--help") == 0)
766 puts(_((" SuperTux " + package_version + "\n"
767 " Please see the file \"README.txt\" for more details.\n").c_str()));
768 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
769 puts(_("Display Options:\n"
770 " -f, --fullscreen Run in fullscreen mode.\n"
771 " -w, --window Run in window mode.\n"
772 " --opengl If OpenGL support was compiled in, this will tell\n"
773 " SuperTux to make use of it.\n"
774 " --sdl Use the SDL software graphical renderer\n"
777 " --disable-sound If sound support was compiled in, this will\n"
778 " disable sound for this session of the game.\n"
779 " --disable-music Like above, but this will disable music.\n"
782 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
783 " --joymap XAXIS:YAXIS:A:B:START\n"
784 " Define how joystick buttons and axis should be mapped\n"
785 " --leveleditor Opens the leveleditor in a file.\n"
786 " --worldmap Opens the specified worldmap file.\n"
787 " --flip-levels Flip levels upside-down.\n"
788 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
789 " --debug Enables the debug mode, which is useful for developers.\n"
790 " --help Display a help message summarizing command-line\n"
791 " options, license and game controls.\n"
792 " --usage Display a brief message summarizing command-line options.\n"
793 " --version Display the version of SuperTux you're running.\n\n"
797 else if (argv[i][0] != '-')
799 level_startup_file = argv[i];
803 /* Unknown - complain! */
813 void usage(char * prog, int ret)
818 /* Determine which stream to write to: */
826 /* Display the usage message: */
828 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
837 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
839 std::set<std::string> dirnames;
841 DIR* dir = opendir(pathname.c_str());
844 struct dirent *direntp;
846 while((direntp = readdir(dir)))
848 dirnames.insert(direntp->d_name);