[Super-tux-commit] supertux/src leveleditor.cpp,1.146,1.147 tile_manager.cpp,1.3,1.4 tile_manager.h,
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-08-05 18:58:33
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32048/src Modified Files: leveleditor.cpp tile_manager.cpp tile_manager.h tilemap.cpp Log Message: Replaced the use of a vector for the tiles placement by a map one. This way, empty tiles are not created, and so, we can now have the organization I posted on the ml. Index: tile_manager.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/tile_manager.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- tile_manager.h 28 Jun 2004 11:30:27 -0000 1.2 +++ tile_manager.h 5 Aug 2004 18:58:08 -0000 1.3 @@ -24,6 +24,7 @@ #include <set> #include <vector> #include <string> +#include <map> class Tile; @@ -44,7 +45,10 @@ TileManager(); ~TileManager(); - std::vector<Tile*> tiles; +// std::vector<Tile*> tiles; + typedef std::map<int, Tile*> Tiles; + Tiles tiles; + static TileManager* instance_ ; static std::set<TileGroup>* tilegroups_; void load_tileset(std::string filename); @@ -65,20 +69,7 @@ unsigned int total_ids() { return tiles.size(); } - Tile& get(unsigned int id) { - - if(id < tiles.size()) - { - return *tiles[id]; - } - else - { - // Never return 0, but return the 0th tile instead so that - // user code doesn't have to check for NULL pointers all over - // the place - return *tiles[0]; - } - } + Tile* get(unsigned int id); }; #endif Index: leveleditor.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/leveleditor.cpp,v retrieving revision 1.146 retrieving revision 1.147 diff -u -d -r1.146 -r1.147 --- leveleditor.cpp 28 Jul 2004 23:06:12 -0000 1.146 +++ leveleditor.cpp 5 Aug 2004 18:58:08 -0000 1.147 @@ -496,7 +496,7 @@ for(std::vector<int>::const_iterator sit = (*it).tiles.begin(); sit != (*it).tiles.end(); ++sit, ++i) { - Tile& tile = TileManager::instance()->get(*sit); +/* Tile& tile = TileManager::instance()->get(*sit); Surface* image; if(tile.editor_images.size() > 0) image = tile.editor_images[0]; @@ -508,7 +508,7 @@ Button* button = new Button(image, it->name, SDLKey(SDLK_a + i), 0, 0, 32, 32); - tilegroups_map[it->name]->additem(button, *sit); + tilegroups_map[it->name]->additem(button, *sit);*/ } } select_tilegroup_menu->additem(MN_HL,"",0,0); Index: tilemap.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/tilemap.cpp,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- tilemap.cpp 20 Jul 2004 18:04:48 -0000 1.17 +++ tilemap.cpp 5 Aug 2004 18:58:08 -0000 1.18 @@ -263,9 +263,9 @@ TileMap::get_tile(int x, int y) const { if(x < 0 || x >= width || y < 0 || y >= height) - return &tilemanager->get(0); + return tilemanager->get(0); - return &tilemanager->get(tiles[y*width + x].id); + return tilemanager->get(tiles[y*width + x].id); } Tile* Index: tile_manager.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/tile_manager.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- tile_manager.cpp 25 Jul 2004 19:03:36 -0000 1.3 +++ tile_manager.cpp 5 Aug 2004 18:58:08 -0000 1.4 @@ -38,9 +38,8 @@ TileManager::~TileManager() { - for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) { - delete *i; - } + for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) + delete i->second; delete tilegroups_; } @@ -51,9 +50,8 @@ return; // free old tiles - for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) { - delete *i; - } + for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) + delete i->second; tiles.clear(); lisp_object_t* root_obj = lisp_read_from_file(filename); @@ -78,15 +76,17 @@ int tile_id = tile->read(reader); if(tile_id < 0) { std::cerr - << "Warning: parse error when reading a tile, skipping.\n"; + << "Warning: parse error when reading a tile (id < 0), skipping.\n"; continue; } - tile_id += tileset_id; + tiles.insert(std::make_pair(tile_id, tile)); + +/* tile_id += tileset_id; if(tile_id >= int(tiles.size())) tiles.resize(tile_id+1); - tiles[tile_id] = tile; + tiles[tile_id] = tile;*/ } else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0) { @@ -137,21 +137,51 @@ if(c == 0) return; - Tile& tile = get(c); + Tile* tile = get(c); - if(!tile.images.size()) + if(!tile->images.size()) return; - if(tile.images.size() > 1) + if(tile->images.size() > 1) { size_t frame - = ((global_frame_counter*25) / tile.anim_speed) % tile.images.size(); - context.draw_surface(tile.images[frame], pos, layer); + = ((global_frame_counter*25) / tile->anim_speed) % tile->images.size(); + context.draw_surface(tile->images[frame], pos, layer); } - else if (tile.images.size() == 1) + else if (tile->images.size() == 1) { - context.draw_surface(tile.images[0], pos, layer); + context.draw_surface(tile->images[0], pos, layer); } } +Tile* +TileManager::get(unsigned int id) +{ +Tiles::iterator i = tiles.find(id); + +if(i == tiles.end()) + { + std::cerr << "Warning: Asked for a non-existing tile id. Ignoring.\n"; + // Never return 0, but return the first tile instead so that + // user code doesn't have to check for NULL pointers all over + // the place + i = tiles.begin(); + return i->second; + } +return i->second; + +/* + if(id < tiles.size()) + { + return *tiles[id]; + } + else + { + // Never return 0, but return the 0th tile instead so that + // user code doesn't have to check for NULL pointers all over + // the place + return *tiles[0]; + }*/ +} + /* EOF */ |