3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "video/drawing_context.h"
31 #include "tile_manager.h"
32 #include "app/globals.h"
33 #include "utils/lispreader.h"
34 #include "utils/lispwriter.h"
37 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES),
40 tilemanager = TileManager::instance();
46 TileMap::TileMap(LispReader& reader)
47 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES),
50 tilemanager = TileManager::instance();
52 std::string layer_str;
53 if(reader.read_string("layer", layer_str)) {
54 if(layer_str == "background")
55 layer = LAYER_BACKGROUNDTILES;
56 else if(layer_str == "interactive")
58 else if(layer_str == "foreground")
59 layer = LAYER_FOREGROUNDTILES;
61 std::cerr << "Unknown layer '" << layer_str << "' in tilemap.\n";
64 reader.read_bool("solid", solid);
65 reader.read_float("speed", speed);
67 if(solid && speed != 1) {
68 std::cout << "Speed of solid tilemap is not 1. fixing.\n";
74 if(!reader.read_int("width", width) ||
75 !reader.read_int("height", height))
76 throw std::runtime_error("No width or height specified in tilemap.");
78 if(!reader.read_int_vector("tiles", tiles))
79 throw std::runtime_error("No tiles in tilemap.");
81 if(int(tiles.size()) != width*height)
82 throw std::runtime_error("wrong number of tiles in tilemap.");
85 TileMap::TileMap(int layer_, bool solid_, size_t width_, size_t height_)
86 : solid(solid_), speed(1), width(0), height(0), layer(layer_),
89 tilemanager = TileManager::instance();
91 resize(width_, height_);
102 TileMap::write(LispWriter& writer)
104 writer.start_list("tilemap");
106 if(layer == LAYER_BACKGROUNDTILES)
107 writer.write_string("layer", "background");
108 else if(layer == LAYER_TILES)
109 writer.write_string("layer", "interactive");
110 else if(layer == LAYER_FOREGROUNDTILES)
111 writer.write_string("layer", "foreground");
113 writer.write_string("layer", "unknown");
114 std::cerr << "Warning unknown layer in tilemap.\n";
117 writer.write_bool("solid", solid);
118 writer.write_float("speed", speed);
119 writer.write_int("width", width);
120 writer.write_int("height", height);
121 writer.write_int_vector("tiles", tiles);
123 writer.end_list("tilemap");
127 TileMap::action(float )
132 TileMap::draw(DrawingContext& context)
134 context.push_transform();
137 context.set_drawing_effect(VERTICAL_FLIP);
138 float trans_x = roundf(context.get_translation().x);
139 float trans_y = roundf(context.get_translation().y);
140 context.set_translation(Vector(trans_x * speed, trans_y * speed));
142 /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
143 * I have no idea why */
144 float start_x = roundf(context.get_translation().x);
145 float start_y = roundf(context.get_translation().y);
146 float end_x = std::min(start_x + screen->w, float(width * 32));
147 float end_y = std::min(start_y + screen->h, float(height * 32));
148 start_x -= int(start_x) % 32;
149 start_y -= int(start_y) % 32;
150 int tsx = int(start_x / 32); // tilestartindex x
151 int tsy = int(start_y / 32); // tilestartindex y
155 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
156 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
157 const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
159 tile->draw(context, pos, layer);
163 context.pop_transform();
167 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
168 int newlayer, bool newsolid)
170 assert(int(newt.size()) == newwidth * newheight);
175 tiles.resize(newt.size());
185 TileMap::resize(int new_width, int new_height)
187 if(new_width < width) {
188 // remap tiles for new width
189 for(int y = 0; y < height && y < new_height; ++y) {
190 for(int x = 0; x < new_width; ++x) {
191 tiles[y * new_width + x] = tiles[y * width + x];
196 tiles.resize(new_width * new_height);
198 if(new_width > width) {
200 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
201 for(int x = new_width-1; x >= 0; --x) {
203 tiles[y * new_width + x] = 0;
207 tiles[y * new_width + x] = tiles[y * width + x];
217 TileMap::do_vertical_flip()
219 // remap tiles vertically flipped
220 for(int y = 0; y < height / 2; ++y) {
221 for(int x = 0; x < width; ++x) {
222 std::swap(tiles[y*width + x], tiles[(((height-1)*width) - (y*width)) + x]);
226 vertical_flip = true;
230 TileMap::get_tile(int x, int y) const
232 if(x < 0 || x >= width || y < 0 || y >= height) {
234 //std::cout << "Warning: tile outside tilemap requested!\n";
236 return tilemanager->get(0);
239 return tilemanager->get(tiles[y*width + x]);
243 TileMap::get_tile_at(const Vector& pos) const
245 return get_tile(int(pos.x)/32, int(pos.y)/32);
249 TileMap::change(int x, int y, uint32_t newtile)
251 assert(x >= 0 && x < width && y >= 0 && y < height);
252 tiles[y*width + x] = newtile;
256 TileMap::change_at(const Vector& pos, uint32_t newtile)
258 change(int(pos.x)/32, int(pos.y)/32, newtile);