Thread: [Super-tux-commit] supertux/src Makefile.am,1.36,1.37 level.cpp,1.92,1.93 level.h,1.58,1.59 level_su
Brought to you by:
wkendrick
From: Ingo R. <gr...@us...> - 2004-06-16 00:40:52
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17535 Modified Files: Makefile.am level.cpp level.h level_subset.cpp level_subset.h leveleditor.cpp leveleditor.h setup.cpp setup.h title.cpp Log Message: - moved level subsets into their own file - changed level subsets so that they do a readdir() instead of iterating ovre level1, level2,.. until error, this should also allow abitary level file names Index: level_subset.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level_subset.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- level_subset.cpp 15 Jun 2004 22:56:42 -0000 1.1 +++ level_subset.cpp 16 Jun 2004 00:40:42 -0000 1.2 @@ -18,20 +18,28 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. +#include <assert.h> #include "setup.h" #include "level.h" #include "globals.h" #include "screen/surface.h" #include "level_subset.h" +static bool has_suffix(const std::string& data, const std::string& suffix) +{ + if (data.length() >= suffix.length()) + return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0; + else + return false; +} + LevelSubset::LevelSubset() - : image(0), levels(0) + : levels(0) { } LevelSubset::~LevelSubset() { - delete image; } void LevelSubset::create(const std::string& subset_name) @@ -42,112 +50,74 @@ new_subset.title = "Unknown Title"; new_subset.description = "No description so far."; new_subset.save(); - //new_lev.save(subset_name, 1, 0); } -void LevelSubset::parse (lisp_object_t* cursor) +void LevelSubset::read_info_file(const std::string& info_file) { - while(!lisp_nil_p(cursor)) + lisp_object_t* root_obj = lisp_read_from_file(info_file); + lisp_object_t* cur = lisp_car(root_obj); + + if (lisp_symbol_p(cur) && strcmp(lisp_symbol(cur), "supertux-level-subset") == 0) { - lisp_object_t* cur = lisp_car(cursor); - char *s; + LispReader reader(lisp_cdr(root_obj)); - if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur))) - { - printf("Not good"); - } - else - { - if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0) - { - if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL) - { - title = s; - } - } - else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0) - { - if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL) - { - description = s; - } - } - } - cursor = lisp_cdr (cursor); + reader.read_string("title", title); + reader.read_string("description", description); + reader.read_string_vector("levels", levels); } + else + { + std::cout << "LevelSubset: parse error in info file: " << info_file << std::endl; + } + + lisp_free(root_obj); } void LevelSubset::load(const char* subset) { - FILE* fi; - char filename[1024]; - char str[1024]; - int i; - lisp_object_t* root_obj = 0; - name = subset; - - snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset); - if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset); - if(faccessible(filename)) + + // Check in which directory our subset is located (ie. ~/.supertux/ + // or SUPERTUX_DATADIR) + char filename[1024]; + snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset); + if (access(filename, R_OK) == 0) { - fi = fopen(filename, "r"); - if (fi == NULL) - { - perror(filename); - } - lisp_stream_t stream; - lisp_stream_init_file (&stream, fi); - root_obj = lisp_read (&stream); - - if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) - { - printf("World: Parse Error in file %s", filename); - } - - lisp_object_t* cur = lisp_car(root_obj); - - if (!lisp_symbol_p (cur)) - { - printf("World: Read error in %s",filename); - } - - if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0) - { - parse(lisp_cdr(root_obj)); - - } - - lisp_free(root_obj); - fclose(fi); + directory = filename; + } + else + { + snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset); + if (access(filename, R_OK) == 0) + directory = filename; + else + std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl; + } + + read_info_file(directory + "info"); - snprintf(str, 1024, "%s.png", filename); - if(faccessible(str)) + if (levels.empty()) + { // Level info file doesn't define any levels, so read the + // directory to see what we can find + std::vector<std::string> files; + + snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset); + if(access(filename, R_OK) == 0) { - delete image; - image = new Surface(str,IGNORE_ALPHA); + files = read_directory(filename); } else { - snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str()); - delete image; - image = new Surface(filename,IGNORE_ALPHA); + snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset); + files = read_directory(filename); } - } - - for(i=1; i != -1; ++i) - { - /* Get the number of levels in this subset */ - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i); - if(!faccessible(filename)) + + for(std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) { - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i); - if(!faccessible(filename)) - break; + if (has_suffix(*i, ".stl")) + levels.push_back(*i); } } - levels = --i; } void @@ -172,7 +142,7 @@ } /* Write header: */ - fprintf(fi,";SuperTux-Level-Subset\n"); + fprintf(fi,";; SuperTux-Level-Subset\n"); fprintf(fi,"(supertux-level-subset\n"); /* Save title info: */ @@ -186,20 +156,24 @@ } } +void +LevelSubset::add_level(const std::string& name) +{ + levels.push_back(name); +} + std::string LevelSubset::get_level_filename(unsigned int num) { - char filename[1024]; - - // Load data file: - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, - name.c_str(), num); - if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), - name.c_str(), num); + assert(num < levels.size()); - return std::string(filename); + return directory + levels[num]; } -/* EOF */ +int +LevelSubset::get_num_levels() const +{ + return levels.size(); +} +/* EOF */ Index: setup.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/setup.h,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- setup.h 28 Apr 2004 13:18:54 -0000 1.19 +++ setup.h 16 Jun 2004 00:40:42 -0000 1.20 @@ -20,6 +20,8 @@ #ifndef SUPERTUX_SETUP_H #define SUPERTUX_SETUP_H +#include <vector> +#include <string> #include "menu.h" #include "sound.h" #include "type.h" @@ -27,6 +29,8 @@ int faccessible(const char *filename); int fcreatedir(const char* relative_dir); int fwriteable(const char *filename); +std::vector<std::string> read_directory(const std::string& pathname); + FILE * opendata(const char * filename, const char * mode); string_list_type dsubdirs(const char *rel_path, const char* expected_file); string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str); Index: level_subset.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level_subset.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- level_subset.h 15 Jun 2004 22:56:42 -0000 1.1 +++ level_subset.h 16 Jun 2004 00:40:42 -0000 1.2 @@ -21,6 +21,7 @@ #ifndef SUPERTUX_LEVEL_SUBSET_H #define SUPERTUX_LEVEL_SUBSET_H +#include <vector> #include <string> #include "lispreader.h" @@ -30,6 +31,14 @@ It could be extended to handle manipulation of subsets. */ class LevelSubset { +private: + /** Directory in which the level subset is stored */ + std::string directory; + + /** Level filenames without the leading path ("level1.stl", + "level3.stl", ...) */ + std::vector<std::string> levels; + public: LevelSubset(); ~LevelSubset(); @@ -38,16 +47,18 @@ void load(const char* subset); void save(); + void add_level(const std::string& name); + std::string get_level_filename(unsigned int i); + int get_num_levels() const; std::string name; std::string title; std::string description; Surface* image; - int levels; - + private: - void parse(lisp_object_t* cursor); + void read_info_file(const std::string& info_file); }; #endif Index: level.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level.h,v retrieving revision 1.58 retrieving revision 1.59 diff -u -d -r1.58 -r1.59 --- level.h 14 Jun 2004 22:45:23 -0000 1.58 +++ level.h 16 Jun 2004 00:40:42 -0000 1.59 @@ -24,37 +24,8 @@ #include <map> #include <string> -#include "screen/surface.h" -#include "lispreader.h" -#include "musicref.h" - -class Tile; - -/** This type holds meta-information about a level-subset. - It could be extended to handle manipulation of subsets. */ -class LevelSubset -{ -public: - LevelSubset(); - ~LevelSubset(); - - static void create(const std::string& subset_name); - void load(const char* subset); - void save(); - - std::string get_level_filename(unsigned int i); - - std::string name; - std::string title; - std::string description; - Surface* image; - int levels; - -private: - void parse(lisp_object_t* cursor); -}; - class Sector; +class LispReader; class Level { Index: level.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/level.cpp,v retrieving revision 1.92 retrieving revision 1.93 diff -u -d -r1.92 -r1.93 --- level.cpp 9 Jun 2004 19:56:00 -0000 1.92 +++ level.cpp 16 Jun 2004 00:40:41 -0000 1.93 @@ -42,185 +42,6 @@ using namespace std; -LevelSubset::LevelSubset() - : image(0), levels(0) -{ -} - -LevelSubset::~LevelSubset() -{ - delete image; -} - -void LevelSubset::create(const std::string& subset_name) -{ - Level new_lev; - LevelSubset new_subset; - new_subset.name = subset_name; - new_subset.title = "Unknown Title"; - new_subset.description = "No description so far."; - new_subset.save(); - //new_lev.save(subset_name, 1, 0); -} - -void LevelSubset::parse (lisp_object_t* cursor) -{ - while(!lisp_nil_p(cursor)) - { - lisp_object_t* cur = lisp_car(cursor); - char *s; - - if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur))) - { - printf("Not good"); - } - else - { - if (strcmp(lisp_symbol(lisp_car(cur)), "title") == 0) - { - if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL) - { - title = s; - } - } - else if (strcmp(lisp_symbol(lisp_car(cur)), "description") == 0) - { - if(( s = lisp_string(lisp_car(lisp_cdr(cur)))) != NULL) - { - description = s; - } - } - } - cursor = lisp_cdr (cursor); - } -} - -void LevelSubset::load(const char* subset) -{ - FILE* fi; - char filename[1024]; - char str[1024]; - int i; - lisp_object_t* root_obj = 0; - - name = subset; - - snprintf(filename, 1024, "%s/levels/%s/info", st_dir, subset); - if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/info", datadir.c_str(), subset); - if(faccessible(filename)) - { - fi = fopen(filename, "r"); - if (fi == NULL) - { - perror(filename); - } - lisp_stream_t stream; - lisp_stream_init_file (&stream, fi); - root_obj = lisp_read (&stream); - - if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) - { - printf("World: Parse Error in file %s", filename); - } - - lisp_object_t* cur = lisp_car(root_obj); - - if (!lisp_symbol_p (cur)) - { - printf("World: Read error in %s",filename); - } - - if (strcmp(lisp_symbol(cur), "supertux-level-subset") == 0) - { - parse(lisp_cdr(root_obj)); - - } - - lisp_free(root_obj); - fclose(fi); - - snprintf(str, 1024, "%s.png", filename); - if(faccessible(str)) - { - delete image; - image = new Surface(str,IGNORE_ALPHA); - } - else - { - snprintf(filename, 1024, "%s/images/status/level-subset-info.png", datadir.c_str()); - delete image; - image = new Surface(filename,IGNORE_ALPHA); - } - } - - for(i=1; i != -1; ++i) - { - /* Get the number of levels in this subset */ - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, subset,i); - if(!faccessible(filename)) - { - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), subset,i); - if(!faccessible(filename)) - break; - } - } - levels = --i; -} - -void -LevelSubset::save() -{ - FILE* fi; - string filename; - - /* Save data file: */ - filename = "/levels/" + name + "/"; - - fcreatedir(filename.c_str()); - filename = string(st_dir) + "/levels/" + name + "/info"; - if(!fwriteable(filename.c_str())) - filename = datadir + "/levels/" + name + "/info"; - if(fwriteable(filename.c_str())) - { - fi = fopen(filename.c_str(), "w"); - if (fi == NULL) - { - perror(filename.c_str()); - } - - /* Write header: */ - fprintf(fi,";SuperTux-Level-Subset\n"); - fprintf(fi,"(supertux-level-subset\n"); - - /* Save title info: */ - fprintf(fi," (title \"%s\")\n", title.c_str()); - - /* Save the description: */ - fprintf(fi," (description \"%s\")\n", description.c_str()); - - fprintf( fi,")"); - fclose(fi); - } -} - -std::string -LevelSubset::get_level_filename(unsigned int num) -{ - char filename[1024]; - - // Load data file: - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", st_dir, - name.c_str(), num); - if(!faccessible(filename)) - snprintf(filename, 1024, "%s/levels/%s/level%d.stl", datadir.c_str(), - name.c_str(), num); - - return std::string(filename); -} - -//--------------------------------------------------------------------------- - Level::Level() : name("noname"), author("mr. x"), time_left(500) { Index: setup.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/setup.cpp,v retrieving revision 1.100 retrieving revision 1.101 diff -u -d -r1.100 -r1.101 --- setup.cpp 15 Jun 2004 22:38:52 -0000 1.100 +++ setup.cpp 16 Jun 2004 00:40:42 -0000 1.101 @@ -1112,3 +1112,24 @@ exit(ret); } +std::vector<std::string> read_directory(const std::string& pathname) +{ + std::vector<std::string> dirnames; + + DIR* dir = opendir(pathname.c_str()); + if (dir) + { + struct dirent *direntp; + + while((direntp = readdir(dir))) + { + dirnames.push_back(direntp->d_name); + } + + closedir(dir); + } + + return dirnames; +} + +/* EOF */ Index: leveleditor.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/leveleditor.cpp,v retrieving revision 1.139 retrieving revision 1.140 diff -u -d -r1.139 -r1.140 --- leveleditor.cpp 14 Jun 2004 22:45:23 -0000 1.139 +++ leveleditor.cpp 16 Jun 2004 00:40:42 -0000 1.140 @@ -822,8 +822,8 @@ le_object_properties_bt->draw(context); } - sprintf(str, "%d/%d", le_levelnb,le_level_subset->levels); - context.draw_text(white_text, str, Vector((le_level_subset->levels < 10) ? -10 : 0, 16), LAYER_GUI); + sprintf(str, "%d/%d", le_levelnb, le_level_subset->get_num_levels()); + context.draw_text(white_text, str, Vector((le_level_subset->get_num_levels() < 10) ? -10 : 0, 16), LAYER_GUI); if(!le_help_shown) context.draw_text(white_small_text, "F1 for Help", Vector(10, 430), LAYER_GUI); @@ -1153,7 +1153,7 @@ le_next_level_bt->event(event); if(le_next_level_bt->get_state() == BUTTON_CLICKED) { - if(le_levelnb < le_level_subset->levels) + if(le_levelnb < le_level_subset->get_num_levels()) { goto_level(le_levelnb+1); } @@ -1165,8 +1165,8 @@ Surface* surf = new Surface(le_level->get_sector("main")->background->get_image(), false); if(confirm_dialog(surf, str)) { - new_lev.save(le_level_subset->name.c_str()); - le_level_subset->levels = le_levelnb; + le_level_subset->add_level("newlevel.stl"); + new_lev.save(le_level_subset->get_level_filename(le_levelnb+1)); goto_level(le_levelnb); } if(surf != NULL) Index: title.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/title.cpp,v retrieving revision 1.97 retrieving revision 1.98 diff -u -d -r1.97 -r1.98 --- title.cpp 14 Jun 2004 22:45:23 -0000 1.97 +++ title.cpp 16 Jun 2004 00:40:42 -0000 1.98 @@ -129,7 +129,7 @@ contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0); contrib_subset_menu->additem(MN_HL,"",0,0); - for (int i = 1; i <= subset.levels; ++i) + for (int i = 0; i < subset.get_num_levels(); ++i) { Level* level = new Level; level->load(subset.get_level_filename(i)); Index: Makefile.am =================================================================== RCS file: /cvsroot/super-tux/supertux/src/Makefile.am,v retrieving revision 1.36 retrieving revision 1.37 diff -u -d -r1.36 -r1.37 --- Makefile.am 14 Jun 2004 22:45:23 -0000 1.36 +++ Makefile.am 16 Jun 2004 00:40:41 -0000 1.37 @@ -44,6 +44,8 @@ interactive_object.h \ level.cpp \ level.h \ +level_subset.cpp \ +level_subset.h \ leveleditor.cpp \ leveleditor.h \ lispreader.cpp \ Index: leveleditor.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/leveleditor.h,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- leveleditor.h 14 Jun 2004 22:45:23 -0000 1.14 +++ leveleditor.h 16 Jun 2004 00:40:42 -0000 1.15 @@ -27,6 +27,8 @@ #include "game_object.h" #include "screen/surface.h" #include "level.h" +#include "level_subset.h" +#include "moving_object.h" #include "button.h" #include "menu.h" |