- moved tilemanager into its own class
[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)
37 {
38   tilemanager = TileManager::instance();
39 }
40
41 TileMap::TileMap(LispReader& reader)
42   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES)
43 {
44   tilemanager = TileManager::instance();
45
46   std::string layer;
47   if(reader.read_string("layer", layer)) {
48     if(layer == "background")
49       layer = LAYER_BACKGROUNDTILES;
50     else if(layer == "interactive")
51       layer = LAYER_TILES;
52     else if(layer == "foreground")
53       layer = LAYER_FOREGROUNDTILES;
54     else
55       std::cout << "Unknown layer '" << layer << "' 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()
87 {
88 }
89
90 void
91 TileMap::write(LispWriter& writer)
92 {
93   writer.start_list("tilemap");
94
95   if(layer == LAYER_BACKGROUNDTILES)
96     writer.write_string("layer", "background");
97   else if(layer == LAYER_TILES)
98     writer.write_string("layer", "interactive");
99   else if(layer == LAYER_FOREGROUNDTILES)
100     writer.write_string("layer", "foreground");
101   else {
102     std::cout << "Warning unknown layer in tilemap.\n";
103   }
104
105   writer.write_bool("solid", solid);
106   writer.write_float("speed", speed);
107   writer.write_int("width", width);
108   writer.write_int("height", height);
109
110   std::vector<unsigned int> tmp_tiles;
111   tmp_tiles.resize(tiles.size());
112   for(unsigned int i = 0; i < tmp_tiles.size(); ++i)
113     {
114       tmp_tiles[i] = tiles[i].id;
115     }
116   writer.write_int_vector("tiles", tmp_tiles);
117   
118   writer.end_list("tilemap");
119 }
120
121 void
122 TileMap::action(float )
123 {
124 }
125
126 void
127 TileMap::draw(DrawingContext& context)
128 {
129   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
130    * I have no idea why */
131   float start_x = roundf(context.get_translation().x * speed);
132   float start_y = roundf(context.get_translation().y * speed);
133   float end_x = std::min(start_x + screen->w, float(width * 32));
134   float end_y = std::min(start_y + screen->h, float(height * 32));
135   start_x -= int(start_x) % 32;
136   start_y -= int(start_y) % 32;  
137   int tsx = int(start_x / 32); // tilestartindex x
138   int tsy = int(start_y / 32); // tilestartindex y
139   
140   Vector pos;
141   int tx, ty;
142   for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
143     for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
144       if (!tiles[ty*width + tx].hidden)
145         tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer);
146     }
147   }
148 }
149
150 void
151 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
152     int newlayer, bool newsolid)
153 {
154   assert(int(newt.size()) == newwidth * newheight);
155
156   width  = newwidth;
157   height = newheight;
158
159   tiles.resize(newt.size());
160   for(unsigned int i = 0; i < newt.size(); ++i)
161     {
162       tiles[i].hidden = false;
163       tiles[i].id = newt[i];
164     }
165
166   layer  = newlayer;
167   solid  = newsolid;
168 }
169
170 void
171 TileMap::resize(int new_width, int new_height)
172 {
173   if(new_width < width) {
174     // remap tiles for new width
175     for(int y = 0; y < height && y < new_height; ++y) {
176       for(int x = 0; x < new_width; ++x) {
177         tiles[y * new_width + x] = tiles[y * width + x];
178       }
179     }
180   }
181                                                                                 
182   tiles.resize(new_width * new_height);
183                                                                                 
184   if(new_width > width) {
185     // remap tiles
186     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
187       for(int x = new_width-1; x >= 0; --x) {
188         if(x >= width) {
189           tiles[y * new_width + x] = TileId();
190         } else {
191           tiles[y * new_width + x] = tiles[y * width + x];
192         }
193       }
194     }
195   }
196
197   height = new_height;
198   width = new_width;
199 }
200
201 Tile*
202 TileMap::get_tile(int x, int y) const
203 {
204   if(x < 0 || x >= width || y < 0 || y >= height)
205     return &tilemanager->get(0);
206
207   return &tilemanager->get(tiles[y*width + x].id);
208 }
209
210 Tile*
211 TileMap::get_tile_at(const Vector& pos) const
212 {
213   return get_tile(int(pos.x)/32, int(pos.y)/32);
214 }
215
216 TileId&
217 TileMap::get_tile_id_at(const Vector& pos)
218 {
219   int x = int(pos.x)/32;
220   int y = int(pos.y)/32;
221   
222   return tiles[y*width + x];
223 }
224
225 void
226 TileMap::change(int x, int y, unsigned int newtile)
227 {
228   assert(x >= 0 && x < width && y >= 0 && y < height);
229   tiles[y*width + x].id = newtile;
230 }
231
232 void
233 TileMap::change_at(const Vector& pos, unsigned int newtile)
234 {
235   change(int(pos.x)/32, int(pos.y)/32, newtile);
236 }