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