kugelblitz electrifies water on contact - electrified water is baaaaaaad
[supertux.git] / src / object / tilemap.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 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 #include <config.h>
20
21 #include <cassert>
22 #include <algorithm>
23 #include <iostream>
24 #include <stdexcept>
25 #include <cmath>
26
27 #include "tilemap.hpp"
28 #include "video/drawing_context.hpp"
29 #include "level.hpp"
30 #include "tile.hpp"
31 #include "resources.hpp"
32 #include "tile_manager.hpp"
33 #include "lisp/lisp.hpp"
34 #include "lisp/writer.hpp"
35 #include "object_factory.hpp"
36 #include "main.hpp"
37
38 TileMap::TileMap()
39   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES),
40     drawing_effect(0)
41 {
42   tilemanager = tile_manager;
43
44   if(solid)
45     flags |= FLAG_SOLID;
46 }
47
48 TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager)
49   : solid(false), speed(1), width(-1), height(-1), layer(LAYER_TILES),
50     drawing_effect(0)
51 {
52   tilemanager = new_tile_manager;
53   if(tilemanager == 0)
54     tilemanager = tile_manager;
55
56   std::string layer_str;
57   if(reader.get("layer", layer_str)) {
58     if(layer_str == "background")
59       layer = LAYER_BACKGROUNDTILES;
60     else if(layer_str == "interactive")
61       layer = LAYER_TILES;
62     else if(layer_str == "foreground")
63       layer = LAYER_FOREGROUNDTILES;
64     else
65       std::cerr << "Unknown layer '" << layer_str << "' in tilemap.\n";
66   }
67
68   reader.get("solid", solid);
69   reader.get("speed", speed);
70
71   if(solid && speed != 1) {
72     std::cout << "Speed of solid tilemap is not 1. fixing.\n";
73     speed = 1;
74   }
75   if(solid)
76     flags |= FLAG_SOLID;
77  
78   reader.get("width", width);
79   reader.get("height", height);
80   if(width < 0 || height < 0)
81     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
82
83   if(!reader.get_vector("tiles", tiles))
84     throw std::runtime_error("No tiles in tilemap.");
85
86   if(int(tiles.size()) != width*height) {
87     throw std::runtime_error("wrong number of tiles in tilemap.");
88   }
89
90   // make sure all tiles are loaded
91   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
92     tilemanager->get(*i);
93 }
94
95 TileMap::TileMap(int layer_, bool solid_, size_t width_, size_t height_)
96   : solid(solid_), speed(1), width(0), height(0), layer(layer_),
97     drawing_effect(0)
98 {
99   tilemanager = tile_manager;
100   
101   resize(width_, height_);
102
103   if(solid)
104     flags |= FLAG_SOLID;  
105 }
106
107 TileMap::~TileMap()
108 {
109 }
110
111 void
112 TileMap::write(lisp::Writer& writer)
113 {
114   writer.start_list("tilemap");
115
116   if(layer == LAYER_BACKGROUNDTILES)
117     writer.write_string("layer", "background");
118   else if(layer == LAYER_TILES)
119     writer.write_string("layer", "interactive");
120   else if(layer == LAYER_FOREGROUNDTILES)
121     writer.write_string("layer", "foreground");
122   else {
123     writer.write_string("layer", "unknown");
124     std::cerr << "Warning unknown layer in tilemap.\n";
125   }
126
127   writer.write_bool("solid", solid);
128   writer.write_float("speed", speed);
129   writer.write_int("width", width);
130   writer.write_int("height", height);
131   writer.write_int_vector("tiles", tiles);
132   
133   writer.end_list("tilemap");
134 }
135
136 void
137 TileMap::update(float )
138 {
139 }
140
141 void
142 TileMap::draw(DrawingContext& context)
143 {
144   context.push_transform();
145
146   if(drawing_effect != 0)
147     context.set_drawing_effect(drawing_effect); 
148   float trans_x = roundf(context.get_translation().x);
149   float trans_y = roundf(context.get_translation().y);
150   context.set_translation(Vector(trans_x * speed, trans_y * speed));
151
152   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
153    * I have no idea why */
154   float start_x = roundf(context.get_translation().x);
155   if(start_x < 0)
156     start_x = 0;
157   float start_y = roundf(context.get_translation().y);
158   if(start_y < 0)
159     start_y = 0;
160   float end_x = std::min(start_x + SCREEN_WIDTH, float(width * 32));
161   float end_y = std::min(start_y + SCREEN_HEIGHT, float(height * 32));
162   start_x -= int(start_x) % 32;
163   start_y -= int(start_y) % 32;  
164   int tsx = int(start_x / 32); // tilestartindex x
165   int tsy = int(start_y / 32); // tilestartindex y
166
167   Vector pos;
168   int tx, ty;
169   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
170     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
171       const Tile* tile = tilemanager->get(tiles[ty*width + tx]);
172       assert(tile != 0);
173       tile->draw(context, pos, layer);
174     }
175   }
176
177 #if 0
178   if (debug_grid)
179   {
180     for (pos.x = start_x; pos.x < end_x; pos.x += 32)
181     {
182        context.draw_filled_rect(Vector (pos.x, start_y), Vector(1, fabsf(start_y - end_y)),
183                   Color(225, 225, 225), LAYER_GUI-50);
184     }
185
186     for (pos.y = start_y; pos.y < end_y; pos.y += 32)
187     {
188        context.draw_filled_rect(Vector (start_x, pos.y), Vector(fabsf(start_x - end_x), 1),
189                   Color(225, 225, 225), LAYER_GUI-50);
190     }
191   }
192 #endif
193
194   context.pop_transform();
195 }
196
197 void
198 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
199     int newlayer, bool newsolid)
200 {
201   if(int(newt.size()) != newwidth * newheight)
202     throw std::runtime_error("Wrong tilecount count.");
203
204   width  = newwidth;
205   height = newheight;
206
207   tiles.resize(newt.size());
208   tiles = newt;
209
210   layer  = newlayer;
211   solid  = newsolid;
212   if(solid)
213     flags |= FLAG_SOLID;
214
215   // make sure all tiles are loaded
216   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
217     tilemanager->get(*i);                                        
218 }
219
220 void
221 TileMap::resize(int new_width, int new_height)
222 {
223   if(new_width < width) {
224     // remap tiles for new width
225     for(int y = 0; y < height && y < new_height; ++y) {
226       for(int x = 0; x < new_width; ++x) {
227         tiles[y * new_width + x] = tiles[y * width + x];
228       }
229     }
230   }
231                                                                                 
232   tiles.resize(new_width * new_height);
233                                                                                 
234   if(new_width > width) {
235     // remap tiles
236     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
237       for(int x = new_width-1; x >= 0; --x) {
238         if(x >= width) {
239           tiles[y * new_width + x] = 0;
240           continue;
241         }
242         
243         tiles[y * new_width + x] = tiles[y * width + x];
244       }
245     }
246   }
247
248   height = new_height;
249   width = new_width;
250 }
251
252 const Tile*
253 TileMap::get_tile(int x, int y) const
254 {
255   if(x < 0 || x >= width || y < 0 || y >= height) {
256 #ifdef DEBUG
257     //std::cout << "Warning: tile outside tilemap requested!\n";
258 #endif
259     return tilemanager->get(0);
260   }
261
262   return tilemanager->get(tiles[y*width + x]);
263 }
264
265 const Tile*
266 TileMap::get_tile_at(const Vector& pos) const
267 {
268   return get_tile(int(pos.x)/32, int(pos.y)/32);
269 }
270
271 void
272 TileMap::change(int x, int y, uint32_t newtile)
273 {
274   assert(x >= 0 && x < width && y >= 0 && y < height);
275   tiles[y*width + x] = newtile;
276 }
277
278 void
279 TileMap::change_at(const Vector& pos, uint32_t newtile)
280 {
281   change(int(pos.x)/32, int(pos.y)/32, newtile);
282 }
283
284 void
285 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
286 {
287   for (size_t x = 0; x < get_width(); x++)
288     for (size_t y = 0; y < get_height(); y++) {
289       if (get_tile(x,y)->getID() == oldtile) change(x,y,newtile);
290     }
291 }
292
293 IMPLEMENT_FACTORY(TileMap, "tilemap");