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