+
+ context.pop_transform();
+}
+
+void
+TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
+ int newlayer, bool newsolid)
+{
+ assert(int(newt.size()) == newwidth * newheight);
+
+ width = newwidth;
+ height = newheight;
+
+ tiles.resize(newt.size());
+ tiles = newt;
+
+ layer = newlayer;
+ solid = newsolid;
+ if(solid)
+ flags |= FLAG_SOLID;
+}
+
+void
+TileMap::resize(int new_width, int new_height)
+{
+ if(new_width < width) {
+ // remap tiles for new width
+ for(int y = 0; y < height && y < new_height; ++y) {
+ for(int x = 0; x < new_width; ++x) {
+ tiles[y * new_width + x] = tiles[y * width + x];
+ }
+ }
+ }
+
+ tiles.resize(new_width * new_height);
+
+ if(new_width > width) {
+ // remap tiles
+ for(int y = std::min(height, new_height)-1; y >= 0; --y) {
+ for(int x = new_width-1; x >= 0; --x) {
+ if(x >= width) {
+ tiles[y * new_width + x] = 0;
+ continue;
+ }
+
+ tiles[y * new_width + x] = tiles[y * width + x];
+ }
+ }
+ }
+
+ height = new_height;
+ width = new_width;
+}
+
+void
+TileMap::do_vertical_flip()
+{
+ // remap tiles vertically flipped
+ for(int y = 0; y < height / 2; ++y) {
+ for(int x = 0; x < width; ++x) {
+ std::swap(tiles[y*width + x], tiles[(((height-1)*width) - (y*width)) + x]);
+ }
+ }
+
+ vertical_flip = true;
+}
+
+const Tile*
+TileMap::get_tile(int x, int y) const
+{
+ if(x < 0 || x >= width || y < 0 || y >= height) {
+#ifdef DEBUG
+ //std::cout << "Warning: tile outside tilemap requested!\n";
+#endif
+ return tilemanager->get(0);
+ }
+
+ return tilemanager->get(tiles[y*width + x]);
+}
+
+const Tile*
+TileMap::get_tile_at(const Vector& pos) const
+{
+ return get_tile(int(pos.x)/32, int(pos.y)/32);
+}
+
+void
+TileMap::change(int x, int y, uint32_t newtile)
+{
+ assert(x >= 0 && x < width && y >= 0 && y < height);
+ tiles[y*width + x] = newtile;
+}
+
+void
+TileMap::change_at(const Vector& pos, uint32_t newtile)
+{
+ change(int(pos.x)/32, int(pos.y)/32, newtile);