qMax <qwiglydee@gmail.com>'s font patch, adds unicode support and support for drawing...
[supertux.git] / src / tile_set.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2008 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
19 //  02111-1307, USA.
20 #include <config.h>
21 #include <stdexcept>
22
23 #include "tile_set.hpp"
24 #include "log.hpp"
25 #include "file_system.hpp"
26 #include "lisp/parser.hpp"
27 #include "lisp/lisp.hpp"
28 #include "lisp/list_iterator.hpp"
29
30 TileSet::TileSet()
31   : tiles_path(""), tiles_loaded(false)
32 {
33   tiles.resize(1, 0);
34   tiles[0] = new Tile(this);
35 }
36
37 TileSet::TileSet(const std::string& filename)
38   : tiles_path(""), tiles_loaded(true)
39 {
40   tiles_path = FileSystem::dirname(filename);
41
42   tiles.resize(1, 0);
43   tiles[0] = new Tile(this);
44
45   lisp::Parser parser;
46   const lisp::Lisp* root = parser.parse(filename);
47
48   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
49   if(!tiles_lisp)
50     throw std::runtime_error("file is not a supertux tiles file.");
51
52   lisp::ListIterator iter(tiles_lisp);
53   while(iter.next()) {
54     if(iter.item() == "tile") {
55       Tile* tile = new Tile(this);
56       uint32_t id = tile->parse(*(iter.lisp()));
57
58       if(id >= tiles.size())
59         tiles.resize(id+1, 0);
60
61       if(tiles[id] != 0) {
62         log_warning << "Tile with ID " << id << " redefined" << std::endl;
63         delete tile;
64       } else {
65         tiles[id] = tile;
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       std::string image;
75
76       // width and height of the image in tile units, this is used for two
77       // purposes:
78       //  a) so we don't have to load the image here to know its dimensions
79       //  b) so that the resulting 'tiles' entry is more robust,
80       //  ie. enlarging the image won't break the tile id mapping
81       // FIXME: height is actually not used, since width might be enough for
82       // all purposes, still feels somewhat more natural this way
83       unsigned int width  = 0;
84       unsigned int height = 0;
85
86       iter.lisp()->get_vector("ids",        ids);
87       iter.lisp()->get_vector("attributes", attributes);
88       iter.lisp()->get("image",      image);
89       iter.lisp()->get("width",      width);
90       iter.lisp()->get("height",     height);
91
92       if (ids.size() != attributes.size()) {
93         std::ostringstream err;
94         err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
95           << ") mismatch for image '" << image << "', but must be equal";
96         throw std::runtime_error(err.str());
97       }
98
99       for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
100         if (ids[i] == 0)
101           continue;
102
103         if(ids[i] >= tiles.size())
104           tiles.resize(ids[i]+1, 0);
105
106         int x = 32*(i % width);
107         int y = 32*(i / width);
108         Tile* tile = new Tile(this, attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
109         if (tiles[ids[i]] == 0) {
110           tiles[ids[i]] = tile;
111         } else {
112           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
113           delete tile;
114         }
115       }
116     } else if(iter.item() == "properties") {
117       // deprecated
118     } else {
119       log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
120     }
121   }
122 }
123
124 TileSet::~TileSet()
125 {
126   if(tiles_loaded) {
127     for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
128       delete *i;
129   }
130 }
131
132 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
133                     uint32_t offset)
134 {
135   for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
136     uint32_t dest_id = id - start + offset;
137
138     if(dest_id >= tiles.size())
139       tiles.resize(dest_id + 1, 0);
140
141     if(dest_id == 0)
142       continue;
143
144     Tile *tile = tileset->tiles[id];
145     if(tile == NULL)
146         continue;
147
148     if(tiles[dest_id] != NULL) {
149       log_warning << "tileset merge resulted in multiple definitions for id "
150                   << dest_id << "(originally " << id << ")" << std::endl;
151     }
152     tiles[dest_id] = tile;
153   }
154 }