2 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "supertux/tile.hpp"
20 #include "lisp/lisp.hpp"
21 #include "supertux/tile_set.hpp"
22 #include "supertux/timer.hpp"
23 #include "video/drawing_context.hpp"
25 Tile::Tile(const TileSet *new_tileset) :
33 Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect,
34 uint32_t attributes, uint32_t data, float animfps) :
36 attributes(attributes),
40 for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
41 imagespecs.push_back(ImageSpec(*i, rect));
48 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
55 Tile::parse(const Reader& reader)
58 if(!reader.get("id", id)) {
59 throw std::runtime_error("Missing tile-id.");
63 if(reader.get("solid", value) && value)
65 if(reader.get("unisolid", value) && value)
66 attributes |= UNISOLID | SOLID;
67 if(reader.get("brick", value) && value)
69 if(reader.get("ice", value) && value)
71 if(reader.get("water", value) && value)
73 if(reader.get("hurts", value) && value)
75 if(reader.get("fire", value) && value)
77 if(reader.get("fullbox", value) && value)
78 attributes |= FULLBOX;
79 if(reader.get("coin", value) && value)
81 if(reader.get("goal", value) && value)
84 if(reader.get("north", value) && value)
85 data |= WORLDMAP_NORTH;
86 if(reader.get("south", value) && value)
87 data |= WORLDMAP_SOUTH;
88 if(reader.get("west", value) && value)
89 data |= WORLDMAP_WEST;
90 if(reader.get("east", value) && value)
91 data |= WORLDMAP_EAST;
92 if(reader.get("stop", value) && value)
93 data |= WORLDMAP_STOP;
95 reader.get("data", data);
96 reader.get("anim-fps", anim_fps);
98 if(reader.get("slope-type", data)) {
99 attributes |= SOLID | SLOPE;
102 const lisp::Lisp* images;
104 images = reader.get_lisp("editor-images");
106 parse_images(*images);
109 images = reader.get_lisp("images");
111 parse_images(*images);
116 correct_attributes();
121 Tile::parse_images(const Reader& images_lisp)
123 const lisp::Lisp* list = &images_lisp;
125 const lisp::Lisp* cur = list->get_car();
126 if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
129 imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
130 } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
131 cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
132 cur->get_car()->get_symbol() == "region") {
133 const lisp::Lisp* ptr = cur->get_cdr();
136 float x = 0, y = 0, w = 0, h = 0;
137 ptr->get_car()->get(file); ptr = ptr->get_cdr();
138 ptr->get_car()->get(x); ptr = ptr->get_cdr();
139 ptr->get_car()->get(y); ptr = ptr->get_cdr();
140 ptr->get_car()->get(w); ptr = ptr->get_cdr();
141 ptr->get_car()->get(h);
142 imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
144 log_warning << "Expected string or list in images tag" << std::endl;
148 list = list->get_cdr();
155 const std::string& tiles_path = tileset->tiles_path;
157 assert(images.size() == 0);
158 for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
159 imagespecs.end(); ++i) {
160 const ImageSpec& spec = *i;
162 std::string file = tiles_path + spec.file;
163 if(spec.rect.get_width() <= 0) {
164 surface = new Surface(file);
166 surface = new Surface(file,
167 (int) spec.rect.p1.x,
168 (int) spec.rect.p1.y,
169 (int) spec.rect.get_width(),
170 (int) spec.rect.get_height());
172 images.push_back(surface);
177 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
179 if(images.size() > 1) {
180 size_t frame = size_t(game_time * anim_fps) % images.size();
181 context.draw_surface(images[frame], pos, z_pos);
182 } else if (images.size() == 1) {
183 context.draw_surface(images[0], pos, z_pos);
187 void Tile::correct_attributes()
189 //Fix little oddities in attributes (not many, currently...)
190 if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
192 //But still be vocal about it
193 log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;