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