More -Weffc++ cleanup
[supertux.git] / src / supertux / tile_set.cpp
1 //  SuperTux
2 //  Copyright (C) 2008 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/tile_set.hpp"
18
19 #include <stdexcept>
20 #include <sstream>
21
22 #include "lisp/list_iterator.hpp"
23 #include "lisp/parser.hpp"
24 #include "util/file_system.hpp"
25
26 TileSet::TileSet() :
27   tiles(),
28   tiles_path(""), 
29   tiles_loaded(false)
30 {
31   tiles.resize(1, 0);
32   tiles[0] = new Tile(this);
33 }
34
35 TileSet::TileSet(const std::string& filename)
36   : tiles_path(""), tiles_loaded(true)
37 {
38   tiles_path = FileSystem::dirname(filename);
39
40   tiles.resize(1, 0);
41   tiles[0] = new Tile(this);
42
43   lisp::Parser parser;
44   const lisp::Lisp* root = parser.parse(filename);
45
46   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
47   if(!tiles_lisp)
48     throw std::runtime_error("file is not a supertux tiles file.");
49
50   lisp::ListIterator iter(tiles_lisp);
51   while(iter.next()) {
52     if(iter.item() == "tile") {
53       Tile* tile = new Tile(this);
54       uint32_t id = tile->parse(*(iter.lisp()));
55
56       if(id >= tiles.size())
57         tiles.resize(id+1, 0);
58
59       if(tiles[id] != 0) {
60         log_warning << "Tile with ID " << id << " redefined" << std::endl;
61         delete tile;
62       } else {
63         tiles[id] = tile;
64       }
65     } else if(iter.item() == "tilegroup") {
66       /* tilegroups are only interesting for the editor */
67     } else if (iter.item() == "tiles") {
68       // List of ids (use 0 if the tile should be ignored)
69       std::vector<uint32_t> ids;
70       // List of attributes of the tile
71       std::vector<uint32_t> attributes;
72       // List of data for the tiles
73       std::vector<uint32_t> datas;
74       //List of frames that the tiles come in
75       std::vector<std::string> images;
76
77       // width and height of the image in tile units, this is used for two
78       // purposes:
79       //  a) so we don't have to load the image here to know its dimensions
80       //  b) so that the resulting 'tiles' entry is more robust,
81       //  ie. enlarging the image won't break the tile id mapping
82       // FIXME: height is actually not used, since width might be enough for
83       // all purposes, still feels somewhat more natural this way
84       unsigned int width  = 0;
85       unsigned int height = 0;
86
87       iter.lisp()->get("ids",        ids);
88       bool has_attributes = iter.lisp()->get("attributes", attributes);
89       bool has_datas = iter.lisp()->get("datas", datas);
90
91       if(!iter.lisp()->get("image",      images))
92         iter.lisp()->get( "images",      images);
93
94       iter.lisp()->get("width",      width);
95       iter.lisp()->get("height",     height);
96
97       float animfps = 10;
98       iter.lisp()->get("anim-fps",     animfps);
99
100       if(images.size() <= 0) {
101         throw std::runtime_error("No images in tile.");
102       }
103       if(animfps < 0) {
104         throw std::runtime_error("Negative fps.");
105       }
106       if (ids.size() != width*height) {
107         std::ostringstream err;
108         err << "Number of ids (" << ids.size() <<  ") and size of image (" << width*height
109             << ") mismatch for image '" << images[0] << "', but must be equal";
110         throw std::runtime_error(err.str());
111       }
112
113       if (has_attributes && ids.size() != attributes.size()) {
114         std::ostringstream err;
115         err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
116             << ") mismatch for image '" << images[0] << "', but must be equal";
117         throw std::runtime_error(err.str());
118       }
119
120       if (has_datas && ids.size() != datas.size()) {
121         std::ostringstream err;
122         err << "Number of ids (" << ids.size() <<  ") and datas (" << datas.size()
123             << ") mismatch for image '" << images[0] << "', but must be equal";
124         throw std::runtime_error(err.str());
125       }
126
127       for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
128         if (ids[i] == 0)
129           continue;
130
131         if(ids[i] >= tiles.size())
132           tiles.resize(ids[i]+1, 0);
133
134         int x = 32*(i % width);
135         int y = 32*(i / width);
136         Tile* tile = new Tile(this, images, Rect(x, y, x + 32, y + 32),
137                               (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps);
138         if (tiles[ids[i]] == 0) {
139           tiles[ids[i]] = tile;
140         } else {
141           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
142           delete tile;
143         }
144       }
145     } else if(iter.item() == "properties") {
146       // deprecated
147     } else {
148       log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
149     }
150   }
151   if (0)
152   { // enable this if you want to see a list of free tiles
153     log_info << "Last Tile ID is " << tiles.size()-1 << std::endl;
154     int last = -1;
155     for(int i = 0; i < int(tiles.size()); ++i)
156     {
157       if (tiles[i] == 0 && last == -1)
158       {
159         last = i;
160       }
161       else if (tiles[i] && last != -1)
162       {
163         log_info << "Free Tile IDs (" << i - last << "): " << last << " - " << i-1 << std::endl;
164         last = -1;
165       }
166     }
167   }
168   if (0)
169   { // enable this to dump the (large) list of tiles to log_debug
170     // Two dumps are identical iff the tilesets specify identical tiles
171     log_debug << "Tileset in " << filename << std::endl;
172     for(int i = 0; i < int(tiles.size()); ++i)
173     {
174       if(tiles[i] == 0)
175         continue;
176       Tile* t = tiles[i];
177       log_debug << " Tile: id " << i << ", data " << t->data << ", attributes " << t->attributes << ":" << std::endl;
178       for(std::vector<Tile::ImageSpec>::iterator im = t->imagespecs.begin(); im !=
179             t->imagespecs.end(); ++im) {
180         log_debug << "  Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
181       }
182     }
183   }
184 }
185
186 TileSet::~TileSet()
187 {
188   if(tiles_loaded) {
189     for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
190       delete *i;
191   }
192 }
193
194 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
195                     uint32_t offset)
196 {
197   for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
198     uint32_t dest_id = id - start + offset;
199
200     if(dest_id >= tiles.size())
201       tiles.resize(dest_id + 1, 0);
202
203     if(dest_id == 0)
204       continue;
205
206     Tile *tile = tileset->tiles[id];
207     if(tile == NULL)
208       continue;
209
210     if(tiles[dest_id] != NULL) {
211       log_warning << "tileset merge resulted in multiple definitions for id "
212                   << dest_id << "(originally " << id << ")" << std::endl;
213     }
214     tiles[dest_id] = tile;
215   }
216 }
217
218 /* EOF */