c816d36f5f50f76731963164c3ea57c9e9072ad0
[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 <memory>
20 #include <stdexcept>
21 #include <sstream>
22
23 #include "lisp/list_iterator.hpp"
24 #include "lisp/parser.hpp"
25 #include "util/file_system.hpp"
26
27 TileSet::TileSet() :
28   tiles(),
29   tiles_path(), 
30   tiles_loaded(false)
31 {
32   tiles.resize(1, 0);
33   tiles[0] = new Tile(this);
34 }
35
36 TileSet::TileSet(const std::string& filename) :
37   tiles(),
38   tiles_path(), 
39   tiles_loaded(true)
40 {
41   tiles_path = FileSystem::dirname(filename);
42
43   tiles.resize(1, 0);
44   tiles[0] = new Tile(this);
45
46   lisp::Parser parser;
47   const lisp::Lisp* root = parser.parse(filename);
48
49   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
50   if(!tiles_lisp)
51     throw std::runtime_error("file is not a supertux tiles file.");
52
53   lisp::ListIterator iter(tiles_lisp);
54   while(iter.next()) {
55     if(iter.item() == "tile") {
56       std::auto_ptr<Tile> tile(new Tile(this));
57       uint32_t id = tile->parse(*(iter.lisp()));
58
59       if(id >= tiles.size())
60         tiles.resize(id+1, 0);
61
62       if(tiles[id] != 0) {
63         log_warning << "Tile with ID " << id << " redefined" << std::endl;
64       } else {
65         tiles[id] = tile.release();
66       }
67     } else if(iter.item() == "tilegroup") {
68       /* tilegroups are only interesting for the editor */
69     } else if (iter.item() == "tiles") {
70       // List of ids (use 0 if the tile should be ignored)
71       std::vector<uint32_t> ids;
72       // List of attributes of the tile
73       std::vector<uint32_t> attributes;
74       // List of data for the tiles
75       std::vector<uint32_t> datas;
76       //List of frames that the tiles come in
77       std::vector<std::string> images;
78
79       // width and height of the image in tile units, this is used for two
80       // purposes:
81       //  a) so we don't have to load the image here to know its dimensions
82       //  b) so that the resulting 'tiles' entry is more robust,
83       //  ie. enlarging the image won't break the tile id mapping
84       // FIXME: height is actually not used, since width might be enough for
85       // all purposes, still feels somewhat more natural this way
86       unsigned int width  = 0;
87       unsigned int height = 0;
88
89       iter.lisp()->get("ids",        ids);
90       bool has_attributes = iter.lisp()->get("attributes", attributes);
91       bool has_datas = iter.lisp()->get("datas", datas);
92
93       if(!iter.lisp()->get("image",      images))
94         iter.lisp()->get( "images",      images);
95
96       iter.lisp()->get("width",      width);
97       iter.lisp()->get("height",     height);
98
99       float animfps = 10;
100       iter.lisp()->get("anim-fps",     animfps);
101
102       if(images.size() <= 0) {
103         throw std::runtime_error("No images in tile.");
104       }
105       if(animfps < 0) {
106         throw std::runtime_error("Negative fps.");
107       }
108       if (ids.size() != width*height) {
109         std::ostringstream err;
110         err << "Number of ids (" << ids.size() <<  ") and size of image (" << width*height
111             << ") mismatch for image '" << images[0] << "', but must be equal";
112         throw std::runtime_error(err.str());
113       }
114
115       if (has_attributes && ids.size() != attributes.size()) {
116         std::ostringstream err;
117         err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
118             << ") mismatch for image '" << images[0] << "', but must be equal";
119         throw std::runtime_error(err.str());
120       }
121
122       if (has_datas && ids.size() != datas.size()) {
123         std::ostringstream err;
124         err << "Number of ids (" << ids.size() <<  ") and datas (" << datas.size()
125             << ") mismatch for image '" << images[0] << "', but must be equal";
126         throw std::runtime_error(err.str());
127       }
128
129       for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
130         if (ids[i] == 0)
131           continue;
132
133         if(ids[i] >= tiles.size())
134           tiles.resize(ids[i]+1, 0);
135
136         int x = 32*(i % width);
137         int y = 32*(i / width);
138         std::auto_ptr<Tile> tile(new Tile(this, images, Rect(x, y, x + 32, y + 32),
139                                           (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps));
140         if (tiles[ids[i]] == 0) {
141           tiles[ids[i]] = tile.release();
142         } else {
143           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
144         }
145       }
146     } else if(iter.item() == "properties") {
147       // deprecated
148     } else {
149       log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
150     }
151   }
152   if (0)
153   { // enable this if you want to see a list of free tiles
154     log_info << "Last Tile ID is " << tiles.size()-1 << std::endl;
155     int last = -1;
156     for(int i = 0; i < int(tiles.size()); ++i)
157     {
158       if (tiles[i] == 0 && last == -1)
159       {
160         last = i;
161       }
162       else if (tiles[i] && last != -1)
163       {
164         log_info << "Free Tile IDs (" << i - last << "): " << last << " - " << i-1 << std::endl;
165         last = -1;
166       }
167     }
168   }
169   if (0)
170   { // enable this to dump the (large) list of tiles to log_debug
171     // Two dumps are identical iff the tilesets specify identical tiles
172     log_debug << "Tileset in " << filename << std::endl;
173     for(int i = 0; i < int(tiles.size()); ++i)
174     {
175       if(tiles[i] != 0)
176       {
177         tiles[i]->print_debug(i);
178       }
179     }
180   }
181 }
182
183 TileSet::~TileSet()
184 {
185   if(tiles_loaded) {
186     for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
187       delete *i;
188   }
189 }
190
191 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
192                     uint32_t offset)
193 {
194   for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
195     uint32_t dest_id = id - start + offset;
196
197     if(dest_id >= tiles.size())
198       tiles.resize(dest_id + 1, 0);
199
200     if(dest_id == 0)
201       continue;
202
203     Tile *tile = tileset->tiles[id];
204     if(tile == NULL)
205       continue;
206
207     if(tiles[dest_id] != NULL) {
208       log_warning << "tileset merge resulted in multiple definitions for id "
209                   << dest_id << "(originally " << id << ")" << std::endl;
210     }
211     tiles[dest_id] = tile;
212   }
213 }
214
215 /* EOF */