From 5a542dea3c6043703683b68fcaa774f6cb0d9127 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Thu, 25 Nov 2004 19:48:36 +0000 Subject: [PATCH] make supertux accepts normal paths on the commandline SVN-Revision: 2198 --- lib/app/setup.cpp | 21 +++++++++++---------- src/gameloop.cpp | 11 ++++++----- src/gameloop.h | 5 +++-- src/level.cpp | 14 +------------- src/level.h | 1 + src/level_subset.cpp | 23 ++++++++++++----------- src/resources.cpp | 13 +++++++++++++ src/resources.h | 4 ++++ src/title.cpp | 6 ++---- src/worldmap.cpp | 3 ++- 10 files changed, 55 insertions(+), 46 deletions(-) diff --git a/lib/app/setup.cpp b/lib/app/setup.cpp index 20de54224..0a29dc4b9 100644 --- a/lib/app/setup.cpp +++ b/lib/app/setup.cpp @@ -280,10 +280,9 @@ void Setup::directories() st_dir = home + "/." + package_symbol_name; /* Remove .supertux config-file from old SuperTux versions */ - if(FileSystem::faccessible(st_dir)) - { - remove(st_dir.c_str()); - } + if(FileSystem::faccessible(st_dir)) { + remove(st_dir.c_str()); + } st_save_dir = st_dir + "/save"; @@ -296,7 +295,7 @@ void Setup::directories() // try current directory as datadir if(datadir.empty()) { if(FileSystem::faccessible("./data/intro.txt")) - datadir = "./data"; + datadir = "./data/"; } // User has not that a datadir, so we try some magic @@ -314,23 +313,25 @@ void Setup::directories() { std::string exedir = std::string(dirname(exe_file)) + "/"; - datadir = exedir + "../data"; // SuperTux run from source dir + datadir = exedir + "../data/"; // SuperTux run from source dir if (access(datadir.c_str(), F_OK) != 0) { - datadir = exedir + "../../data"; //SuperTux run from source dir (with libtool script) + 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 + 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()); diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 83d7da72f..fc9f471c1 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -80,11 +80,11 @@ bool compare_last(std::string& haystack, std::string needle) return false; } -GameSession::GameSession(const std::string& levelname_, int mode, +GameSession::GameSession(const std::string& levelfile_, int mode, bool flip_level_, Statistics* statistics) : level(0), currentsector(0), st_gl_mode(mode), - end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_), - best_level_statistics(statistics) + end_sequence(NO_ENDSEQUENCE), levelfile(levelfile_), + flip_level(flip_level_), best_level_statistics(statistics) { current_ = this; @@ -124,7 +124,7 @@ GameSession::restart_level() currentsector = 0; level = new Level; - level->load(levelname); + level->load(levelfile); if(flip_level) level->do_vertical_flip(); @@ -726,7 +726,8 @@ GameSession::run() while (exit_status == ES_NONE) { Uint32 ticks = SDL_GetTicks(); float elapsed_time = float(ticks - lastticks) / 1000.; - global_time += elapsed_time; + if(!game_pause) + global_time += elapsed_time; lastticks = ticks; // 40fps is minimum diff --git a/src/gameloop.h b/src/gameloop.h index 1d01487e6..d1898be8f 100644 --- a/src/gameloop.h +++ b/src/gameloop.h @@ -78,7 +78,7 @@ private: bool game_pause; - std::string levelname; + std::string levelfile; bool flip_level; // the sector and spawnpoint we shoudl spawn after this frame @@ -93,7 +93,8 @@ public: DrawingContext* context; Timer2 time_left; - GameSession(const std::string& level, int mode, bool flip_level_ = false, Statistics* statistics = NULL); + GameSession(const std::string& levelfile, int mode, bool flip_level_ = false, + Statistics* statistics = 0); ~GameSession(); /** Enter the busy loop */ diff --git a/src/level.cpp b/src/level.cpp index f317b22ac..8788cc08b 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -62,20 +62,8 @@ Level::create(const std::string& filename) } void -Level::load(const std::string& filename) +Level::load(const std::string& filepath) { - std::string filepath; - filepath = st_dir + "/levels/" + filename; - if (access(filepath.c_str(), R_OK) != 0) - { - filepath = datadir + "/levels/" + filename; - if (access(filepath.c_str(), R_OK) != 0) - { - std::cerr << "Error: Level: couldn't find level: " << filename << std::endl; - return; - } - } - LispReader* level = LispReader::load(filepath, "supertux-level"); int version = 1; diff --git a/src/level.h b/src/level.h index cc280b5e3..d627d8245 100644 --- a/src/level.h +++ b/src/level.h @@ -51,6 +51,7 @@ public: Level(); ~Level(); + // loads a levelfile void load(const std::string& filename); void save(const std::string& filename); static void create(const std::string& filename); diff --git a/src/level_subset.cpp b/src/level_subset.cpp index 0a67f1daf..19edf4772 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -20,10 +20,13 @@ #include +#include +#include #include #include #include "app/setup.h" #include "level.h" +#include "resources.h" #include "app/globals.h" #include "video/surface.h" #include "level_subset.h" @@ -89,14 +92,13 @@ void LevelSubset::load(const std::string& subset) // Check in which directory our subset is located (ie. ~/.supertux/ // or SUPERTUX_DATADIR) - std::string filename; - filename = st_dir + "/levels/" + subset + "/info"; - if (access(filename.c_str(), R_OK) != 0) - { - filename = datadir + "/levels/" + subset + "/info"; - if (access(filename.c_str(), R_OK) != 0) - std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl; - } + std::string filename = get_resource_filename( + std::string("levels/") + subset + "/info"); + if(filename == "") { + std::stringstream msg; + msg << "Couldn't find level subset '" << subset << "'."; + throw new std::runtime_error(msg.str()); + } read_info_file(filename); @@ -115,7 +117,8 @@ void LevelSubset::load(const std::string& subset) for(std::set::iterator i = files.begin(); i != files.end(); ++i) { if (has_suffix(*i, ".stl")) - levels.push_back(subset+ "/" + *i); + levels.push_back(get_resource_filename( + std::string("levels/" + subset+ "/" + *i))); } } } @@ -177,5 +180,3 @@ LevelSubset::get_num_levels() const { return levels.size(); } - -/* EOF */ diff --git a/src/resources.cpp b/src/resources.cpp index ac23078d4..da929586f 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -304,3 +304,16 @@ void unloadshared(void) sprite_manager = 0; } +std::string get_resource_filename(const std::string& resource) +{ + std::string filepath = st_dir + resource; + if(access(filepath.c_str(), R_OK) == 0) + return filepath; + + filepath = datadir + resource; + if(access(filepath.c_str(), R_OK) == 0) + return filepath; + + std::cerr << "Couldn't find resource: '" << resource << "'." << std::endl; + return ""; +} diff --git a/src/resources.h b/src/resources.h index 2519b046f..f399af88a 100644 --- a/src/resources.h +++ b/src/resources.h @@ -93,6 +93,10 @@ extern Font* white_small_text; extern Font* white_big_text; extern Font* yellow_nums; +// maps a virtual resource path to a real path (ie. levels/bla is mapped to +// $DATADIR/levels/bla or $HOME/.supertux/levels/bla) +std::string get_resource_filename(const std::string& resource); + void loadshared(); void unloadshared(); diff --git a/src/title.cpp b/src/title.cpp index e15011296..4874916c1 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -322,7 +322,8 @@ void title(void) Ticks::pause_init(); - titlesession = new GameSession("misc/menu.stl", ST_GL_DEMO_GAME); + titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"), + ST_GL_DEMO_GAME); /* Load images: */ bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false); @@ -483,6 +484,3 @@ void title(void) delete img_choose_subset; } - -// EOF // - diff --git a/src/worldmap.cpp b/src/worldmap.cpp index 4bbc4b29d..974c75c8c 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -863,7 +863,8 @@ WorldMap::update(float delta) // do a shriking fade to the level shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),(level->pos.y*32 + 16 + offset.y)), 500); - GameSession session(level->name, + GameSession session( + get_resource_filename(std::string("levels/" + level->name)), ST_GL_LOAD_LEVEL_FILE, level->vertical_flip, &level->statistics); -- 2.11.0