- fixed the bouncybrick artifact bug
[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
20 #include <cassert>
21 #include <algorithm>
22 #include <iostream>
23 #include <stdexcept>
24 #include <cmath>
25
26 #include "tilemap.h"
27 #include "screen/drawing_context.h"
28 #include "level.h"
29 #include "tile.h"
30 #include "globals.h"
31 #include "lispreader.h"
32 #include "lispwriter.h"
33
34 TileMap::TileMap()
35   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
36 {
37   tilemanager = TileManager::instance();
38 }
39
40 TileMap::TileMap(LispReader& reader)
41   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
42 {
43   tilemanager = TileManager::instance();
44
45   std::string layer;
46   if(reader.read_string("layer", layer)) {
47     if(layer == "background")
48       layer = LAYER_BACKGROUNDTILES;
49     else if(layer == "interactive")
50       layer = LAYER_TILES;
51     else if(layer == "foreground")
52       layer = LAYER_FOREGROUNDTILES;
53     else
54       std::cout << "Unknown layer '" << layer << "' in tilemap.\n";
55   }
56
57   reader.read_bool("solid", solid);
58   reader.read_float("speed", speed);
59
60   if(solid && speed != 1) {
61     std::cout << "Speed of solid tilemap is not 1. fixing.\n";
62     speed = 1;
63   }
64   
65   if(!reader.read_int("width", width) ||
66      !reader.read_int("height", height))
67     throw std::runtime_error("No width or height specified in tilemap.");
68
69   std::vector<unsigned int> tmp_tiles;
70   
71   if(!reader.read_int_vector("tiles", tmp_tiles))
72     throw std::runtime_error("No tiles in tilemap.");
73
74   tiles.resize(tmp_tiles.size());
75   for(unsigned int i = 0; i < tmp_tiles.size(); ++i)
76     {
77       tiles[i].hidden = false;
78       tiles[i].id = tmp_tiles[i];
79     }
80
81   if(int(tiles.size()) != width*height)
82     throw std::runtime_error("wrong number of tiles in tilemap.");
83 }
84
85 TileMap::~TileMap()
86 {
87 }
88
89 void
90 TileMap::write(LispWriter& writer)
91 {
92   writer.start_list("tilemap");
93
94   if(layer == LAYER_BACKGROUNDTILES)
95     writer.write_string("layer", "background");
96   else if(layer == LAYER_TILES)
97     writer.write_string("layer", "interactive");
98   else if(layer == LAYER_FOREGROUNDTILES)
99     writer.write_string("layer", "foreground");
100   else {
101     std::cout << "Warning unknown layer in tilemap.\n";
102   }
103
104   writer.write_bool("solid", solid);
105   writer.write_float("speed", speed);
106   writer.write_int("width", width);
107   writer.write_int("height", height);
108
109   std::vector<unsigned int> tmp_tiles;
110   tmp_tiles.resize(tiles.size());
111   for(unsigned int i = 0; i < tmp_tiles.size(); ++i)
112     {
113       tmp_tiles[i] = tiles[i].id;
114     }
115   writer.write_int_vector("tiles", tmp_tiles);
116   
117   writer.end_list("tilemap");
118 }
119
120 void
121 TileMap::action(float )
122 {
123 }
124
125 void
126 TileMap::draw(DrawingContext& context)
127 {
128   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
129    * I have no idea why */
130   float start_x = roundf(context.get_translation().x * speed);
131   float start_y = roundf(context.get_translation().y * speed);
132   float end_x = std::min(start_x + screen->w, float(width * 32));
133   float end_y = std::min(start_y + screen->h, float(height * 32));
134   start_x -= int(start_x) % 32;
135   start_y -= int(start_y) % 32;  
136   int tsx = int(start_x / 32); // tilestartindex x
137   int tsy = int(start_y / 32); // tilestartindex y
138   
139   Vector pos;
140   int tx, ty;
141   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
142     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
143       if (!tiles[ty*width + tx].hidden)
144         tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
145     }
146   }
147 }
148
149 void
150 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
151     int newlayer, bool newsolid)
152 {
153   assert(int(newt.size()) == newwidth * newheight);
154
155   width  = newwidth;
156   height = newheight;
157
158   tiles.resize(newt.size());
159   for(unsigned int i = 0; i < newt.size(); ++i)
160     {
161       tiles[i].hidden = false;
162       tiles[i].id = newt[i];
163     }
164
165   layer  = newlayer;
166   solid  = newsolid;
167 }
168
169 void
170 TileMap::resize(int new_width, int new_height)
171 {
172   if(new_width < width) {
173     // remap tiles for new width
174     for(int y = 0; y < height && y < new_height; ++y) {
175       for(int x = 0; x < new_width; ++x) {
176         tiles[y * new_width + x] = tiles[y * width + x];
177       }
178     }
179   }
180                                                                                 
181   tiles.resize(new_width * new_height);
182                                                                                 
183   if(new_width > width) {
184     // remap tiles
185     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
186       for(int x = new_width-1; x >= 0; --x) {
187         if(x >= width) {
188           tiles[y * new_width + x] = TileId();
189         } else {
190           tiles[y * new_width + x] = tiles[y * width + x];
191         }
192       }
193     }
194   }
195
196   height = new_height;
197   width = new_width;
198 }
199
200 Tile*
201 TileMap::get_tile(int x, int y) const
202 {
203   if(x < 0 || x >= width || y < 0 || y >= height)
204     return &tilemanager->get(0);
205
206   return &tilemanager->get(tiles[y*width + x].id);
207 }
208
209 Tile*
210 TileMap::get_tile_at(const Vector& pos) const
211 {
212   return get_tile(int(pos.x)/32, int(pos.y)/32);
213 }
214
215 TileId&
216 TileMap::get_tile_id_at(const Vector& pos)
217 {
218   int x = int(pos.x)/32;
219   int y = int(pos.y)/32;
220   
221   return tiles[y*width + x];
222 }
223
224 void
225 TileMap::change(int x, int y, unsigned int newtile)
226 {
227   assert(x >= 0 && x < width && y >= 0 && y < height);
228   tiles[y*width + x].id = newtile;
229 }
230
231 void
232 TileMap::change_at(const Vector& pos, unsigned int newtile)
233 {
234   change(int(pos.x)/32, int(pos.y)/32, newtile);
235 }