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];
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);
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_;
}
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);
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)
{
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 */
#include <set>
#include <vector>
#include <string>
+#include <map>
class Tile;
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);
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