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/defines.h"
46 #include "../app/setup.h"
47 #include "../video/screen.h"
48 #include "../video/surface.h"
49 #include "../gui/menu.h"
50 #include "../utils/configfile.h"
51 #include "../audio/sound_manager.h"
52 #include "../app/gettext.h"
54 using namespace SuperTux;
57 #define mkdir(dir, mode) mkdir(dir)
58 // on win32 we typically don't want LFS paths
60 #define DATA_PREFIX "./data/"
63 /* Local function prototypes: */
66 void usage(char * prog, int ret);
68 /* Does the given file exist and is it accessible? */
69 int FileSystem::faccessible(const char *filename)
72 if (stat(filename, &filestat) == -1)
78 if(S_ISREG(filestat.st_mode))
85 /* Can we write to this location? */
86 int FileSystem::fwriteable(const char *filename)
89 fi = fopen(filename, "wa");
97 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
98 int FileSystem::fcreatedir(const char* relative_dir)
101 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
102 if(mkdir(path,0755) != 0)
104 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
105 if(mkdir(path,0755) != 0)
120 /* Get all names of sub-directories in a certain directory. */
121 /* Returns the number of sub-directories found. */
122 /* Note: The user has to free the allocated space. */
123 string_list_type FileSystem::dsubdirs(const char *rel_path,const char* expected_file)
126 struct dirent *direntp;
127 string_list_type sdirs;
131 string_list_init(&sdirs);
132 sprintf(path,"%s/%s",st_dir,rel_path);
133 if((dirStructP = opendir(path)) != NULL)
135 while((direntp = readdir(dirStructP)) != NULL)
137 char absolute_filename[1024];
140 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
142 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
144 if(expected_file != NULL)
146 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
147 if(!faccessible(filename))
151 string_list_add_item(&sdirs,direntp->d_name);
154 closedir(dirStructP);
157 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
158 if((dirStructP = opendir(path)) != NULL)
160 while((direntp = readdir(dirStructP)) != NULL)
162 char absolute_filename[1024];
165 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
167 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
169 if(expected_file != NULL)
171 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
172 if(!faccessible(filename))
178 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
179 if(faccessible(filename))
184 string_list_add_item(&sdirs,direntp->d_name);
187 closedir(dirStructP);
193 string_list_type FileSystem::dfiles(const char *rel_path, const char* glob, const char* exception_str)
196 struct dirent *direntp;
197 string_list_type sdirs;
200 string_list_init(&sdirs);
201 sprintf(path,"%s/%s",st_dir,rel_path);
202 if((dirStructP = opendir(path)) != NULL)
204 while((direntp = readdir(dirStructP)) != NULL)
206 char absolute_filename[1024];
209 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
211 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
213 if(exception_str != NULL)
215 if(strstr(direntp->d_name,exception_str) != NULL)
219 if(strstr(direntp->d_name,glob) == NULL)
222 string_list_add_item(&sdirs,direntp->d_name);
225 closedir(dirStructP);
228 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
229 if((dirStructP = opendir(path)) != NULL)
231 while((direntp = readdir(dirStructP)) != NULL)
233 char absolute_filename[1024];
236 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
238 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
240 if(exception_str != NULL)
242 if(strstr(direntp->d_name,exception_str) != NULL)
246 if(strstr(direntp->d_name,glob) == NULL)
249 string_list_add_item(&sdirs,direntp->d_name);
252 closedir(dirStructP);
258 void Setup::info(const std::string& _package_name, const std::string& _package_symbol_name, const std::string& _package_version)
260 package_name = _package_name;
261 package_symbol_name = _package_symbol_name;
262 package_version = _package_version;
266 /* Set SuperTux configuration and save directories */
267 void Setup::directories(void)
271 /* Get home directory (from $HOME variable)... if we can't determine it,
272 use the current directory ("."): */
273 if (getenv("HOME") != NULL)
274 home = getenv("HOME");
278 std::string st_dir_tmp = "/." + package_symbol_name;
279 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
280 strlen(st_dir_tmp.c_str()) + 1));
281 strcpy(st_dir, home);
282 strcat(st_dir,st_dir_tmp.c_str());
284 /* Remove .supertux config-file from old SuperTux versions */
285 if(FileSystem::faccessible(st_dir))
291 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
293 strcpy(st_save_dir,st_dir);
294 strcat(st_save_dir,"/save");
296 /* Create them. In the case they exist they won't destroy anything. */
298 mkdir(st_save_dir, 0755);
300 sprintf(str, "%s/levels", st_dir);
303 // User has not that a datadir, so we try some magic
308 char exe_file[PATH_MAX];
309 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
311 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
312 datadir = DATA_PREFIX;
316 std::string exedir = std::string(dirname(exe_file)) + "/";
318 datadir = exedir + "../data"; // SuperTux run from source dir
319 if (access(datadir.c_str(), F_OK) != 0)
321 datadir = exedir + "../share/" + package_symbol_name; // SuperTux run from PATH
322 if (access(datadir.c_str(), F_OK) != 0)
323 { // If all fails, fall back to compiled path
324 datadir = DATA_PREFIX;
329 datadir = DATA_PREFIX;
332 printf("Datadir: %s\n", datadir.c_str());
335 void Setup::general(void)
337 /* Seed random number generator: */
339 srand(SDL_GetTicks());
341 /* Set icon image: */
345 /* Unicode needed for input handling: */
347 SDL_EnableUNICODE(1);
349 /* Load global images: */
350 gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18);
351 blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3);
352 white_text = new Font(datadir + "/images/fonts/white.png",
354 gray_text = new Font(datadir + "/images/fonts/gray.png",
356 white_small_text = new Font(datadir + "/images/fonts/white-small.png",
358 white_big_text = new Font(datadir + "/images/fonts/white-big.png",
359 Font::TEXT, 20,22, 3);
360 yellow_nums = new Font(datadir + "/images/fonts/numbers.png",
363 /* Load GUI/menu images: */
364 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
365 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
366 back = new Surface(datadir + "/images/status/back.png", true);
367 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
368 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
370 /* Load the mouse-cursor */
371 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
372 MouseCursor::set_current(mouse_cursor);
376 void Setup::general_free(void)
379 /* Free global images: */
384 delete white_small_text;
385 delete white_big_text;
388 /* Free GUI/menu images: */
390 delete checkbox_checked;
395 /* Free mouse-cursor */
400 void Setup::video(unsigned int screen_w, unsigned int screen_h)
402 /* Init SDL Video: */
403 if (SDL_Init(SDL_INIT_VIDEO) < 0)
406 "\nError: I could not initialize video!\n"
407 "The Simple DirectMedia error that occured was:\n"
408 "%s\n\n", SDL_GetError());
414 video_gl(screen_w, screen_h);
416 video_sdl(screen_w, screen_h);
418 Surface::reload_all();
420 /* Set window manager stuff: */
421 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
424 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
428 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
432 "\nWarning: I could not set up fullscreen video for "
434 "The Simple DirectMedia error that occured was:\n"
435 "%s\n\n", SDL_GetError());
436 use_fullscreen = false;
441 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
446 "\nError: I could not set up video for 800x600 mode.\n"
447 "The Simple DirectMedia error that occured was:\n"
448 "%s\n\n", SDL_GetError());
454 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
458 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
459 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
460 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
461 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
462 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
466 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
470 "\nWarning: I could not set up fullscreen video for "
472 "The Simple DirectMedia error that occured was:\n"
473 "%s\n\n", SDL_GetError());
474 use_fullscreen = false;
479 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
484 "\nError: I could not set up video for 640x480 mode.\n"
485 "The Simple DirectMedia error that occured was:\n"
486 "%s\n\n", SDL_GetError());
492 * Set up OpenGL for 2D rendering.
494 glDisable(GL_DEPTH_TEST);
495 glDisable(GL_CULL_FACE);
497 glViewport(0, 0, screen->w, screen->h);
498 glMatrixMode(GL_PROJECTION);
500 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
502 glMatrixMode(GL_MODELVIEW);
504 glTranslatef(0.0f, 0.0f, 0.0f);
510 void Setup::joystick(void)
517 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
519 fprintf(stderr, "Warning: I could not initialize joystick!\n"
520 "The Simple DirectMedia error that occured was:\n"
521 "%s\n\n", SDL_GetError());
523 use_joystick = false;
528 if (SDL_NumJoysticks() <= 0)
530 fprintf(stderr, "Info: No joysticks were found.\n");
532 use_joystick = false;
536 js = SDL_JoystickOpen(joystick_num);
540 fprintf(stderr, "Warning: Could not open joystick %d.\n"
541 "The Simple DirectMedia error that occured was:\n"
542 "%s\n\n", joystick_num, SDL_GetError());
544 use_joystick = false;
548 if (SDL_JoystickNumAxes(js) < 2)
551 "Warning: Joystick does not have enough axes!\n");
553 use_joystick = false;
557 if (SDL_JoystickNumButtons(js) < 2)
561 "Joystick does not have enough buttons!\n");
563 use_joystick = false;
571 void Setup::audio(void)
574 /* Init SDL Audio silently even if --disable-sound : */
576 if (SoundManager::get()->audio_device_available())
578 if (SDL_Init(SDL_INIT_AUDIO) < 0)
580 /* only print out message if sound or music
581 was not disabled at command-line
583 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
586 "\nWarning: I could not initialize audio!\n"
587 "The Simple DirectMedia error that occured was:\n"
588 "%s\n\n", SDL_GetError());
590 /* keep the programming logic the same :-)
591 because in this case, use_sound & use_music' values are ignored
592 when there's no available audio device
594 SoundManager::get()->enable_sound(false);
595 SoundManager::get()->enable_music(false);
596 SoundManager::get()->set_audio_device_available(false);
601 /* Open sound silently regarless the value of "use_sound": */
603 if (SoundManager::get()->audio_device_available())
605 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
607 /* only print out message if sound or music
608 was not disabled at command-line
610 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
613 "\nWarning: I could not set up audio for 44100 Hz "
615 "The Simple DirectMedia error that occured was:\n"
616 "%s\n\n", SDL_GetError());
618 SoundManager::get()->enable_sound(false);
619 SoundManager::get()->enable_music(false);
620 SoundManager::get()->set_audio_device_available(false);
627 /* --- SHUTDOWN --- */
629 void Termination::shutdown(void)
631 SoundManager::get()->close_audio();
638 void Termination::abort(const std::string& reason, const std::string& details)
640 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
645 /* Set Icon (private) */
654 /* Load icon into a surface: */
656 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
660 "\nError: I could not load the icon image: %s%s\n"
661 "The Simple DirectMedia error that occured was:\n"
662 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
669 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
670 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
671 memset(mask, 0xFF, masklen);
676 SDL_WM_SetIcon(icon, NULL);//mask);
679 /* Free icon surface & mask: */
682 SDL_FreeSurface(icon);
686 /* Parse command-line arguments: */
688 void Setup::parseargs(int argc, char * argv[])
694 /* Parse arguments: */
696 for (i = 1; i < argc; i++)
698 if (strcmp(argv[i], "--fullscreen") == 0 ||
699 strcmp(argv[i], "-f") == 0)
701 use_fullscreen = true;
703 else if (strcmp(argv[i], "--window") == 0 ||
704 strcmp(argv[i], "-w") == 0)
706 use_fullscreen = false;
708 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
711 joystick_num = atoi(argv[++i]);
713 else if (strcmp(argv[i], "--joymap") == 0)
716 if (sscanf(argv[++i],
718 &joystick_keymap.x_axis,
719 &joystick_keymap.y_axis,
720 &joystick_keymap.a_button,
721 &joystick_keymap.b_button,
722 &joystick_keymap.start_button) != 5)
724 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
728 std::cout << "Using new joymap:\n"
729 << " X-Axis: " << joystick_keymap.x_axis << "\n"
730 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
731 << " A-Button: " << joystick_keymap.a_button << "\n"
732 << " B-Button: " << joystick_keymap.b_button << "\n"
733 << " Start-Button: " << joystick_keymap.start_button << std::endl;
736 else if (strcmp(argv[i], "--leveleditor") == 0)
738 launch_leveleditor_mode = true;
740 else if (strcmp(argv[i], "--worldmap") == 0)
742 launch_worldmap_mode = true;
744 else if (strcmp(argv[i], "--datadir") == 0
745 || strcmp(argv[i], "-d") == 0 )
750 else if (strcmp(argv[i], "--show-fps") == 0)
752 /* Use full screen: */
756 else if (strcmp(argv[i], "--opengl") == 0 ||
757 strcmp(argv[i], "-gl") == 0)
765 else if (strcmp(argv[i], "--sdl") == 0)
769 else if (strcmp(argv[i], "--usage") == 0)
775 else if (strcmp(argv[i], "--version") == 0)
778 printf((package_name + package_version + "\n").c_str() );
781 else if (strcmp(argv[i], "--disable-sound") == 0)
783 /* Disable the compiled in sound feature */
784 printf("Sounds disabled \n");
785 SoundManager::get()->enable_sound(false);
787 else if (strcmp(argv[i], "--disable-music") == 0)
789 /* Disable the compiled in sound feature */
790 printf("Music disabled \n");
791 SoundManager::get()->enable_music(false);
793 else if (strcmp(argv[i], "--debug") == 0)
795 /* Enable the debug-mode */
799 else if (strcmp(argv[i], "--help") == 0)
801 puts(_(" SuperTux " VERSION "\n"
802 " Please see the file \"README.txt\" for more details.\n"));
803 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
804 puts(_("Display Options:\n"
805 " -f, --fullscreen Run in fullscreen mode.\n"
806 " -w, --window Run in window mode.\n"
807 " --opengl If OpenGL support was compiled in, this will tell\n"
808 " SuperTux to make use of it.\n"
809 " --sdl Use the SDL software graphical renderer\n"
812 " --disable-sound If sound support was compiled in, this will\n"
813 " disable sound for this session of the game.\n"
814 " --disable-music Like above, but this will disable music.\n"
817 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
818 " --joymap XAXIS:YAXIS:A:B:START\n"
819 " Define how joystick buttons and axis should be mapped\n"
820 " --leveleditor Opens the leveleditor in a file.\n"
821 " --worldmap Opens the specified worldmap file.\n"
822 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
823 " --debug Enables the debug mode, which is useful for developers.\n"
824 " --help Display a help message summarizing command-line\n"
825 " options, license and game controls.\n"
826 " --usage Display a brief message summarizing command-line options.\n"
827 " --version Display the version of SuperTux you're running.\n\n"
831 else if (argv[i][0] != '-')
833 level_startup_file = argv[i];
837 /* Unknown - complain! */
847 void usage(char * prog, int ret)
852 /* Determine which stream to write to: */
860 /* Display the usage message: */
862 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] FILENAME\n"),
871 std::vector<std::string> FileSystem::read_directory(const std::string& pathname)
873 std::vector<std::string> dirnames;
875 DIR* dir = opendir(pathname.c_str());
878 struct dirent *direntp;
880 while((direntp = readdir(dir)))
882 dirnames.push_back(direntp->d_name);