4 // Copyright (C) 2006 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 "tilemap.hpp"
29 #include "video/drawing_context.hpp"
32 #include "resources.hpp"
33 #include "tile_manager.hpp"
34 #include "lisp/lisp.hpp"
35 #include "lisp/writer.hpp"
36 #include "object_factory.hpp"
39 #include "scripting/tilemap.hpp"
40 #include "scripting/squirrel_util.hpp"
43 : solid(false), speed(1), width(0), height(0), z_pos(0), x_offset(0), y_offset(0),
44 drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0), remaining_fade_time(0)
46 tilemanager = tile_manager;
49 TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager)
50 : solid(false), speed(1), width(-1), height(-1), z_pos(0),
51 x_offset(0), y_offset(0),
52 drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0),
53 remaining_fade_time(0)
55 tilemanager = new_tile_manager;
57 tilemanager = tile_manager;
59 reader.get("name", name);
60 reader.get("z-pos", z_pos);
61 reader.get("solid", solid);
62 reader.get("speed", speed);
64 if(solid && speed != 1) {
65 log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
69 const lisp::Lisp* pathLisp = reader.get_lisp("path");
71 path.reset(new Path());
72 path->read(*pathLisp);
73 walker.reset(new PathWalker(path.get(), /*running*/false));
74 Vector v = path->get_base();
79 reader.get("width", width);
80 reader.get("height", height);
81 if(width < 0 || height < 0)
82 throw std::runtime_error("Invalid/No width/height specified in tilemap.");
84 if(!reader.get_vector("tiles", tiles))
85 throw std::runtime_error("No tiles in tilemap.");
87 if(int(tiles.size()) != width*height) {
88 throw std::runtime_error("wrong number of tiles in tilemap.");
91 // make sure all tiles are loaded
92 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
96 TileMap::TileMap(std::string name, int z_pos, bool solid, size_t width, size_t height)
97 : solid(solid), speed(1), width(0), height(0), z_pos(z_pos),
98 x_offset(0), y_offset(0), drawing_effect(NO_EFFECT), alpha(1.0),
99 current_alpha(1.0), remaining_fade_time(0)
102 tilemanager = tile_manager;
104 resize(width, height);
112 TileMap::write(lisp::Writer& writer)
114 writer.start_list("tilemap");
116 writer.write_int("z-pos", z_pos);
118 writer.write_bool("solid", solid);
119 writer.write_float("speed", speed);
120 writer.write_int("width", width);
121 writer.write_int("height", height);
122 writer.write_int_vector("tiles", tiles);
124 writer.end_list("tilemap");
128 TileMap::update(float elapsed_time)
130 // handle tilemap fading
131 if (current_alpha != alpha) {
132 remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
133 if (remaining_fade_time == 0.0f) {
134 current_alpha = alpha;
136 float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
137 if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
138 if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
142 // if we have a path to follow, follow it
144 Vector v = walker->advance(elapsed_time);
151 TileMap::draw(DrawingContext& context)
153 context.push_transform();
155 if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
156 if(current_alpha != 1.0) context.set_alpha(current_alpha);
158 float trans_x = roundf(context.get_translation().x);
159 float trans_y = roundf(context.get_translation().y);
160 context.set_translation(Vector(trans_x * speed, trans_y * speed));
162 /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
163 * I have no idea why */
164 float start_x = ((int)((roundf(context.get_translation().x) - roundf(x_offset)) / 32)) * 32 + roundf(x_offset);
165 float start_y = ((int)((roundf(context.get_translation().y) - roundf(y_offset)) / 32)) * 32 + roundf(y_offset);
166 float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + roundf(x_offset)));
167 float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + roundf(y_offset)));
168 int tsx = int((start_x - roundf(x_offset)) / 32); // tilestartindex x
169 int tsy = int((start_y - roundf(y_offset)) / 32); // tilestartindex y
173 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
174 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
175 if ((tx < 0) || (ty < 0)) continue;
176 const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
178 tile->draw(context, pos, z_pos);
182 context.pop_transform();
186 TileMap::goto_node(int node_no)
188 if (!walker.get()) return;
189 walker->goto_node(node_no);
193 TileMap::start_moving()
195 if (!walker.get()) return;
196 walker->start_moving();
200 TileMap::stop_moving()
202 if (!walker.get()) return;
203 walker->stop_moving();
207 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
209 if (name.empty()) return;
210 if (!walker.get()) return;
211 Scripting::TileMap* interface = new Scripting::TileMap(this);
212 expose_object(vm, table_idx, interface, name, true);
216 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
218 if (name.empty()) return;
219 if (!walker.get()) return;
220 Scripting::unexpose_object(vm, table_idx, name);
224 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
225 int new_z_pos, bool newsolid)
227 if(int(newt.size()) != newwidth * newheight)
228 throw std::runtime_error("Wrong tilecount count.");
233 tiles.resize(newt.size());
239 // make sure all tiles are loaded
240 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
241 tilemanager->get(*i);
245 TileMap::resize(int new_width, int new_height)
247 if(new_width < width) {
248 // remap tiles for new width
249 for(int y = 0; y < height && y < new_height; ++y) {
250 for(int x = 0; x < new_width; ++x) {
251 tiles[y * new_width + x] = tiles[y * width + x];
256 tiles.resize(new_width * new_height);
258 if(new_width > width) {
260 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
261 for(int x = new_width-1; x >= 0; --x) {
263 tiles[y * new_width + x] = 0;
267 tiles[y * new_width + x] = tiles[y * width + x];
277 TileMap::get_tile(int x, int y) const
279 if(x < 0 || x >= width || y < 0 || y >= height) {
280 //log_warning << "tile outside tilemap requested" << std::endl;
281 return tilemanager->get(0);
284 return tilemanager->get(tiles[y*width + x]);
288 TileMap::get_tile_at(const Vector& pos) const
290 return get_tile(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
294 TileMap::change(int x, int y, uint32_t newtile)
296 assert(x >= 0 && x < width && y >= 0 && y < height);
297 tiles[y*width + x] = newtile;
301 TileMap::change_at(const Vector& pos, uint32_t newtile)
303 change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
307 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
309 for (size_t x = 0; x < get_width(); x++)
310 for (size_t y = 0; y < get_height(); y++) {
311 if (get_tile(x,y)->getID() == oldtile) change(x,y,newtile);
316 TileMap::fade(float alpha, float seconds)
319 this->remaining_fade_time = seconds;
322 IMPLEMENT_FACTORY(TileMap, "tilemap");