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) :
35 Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect,
36 uint32_t attributes, uint32_t data, float animfps) :
40 attributes(attributes),
44 for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
45 imagespecs.push_back(ImageSpec(*i, rect));
52 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
59 Tile::parse(const Reader& reader)
62 if(!reader.get("id", id)) {
63 throw std::runtime_error("Missing tile-id.");
67 if(reader.get("solid", value) && value)
69 if(reader.get("unisolid", value) && value)
70 attributes |= UNISOLID | SOLID;
71 if(reader.get("brick", value) && value)
73 if(reader.get("ice", value) && value)
75 if(reader.get("water", value) && value)
77 if(reader.get("hurts", value) && value)
79 if(reader.get("fire", value) && value)
81 if(reader.get("fullbox", value) && value)
82 attributes |= FULLBOX;
83 if(reader.get("coin", value) && value)
85 if(reader.get("goal", value) && value)
88 if(reader.get("north", value) && value)
89 data |= WORLDMAP_NORTH;
90 if(reader.get("south", value) && value)
91 data |= WORLDMAP_SOUTH;
92 if(reader.get("west", value) && value)
93 data |= WORLDMAP_WEST;
94 if(reader.get("east", value) && value)
95 data |= WORLDMAP_EAST;
96 if(reader.get("stop", value) && value)
97 data |= WORLDMAP_STOP;
99 reader.get("data", data);
100 reader.get("anim-fps", anim_fps);
102 if(reader.get("slope-type", data)) {
103 attributes |= SOLID | SLOPE;
106 const lisp::Lisp* images;
108 images = reader.get_lisp("editor-images");
110 parse_images(*images);
113 images = reader.get_lisp("images");
115 parse_images(*images);
120 correct_attributes();
125 Tile::parse_images(const Reader& images_lisp)
127 const lisp::Lisp* list = &images_lisp;
129 const lisp::Lisp* cur = list->get_car();
130 if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
133 imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
134 } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
135 cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
136 cur->get_car()->get_symbol() == "region") {
137 const lisp::Lisp* ptr = cur->get_cdr();
140 float x = 0, y = 0, w = 0, h = 0;
141 ptr->get_car()->get(file); ptr = ptr->get_cdr();
142 ptr->get_car()->get(x); ptr = ptr->get_cdr();
143 ptr->get_car()->get(y); ptr = ptr->get_cdr();
144 ptr->get_car()->get(w); ptr = ptr->get_cdr();
145 ptr->get_car()->get(h);
146 imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
148 log_warning << "Expected string or list in images tag" << std::endl;
152 list = list->get_cdr();
159 const std::string& tiles_path = tileset->tiles_path;
161 assert(images.size() == 0);
162 for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
163 imagespecs.end(); ++i) {
164 const ImageSpec& spec = *i;
166 std::string file = tiles_path + spec.file;
167 if(spec.rect.get_width() <= 0) {
168 surface = new Surface(file);
170 surface = new Surface(file,
171 (int) spec.rect.p1.x,
172 (int) spec.rect.p1.y,
173 (int) spec.rect.get_width(),
174 (int) spec.rect.get_height());
176 images.push_back(surface);
181 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
183 if(images.size() > 1) {
184 size_t frame = size_t(game_time * anim_fps) % images.size();
185 context.draw_surface(images[frame], pos, z_pos);
186 } else if (images.size() == 1) {
187 context.draw_surface(images[0], pos, z_pos);
191 void Tile::correct_attributes()
193 //Fix little oddities in attributes (not many, currently...)
194 if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
196 //But still be vocal about it
197 log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;