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