From: Ricardo Cruz Date: Thu, 5 Aug 2004 18:58:08 +0000 (+0000) Subject: Replaced the use of a vector for the tiles placement by a map one. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=9f43687076203b3769cd2b9a499d1fb57cd8c40f;p=supertux.git 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. SVN-Revision: 1711 --- diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index 790aa0fa7..daa38d298 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -496,7 +496,7 @@ void LevelEditor::init_menus() for(std::vector::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 @@ void LevelEditor::init_menus() 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); diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index 7d6841a10..fe28d495e 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -38,9 +38,8 @@ TileManager::TileManager() TileManager::~TileManager() { - for(std::vector::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 @@ void TileManager::load_tileset(std::string filename) return; // free old tiles - for(std::vector::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 @@ void TileManager::load_tileset(std::string filename) 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 @@ TileManager::draw_tile(DrawingContext& context, unsigned int c, 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) + { + context.draw_surface(tile->images[0], pos, layer); } - else if (tile.images.size() == 1) +} + +Tile* +TileManager::get(unsigned int id) +{ +Tiles::iterator i = tiles.find(id); + +if(i == tiles.end()) { - context.draw_surface(tile.images[0], pos, layer); + 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 */ diff --git a/src/tile_manager.h b/src/tile_manager.h index a72777b13..67c751222 100644 --- a/src/tile_manager.h +++ b/src/tile_manager.h @@ -24,6 +24,7 @@ #include #include #include +#include class Tile; @@ -44,7 +45,10 @@ class TileManager TileManager(); ~TileManager(); - std::vector tiles; +// std::vector tiles; + typedef std::map Tiles; + Tiles tiles; + static TileManager* instance_ ; static std::set* tilegroups_; void load_tileset(std::string filename); @@ -65,20 +69,7 @@ class TileManager 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 diff --git a/src/tilemap.cpp b/src/tilemap.cpp index 4b7e1239e..a50bee38a 100644 --- a/src/tilemap.cpp +++ b/src/tilemap.cpp @@ -263,9 +263,9 @@ Tile* 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*