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"
31 #include "lispreader.h"
32 #include "lispwriter.h"
35 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
37 tilemanager = TileManager::instance();
40 TileMap::TileMap(LispReader& reader)
41 : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
43 tilemanager = TileManager::instance();
46 if(reader.read_string("layer", layer)) {
47 if(layer == "background")
48 layer = LAYER_BACKGROUNDTILES;
49 else if(layer == "interactive")
51 else if(layer == "foreground")
52 layer = LAYER_FOREGROUNDTILES;
54 std::cout << "Unknown layer '" << layer << "' in tilemap.\n";
57 reader.read_bool("solid", solid);
58 reader.read_float("speed", speed);
60 if(solid && speed != 1) {
61 std::cout << "Speed of solid tilemap is not 1. fixing.\n";
65 if(!reader.read_int("width", width) ||
66 !reader.read_int("height", height))
67 throw std::runtime_error("No width or height specified in tilemap.");
69 if(!reader.read_int_vector("tiles", tiles))
70 throw std::runtime_error("No tiles in tilemap.");
71 if(int(tiles.size()) != width*height)
72 throw std::runtime_error("wrong number of tiles in tilemap.");
80 TileMap::write(LispWriter& writer)
82 writer.start_list("tilemap");
84 if(layer == LAYER_BACKGROUNDTILES)
85 writer.write_string("layer", "background");
86 else if(layer == LAYER_TILES)
87 writer.write_string("layer", "interactive");
88 else if(layer == LAYER_FOREGROUNDTILES)
89 writer.write_string("layer", "foreground");
91 std::cout << "Warning unknown layer in tilemap.\n";
94 writer.write_bool("solid", solid);
95 writer.write_float("speed", speed);
96 writer.write_int("width", width);
97 writer.write_int("height", height);
98 writer.write_int_vector("tiles", tiles);
100 writer.end_list("tilemap");
104 TileMap::action(float )
109 TileMap::draw(DrawingContext& context)
111 /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
112 * I have no idea why */
113 float start_x = roundf(context.get_translation().x * speed);
114 float start_y = roundf(context.get_translation().y * speed);
115 float end_x = std::min(start_x + screen->w, float(width * 32));
116 float end_y = std::min(start_y + screen->h, float(height * 32));
117 start_x -= int(start_x) % 32;
118 start_y -= int(start_y) % 32;
119 int tsx = int(start_x / 32); // tilestartindex x
120 int tsy = int(start_y / 32); // tilestartindex y
124 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
125 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
126 tilemanager->draw_tile(context, tiles[ty*width + tx], pos, layer);
132 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
133 int newlayer, bool newsolid)
135 assert(int(newt.size()) == newwidth * newheight);
145 TileMap::resize(int new_width, int new_height)
147 if(new_width < width) {
148 // remap tiles for new width
149 for(int y = 0; y < height && y < new_height; ++y) {
150 for(int x = 0; x < new_width; ++x) {
151 tiles[y * new_width + x] = tiles[y * width + x];
156 tiles.resize(new_width * new_height);
158 if(new_width > width) {
160 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
161 for(int x = new_width-1; x >= 0; --x) {
163 tiles[y * new_width + x] = 0;
165 tiles[y * new_width + x] = tiles[y * width + x];
176 TileMap::get_tile(int x, int y) const
178 if(x < 0 || x >= width || y < 0 || y >= height)
179 return tilemanager->get(0);
181 return tilemanager->get(tiles[y*width + x]);
185 TileMap::get_tile_at(const Vector& pos) const
187 return get_tile(int(pos.x)/32, int(pos.y)/32);
191 TileMap::get_tile_id_at(const Vector& pos) const
193 int x = int(pos.x)/32;
194 int y = int(pos.y)/32;
195 return tiles[y*width + x];
199 TileMap::change(int x, int y, unsigned int newtile)
201 assert(x >= 0 && x < width && y >= 0 && y < height);
202 tiles[y*width + x] = newtile;
206 TileMap::change_at(const Vector& pos, unsigned int newtile)
208 change(int(pos.x)/32, int(pos.y)/32, newtile);