some cleanups memory leak fixes and moving of source files
[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 "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   if (debug_grid)
164   {
165     for (pos.x = start_x; pos.x < end_x; pos.x += 32)
166     {
167        context.draw_filled_rect(Vector (pos.x, start_y), Vector(1, fabsf(start_y - end_y)),
168                   Color(225, 225, 225), LAYER_GUI-50);
169     }
170
171     for (pos.y = start_y; pos.y < end_y; pos.y += 32)
172     {
173        context.draw_filled_rect(Vector (start_x, pos.y), Vector(fabsf(start_x - end_x), 1),
174                   Color(225, 225, 225), LAYER_GUI-50);
175     }
176   }
177
178   context.pop_transform();
179 }
180
181 void
182 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
183     int newlayer, bool newsolid)
184 {
185   assert(int(newt.size()) == newwidth * newheight);
186
187   width  = newwidth;
188   height = newheight;
189
190   tiles.resize(newt.size());
191   tiles = newt;
192
193   layer  = newlayer;
194   solid  = newsolid;
195   if(solid)
196     flags |= FLAG_SOLID;
197 }
198
199 void
200 TileMap::resize(int new_width, int new_height)
201 {
202   if(new_width < width) {
203     // remap tiles for new width
204     for(int y = 0; y < height && y < new_height; ++y) {
205       for(int x = 0; x < new_width; ++x) {
206         tiles[y * new_width + x] = tiles[y * width + x];
207       }
208     }
209   }
210                                                                                 
211   tiles.resize(new_width * new_height);
212                                                                                 
213   if(new_width > width) {
214     // remap tiles
215     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
216       for(int x = new_width-1; x >= 0; --x) {
217         if(x >= width) {
218           tiles[y * new_width + x] = 0;
219           continue;
220         }
221         
222         tiles[y * new_width + x] = tiles[y * width + x];
223       }
224     }
225   }
226
227   height = new_height;
228   width = new_width;
229 }
230
231 void
232 TileMap::do_vertical_flip()
233 {
234   // remap tiles vertically flipped
235   for(int y = 0; y < height / 2; ++y) {
236     for(int x = 0; x < width; ++x) {
237       std::swap(tiles[y*width + x], tiles[(((height-1)*width) - (y*width)) + x]);
238     }
239   }
240
241   vertical_flip = true;
242 }
243
244 const Tile*
245 TileMap::get_tile(int x, int y) const
246 {
247   if(x < 0 || x >= width || y < 0 || y >= height) {
248 #ifdef DEBUG
249     //std::cout << "Warning: tile outside tilemap requested!\n";
250 #endif
251     return tilemanager->get(0);
252   }
253
254   return tilemanager->get(tiles[y*width + x]);
255 }
256
257 const Tile*
258 TileMap::get_tile_at(const Vector& pos) const
259 {
260   return get_tile(int(pos.x)/32, int(pos.y)/32);
261 }
262
263 void
264 TileMap::change(int x, int y, uint32_t newtile)
265 {
266   assert(x >= 0 && x < width && y >= 0 && y < height);
267   tiles[y*width + x] = newtile;
268 }
269
270 void
271 TileMap::change_at(const Vector& pos, uint32_t newtile)
272 {
273   change(int(pos.x)/32, int(pos.y)/32, newtile);
274 }