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 + "../../data"; //SuperTux run from source dir (with libtool script)
307 if (access(datadir.c_str(), F_OK) != 0)
309 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
310 if (access(datadir.c_str(), F_OK) != 0)
311 { // If all fails, fall back to compiled path
312 datadir = DATA_PREFIX;
318 datadir = DATA_PREFIX;
321 printf("Datadir: %s\n", datadir.c_str());
324 void Setup::general(void)
326 /* Seed random number generator: */
328 srand(SDL_GetTicks());
330 /* Set icon image: */
334 /* Unicode needed for input handling: */
336 SDL_EnableUNICODE(1);
338 /* Load GUI/menu images: */
339 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
340 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
341 back = new Surface(datadir + "/images/status/back.png", true);
342 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
343 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
345 /* Load the mouse-cursor */
346 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
347 MouseCursor::set_current(mouse_cursor);
351 void Setup::general_free(void)
354 /* Free GUI/menu images: */
356 delete checkbox_checked;
361 /* Free mouse-cursor */
366 void Setup::video(unsigned int screen_w, unsigned int screen_h)
368 /* Init SDL Video: */
369 if (SDL_Init(SDL_INIT_VIDEO) < 0)
372 "\nError: I could not initialize video!\n"
373 "The Simple DirectMedia error that occured was:\n"
374 "%s\n\n", SDL_GetError());
380 video_gl(screen_w, screen_h);
382 video_sdl(screen_w, screen_h);
384 Surface::reload_all();
386 /* Set window manager stuff: */
387 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
390 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
394 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
398 "\nWarning: I could not set up fullscreen video for "
400 "The Simple DirectMedia error that occured was:\n"
401 "%s\n\n", SDL_GetError());
402 use_fullscreen = false;
407 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
412 "\nError: I could not set up video for 800x600 mode.\n"
413 "The Simple DirectMedia error that occured was:\n"
414 "%s\n\n", SDL_GetError());
420 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
424 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
425 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
426 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
427 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
428 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
432 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
436 "\nWarning: I could not set up fullscreen video for "
438 "The Simple DirectMedia error that occured was:\n"
439 "%s\n\n", SDL_GetError());
440 use_fullscreen = false;
445 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
450 "\nError: I could not set up video for 640x480 mode.\n"
451 "The Simple DirectMedia error that occured was:\n"
452 "%s\n\n", SDL_GetError());
458 * Set up OpenGL for 2D rendering.
460 glDisable(GL_DEPTH_TEST);
461 glDisable(GL_CULL_FACE);
463 glViewport(0, 0, screen->w, screen->h);
464 glMatrixMode(GL_PROJECTION);
466 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
468 glMatrixMode(GL_MODELVIEW);
470 glTranslatef(0.0f, 0.0f, 0.0f);
476 void Setup::joystick(void)
483 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
485 fprintf(stderr, "Warning: I could not initialize joystick!\n"
486 "The Simple DirectMedia error that occured was:\n"
487 "%s\n\n", SDL_GetError());
489 use_joystick = false;
494 if (SDL_NumJoysticks() <= 0)
496 fprintf(stderr, "Info: No joysticks were found.\n");
498 use_joystick = false;
502 js = SDL_JoystickOpen(joystick_num);
506 fprintf(stderr, "Warning: Could not open joystick %d.\n"
507 "The Simple DirectMedia error that occured was:\n"
508 "%s\n\n", joystick_num, SDL_GetError());
510 use_joystick = false;
514 if (SDL_JoystickNumAxes(js) < 2)
517 "Warning: Joystick does not have enough axes!\n");
519 use_joystick = false;
523 if (SDL_JoystickNumButtons(js) < 2)
527 "Joystick does not have enough buttons!\n");
529 use_joystick = false;
537 void Setup::audio(void)
540 /* Init SDL Audio silently even if --disable-sound : */
542 if (SoundManager::get()->audio_device_available())
544 if (SDL_Init(SDL_INIT_AUDIO) < 0)
546 /* only print out message if sound or music
547 was not disabled at command-line
549 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
552 "\nWarning: I could not initialize audio!\n"
553 "The Simple DirectMedia error that occured was:\n"
554 "%s\n\n", SDL_GetError());
556 /* keep the programming logic the same :-)
557 because in this case, use_sound & use_music' values are ignored
558 when there's no available audio device
560 SoundManager::get()->enable_sound(false);
561 SoundManager::get()->enable_music(false);
562 SoundManager::get()->set_audio_device_available(false);
567 /* Open sound silently regarless the value of "use_sound": */
569 if (SoundManager::get()->audio_device_available())
571 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
573 /* only print out message if sound or music
574 was not disabled at command-line
576 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
579 "\nWarning: I could not set up audio for 44100 Hz "
581 "The Simple DirectMedia error that occured was:\n"
582 "%s\n\n", SDL_GetError());
584 SoundManager::get()->enable_sound(false);
585 SoundManager::get()->enable_music(false);
586 SoundManager::get()->set_audio_device_available(false);
593 /* --- SHUTDOWN --- */
595 void Termination::shutdown(void)
598 SoundManager::get()->close_audio();
604 void Termination::abort(const std::string& reason, const std::string& details)
606 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
611 /* Set Icon (private) */
620 /* Load icon into a surface: */
622 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
626 "\nError: I could not load the icon image: %s%s\n"
627 "The Simple DirectMedia error that occured was:\n"
628 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
635 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
636 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
637 memset(mask, 0xFF, masklen);
642 SDL_WM_SetIcon(icon, NULL);//mask);
645 /* Free icon surface & mask: */
648 SDL_FreeSurface(icon);
652 /* Parse command-line arguments: */
654 void Setup::parseargs(int argc, char * argv[])
660 /* Parse arguments: */
662 for (i = 1; i < argc; i++)
664 if (strcmp(argv[i], "--fullscreen") == 0 ||
665 strcmp(argv[i], "-f") == 0)
667 use_fullscreen = true;
669 else if (strcmp(argv[i], "--window") == 0 ||
670 strcmp(argv[i], "-w") == 0)
672 use_fullscreen = false;
674 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
677 joystick_num = atoi(argv[++i]);
679 else if (strcmp(argv[i], "--joymap") == 0)
682 if (sscanf(argv[++i],
684 &joystick_keymap.x_axis,
685 &joystick_keymap.y_axis,
686 &joystick_keymap.a_button,
687 &joystick_keymap.b_button,
688 &joystick_keymap.start_button) != 5)
690 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
694 std::cout << "Using new joymap:\n"
695 << " X-Axis: " << joystick_keymap.x_axis << "\n"
696 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
697 << " A-Button: " << joystick_keymap.a_button << "\n"
698 << " B-Button: " << joystick_keymap.b_button << "\n"
699 << " Start-Button: " << joystick_keymap.start_button << std::endl;
702 else if (strcmp(argv[i], "--leveleditor") == 0)
704 launch_leveleditor_mode = true;
706 else if (strcmp(argv[i], "--worldmap") == 0)
708 launch_worldmap_mode = true;
710 else if (strcmp(argv[i], "--flip-levels") == 0)
712 flip_levels_mode = true;
714 else if (strcmp(argv[i], "--datadir") == 0
715 || strcmp(argv[i], "-d") == 0 )
720 else if (strcmp(argv[i], "--show-fps") == 0)
722 /* Use full screen: */
726 else if (strcmp(argv[i], "--opengl") == 0 ||
727 strcmp(argv[i], "-gl") == 0)
735 else if (strcmp(argv[i], "--sdl") == 0)
739 else if (strcmp(argv[i], "--usage") == 0)
745 else if (strcmp(argv[i], "--version") == 0)
748 printf((package_name + package_version + "\n").c_str() );
751 else if (strcmp(argv[i], "--disable-sound") == 0)
753 /* Disable the compiled in sound feature */
754 printf("Sounds disabled \n");
755 SoundManager::get()->enable_sound(false);
757 else if (strcmp(argv[i], "--disable-music") == 0)
759 /* Disable the compiled in sound feature */
760 printf("Music disabled \n");
761 SoundManager::get()->enable_music(false);
763 else if (strcmp(argv[i], "--debug") == 0)
765 /* Enable the debug-mode */
769 else if (strcmp(argv[i], "--help") == 0)
771 puts(_((" SuperTux " + package_version + "\n"
772 " Please see the file \"README.txt\" for more details.\n").c_str()));
773 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
774 puts(_("Display Options:\n"
775 " -f, --fullscreen Run in fullscreen mode.\n"
776 " -w, --window Run in window mode.\n"
777 " --opengl If OpenGL support was compiled in, this will tell\n"
778 " SuperTux to make use of it.\n"
779 " --sdl Use the SDL software graphical renderer\n"
782 " --disable-sound If sound support was compiled in, this will\n"
783 " disable sound for this session of the game.\n"
784 " --disable-music Like above, but this will disable music.\n"
787 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
788 " --joymap XAXIS:YAXIS:A:B:START\n"
789 " Define how joystick buttons and axis should be mapped\n"
790 " --leveleditor Opens the leveleditor in a file.\n"
791 " --worldmap Opens the specified worldmap file.\n"
792 " --flip-levels Flip levels upside-down.\n"
793 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
794 " --debug Enables the debug mode, which is useful for developers.\n"
795 " --help Display a help message summarizing command-line\n"
796 " options, license and game controls.\n"
797 " --usage Display a brief message summarizing command-line options.\n"
798 " --version Display the version of SuperTux you're running.\n\n"
802 else if (argv[i][0] != '-')
804 level_startup_file = argv[i];
808 /* Unknown - complain! */
818 void usage(char * prog, int ret)
823 /* Determine which stream to write to: */
831 /* Display the usage message: */
833 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"),
842 std::set<std::string> FileSystem::read_directory(const std::string& pathname)
844 std::set<std::string> dirnames;
846 DIR* dir = opendir(pathname.c_str());
849 struct dirent *direntp;
851 while((direntp = readdir(dir)))
853 dirnames.insert(direntp->d_name);