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