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 char *filename)
71 if (stat(filename, &filestat) == -1)
77 if(S_ISREG(filestat.st_mode))
84 /* Can we write to this location? */
85 int FileSystem::fwriteable(const char *filename)
88 fi = fopen(filename, "wa");
96 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
97 int FileSystem::fcreatedir(const char* relative_dir)
100 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
101 if(mkdir(path,0755) != 0)
103 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
104 if(mkdir(path,0755) != 0)
119 /* Get all names of sub-directories in a certain directory. */
120 /* Returns the number of sub-directories found. */
121 /* Note: The user has to free the allocated space. */
122 string_list_type FileSystem::dsubdirs(const char *rel_path,const char* expected_file)
125 struct dirent *direntp;
126 string_list_type sdirs;
130 string_list_init(&sdirs);
131 sprintf(path,"%s/%s",st_dir,rel_path);
132 if((dirStructP = opendir(path)) != NULL)
134 while((direntp = readdir(dirStructP)) != NULL)
136 char absolute_filename[1024];
139 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
141 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
143 if(expected_file != NULL)
145 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
146 if(!faccessible(filename))
150 string_list_add_item(&sdirs,direntp->d_name);
153 closedir(dirStructP);
156 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
157 if((dirStructP = opendir(path)) != NULL)
159 while((direntp = readdir(dirStructP)) != NULL)
161 char absolute_filename[1024];
164 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
166 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
168 if(expected_file != NULL)
170 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
171 if(!faccessible(filename))
177 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
178 if(faccessible(filename))
183 string_list_add_item(&sdirs,direntp->d_name);
186 closedir(dirStructP);
192 string_list_type FileSystem::dfiles(const char *rel_path, const char* glob, const char* exception_str)
195 struct dirent *direntp;
196 string_list_type sdirs;
199 string_list_init(&sdirs);
200 sprintf(path,"%s/%s",st_dir,rel_path);
201 if((dirStructP = opendir(path)) != NULL)
203 while((direntp = readdir(dirStructP)) != NULL)
205 char absolute_filename[1024];
208 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
210 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
212 if(exception_str != NULL)
214 if(strstr(direntp->d_name,exception_str) != NULL)
218 if(strstr(direntp->d_name,glob) == NULL)
221 string_list_add_item(&sdirs,direntp->d_name);
224 closedir(dirStructP);
227 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
228 if((dirStructP = opendir(path)) != NULL)
230 while((direntp = readdir(dirStructP)) != NULL)
232 char absolute_filename[1024];
235 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
237 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
239 if(exception_str != NULL)
241 if(strstr(direntp->d_name,exception_str) != NULL)
245 if(strstr(direntp->d_name,glob) == NULL)
248 string_list_add_item(&sdirs,direntp->d_name);
251 closedir(dirStructP);
257 void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
259 package_name = _package_name;
260 package_symbol_name = _package_symbol_name;
261 package_version = _package_version;
265 /* Set SuperTux configuration and save directories */
266 void Setup::directories(void)
270 /* Get home directory (from $HOME variable)... if we can't determine it,
271 use the current directory ("."): */
272 if (getenv("HOME") != NULL)
273 home = getenv("HOME");
277 std::string st_dir_tmp = "/." + package_symbol_name;
278 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
279 strlen(st_dir_tmp.c_str()) + 1));
280 strcpy(st_dir, home);
281 strcat(st_dir,st_dir_tmp.c_str());
283 /* Remove .supertux config-file from old SuperTux versions */
284 if(FileSystem::faccessible(st_dir))
290 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
292 strcpy(st_save_dir,st_dir);
293 strcat(st_save_dir,"/save");
295 /* Create them. In the case they exist they won't destroy anything. */
297 mkdir(st_save_dir, 0755);
299 sprintf(str, "%s/levels", st_dir);
302 // User has not that a datadir, so we try some magic
307 char exe_file[PATH_MAX];
308 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
310 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
311 datadir = DATA_PREFIX;
315 std::string exedir = std::string(dirname(exe_file)) + "/";
317 datadir = exedir + "../data"; // SuperTux run from source dir
318 if (access(datadir.c_str(), F_OK) != 0)
320 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
321 if (access(datadir.c_str(), F_OK) != 0)
322 { // If all fails, fall back to compiled path
323 datadir = DATA_PREFIX;
328 datadir = DATA_PREFIX;
331 printf("Datadir: %s\n", datadir.c_str());
334 void Setup::general(void)
336 /* Seed random number generator: */
338 srand(SDL_GetTicks());
340 /* Set icon image: */
344 /* Unicode needed for input handling: */
346 SDL_EnableUNICODE(1);
348 /* Load GUI/menu images: */
349 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
350 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
351 back = new Surface(datadir + "/images/status/back.png", true);
352 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
353 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
355 /* Load the mouse-cursor */
356 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
357 MouseCursor::set_current(mouse_cursor);
361 void Setup::general_free(void)
364 /* Free GUI/menu images: */
366 delete checkbox_checked;
371 /* Free mouse-cursor */
376 void Setup::video(unsigned int screen_w, unsigned int screen_h)
378 /* Init SDL Video: */
379 if (SDL_Init(SDL_INIT_VIDEO) < 0)
382 "\nError: I could not initialize video!\n"
383 "The Simple DirectMedia error that occured was:\n"
384 "%s\n\n", SDL_GetError());
390 video_gl(screen_w, screen_h);
392 video_sdl(screen_w, screen_h);
394 Surface::reload_all();
396 /* Set window manager stuff: */
397 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
400 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
404 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
408 "\nWarning: I could not set up fullscreen video for "
410 "The Simple DirectMedia error that occured was:\n"
411 "%s\n\n", SDL_GetError());
412 use_fullscreen = false;
417 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
422 "\nError: I could not set up video for 800x600 mode.\n"
423 "The Simple DirectMedia error that occured was:\n"
424 "%s\n\n", SDL_GetError());
430 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
434 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
435 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
436 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
437 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
438 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
442 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
446 "\nWarning: I could not set up fullscreen video for "
448 "The Simple DirectMedia error that occured was:\n"
449 "%s\n\n", SDL_GetError());
450 use_fullscreen = false;
455 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
460 "\nError: I could not set up video for 640x480 mode.\n"
461 "The Simple DirectMedia error that occured was:\n"
462 "%s\n\n", SDL_GetError());
468 * Set up OpenGL for 2D rendering.
470 glDisable(GL_DEPTH_TEST);
471 glDisable(GL_CULL_FACE);
473 glViewport(0, 0, screen->w, screen->h);
474 glMatrixMode(GL_PROJECTION);
476 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
478 glMatrixMode(GL_MODELVIEW);
480 glTranslatef(0.0f, 0.0f, 0.0f);
486 void Setup::joystick(void)
493 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
495 fprintf(stderr, "Warning: I could not initialize joystick!\n"
496 "The Simple DirectMedia error that occured was:\n"
497 "%s\n\n", SDL_GetError());
499 use_joystick = false;
504 if (SDL_NumJoysticks() <= 0)
506 fprintf(stderr, "Info: No joysticks were found.\n");
508 use_joystick = false;
512 js = SDL_JoystickOpen(joystick_num);
516 fprintf(stderr, "Warning: Could not open joystick %d.\n"
517 "The Simple DirectMedia error that occured was:\n"
518 "%s\n\n", joystick_num, SDL_GetError());
520 use_joystick = false;
524 if (SDL_JoystickNumAxes(js) < 2)
527 "Warning: Joystick does not have enough axes!\n");
529 use_joystick = false;
533 if (SDL_JoystickNumButtons(js) < 2)
537 "Joystick does not have enough buttons!\n");
539 use_joystick = false;
547 void Setup::audio(void)
550 /* Init SDL Audio silently even if --disable-sound : */
552 if (SoundManager::get()->audio_device_available())
554 if (SDL_Init(SDL_INIT_AUDIO) < 0)
556 /* only print out message if sound or music
557 was not disabled at command-line
559 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
562 "\nWarning: I could not initialize audio!\n"
563 "The Simple DirectMedia error that occured was:\n"
564 "%s\n\n", SDL_GetError());
566 /* keep the programming logic the same :-)
567 because in this case, use_sound & use_music' values are ignored
568 when there's no available audio device
570 SoundManager::get()->enable_sound(false);
571 SoundManager::get()->enable_music(false);
572 SoundManager::get()->set_audio_device_available(false);
577 /* Open sound silently regarless the value of "use_sound": */
579 if (SoundManager::get()->audio_device_available())
581 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
583 /* only print out message if sound or music
584 was not disabled at command-line
586 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
589 "\nWarning: I could not set up audio for 44100 Hz "
591 "The Simple DirectMedia error that occured was:\n"
592 "%s\n\n", SDL_GetError());
594 SoundManager::get()->enable_sound(false);
595 SoundManager::get()->enable_music(false);
596 SoundManager::get()->set_audio_device_available(false);
603 /* --- SHUTDOWN --- */
605 void Termination::shutdown(void)
608 SoundManager::get()->close_audio();
614 void Termination::abort(const std::string& reason, const std::string& details)
616 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
621 /* Set Icon (private) */
630 /* Load icon into a surface: */
632 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
636 "\nError: I could not load the icon image: %s%s\n"
637 "The Simple DirectMedia error that occured was:\n"
638 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
645 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
646 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
647 memset(mask, 0xFF, masklen);
652 SDL_WM_SetIcon(icon, NULL);//mask);
655 /* Free icon surface & mask: */
658 SDL_FreeSurface(icon);
662 /* Parse command-line arguments: */
664 void Setup::parseargs(int argc, char * argv[])
670 /* Parse arguments: */
672 for (i = 1; i < argc; i++)
674 if (strcmp(argv[i], "--fullscreen") == 0 ||
675 strcmp(argv[i], "-f") == 0)
677 use_fullscreen = true;
679 else if (strcmp(argv[i], "--window") == 0 ||
680 strcmp(argv[i], "-w") == 0)
682 use_fullscreen = false;
684 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
687 joystick_num = atoi(argv[++i]);
689 else if (strcmp(argv[i], "--joymap") == 0)
692 if (sscanf(argv[++i],
694 &joystick_keymap.x_axis,
695 &joystick_keymap.y_axis,
696 &joystick_keymap.a_button,
697 &joystick_keymap.b_button,
698 &joystick_keymap.start_button) != 5)
700 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
704 std::cout << "Using new joymap:\n"
705 << " X-Axis: " << joystick_keymap.x_axis << "\n"
706 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
707 << " A-Button: " << joystick_keymap.a_button << "\n"
708 << " B-Button: " << joystick_keymap.b_button << "\n"
709 << " Start-Button: " << joystick_keymap.start_button << std::endl;
712 else if (strcmp(argv[i], "--leveleditor") == 0)
714 launch_leveleditor_mode = true;
716 else if (strcmp(argv[i], "--worldmap") == 0)
718 launch_worldmap_mode = true;
720 else if (strcmp(argv[i], "--datadir") == 0
721 || strcmp(argv[i], "-d") == 0 )
726 else if (strcmp(argv[i], "--show-fps") == 0)
728 /* Use full screen: */
732 else if (strcmp(argv[i], "--opengl") == 0 ||
733 strcmp(argv[i], "-gl") == 0)
741 else if (strcmp(argv[i], "--sdl") == 0)
745 else if (strcmp(argv[i], "--usage") == 0)
751 else if (strcmp(argv[i], "--version") == 0)
754 printf((package_name + package_version + "\n").c_str() );
757 else if (strcmp(argv[i], "--disable-sound") == 0)
759 /* Disable the compiled in sound feature */
760 printf("Sounds disabled \n");
761 SoundManager::get()->enable_sound(false);
763 else if (strcmp(argv[i], "--disable-music") == 0)
765 /* Disable the compiled in sound feature */
766 printf("Music disabled \n");
767 SoundManager::get()->enable_music(false);
769 else if (strcmp(argv[i], "--debug") == 0)
771 /* Enable the debug-mode */
775 else if (strcmp(argv[i], "--help") == 0)
777 puts(_((" SuperTux " + package_version + "\n"
778 " Please see the file \"README.txt\" for more details.\n").c_str()));
779 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
780 puts(_("Display Options:\n"
781 " -f, --fullscreen Run in fullscreen mode.\n"
782 " -w, --window Run in window mode.\n"
783 " --opengl If OpenGL support was compiled in, this will tell\n"
784 " SuperTux to make use of it.\n"
785 " --sdl Use the SDL software graphical renderer\n"
788 " --disable-sound If sound support was compiled in, this will\n"
789 " disable sound for this session of the game.\n"
790 " --disable-music Like above, but this will disable music.\n"
793 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
794 " --joymap XAXIS:YAXIS:A:B:START\n"
795 " Define how joystick buttons and axis should be mapped\n"
796 " --leveleditor Opens the leveleditor in a file.\n"
797 " --worldmap Opens the specified worldmap file.\n"
798 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
799 " --debug Enables the debug mode, which is useful for developers.\n"
800 " --help Display a help message summarizing command-line\n"
801 " options, license and game controls.\n"
802 " --usage Display a brief message summarizing command-line options.\n"
803 " --version Display the version of SuperTux you're running.\n\n"
807 else if (argv[i][0] != '-')
809 level_startup_file = argv[i];
813 /* Unknown - complain! */
823 void usage(char * prog, int ret)
828 /* Determine which stream to write to: */
836 /* Display the usage message: */
838 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] FILENAME\n"),
847 std::vector<std::string> FileSystem::read_directory(const std::string& pathname)
849 std::vector<std::string> dirnames;
851 DIR* dir = opendir(pathname.c_str());
854 struct dirent *direntp;
856 while((direntp = readdir(dir)))
858 dirnames.push_back(direntp->d_name);