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");
268 /* Set SuperTux configuration and save directories */
269 void Setup::directories()
272 /* Get home directory (from $HOME variable)... if we can't determine it,
273 use the current directory ("."): */
274 if (getenv("HOME") != NULL)
275 home = getenv("HOME");
279 st_dir = home + "/." + package_symbol_name;
281 /* Remove .supertux config-file from old SuperTux versions */
282 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;
332 datadir = DATA_PREFIX;
335 printf("Datadir: %s\n", datadir.c_str());
338 void Setup::general(void)
340 /* Seed random number generator: */
342 srand(SDL_GetTicks());
344 /* Set icon image: */
348 /* Unicode needed for input handling: */
350 SDL_EnableUNICODE(1);
352 /* Load GUI/menu images: */
353 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
354 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
355 back = new Surface(datadir + "/images/status/back.png", true);
356 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
357 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
359 /* Load the mouse-cursor */
360 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
361 MouseCursor::set_current(mouse_cursor);
365 void Setup::general_free(void)
368 /* Free GUI/menu images: */
370 delete checkbox_checked;
375 /* Free mouse-cursor */
380 void Setup::video(unsigned int screen_w, unsigned int screen_h)
382 /* Init SDL Video: */
383 if (SDL_Init(SDL_INIT_VIDEO) < 0)
386 "\nError: I could not initialize video!\n"
387 "The Simple DirectMedia error that occured was:\n"
388 "%s\n\n", SDL_GetError());
394 video_gl(screen_w, screen_h);
396 video_sdl(screen_w, screen_h);
398 Surface::reload_all();
400 /* Set window manager stuff: */
401 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
404 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
408 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
412 "\nWarning: I could not set up fullscreen video for "
414 "The Simple DirectMedia error that occured was:\n"
415 "%s\n\n", SDL_GetError());
416 use_fullscreen = false;
421 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
426 "\nError: I could not set up video for 800x600 mode.\n"
427 "The Simple DirectMedia error that occured was:\n"
428 "%s\n\n", SDL_GetError());
434 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
438 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
439 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
440 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
441 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
442 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
446 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
450 "\nWarning: I could not set up fullscreen video for "
452 "The Simple DirectMedia error that occured was:\n"
453 "%s\n\n", SDL_GetError());
454 use_fullscreen = false;
459 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
464 "\nError: I could not set up video for 640x480 mode.\n"
465 "The Simple DirectMedia error that occured was:\n"
466 "%s\n\n", SDL_GetError());
472 * Set up OpenGL for 2D rendering.
474 glDisable(GL_DEPTH_TEST);
475 glDisable(GL_CULL_FACE);
477 glViewport(0, 0, screen->w, screen->h);
478 glMatrixMode(GL_PROJECTION);
480 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
482 glMatrixMode(GL_MODELVIEW);
484 glTranslatef(0.0f, 0.0f, 0.0f);
490 void Setup::joystick(void)
497 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
499 fprintf(stderr, "Warning: I could not initialize joystick!\n"
500 "The Simple DirectMedia error that occured was:\n"
501 "%s\n\n", SDL_GetError());
503 use_joystick = false;
508 if (SDL_NumJoysticks() <= 0)
510 fprintf(stderr, "Info: No joysticks were found.\n");
512 use_joystick = false;
516 js = SDL_JoystickOpen(joystick_num);
520 fprintf(stderr, "Warning: Could not open joystick %d.\n"
521 "The Simple DirectMedia error that occured was:\n"
522 "%s\n\n", joystick_num, SDL_GetError());
524 use_joystick = false;
528 if (SDL_JoystickNumAxes(js) < 2)
531 "Warning: Joystick does not have enough axes!\n");
533 use_joystick = false;
537 if (SDL_JoystickNumButtons(js) < 2)
541 "Joystick does not have enough buttons!\n");
543 use_joystick = false;
551 void Setup::audio(void)
554 /* Init SDL Audio silently even if --disable-sound : */
556 if (SoundManager::get()->audio_device_available())
558 if (SDL_Init(SDL_INIT_AUDIO) < 0)
560 /* only print out message if sound or music
561 was not disabled at command-line
563 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
566 "\nWarning: I could not initialize audio!\n"
567 "The Simple DirectMedia error that occured was:\n"
568 "%s\n\n", SDL_GetError());
570 /* keep the programming logic the same :-)
571 because in this case, use_sound & use_music' values are ignored
572 when there's no available audio device
574 SoundManager::get()->enable_sound(false);
575 SoundManager::get()->enable_music(false);
576 SoundManager::get()->set_audio_device_available(false);
581 /* Open sound silently regarless the value of "use_sound": */
583 if (SoundManager::get()->audio_device_available())
585 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
587 /* only print out message if sound or music
588 was not disabled at command-line
590 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
593 "\nWarning: I could not set up audio for 44100 Hz "
595 "The Simple DirectMedia error that occured was:\n"
596 "%s\n\n", SDL_GetError());
598 SoundManager::get()->enable_sound(false);
599 SoundManager::get()->enable_music(false);
600 SoundManager::get()->set_audio_device_available(false);
607 /* --- SHUTDOWN --- */
609 void Termination::shutdown(void)
612 SoundManager::get()->close_audio();
618 void Termination::abort(const std::string& reason, const std::string& details)
620 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
625 /* Set Icon (private) */
634 /* Load icon into a surface: */
636 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
640 "\nError: I could not load the icon image: %s%s\n"
641 "The Simple DirectMedia error that occured was:\n"
642 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
649 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
650 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
651 memset(mask, 0xFF, masklen);
656 SDL_WM_SetIcon(icon, NULL);//mask);
659 /* Free icon surface & mask: */
662 SDL_FreeSurface(icon);
666 /* Parse command-line arguments: */
668 void Setup::parseargs(int argc, char * argv[])
674 /* Parse arguments: */
676 for (i = 1; i < argc; i++)
678 if (strcmp(argv[i], "--fullscreen") == 0 ||
679 strcmp(argv[i], "-f") == 0)
681 use_fullscreen = true;
683 else if (strcmp(argv[i], "--window") == 0 ||
684 strcmp(argv[i], "-w") == 0)
686 use_fullscreen = false;
688 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
691 joystick_num = atoi(argv[++i]);
693 else if (strcmp(argv[i], "--joymap") == 0)
696 if (sscanf(argv[++i],
698 &joystick_keymap.x_axis,
699 &joystick_keymap.y_axis,
700 &joystick_keymap.a_button,
701 &joystick_keymap.b_button,
702 &joystick_keymap.start_button) != 5)
704 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
708 std::cout << "Using new joymap:\n"
709 << " X-Axis: " << joystick_keymap.x_axis << "\n"
710 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
711 << " A-Button: " << joystick_keymap.a_button << "\n"
712 << " B-Button: " << joystick_keymap.b_button << "\n"
713 << " Start-Button: " << joystick_keymap.start_button << std::endl;
716 else if (strcmp(argv[i], "--leveleditor") == 0)
718 launch_leveleditor_mode = true;
720 else if (strcmp(argv[i], "--worldmap") == 0)
722 launch_worldmap_mode = true;
724 else if (strcmp(argv[i], "--flip-levels") == 0)
726 flip_levels_mode = true;
728 else if (strcmp(argv[i], "--datadir") == 0
729 || strcmp(argv[i], "-d") == 0 )
734 else if (strcmp(argv[i], "--show-fps") == 0)
736 /* Use full screen: */
740 else if (strcmp(argv[i], "--opengl") == 0 ||
741 strcmp(argv[i], "-gl") == 0)
749 else if (strcmp(argv[i], "--sdl") == 0)
753 else if (strcmp(argv[i], "--usage") == 0)
759 else if (strcmp(argv[i], "--version") == 0)
762 printf((package_name + " " + package_version + "\n").c_str() );
765 else if (strcmp(argv[i], "--disable-sound") == 0)
767 /* Disable the compiled in sound feature */
768 printf("Sounds disabled \n");
769 SoundManager::get()->enable_sound(false);
771 else if (strcmp(argv[i], "--disable-music") == 0)
773 /* Disable the compiled in sound feature */
774 printf("Music disabled \n");
775 SoundManager::get()->enable_music(false);
777 else if (strcmp(argv[i], "--debug") == 0)
779 /* Enable the debug-mode */
783 else if (strcmp(argv[i], "--help") == 0)
785 puts(_((" SuperTux " + package_version + "\n"
786 " Please see the file \"README.txt\" for more details.\n").c_str()));
787 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
788 puts(_("Display Options:\n"
789 " -f, --fullscreen Run in fullscreen mode.\n"
790 " -w, --window Run in window mode.\n"
791 " --opengl If OpenGL support was compiled in, this will tell\n"
792 " SuperTux to make use of it.\n"
793 " --sdl Use the SDL software graphical renderer\n"
796 " --disable-sound If sound support was compiled in, this will\n"
797 " disable sound for this session of the game.\n"
798 " --disable-music Like above, but this will disable music.\n"
801 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
802 " --joymap XAXIS:YAXIS:A:B:START\n"
803 " Define how joystick buttons and axis should be mapped\n"
804 " --leveleditor Opens the leveleditor in a file.\n"
805 " --worldmap Opens the specified worldmap file.\n"
806 " --flip-levels Flip levels upside-down.\n"
807 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
808 " --debug Enables the debug mode, which is useful for developers.\n"
809 " --help Display a help message summarizing command-line\n"
810 " options, license and game controls.\n"
811 " --usage Display a brief message summarizing command-line options.\n"
812 " --version Display the version of SuperTux you're running.\n\n"
816 else if (argv[i][0] != '-')
818 level_startup_file = argv[i];
822 /* Unknown - complain! */
832 void usage(char * prog, int ret)
837 /* Determine which stream to write to: */
845 /* Display the usage message: */
847 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
856 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
858 std::set<std::string> dirnames;
860 DIR* dir = opendir(pathname.c_str());
863 struct dirent *direntp;
865 while((direntp = readdir(dir)))
867 dirnames.insert(direntp->d_name);