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