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.
27 #include "screen/drawing_context.h"
30 #include "tile_manager.h"
32 #include "lispreader.h"
33 #include "lispwriter.h"
36 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
38 tilemanager = TileManager::instance();
41 TileMap::TileMap(LispReader& reader)
42 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
44 tilemanager = TileManager::instance();
46 std::string layer_str;
47 if(reader.read_string("layer", layer_str)) {
48 if(layer_str == "background")
49 layer = LAYER_BACKGROUNDTILES;
50 else if(layer_str == "interactive")
52 else if(layer_str == "foreground")
53 layer = LAYER_FOREGROUNDTILES;
55 std::cerr << "Unknown layer '" << layer_str << "' in tilemap.\n";
58 reader.read_bool("solid", solid);
59 reader.read_float("speed", speed);
61 if(solid && speed != 1) {
62 std::cout << "Speed of solid tilemap is not 1. fixing.\n";
66 if(!reader.read_int("width", width) ||
67 !reader.read_int("height", height))
68 throw std::runtime_error("No width or height specified in tilemap.");
70 std::vector<unsigned int> tmp_tiles;
72 if(!reader.read_int_vector("tiles", tmp_tiles))
73 throw std::runtime_error("No tiles in tilemap.");
75 tiles.resize(tmp_tiles.size());
76 for(unsigned int i = 0; i < tmp_tiles.size(); ++i)
78 tiles[i].hidden = false;
79 tiles[i].id = tmp_tiles[i];
82 if(int(tiles.size()) != width*height)
83 throw std::runtime_error("wrong number of tiles in tilemap.");
86 TileMap::TileMap(int layer_, bool solid_, size_t width_, size_t height_)
87 : solid(solid_), speed(1), width(width_), height(height_), layer(layer_)
96 TileMap::write(LispWriter& writer)
98 writer.start_list("tilemap");
100 if(layer == LAYER_BACKGROUNDTILES)
101 writer.write_string("layer", "background");
102 else if(layer == LAYER_TILES)
103 writer.write_string("layer", "interactive");
104 else if(layer == LAYER_FOREGROUNDTILES)
105 writer.write_string("layer", "foreground");
107 writer.write_string("layer", "unknown");
108 std::cerr << "Warning unknown layer in tilemap.\n";
111 writer.write_bool("solid", solid);
112 writer.write_float("speed", speed);
113 writer.write_int("width", width);
114 writer.write_int("height", height);
116 std::vector<unsigned int> tmp_tiles;
117 tmp_tiles.resize(tiles.size());
118 for(unsigned int i = 0; i < tmp_tiles.size(); ++i)
120 tmp_tiles[i] = tiles[i].id;
122 writer.write_int_vector("tiles", tmp_tiles);
124 writer.end_list("tilemap");
128 TileMap::action(float )
133 TileMap::draw(DrawingContext& context)
137 /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
138 * I have no idea why */
139 float start_x = roundf(context.get_translation().x);
140 float start_y = roundf(context.get_translation().y);
141 float end_x = std::min(start_x + screen->w, float(width * 32));
142 float end_y = std::min(start_y + screen->h, float(height * 32));
143 start_x -= int(start_x) % 32;
144 start_y -= int(start_y) % 32;
145 int tsx = int(start_x / 32); // tilestartindex x
146 int tsy = int(start_y / 32); // tilestartindex y
150 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
151 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
152 if (!tiles[ty*width + tx].hidden)
153 tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
159 float trans_x = roundf(context.get_translation().x);
160 float trans_y = roundf(context.get_translation().y);
162 context.push_transform();
163 context.set_translation(Vector(trans_x * speed, trans_y * speed));
165 float start_x = roundf(context.get_translation().x);
166 float start_y = roundf(context.get_translation().y);
167 float end_x = std::min(start_x + screen->w, float(width * 32));
168 float end_y = std::min(start_y + screen->h, float(height * 32));
169 start_x -= int(start_x) % 32;
170 start_y -= int(start_y) % 32;
171 int tsx = int(start_x / 32); // tilestartindex x
172 int tsy = int(start_y / 32); // tilestartindex y
176 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
177 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
178 if (!tiles[ty*width + tx].hidden)
179 tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
183 context.pop_transform();
188 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
189 int newlayer, bool newsolid)
191 assert(int(newt.size()) == newwidth * newheight);
196 tiles.resize(newt.size());
197 for(unsigned int i = 0; i < newt.size(); ++i)
199 tiles[i].hidden = false;
200 tiles[i].id = newt[i];
208 TileMap::resize(int new_width, int new_height)
210 if(new_width < width) {
211 // remap tiles for new width
212 for(int y = 0; y < height && y < new_height; ++y) {
213 for(int x = 0; x < new_width; ++x) {
214 tiles[y * new_width + x] = tiles[y * width + x];
219 tiles.resize(new_width * new_height);
221 if(new_width > width) {
223 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
224 for(int x = new_width-1; x >= 0; --x) {
226 tiles[y * new_width + x] = TileId();
228 tiles[y * new_width + x] = tiles[y * width + x];
239 TileMap::get_tile(int x, int y) const
241 if(x < 0 || x >= width || y < 0 || y >= height)
242 return &tilemanager->get(0);
244 return &tilemanager->get(tiles[y*width + x].id);
248 TileMap::get_tile_at(const Vector& pos) const
250 return get_tile(int(pos.x)/32, int(pos.y)/32);
254 TileMap::get_tile_id_at(const Vector& pos)
256 int x = int(pos.x)/32;
257 int y = int(pos.y)/32;
259 return tiles[y*width + x];
263 TileMap::change(int x, int y, unsigned int newtile)
265 assert(x >= 0 && x < width && y >= 0 && y < height);
266 tiles[y*width + x].id = newtile;
270 TileMap::change_at(const Vector& pos, unsigned int newtile)
272 change(int(pos.x)/32, int(pos.y)/32, newtile);