X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Ftile_manager.cpp;h=88ec579863114083827987c235e48f90c5266c53;hb=02d54bd21d3d72e46ecad8f6038666b0384d4eb8;hp=49336e27b4c28f173a7d08d68cf7960c8da4dd55;hpb=700524117b504a0b8aff31a128273501dae3b693;p=supertux.git diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index 49336e27b..88ec57986 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -17,6 +17,7 @@ // 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 "video/drawing_context.h" @@ -28,7 +29,6 @@ #include "scene.h" TileManager* TileManager::instance_ = 0; -std::set* TileManager::tilegroups_ = 0; TileManager::TileManager() { @@ -39,9 +39,7 @@ TileManager::TileManager() TileManager::~TileManager() { for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) - delete i->second; - - delete tilegroups_; + delete *i; } void TileManager::load_tileset(std::string filename) @@ -51,7 +49,7 @@ void TileManager::load_tileset(std::string filename) // free old tiles for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) - delete i->second; + delete *i; tiles.clear(); lisp_object_t* root_obj = lisp_read_from_file(filename); @@ -59,110 +57,59 @@ void TileManager::load_tileset(std::string filename) if (!root_obj) Termination::abort("Couldn't load file", filename); - if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0) - { - lisp_object_t* cur = lisp_cdr(root_obj); - int tileset_id = 0; + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") != 0) + assert(false); - while(!lisp_nil_p(cur)) - { - lisp_object_t* element = lisp_car(cur); + lisp_object_t* cur = lisp_cdr(root_obj); + int tileset_id = 0; - if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0) - { - LispReader reader(lisp_cdr(element)); + while(!lisp_nil_p(cur)) { + lisp_object_t* element = lisp_car(cur); - Tile* tile = new Tile; - int tile_id = tile->read(reader); -/* if(tile_id < 0) { - std::cerr - << "Warning: parse error when reading a tile (id < 0), skipping.\n"; - continue; - }*/ + if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0) + { + LispReader reader(lisp_cdr(element)); - tiles[tile_id] = tile; - } - else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0) - { - LispReader reader(lisp_cdr(element)); - std::string filename; - reader.read_string("file", filename); - filename = datadir + "/images/tilesets/" + filename; - load_tileset(filename); - } - else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0) - { - TileGroup new_; - LispReader reader(lisp_cdr(element)); - reader.read_string("name", new_.name); - reader.read_int_vector("tiles", new_.tiles); - if(!tilegroups_) - tilegroups_ = new std::set; - tilegroups_->insert(new_).first; - } - else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0) - { - LispReader reader(lisp_cdr(element)); - reader.read_int("id", tileset_id); - tileset_id *= 1000; - } - else - { - std::cerr << "Unknown symbol: " << - lisp_symbol(lisp_car(element)) << "\n"; - } + Tile* tile = new Tile; + tile->read(reader); - cur = lisp_cdr(cur); + while(tile->id >= tiles.size()) { + tiles.push_back(0); } - } - else - { - assert(0); - } + tiles[tile->id] = tile; + } + else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0) + { + LispReader reader(lisp_cdr(element)); + std::string filename; + reader.read_string("file", filename); + filename = datadir + "/images/tilesets/" + filename; + load_tileset(filename); + } + else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0) + { + TileGroup new_; + LispReader reader(lisp_cdr(element)); + reader.read_string("name", new_.name); + reader.read_int_vector("tiles", new_.tiles); + tilegroups.insert(new_).first; + } + else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0) + { + LispReader reader(lisp_cdr(element)); + reader.read_int("id", tileset_id); + tileset_id *= 1000; + } + else + { + std::cerr << "Unknown symbol: " << + lisp_symbol(lisp_car(element)) << "\n"; + } + + cur = lisp_cdr(cur); + } lisp_free(root_obj); current_tileset = filename; } -void -TileManager::draw_tile(DrawingContext& context, unsigned int c, - const Vector& pos, int layer) -{ - if(c == 0) - return; - - Tile* tile = get(c); - - if(!tile->images.size()) - return; - - 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); - } - else if (tile->images.size() == 1) - { - 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; -} - -/* EOF */