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 GUI/menu images: */
350 checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
351 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
352 back = new Surface(datadir + "/images/status/back.png", true);
353 arrow_left = new Surface(datadir + "/images/icons/left.png", true);
354 arrow_right = new Surface(datadir + "/images/icons/right.png", true);
356 /* Load the mouse-cursor */
357 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
358 MouseCursor::set_current(mouse_cursor);
362 void Setup::general_free(void)
365 /* Free GUI/menu images: */
367 delete checkbox_checked;
372 /* Free mouse-cursor */
377 void Setup::video(unsigned int screen_w, unsigned int screen_h)
379 /* Init SDL Video: */
380 if (SDL_Init(SDL_INIT_VIDEO) < 0)
383 "\nError: I could not initialize video!\n"
384 "The Simple DirectMedia error that occured was:\n"
385 "%s\n\n", SDL_GetError());
391 video_gl(screen_w, screen_h);
393 video_sdl(screen_w, screen_h);
395 Surface::reload_all();
397 /* Set window manager stuff: */
398 SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str());
401 void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h)
405 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
409 "\nWarning: I could not set up fullscreen video for "
411 "The Simple DirectMedia error that occured was:\n"
412 "%s\n\n", SDL_GetError());
413 use_fullscreen = false;
418 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
423 "\nError: I could not set up video for 800x600 mode.\n"
424 "The Simple DirectMedia error that occured was:\n"
425 "%s\n\n", SDL_GetError());
431 void Setup::video_gl(unsigned int screen_w, unsigned int screen_h)
435 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
436 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
437 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
438 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
439 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
443 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
447 "\nWarning: I could not set up fullscreen video for "
449 "The Simple DirectMedia error that occured was:\n"
450 "%s\n\n", SDL_GetError());
451 use_fullscreen = false;
456 screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL);
461 "\nError: I could not set up video for 640x480 mode.\n"
462 "The Simple DirectMedia error that occured was:\n"
463 "%s\n\n", SDL_GetError());
469 * Set up OpenGL for 2D rendering.
471 glDisable(GL_DEPTH_TEST);
472 glDisable(GL_CULL_FACE);
474 glViewport(0, 0, screen->w, screen->h);
475 glMatrixMode(GL_PROJECTION);
477 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
479 glMatrixMode(GL_MODELVIEW);
481 glTranslatef(0.0f, 0.0f, 0.0f);
487 void Setup::joystick(void)
494 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
496 fprintf(stderr, "Warning: I could not initialize joystick!\n"
497 "The Simple DirectMedia error that occured was:\n"
498 "%s\n\n", SDL_GetError());
500 use_joystick = false;
505 if (SDL_NumJoysticks() <= 0)
507 fprintf(stderr, "Info: No joysticks were found.\n");
509 use_joystick = false;
513 js = SDL_JoystickOpen(joystick_num);
517 fprintf(stderr, "Warning: Could not open joystick %d.\n"
518 "The Simple DirectMedia error that occured was:\n"
519 "%s\n\n", joystick_num, SDL_GetError());
521 use_joystick = false;
525 if (SDL_JoystickNumAxes(js) < 2)
528 "Warning: Joystick does not have enough axes!\n");
530 use_joystick = false;
534 if (SDL_JoystickNumButtons(js) < 2)
538 "Joystick does not have enough buttons!\n");
540 use_joystick = false;
548 void Setup::audio(void)
551 /* Init SDL Audio silently even if --disable-sound : */
553 if (SoundManager::get()->audio_device_available())
555 if (SDL_Init(SDL_INIT_AUDIO) < 0)
557 /* only print out message if sound or music
558 was not disabled at command-line
560 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
563 "\nWarning: I could not initialize audio!\n"
564 "The Simple DirectMedia error that occured was:\n"
565 "%s\n\n", SDL_GetError());
567 /* keep the programming logic the same :-)
568 because in this case, use_sound & use_music' values are ignored
569 when there's no available audio device
571 SoundManager::get()->enable_sound(false);
572 SoundManager::get()->enable_music(false);
573 SoundManager::get()->set_audio_device_available(false);
578 /* Open sound silently regarless the value of "use_sound": */
580 if (SoundManager::get()->audio_device_available())
582 if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0)
584 /* only print out message if sound or music
585 was not disabled at command-line
587 if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled())
590 "\nWarning: I could not set up audio for 44100 Hz "
592 "The Simple DirectMedia error that occured was:\n"
593 "%s\n\n", SDL_GetError());
595 SoundManager::get()->enable_sound(false);
596 SoundManager::get()->enable_music(false);
597 SoundManager::get()->set_audio_device_available(false);
604 /* --- SHUTDOWN --- */
606 void Termination::shutdown(void)
608 SoundManager::get()->close_audio();
615 void Termination::abort(const std::string& reason, const std::string& details)
617 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
622 /* Set Icon (private) */
631 /* Load icon into a surface: */
633 icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str());
637 "\nError: I could not load the icon image: %s%s\n"
638 "The Simple DirectMedia error that occured was:\n"
639 "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError());
646 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
647 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
648 memset(mask, 0xFF, masklen);
653 SDL_WM_SetIcon(icon, NULL);//mask);
656 /* Free icon surface & mask: */
659 SDL_FreeSurface(icon);
663 /* Parse command-line arguments: */
665 void Setup::parseargs(int argc, char * argv[])
671 /* Parse arguments: */
673 for (i = 1; i < argc; i++)
675 if (strcmp(argv[i], "--fullscreen") == 0 ||
676 strcmp(argv[i], "-f") == 0)
678 use_fullscreen = true;
680 else if (strcmp(argv[i], "--window") == 0 ||
681 strcmp(argv[i], "-w") == 0)
683 use_fullscreen = false;
685 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
688 joystick_num = atoi(argv[++i]);
690 else if (strcmp(argv[i], "--joymap") == 0)
693 if (sscanf(argv[++i],
695 &joystick_keymap.x_axis,
696 &joystick_keymap.y_axis,
697 &joystick_keymap.a_button,
698 &joystick_keymap.b_button,
699 &joystick_keymap.start_button) != 5)
701 puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
705 std::cout << "Using new joymap:\n"
706 << " X-Axis: " << joystick_keymap.x_axis << "\n"
707 << " Y-Axis: " << joystick_keymap.y_axis << "\n"
708 << " A-Button: " << joystick_keymap.a_button << "\n"
709 << " B-Button: " << joystick_keymap.b_button << "\n"
710 << " Start-Button: " << joystick_keymap.start_button << std::endl;
713 else if (strcmp(argv[i], "--leveleditor") == 0)
715 launch_leveleditor_mode = true;
717 else if (strcmp(argv[i], "--worldmap") == 0)
719 launch_worldmap_mode = true;
721 else if (strcmp(argv[i], "--datadir") == 0
722 || strcmp(argv[i], "-d") == 0 )
727 else if (strcmp(argv[i], "--show-fps") == 0)
729 /* Use full screen: */
733 else if (strcmp(argv[i], "--opengl") == 0 ||
734 strcmp(argv[i], "-gl") == 0)
742 else if (strcmp(argv[i], "--sdl") == 0)
746 else if (strcmp(argv[i], "--usage") == 0)
752 else if (strcmp(argv[i], "--version") == 0)
755 printf((package_name + package_version + "\n").c_str() );
758 else if (strcmp(argv[i], "--disable-sound") == 0)
760 /* Disable the compiled in sound feature */
761 printf("Sounds disabled \n");
762 SoundManager::get()->enable_sound(false);
764 else if (strcmp(argv[i], "--disable-music") == 0)
766 /* Disable the compiled in sound feature */
767 printf("Music disabled \n");
768 SoundManager::get()->enable_music(false);
770 else if (strcmp(argv[i], "--debug") == 0)
772 /* Enable the debug-mode */
776 else if (strcmp(argv[i], "--help") == 0)
778 puts(_(" SuperTux " VERSION "\n"
779 " Please see the file \"README.txt\" for more details.\n"));
780 printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]);
781 puts(_("Display Options:\n"
782 " -f, --fullscreen Run in fullscreen mode.\n"
783 " -w, --window Run in window mode.\n"
784 " --opengl If OpenGL support was compiled in, this will tell\n"
785 " SuperTux to make use of it.\n"
786 " --sdl Use the SDL software graphical renderer\n"
789 " --disable-sound If sound support was compiled in, this will\n"
790 " disable sound for this session of the game.\n"
791 " --disable-music Like above, but this will disable music.\n"
794 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
795 " --joymap XAXIS:YAXIS:A:B:START\n"
796 " Define how joystick buttons and axis should be mapped\n"
797 " --leveleditor Opens the leveleditor in a file.\n"
798 " --worldmap Opens the specified worldmap file.\n"
799 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
800 " --debug Enables the debug mode, which is useful for developers.\n"
801 " --help Display a help message summarizing command-line\n"
802 " options, license and game controls.\n"
803 " --usage Display a brief message summarizing command-line options.\n"
804 " --version Display the version of SuperTux you're running.\n\n"
808 else if (argv[i][0] != '-')
810 level_startup_file = argv[i];
814 /* Unknown - complain! */
824 void usage(char * prog, int ret)
829 /* Determine which stream to write to: */
837 /* Display the usage message: */
839 fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] FILENAME\n"),
848 std::vector<std::string> FileSystem::read_directory(const std::string& pathname)
850 std::vector<std::string> dirnames;
852 DIR* dir = opendir(pathname.c_str());
855 struct dirent *direntp;
857 while((direntp = readdir(dir)))
859 dirnames.push_back(direntp->d_name);