SecretArea triggers can make tilemaps fade away. See test level.
[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 <cmath>
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
40 TileMap::TileMap()
41   : solid(false), speed(1), width(0), height(0), z_pos(0), x_offset(0), y_offset(0),
42     drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0), remaining_fade_time(0)
43 {
44   tilemanager = tile_manager;
45 }
46
47 TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager)
48   : GameObject(reader), solid(false), speed(1), width(-1), height(-1), z_pos(0), x_offset(0), y_offset(0),
49     drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0), remaining_fade_time(0)
50 {
51   tilemanager = new_tile_manager;
52   if(tilemanager == 0)
53     tilemanager = tile_manager;
54
55   reader.get("z-pos", z_pos);
56   reader.get("solid", solid);
57   reader.get("speed", speed);
58
59   if(solid && speed != 1) {
60     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
61     speed = 1;
62   }
63
64   reader.get("width", width);
65   reader.get("height", height);
66   if(width < 0 || height < 0)
67     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
68
69   if(!reader.get_vector("tiles", tiles))
70     throw std::runtime_error("No tiles in tilemap.");
71
72   if(int(tiles.size()) != width*height) {
73     throw std::runtime_error("wrong number of tiles in tilemap.");
74   }
75
76   // make sure all tiles are loaded
77   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
78     tilemanager->get(*i);
79 }
80
81 TileMap::TileMap(std::string name, int z_pos, bool solid, size_t width, size_t height)
82   : GameObject(name), solid(solid), speed(1), width(0), height(0), z_pos(z_pos),
83     x_offset(0), y_offset(0), drawing_effect(NO_EFFECT), alpha(1.0), current_alpha(1.0), remaining_fade_time(0)
84 {
85   tilemanager = tile_manager;
86
87   resize(width, height);
88 }
89
90 TileMap::~TileMap()
91 {
92 }
93
94 void
95 TileMap::write(lisp::Writer& writer)
96 {
97   writer.start_list("tilemap");
98
99   writer.write_int("z-pos", z_pos);
100
101   writer.write_bool("solid", solid);
102   writer.write_float("speed", speed);
103   writer.write_int("width", width);
104   writer.write_int("height", height);
105   writer.write_int_vector("tiles", tiles);
106
107   writer.end_list("tilemap");
108 }
109
110 void
111 TileMap::update(float elapsed_time)
112 {
113   // handle tilemap fading
114   if (current_alpha != alpha) {
115     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
116     if (remaining_fade_time == 0.0f) {
117       current_alpha = alpha;
118     } else {
119       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
120       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
121       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
122     }
123   }
124
125   // FIXME: testing only
126   static int step = 0;
127   if (step++ > 10) {
128     step = 0;
129     if (name == "risinglava") set_y_offset(get_y_offset() - 1);
130   }
131 }
132
133 void
134 TileMap::draw(DrawingContext& context)
135 {
136   context.push_transform();
137
138   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
139   if(current_alpha != 1.0) context.set_alpha(current_alpha);
140
141   float trans_x = roundf(context.get_translation().x);
142   float trans_y = roundf(context.get_translation().y);
143   context.set_translation(Vector(trans_x * speed, trans_y * speed));
144
145   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
146    * I have no idea why */
147   float start_x = ((int)((roundf(context.get_translation().x) - x_offset) / 32)) * 32 + x_offset;
148   float start_y = ((int)((roundf(context.get_translation().y) - y_offset) / 32)) * 32 + y_offset;
149   float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + x_offset));
150   float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + y_offset));
151   int tsx = int((start_x - x_offset) / 32); // tilestartindex x
152   int tsy = int((start_y - y_offset) / 32); // tilestartindex y
153
154   Vector pos;
155   int tx, ty;
156   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
157     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
158       if ((tx < 0) || (ty < 0)) continue;
159       const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
160       assert(tile != 0);
161       tile->draw(context, pos, z_pos);
162     }
163   }
164
165   context.pop_transform();
166 }
167
168 void
169 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
170     int new_z_pos, bool newsolid)
171 {
172   if(int(newt.size()) != newwidth * newheight)
173     throw std::runtime_error("Wrong tilecount count.");
174
175   width  = newwidth;
176   height = newheight;
177
178   tiles.resize(newt.size());
179   tiles = newt;
180
181   z_pos  = new_z_pos;
182   solid  = newsolid;
183
184   // make sure all tiles are loaded
185   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
186     tilemanager->get(*i);
187 }
188
189 void
190 TileMap::resize(int new_width, int new_height)
191 {
192   if(new_width < width) {
193     // remap tiles for new width
194     for(int y = 0; y < height && y < new_height; ++y) {
195       for(int x = 0; x < new_width; ++x) {
196         tiles[y * new_width + x] = tiles[y * width + x];
197       }
198     }
199   }
200
201   tiles.resize(new_width * new_height);
202
203   if(new_width > width) {
204     // remap tiles
205     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
206       for(int x = new_width-1; x >= 0; --x) {
207         if(x >= width) {
208           tiles[y * new_width + x] = 0;
209           continue;
210         }
211
212         tiles[y * new_width + x] = tiles[y * width + x];
213       }
214     }
215   }
216
217   height = new_height;
218   width = new_width;
219 }
220
221 const Tile*
222 TileMap::get_tile(int x, int y) const
223 {
224   if(x < 0 || x >= width || y < 0 || y >= height) {
225     //log_warning << "tile outside tilemap requested" << std::endl;
226     return tilemanager->get(0);
227   }
228
229   return tilemanager->get(tiles[y*width + x]);
230 }
231
232 const Tile*
233 TileMap::get_tile_at(const Vector& pos) const
234 {
235   return get_tile(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
236 }
237
238 void
239 TileMap::change(int x, int y, uint32_t newtile)
240 {
241   assert(x >= 0 && x < width && y >= 0 && y < height);
242   tiles[y*width + x] = newtile;
243 }
244
245 void
246 TileMap::change_at(const Vector& pos, uint32_t newtile)
247 {
248   change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
249 }
250
251 void
252 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
253 {
254   for (size_t x = 0; x < get_width(); x++)
255     for (size_t y = 0; y < get_height(); y++) {
256       if (get_tile(x,y)->getID() == oldtile) change(x,y,newtile);
257     }
258 }
259
260 void 
261 TileMap::fade(float alpha, float seconds)
262 {
263   this->alpha = alpha;
264   this->remaining_fade_time = seconds;
265 }
266
267 IMPLEMENT_FACTORY(TileMap, "tilemap");