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