X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=lib%2Fapp%2Fsetup.cpp;h=c7972c470323bc8b2a025df720cb5fa51b9d9d49;hb=8e0bad9f82ccbc811a18edd7ce6c6f69c5bca082;hp=c51b0ce0379671d0ce4570f2798f37bc3760ce62;hpb=edaacb3651cf0560314dd008d7243be4b3b2f8c6;p=supertux.git diff --git a/lib/app/setup.cpp b/lib/app/setup.cpp index c51b0ce03..c7972c470 100644 --- a/lib/app/setup.cpp +++ b/lib/app/setup.cpp @@ -17,6 +17,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include + #include #include #include @@ -41,15 +43,14 @@ #include -#include "app/globals.h" -#include "app/defines.h" -#include "app/setup.h" +#include "globals.h" +#include "setup.h" #include "video/screen.h" #include "video/surface.h" #include "gui/menu.h" #include "utils/configfile.h" #include "audio/sound_manager.h" -#include "app/gettext.h" +#include "gettext.h" using namespace SuperTux; @@ -60,21 +61,16 @@ using namespace SuperTux; #define DATA_PREFIX "./data/" #endif -/* Screen proprities: */ -/* Don't use this to test for the actual screen sizes. Use screen->w/h instead! */ -#define SCREEN_W 800 -#define SCREEN_H 600 - /* Local function prototypes: */ void seticon(void); void usage(char * prog, int ret); /* Does the given file exist and is it accessible? */ -int SuperTux::faccessible(const char *filename) +bool FileSystem::faccessible(const std::string& filename) { struct stat filestat; - if (stat(filename, &filestat) == -1) + if (stat(filename.c_str(), &filestat) == -1) { return false; } @@ -88,26 +84,26 @@ int SuperTux::faccessible(const char *filename) } /* Can we write to this location? */ -int SuperTux::fwriteable(const char *filename) +bool FileSystem::fwriteable(const std::string& filename) { FILE* fi; - fi = fopen(filename, "wa"); + fi = fopen(filename.c_str(), "wa"); if (fi == NULL) { return false; } + fclose(fi); return true; } /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/ -int SuperTux::fcreatedir(const char* relative_dir) +bool FileSystem::fcreatedir(const std::string& relative_dir) { - char path[1024]; - snprintf(path, 1024, "%s/%s/", st_dir, relative_dir); - if(mkdir(path,0755) != 0) + std::string path = st_dir + "/" + relative_dir + "/"; + if(mkdir(path.c_str(),0755) != 0) { - snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir); - if(mkdir(path,0755) != 0) + path = datadir + "/" + relative_dir + "/"; + if(mkdir(path.c_str(),0755) != 0) { return false; } @@ -122,101 +118,69 @@ int SuperTux::fcreatedir(const char* relative_dir) } } -FILE * SuperTux::opendata(const char * rel_filename, const char * mode) -{ - char * filename = NULL; - FILE * fi; - - filename = (char *) malloc(sizeof(char) * (strlen(st_dir) + - strlen(rel_filename) + 1)); - - strcpy(filename, st_dir); - /* Open the high score file: */ - - strcat(filename, rel_filename); - - /* Try opening the file: */ - fi = fopen(filename, mode); - - if (fi == NULL) - { - fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename); - - if (strcmp(mode, "r") == 0) - fprintf(stderr, "for read!!!\n"); - else if (strcmp(mode, "w") == 0) - fprintf(stderr, "for write!!!\n"); - } - free( filename ); - - return(fi); -} - /* Get all names of sub-directories in a certain directory. */ /* Returns the number of sub-directories found. */ /* Note: The user has to free the allocated space. */ -string_list_type SuperTux::dsubdirs(const char *rel_path,const char* expected_file) +std::set FileSystem::dsubdirs(const std::string &rel_path,const std::string& expected_file) { DIR *dirStructP; struct dirent *direntp; - string_list_type sdirs; - char filename[1024]; - char path[1024]; + std::set sdirs; + std::string filename; + std::string path = st_dir + "/" + rel_path; - string_list_init(&sdirs); - sprintf(path,"%s/%s",st_dir,rel_path); - if((dirStructP = opendir(path)) != NULL) + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) { - if(expected_file != NULL) + if(!expected_file.empty()) { - sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); + filename = path + "/" + direntp->d_name + "/" + expected_file; if(!faccessible(filename)) continue; } - string_list_add_item(&sdirs,direntp->d_name); + sdirs.insert(direntp->d_name); } } closedir(dirStructP); } - sprintf(path,"%s/%s",datadir.c_str(),rel_path); - if((dirStructP = opendir(path)) != NULL) + path = datadir + "/" + rel_path; + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) { - if(expected_file != NULL) + if(!expected_file.empty()) { - sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); - if(!faccessible(filename)) + filename = path + "/" + direntp->d_name + "/" + expected_file; + if(!faccessible(filename.c_str())) { continue; } else { - sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file); - if(faccessible(filename)) + filename = st_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file; + if(faccessible(filename.c_str())) continue; } } - string_list_add_item(&sdirs,direntp->d_name); + sdirs.insert(direntp->d_name); } } closedir(dirStructP); @@ -225,63 +189,61 @@ string_list_type SuperTux::dsubdirs(const char *rel_path,const char* expected_f return sdirs; } -string_list_type SuperTux::dfiles(const char *rel_path, const char* glob, const char* exception_str) +std::set FileSystem::dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str) { DIR *dirStructP; struct dirent *direntp; - string_list_type sdirs; - char path[1024]; + std::set sdirs; + std::string path = st_dir + "/" + rel_path; - string_list_init(&sdirs); - sprintf(path,"%s/%s",st_dir,rel_path); - if((dirStructP = opendir(path)) != NULL) + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)) { - if(exception_str != NULL) + if(!exception_str.empty()) { - if(strstr(direntp->d_name,exception_str) != NULL) + if(strstr(direntp->d_name,exception_str.c_str()) != NULL) continue; } - if(glob != NULL) - if(strstr(direntp->d_name,glob) == NULL) + if(!glob.empty()) + if(strstr(direntp->d_name,glob.c_str()) == NULL) continue; - string_list_add_item(&sdirs,direntp->d_name); + sdirs.insert(direntp->d_name); } } closedir(dirStructP); } - sprintf(path,"%s/%s",datadir.c_str(),rel_path); - if((dirStructP = opendir(path)) != NULL) + path = datadir + "/" + rel_path; + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)) { - if(exception_str != NULL) + if(!exception_str.empty()) { - if(strstr(direntp->d_name,exception_str) != NULL) + if(strstr(direntp->d_name,exception_str.c_str()) != NULL) continue; } - if(glob != NULL) - if(strstr(direntp->d_name,glob) == NULL) + if(!glob.empty()) + if(strstr(direntp->d_name,glob.c_str()) == NULL) continue; - string_list_add_item(&sdirs,direntp->d_name); + sdirs.insert(direntp->d_name); } } closedir(dirStructP); @@ -290,19 +252,33 @@ string_list_type SuperTux::dfiles(const char *rel_path, const char* glob, const return sdirs; } -void SuperTux::free_strings(char **strings, int num) +std::string FileSystem::dirname(const std::string& filename) { - int i; - for(i=0; i < num; ++i) - free(strings[i]); + std::string::size_type p = filename.find_last_of('/'); + if(p == std::string::npos) + return ""; + + return filename.substr(0, p+1); +} + +void Setup::init(const std::string& _package_name, + const std::string& _package_symbol_name, + const std::string& _package_version) +{ + package_name = _package_name; + package_symbol_name = _package_symbol_name; + package_version = _package_version; + + directories(); + dictionary_manager.add_directory(datadir + "/locale"); + dictionary_manager.set_charset("iso8859-1"); } /* --- SETUP --- */ /* Set SuperTux configuration and save directories */ -void SuperTux::st_directory_setup(void) +void Setup::directories() { - char *home; - char str[1024]; + std::string home; /* Get home directory (from $HOME variable)... if we can't determine it, use the current directory ("."): */ if (getenv("HOME") != NULL) @@ -310,29 +286,26 @@ void SuperTux::st_directory_setup(void) else home = "."; - st_dir = (char *) malloc(sizeof(char) * (strlen(home) + - strlen("/.supertux") + 1)); - strcpy(st_dir, home); - strcat(st_dir, "/.supertux"); + st_dir = home + "/." + package_symbol_name; /* Remove .supertux config-file from old SuperTux versions */ - if(faccessible(st_dir)) - { - remove - (st_dir); - } + if(FileSystem::faccessible(st_dir)) { + remove(st_dir.c_str()); + } - st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1)); - - strcpy(st_save_dir,st_dir); - strcat(st_save_dir,"/save"); + st_save_dir = st_dir + "/save"; /* Create them. In the case they exist they won't destroy anything. */ - mkdir(st_dir, 0755); - mkdir(st_save_dir, 0755); + mkdir(st_dir.c_str(), 0755); + mkdir(st_save_dir.c_str(), 0755); + + mkdir((st_dir + "/levels").c_str(), 0755); - sprintf(str, "%s/levels", st_dir); - mkdir(str, 0755); + // try current directory as datadir + if(datadir.empty()) { + if(FileSystem::faccessible("./data/credits.txt")) + datadir = "./data/"; + } // User has not that a datadir, so we try some magic if (datadir.empty()) @@ -347,26 +320,33 @@ void SuperTux::st_directory_setup(void) } else { - std::string exedir = std::string(dirname(exe_file)) + "/"; - - datadir = exedir + "../data"; // SuperTux run from source dir + std::string exedir = std::string(dirname(exe_file)) + "/"; + + datadir = exedir + "./data/"; // SuperTux run from source dir if (access(datadir.c_str(), F_OK) != 0) { - datadir = exedir + "../share/supertux"; // SuperTux run from PATH + datadir = exedir + "../../../../data/"; //SuperTux run from source dir (with libtool script) + + if (access(datadir.c_str(), F_OK) != 0) + { + datadir = exedir + "../share/" + package_symbol_name + "/"; // SuperTux run from PATH if (access(datadir.c_str(), F_OK) != 0) { // If all fails, fall back to compiled path - datadir = DATA_PREFIX; + datadir = DATA_PREFIX; + datadir += "/"; } + } } } #else - datadir = DATA_PREFIX; + datadir = DATA_PREFIX; + datadir += "/"; #endif } printf("Datadir: %s\n", datadir.c_str()); } -void SuperTux::st_general_setup(void) +void Setup::general(void) { /* Seed random number generator: */ @@ -380,20 +360,6 @@ void SuperTux::st_general_setup(void) SDL_EnableUNICODE(1); - /* Load global images: */ - gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18); - blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3); - white_text = new Font(datadir + "/images/fonts/white.png", - Font::TEXT, 16,18); - gray_text = new Font(datadir + "/images/fonts/gray.png", - Font::TEXT, 16,18); - white_small_text = new Font(datadir + "/images/fonts/white-small.png", - Font::TEXT, 8,9, 1); - white_big_text = new Font(datadir + "/images/fonts/white-big.png", - Font::TEXT, 20,22, 3); - yellow_nums = new Font(datadir + "/images/fonts/numbers.png", - Font::NUM, 32,32); - /* Load GUI/menu images: */ checkbox = new Surface(datadir + "/images/status/checkbox.png", true); checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true); @@ -407,18 +373,9 @@ void SuperTux::st_general_setup(void) } -void SuperTux::st_general_free(void) +void Setup::general_free(void) { - /* Free global images: */ - delete gold_text; - delete white_text; - delete blue_text; - delete gray_text; - delete white_small_text; - delete white_big_text; - delete yellow_nums; - /* Free GUI/menu images: */ delete checkbox; delete checkbox_checked; @@ -431,7 +388,7 @@ void SuperTux::st_general_free(void) } -void SuperTux::st_video_setup(void) +void Setup::video(unsigned int screen_w, unsigned int screen_h) { /* Init SDL Video: */ if (SDL_Init(SDL_INIT_VIDEO) < 0) @@ -445,21 +402,21 @@ void SuperTux::st_video_setup(void) /* Open display: */ if(use_gl) - st_video_setup_gl(); + video_gl(screen_w, screen_h); else - st_video_setup_sdl(); + video_sdl(screen_w, screen_h); Surface::reload_all(); /* Set window manager stuff: */ - SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux"); + SDL_WM_SetCaption((package_name + " " + package_version).c_str(), package_name.c_str()); } -void SuperTux::st_video_setup_sdl(void) +void Setup::video_sdl(unsigned int screen_w, unsigned int screen_h) { if (use_fullscreen) { - screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */ + screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */ if (screen == NULL) { fprintf(stderr, @@ -472,7 +429,7 @@ void SuperTux::st_video_setup_sdl(void) } else { - screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_HWSURFACE | SDL_DOUBLEBUF ); + screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_HWSURFACE | SDL_DOUBLEBUF ); if (screen == NULL) { @@ -485,7 +442,7 @@ void SuperTux::st_video_setup_sdl(void) } } -void SuperTux::st_video_setup_gl(void) +void Setup::video_gl(unsigned int screen_w, unsigned int screen_h) { #ifndef NOOPENGL @@ -497,7 +454,7 @@ void SuperTux::st_video_setup_gl(void) if (use_fullscreen) { - screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */ + screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */ if (screen == NULL) { fprintf(stderr, @@ -510,7 +467,7 @@ void SuperTux::st_video_setup_gl(void) } else { - screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_OPENGL); + screen = SDL_SetVideoMode(screen_w, screen_h, 0, SDL_OPENGL); if (screen == NULL) { @@ -541,7 +498,7 @@ void SuperTux::st_video_setup_gl(void) } -void SuperTux::st_joystick_setup(void) +void Setup::joystick(void) { /* Init Joystick: */ @@ -602,19 +559,19 @@ void SuperTux::st_joystick_setup(void) } } -void SuperTux::st_audio_setup(void) +void Setup::audio(void) { /* Init SDL Audio silently even if --disable-sound : */ - if (audio_device) + if (SoundManager::get()->audio_device_available()) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { /* only print out message if sound or music was not disabled at command-line */ - if (use_sound || use_music) + if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled()) { fprintf(stderr, "\nWarning: I could not initialize audio!\n" @@ -625,23 +582,23 @@ void SuperTux::st_audio_setup(void) because in this case, use_sound & use_music' values are ignored when there's no available audio device */ - use_sound = false; - use_music = false; - audio_device = false; + SoundManager::get()->enable_sound(false); + SoundManager::get()->enable_music(false); + SoundManager::get()->set_audio_device_available(false); } } /* Open sound silently regarless the value of "use_sound": */ - if (audio_device) + if (SoundManager::get()->audio_device_available()) { - if (open_audio(44100, AUDIO_S16, 2, 2048) < 0) + if (SoundManager::get()->open_audio(44100, AUDIO_S16, 2, 2048) < 0) { /* only print out message if sound or music was not disabled at command-line */ - if (use_sound || use_music) + if (SoundManager::get()->sound_enabled() || SoundManager::get()->music_enabled()) { fprintf(stderr, "\nWarning: I could not set up audio for 44100 Hz " @@ -649,9 +606,9 @@ void SuperTux::st_audio_setup(void) "The Simple DirectMedia error that occured was:\n" "%s\n\n", SDL_GetError()); } - use_sound = false; - use_music = false; - audio_device = false; + SoundManager::get()->enable_sound(false); + SoundManager::get()->enable_music(false); + SoundManager::get()->set_audio_device_available(false); } } @@ -660,20 +617,20 @@ void SuperTux::st_audio_setup(void) /* --- SHUTDOWN --- */ -void SuperTux::st_shutdown(void) +void Termination::shutdown(void) { - close_audio(); - SDL_Quit(); config->save(); + SoundManager::get()->close_audio(); + SDL_Quit(); } /* --- ABORT! --- */ -void SuperTux::st_abort(const std::string& reason, const std::string& details) +void Termination::abort(const std::string& reason, const std::string& details) { fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str()); - st_shutdown(); - abort(); + shutdown(); + ::abort(); } /* Set Icon (private) */ @@ -687,13 +644,13 @@ void seticon(void) /* Load icon into a surface: */ - icon = IMG_Load((datadir + "/images/supertux.xpm").c_str()); + icon = IMG_Load((datadir + "/images/" + package_symbol_name + ".xpm").c_str()); if (icon == NULL) { fprintf(stderr, "\nError: I could not load the icon image: %s%s\n" "The Simple DirectMedia error that occured was:\n" - "%s\n\n", datadir.c_str(), "/images/supertux.xpm", SDL_GetError()); + "%s\n\n", datadir.c_str(), ("/images/" + package_symbol_name + ".xpm").c_str(), SDL_GetError()); exit(1); } @@ -719,7 +676,7 @@ void seticon(void) /* Parse command-line arguments: */ -void SuperTux::parseargs(int argc, char * argv[]) +void Setup::parseargs(int argc, char * argv[]) { int i; @@ -775,6 +732,10 @@ void SuperTux::parseargs(int argc, char * argv[]) { launch_worldmap_mode = true; } + else if (strcmp(argv[i], "--flip-levels") == 0) + { + flip_levels_mode = true; + } else if (strcmp(argv[i], "--datadir") == 0 || strcmp(argv[i], "-d") == 0 ) { @@ -809,21 +770,20 @@ void SuperTux::parseargs(int argc, char * argv[]) else if (strcmp(argv[i], "--version") == 0) { /* Show version: */ - printf("SuperTux " VERSION "\n"); + printf((package_name + " " + package_version + "\n").c_str() ); exit(0); } else if (strcmp(argv[i], "--disable-sound") == 0) { /* Disable the compiled in sound feature */ printf("Sounds disabled \n"); - use_sound = false; - audio_device = false; + SoundManager::get()->enable_sound(false); } else if (strcmp(argv[i], "--disable-music") == 0) { /* Disable the compiled in sound feature */ printf("Music disabled \n"); - use_music = false; + SoundManager::get()->enable_music(false); } else if (strcmp(argv[i], "--debug") == 0) { @@ -833,8 +793,8 @@ void SuperTux::parseargs(int argc, char * argv[]) } else if (strcmp(argv[i], "--help") == 0) { /* Show help: */ - puts(_(" SuperTux " VERSION "\n" - " Please see the file \"README.txt\" for more details.\n")); + puts(_((" SuperTux " + package_version + "\n" + " Please see the file \"README.txt\" for more details.\n").c_str())); printf(_("Usage: %s [OPTIONS] FILENAME\n\n"), argv[0]); puts(_("Display Options:\n" " -f, --fullscreen Run in fullscreen mode.\n" @@ -854,6 +814,7 @@ void SuperTux::parseargs(int argc, char * argv[]) " Define how joystick buttons and axis should be mapped\n" " --leveleditor Opens the leveleditor in a file.\n" " --worldmap Opens the specified worldmap file.\n" + " --flip-levels Flip levels upside-down.\n" " -d, --datadir DIR Load Game data from DIR (default: automatic)\n" " --debug Enables the debug mode, which is useful for developers.\n" " --help Display a help message summarizing command-line\n" @@ -894,7 +855,7 @@ void usage(char * prog, int ret) /* Display the usage message: */ - fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] FILENAME\n"), + fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] [--flip-levels] FILENAME\n"), prog); @@ -903,9 +864,9 @@ void usage(char * prog, int ret) exit(ret); } -std::vector SuperTux::read_directory(const std::string& pathname) +std::set FileSystem::read_directory(const std::string& pathname) { - std::vector dirnames; + std::set dirnames; DIR* dir = opendir(pathname.c_str()); if (dir) @@ -914,7 +875,7 @@ std::vector SuperTux::read_directory(const std::string& pathname) while((direntp = readdir(dir))) { - dirnames.push_back(direntp->d_name); + dirnames.insert(direntp->d_name); } closedir(dir);