- /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
- * I have no idea why */
- float start_x = roundf(context.get_translation().x);
- float start_y = roundf(context.get_translation().y);
- float end_x = std::min(start_x + screen->w, float(level->width * 32));
- float end_y = std::min(start_y + screen->h, float(level->height * 32));
- start_x -= int(start_x) % 32;
- start_y -= int(start_y) % 32;
- int tsx = int(start_x / 32); // tilestartindex x
- int tsy = int(start_y / 32); // tilestartindex y
-
- Vector pos;
- int tx, ty;
- for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
- for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
- tilemanager->draw_tile(context, tiles[ty*level->width + tx], pos, layer);
+ if (speed == 1.0)
+ {
+ if(vertical_flip) // flip vertically the tiles, in case we are playing this
+ { // level upside down
+ context.push_transform();
+ context.set_drawing_effect(VERTICAL_FLIP);
+ }
+
+ /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
+ * I have no idea why */
+ float start_x = roundf(context.get_translation().x);
+ float start_y = roundf(context.get_translation().y);
+ float end_x = std::min(start_x + screen->w, float(width * 32));
+ float end_y = std::min(start_y + screen->h, float(height * 32));
+ start_x -= int(start_x) % 32;
+ start_y -= int(start_y) % 32;
+ int tsx = int(start_x / 32); // tilestartindex x
+ int tsy = int(start_y / 32); // tilestartindex y
+
+ Vector pos;
+ int tx, ty;
+ for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
+ for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
+ if(tx < 0 || tx > width || ty < 0 || ty > height)
+ continue; // outside tilemap
+ if (!tiles[ty*width + tx].hidden)
+ tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
+ }
+ }
+
+ if(vertical_flip) // disable flipping, if applied
+ context.pop_transform();
+ }
+ else
+ {
+ float trans_x = roundf(context.get_translation().x);
+ float trans_y = roundf(context.get_translation().y);
+
+ context.push_transform();
+ context.set_translation(Vector(trans_x * speed, trans_y * speed));
+ if(vertical_flip)
+ context.set_drawing_effect(VERTICAL_FLIP);
+
+ float start_x = roundf(context.get_translation().x);
+ float start_y = roundf(context.get_translation().y);
+ float end_x = std::min(start_x + screen->w, float(width * 32));
+ float end_y = std::min(start_y + screen->h, float(height * 32));
+ start_x -= int(start_x) % 32;
+ start_y -= int(start_y) % 32;
+ int tsx = int(start_x / 32); // tilestartindex x
+ int tsy = int(start_y / 32); // tilestartindex y
+
+ Vector pos;
+ int tx, ty;
+ for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
+ for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
+ if(tx < 0 || tx > width || ty < 0 || ty > height)
+ continue; // outside tilemap
+ if (!tiles[ty*width + tx].hidden)
+ tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
+ }
+ }
+
+ 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());
+ for(unsigned int i = 0; i < newt.size(); ++i)
+ {
+ tiles[i].hidden = false;
+ tiles[i].id = newt[i];
+ }
+
+ layer = newlayer;
+ solid = newsolid;
+}
+
+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] = TileId();
+ } else {
+ tiles[y * new_width + x] = tiles[y * width + x];
+ }
+ }