Exposed Tilemap alpha manipulation to squirrel and lisp
[supertux.git] / src / object / tilemap.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <cassert>
23 #include <algorithm>
24 #include <iostream>
25 #include <stdexcept>
26 #include <math.h>
27
28 #include "tilemap.hpp"
29 #include "video/drawing_context.hpp"
30 #include "level.hpp"
31 #include "tile.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"
37 #include "main.hpp"
38 #include "log.hpp"
39 #include "scripting/tilemap.hpp"
40 #include "scripting/squirrel_util.hpp"
41
42 TileMap::TileMap()
43   : solid(false), speed_x(1), speed_y(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),
45     draw_target(DrawingContext::NORMAL)
46 {
47   tilemanager = tile_manager;
48 }
49
50 TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager)
51   : solid(false), speed_x(1), speed_y(1), width(-1), height(-1), z_pos(0),
52     x_offset(0), y_offset(0),
53     drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0),
54     remaining_fade_time(0),
55     draw_target(DrawingContext::NORMAL)
56 {
57   tilemanager = new_tile_manager;
58   if(tilemanager == 0)
59     tilemanager = tile_manager;
60
61   reader.get("name", name);
62   reader.get("z-pos", z_pos);
63   reader.get("solid", solid);
64   reader.get("speed", speed_x);
65   reader.get("speed-y", speed_y);
66
67   if(solid && ((speed_x != 1) || (speed_y != 1))) {
68     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
69     speed_x = 1;
70     speed_y = 1;
71   }
72
73   const lisp::Lisp* pathLisp = reader.get_lisp("path");
74   if (pathLisp) {
75     path.reset(new Path());
76     path->read(*pathLisp);
77     walker.reset(new PathWalker(path.get(), /*running*/false));
78     Vector v = path->get_base();
79     set_x_offset(v.x);
80     set_y_offset(v.y);
81   }
82
83   std::string draw_target_s = "normal";
84   reader.get("draw-target", draw_target_s);
85   if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
86   if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
87
88   if (reader.get("alpha", alpha)) {
89     current_alpha = alpha;
90   }
91
92   reader.get("width", width);
93   reader.get("height", height);
94   if(width < 0 || height < 0)
95     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
96
97   if(!reader.get_vector("tiles", tiles))
98     throw std::runtime_error("No tiles in tilemap.");
99
100   if(int(tiles.size()) != width*height) {
101     throw std::runtime_error("wrong number of tiles in tilemap.");
102   }
103
104   // make sure all tiles are loaded
105   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
106     tilemanager->get(*i);
107 }
108
109 TileMap::TileMap(std::string name, int z_pos, bool solid, size_t width, size_t height)
110   : solid(solid), speed_x(1), speed_y(1), width(0), height(0), z_pos(z_pos),
111     x_offset(0), y_offset(0), drawing_effect(NO_EFFECT), alpha(1.0),
112     current_alpha(1.0), remaining_fade_time(0),
113     draw_target(DrawingContext::NORMAL)
114 {
115   this->name = name;
116   tilemanager = tile_manager;
117
118   resize(width, height);
119 }
120
121 TileMap::~TileMap()
122 {
123 }
124
125 void
126 TileMap::write(lisp::Writer& writer)
127 {
128   writer.start_list("tilemap");
129
130   writer.write_int("z-pos", z_pos);
131
132   writer.write_bool("solid", solid);
133   writer.write_float("speed", speed_x);
134   writer.write_float("speed-y", speed_y);
135   writer.write_int("width", width);
136   writer.write_int("height", height);
137   writer.write_int_vector("tiles", tiles);
138
139   writer.end_list("tilemap");
140 }
141
142 void
143 TileMap::update(float elapsed_time)
144 {
145   // handle tilemap fading
146   if (current_alpha != alpha) {
147     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
148     if (remaining_fade_time == 0.0f) {
149       current_alpha = alpha;
150     } else {
151       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
152       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
153       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
154     }
155     if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
156     if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
157   }
158
159   // if we have a path to follow, follow it
160   if (walker.get()) {
161     Vector v = walker->advance(elapsed_time);
162     set_x_offset(v.x);
163     set_y_offset(v.y);
164   }
165 }
166
167 void
168 TileMap::draw(DrawingContext& context)
169 {
170   // skip draw if current opacity is set to 0.0
171   if (current_alpha == 0.0) return;
172
173   context.push_transform();
174   context.push_target();
175   context.set_target(draw_target);
176
177   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
178   if(current_alpha != 1.0) context.set_alpha(current_alpha);
179
180   float trans_x = roundf(context.get_translation().x);
181   float trans_y = roundf(context.get_translation().y);
182   context.set_translation(Vector(trans_x * speed_x, trans_y * speed_y));
183
184   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
185    * I have no idea why */
186   float start_x = int((roundf(context.get_translation().x) - roundf(x_offset)) / 32) * 32 + roundf(x_offset);
187   float start_y = int((roundf(context.get_translation().y) - roundf(y_offset)) / 32) * 32 + roundf(y_offset);
188   float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + roundf(x_offset)));
189   float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + roundf(y_offset)));
190   int tsx = int((start_x - roundf(x_offset)) / 32); // tilestartindex x
191   int tsy = int((start_y - roundf(y_offset)) / 32); // tilestartindex y
192
193   Vector pos;
194   int tx, ty;
195   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
196     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
197       if ((tx < 0) || (ty < 0)) continue;
198       const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
199       assert(tile != 0);
200       tile->draw(context, pos, z_pos);
201     }
202   }
203
204   context.pop_target();
205   context.pop_transform();
206 }
207
208 void
209 TileMap::goto_node(int node_no)
210 {
211   if (!walker.get()) return;
212   walker->goto_node(node_no);
213 }
214
215 void
216 TileMap::start_moving()
217 {
218   if (!walker.get()) return;
219   walker->start_moving();
220 }
221
222 void
223 TileMap::stop_moving()
224 {
225   if (!walker.get()) return;
226   walker->stop_moving();
227 }
228
229 void
230 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
231 {
232   if (name.empty()) return;
233   Scripting::TileMap* interface = new Scripting::TileMap(this);
234   expose_object(vm, table_idx, interface, name, true);
235 }
236
237 void
238 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
239 {
240   if (name.empty()) return;
241   Scripting::unexpose_object(vm, table_idx, name);
242 }
243
244 void
245 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
246     int new_z_pos, bool newsolid)
247 {
248   if(int(newt.size()) != newwidth * newheight)
249     throw std::runtime_error("Wrong tilecount count.");
250
251   width  = newwidth;
252   height = newheight;
253
254   tiles.resize(newt.size());
255   tiles = newt;
256
257   z_pos  = new_z_pos;
258   solid  = newsolid;
259
260   // make sure all tiles are loaded
261   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
262     tilemanager->get(*i);
263 }
264
265 void
266 TileMap::resize(int new_width, int new_height, int fill_id)
267 {
268   if(new_width < width) {
269     // remap tiles for new width
270     for(int y = 0; y < height && y < new_height; ++y) {
271       for(int x = 0; x < new_width; ++x) {
272         tiles[y * new_width + x] = tiles[y * width + x];
273       }
274     }
275   }
276
277   tiles.resize(new_width * new_height, fill_id);
278
279   if(new_width > width) {
280     // remap tiles
281     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
282       for(int x = new_width-1; x >= 0; --x) {
283         if(x >= width) {
284           tiles[y * new_width + x] = fill_id;
285           continue;
286         }
287
288         tiles[y * new_width + x] = tiles[y * width + x];
289       }
290     }
291   }
292
293   height = new_height;
294   width = new_width;
295 }
296
297 void
298 TileMap::set_solid(bool solid)
299 {
300   this->solid = solid;
301 }
302
303 const Tile*
304 TileMap::get_tile(int x, int y) const
305 {
306   if(x < 0 || x >= width || y < 0 || y >= height) {
307     //log_warning << "tile outside tilemap requested" << std::endl;
308     return tilemanager->get(0);
309   }
310
311   return tilemanager->get(tiles[y*width + x]);
312 }
313
314 const Tile*
315 TileMap::get_tile_at(const Vector& pos) const
316 {
317   return get_tile(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
318 }
319
320 void
321 TileMap::change(int x, int y, uint32_t newtile)
322 {
323   assert(x >= 0 && x < width && y >= 0 && y < height);
324   tiles[y*width + x] = newtile;
325 }
326
327 void
328 TileMap::change_at(const Vector& pos, uint32_t newtile)
329 {
330   change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
331 }
332
333 void
334 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
335 {
336   for (size_t x = 0; x < get_width(); x++)
337     for (size_t y = 0; y < get_height(); y++) {
338       if (get_tile(x,y)->getID() == oldtile) change(x,y,newtile);
339     }
340 }
341
342 void
343 TileMap::fade(float alpha, float seconds)
344 {
345   this->alpha = alpha;
346   this->remaining_fade_time = seconds;
347 }
348
349
350 void 
351 TileMap::set_alpha(float alpha)
352 {
353   this->alpha = alpha;
354   this->current_alpha = alpha;
355   this->remaining_fade_time = 0;
356   if (current_alpha < 0.25) set_solid(false);
357   if (current_alpha > 0.75) set_solid(true);
358 }
359
360 float 
361 TileMap::get_alpha()
362 {
363   return this->current_alpha;
364 }
365
366 IMPLEMENT_FACTORY(TileMap, "tilemap");