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