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)) {
284 remove(st_dir.c_str());
287 st_save_dir = st_dir + "/save";
289 /* Create them. In the case they exist they won't destroy anything. */
290 mkdir(st_dir.c_str(), 0755);
291 mkdir(st_save_dir.c_str(), 0755);
293 mkdir((st_dir + "/levels").c_str(), 0755);
295 // try current directory as datadir
296 if(datadir.empty()) {
297 if(FileSystem::faccessible("./data/intro.txt"))
301 // User has not that a datadir, so we try some magic
306 char exe_file[PATH_MAX];
307 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
309 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
310 datadir = DATA_PREFIX;
314 std::string exedir = std::string(dirname(exe_file)) + "/";
316 datadir = exedir + "../data/"; // SuperTux run from source dir
317 if (access(datadir.c_str(), F_OK) != 0)
319 datadir = exedir + "../../data/"; //SuperTux run from source dir (with libtool script)
321 if (access(datadir.c_str(), F_OK) != 0)
323 datadir = exedir + "../share/" + package_symbol_name + "/"; // SuperTux run from PATH
324 if (access(datadir.c_str(), F_OK) != 0)
325 { // If all fails, fall back to compiled path
326 datadir = DATA_PREFIX;
333 datadir = DATA_PREFIX;
337 printf("Datadir: %s\n", datadir.c_str());
340 void Setup::general(void)
342 /* Seed random number generator: */
344 srand(SDL_GetTicks());
346 /* Set icon image: */
350 /* Unicode needed for input handling: */
352 SDL_EnableUNICODE(1);
354 /* Load GUI/menu images: */
355 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
356 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
357 back = new Surface(datadir + "/images/status/back.png", true);
358 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
359 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
361 /* Load the mouse-cursor */
362 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
363 MouseCursor::set_current(mouse_cursor);
367 void Setup::general_free(void)
370 /* Free GUI/menu images: */
372 delete checkbox_checked;
377 /* Free mouse-cursor */
382 void Setup::video(unsigned int screen_w, unsigned int screen_h)
384 /* Init SDL Video: */
385 if (SDL_Init(SDL_INIT_VIDEO) < 0)
388 "\nError: I could not initialize video!\n"
389 "The Simple DirectMedia error that occured was:\n"
390 "%s\n\n", SDL_GetError());
396 video_gl(screen_w, screen_h);
398 video_sdl(screen_w, screen_h);
400 Surface::reload_all();
402 /* Set window manager stuff: */
403 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
406 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
410 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
414 "\nWarning: I could not set up fullscreen video for "
416 "The Simple DirectMedia error that occured was:\n"
417 "%s\n\n", SDL_GetError());
418 use_fullscreen = false;
423 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
428 "\nError: I could not set up video for 800x600 mode.\n"
429 "The Simple DirectMedia error that occured was:\n"
430 "%s\n\n", SDL_GetError());
436 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
440 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
441 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
442 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
443 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
444 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
448 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
452 "\nWarning: I could not set up fullscreen video for "
454 "The Simple DirectMedia error that occured was:\n"
455 "%s\n\n", SDL_GetError());
456 use_fullscreen = false;
461 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
466 "\nError: I could not set up video for 640x480 mode.\n"
467 "The Simple DirectMedia error that occured was:\n"
468 "%s\n\n", SDL_GetError());
474 * Set up OpenGL for 2D rendering.
476 glDisable(GL_DEPTH_TEST);
477 glDisable(GL_CULL_FACE);
479 glViewport(0, 0, screen->w, screen->h);
480 glMatrixMode(GL_PROJECTION);
482 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
484 glMatrixMode(GL_MODELVIEW);
486 glTranslatef(0.0f, 0.0f, 0.0f);
492 void Setup::joystick(void)
499 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
501 fprintf(stderr, "Warning: I could not initialize joystick!\n"
502 "The Simple DirectMedia error that occured was:\n"
503 "%s\n\n", SDL_GetError());
505 use_joystick = false;
510 if (SDL_NumJoysticks() <= 0)
512 fprintf(stderr, "Info: No joysticks were found.\n");
514 use_joystick = false;
518 js = SDL_JoystickOpen(joystick_num);
522 fprintf(stderr, "Warning: Could not open joystick %d.\n"
523 "The Simple DirectMedia error that occured was:\n"
524 "%s\n\n", joystick_num, SDL_GetError());
526 use_joystick = false;
530 if (SDL_JoystickNumAxes(js) < 2)
533 "Warning: Joystick does not have enough axes!\n");
535 use_joystick = false;
539 if (SDL_JoystickNumButtons(js) < 2)
543 "Joystick does not have enough buttons!\n");
545 use_joystick = false;
553 void Setup::audio(void)
556 /* Init SDL Audio silently even if --disable-sound : */
558 if (SoundManager::get()->audio_device_available())
560 if (SDL_Init(SDL_INIT_AUDIO) < 0)
562 /* only print out message if sound or music
563 was not disabled at command-line
565 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
568 "\nWarning: I could not initialize audio!\n"
569 "The Simple DirectMedia error that occured was:\n"
570 "%s\n\n", SDL_GetError());
572 /* keep the programming logic the same :-)
573 because in this case, use_sound & use_music' values are ignored
574 when there's no available audio device
576 SoundManager::get()->enable_sound(false);
577 SoundManager::get()->enable_music(false);
578 SoundManager::get()->set_audio_device_available(false);
583 /* Open sound silently regarless the value of "use_sound": */
585 if (SoundManager::get()->audio_device_available())
587 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
589 /* only print out message if sound or music
590 was not disabled at command-line
592 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
595 "\nWarning: I could not set up audio for 44100 Hz "
597 "The Simple DirectMedia error that occured was:\n"
598 "%s\n\n", SDL_GetError());
600 SoundManager::get()->enable_sound(false);
601 SoundManager::get()->enable_music(false);
602 SoundManager::get()->set_audio_device_available(false);
609 /* --- SHUTDOWN --- */
611 void Termination::shutdown(void)
614 SoundManager::get()->close_audio();
620 void Termination::abort(const std::string& reason, const std::string& details)
622 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
627 /* Set Icon (private) */
636 /* Load icon into a surface: */
638 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
642 "\nError: I could not load the icon image: %s%s\n"
643 "The Simple DirectMedia error that occured was:\n"
644 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
651 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
652 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
653 memset(mask, 0xFF, masklen);
658 SDL_WM_SetIcon(icon, NULL);//mask);
661 /* Free icon surface & mask: */
664 SDL_FreeSurface(icon);
668 /* Parse command-line arguments: */
670 void Setup::parseargs(int argc, char * argv[])
676 /* Parse arguments: */
678 for (i = 1; i < argc; i++)
680 if (strcmp(argv[i], "--fullscreen") == 0 ||
681 strcmp(argv[i], "-f") == 0)
683 use_fullscreen = true;
685 else if (strcmp(argv[i], "--window") == 0 ||
686 strcmp(argv[i], "-w") == 0)
688 use_fullscreen = false;
690 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
693 joystick_num = atoi(argv[++i]);
695 else if (strcmp(argv[i], "--joymap") == 0)
698 if (sscanf(argv[++i],
700 &joystick_keymap.x_axis,
701 &joystick_keymap.y_axis,
702 &joystick_keymap.a_button,
703 &joystick_keymap.b_button,
704 &joystick_keymap.start_button) != 5)
706 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
710 std::cout << "Using new joymap:\n"
711 << " X-Axis: " << joystick_keymap.x_axis << "\n"
712 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
713 << " A-Button: " << joystick_keymap.a_button << "\n"
714 << " B-Button: " << joystick_keymap.b_button << "\n"
715 << " Start-Button: " << joystick_keymap.start_button << std::endl;
718 else if (strcmp(argv[i], "--leveleditor") == 0)
720 launch_leveleditor_mode = true;
722 else if (strcmp(argv[i], "--worldmap") == 0)
724 launch_worldmap_mode = true;
726 else if (strcmp(argv[i], "--flip-levels") == 0)
728 flip_levels_mode = true;
730 else if (strcmp(argv[i], "--datadir") == 0
731 || strcmp(argv[i], "-d") == 0 )
736 else if (strcmp(argv[i], "--show-fps") == 0)
738 /* Use full screen: */
742 else if (strcmp(argv[i], "--opengl") == 0 ||
743 strcmp(argv[i], "-gl") == 0)
751 else if (strcmp(argv[i], "--sdl") == 0)
755 else if (strcmp(argv[i], "--usage") == 0)
761 else if (strcmp(argv[i], "--version") == 0)
764 printf((package_name + " " + package_version + "\n").c_str() );
767 else if (strcmp(argv[i], "--disable-sound") == 0)
769 /* Disable the compiled in sound feature */
770 printf("Sounds disabled \n");
771 SoundManager::get()->enable_sound(false);
773 else if (strcmp(argv[i], "--disable-music") == 0)
775 /* Disable the compiled in sound feature */
776 printf("Music disabled \n");
777 SoundManager::get()->enable_music(false);
779 else if (strcmp(argv[i], "--debug") == 0)
781 /* Enable the debug-mode */
785 else if (strcmp(argv[i], "--help") == 0)
787 puts(_((" SuperTux " + package_version + "\n"
788 " Please see the file \"README.txt\" for more details.\n").c_str()));
789 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
790 puts(_("Display Options:\n"
791 " -f, --fullscreen Run in fullscreen mode.\n"
792 " -w, --window Run in window mode.\n"
793 " --opengl If OpenGL support was compiled in, this will tell\n"
794 " SuperTux to make use of it.\n"
795 " --sdl Use the SDL software graphical renderer\n"
798 " --disable-sound If sound support was compiled in, this will\n"
799 " disable sound for this session of the game.\n"
800 " --disable-music Like above, but this will disable music.\n"
803 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
804 " --joymap XAXIS:YAXIS:A:B:START\n"
805 " Define how joystick buttons and axis should be mapped\n"
806 " --leveleditor Opens the leveleditor in a file.\n"
807 " --worldmap Opens the specified worldmap file.\n"
808 " --flip-levels Flip levels upside-down.\n"
809 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
810 " --debug Enables the debug mode, which is useful for developers.\n"
811 " --help Display a help message summarizing command-line\n"
812 " options, license and game controls.\n"
813 " --usage Display a brief message summarizing command-line options.\n"
814 " --version Display the version of SuperTux you're running.\n\n"
818 else if (argv[i][0] != '-')
820 level_startup_file = argv[i];
824 /* Unknown - complain! */
834 void usage(char * prog, int ret)
839 /* Determine which stream to write to: */
847 /* Display the usage message: */
849 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
858 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
860 std::set<std::string> dirnames;
862 DIR* dir = opendir(pathname.c_str());
865 struct dirent *direntp;
867 while((direntp = readdir(dir)))
869 dirnames.insert(direntp->d_name);