4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
27 #include "lisp/lisp.hpp"
29 #include "resources.hpp"
31 #include "math/vector.hpp"
32 #include "video/drawing_context.hpp"
35 : id(0), editor_image(0), attributes(0), data(0), anim_fps(1)
39 Tile::Tile(unsigned int id_, Uint32 attributes_, const ImageSpec& imagespec)
40 : id(id_), editor_image(0), attributes(attributes_), data(0), anim_fps(1)
42 imagespecs.push_back(imagespec);
47 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
55 Tile::parse(const lisp::Lisp& reader)
57 if(!reader.get("id", id)) {
58 throw std::runtime_error("Missing tile-id.");
62 if(reader.get("solid", value) && value)
64 if(reader.get("unisolid", value) && value)
65 attributes |= UNISOLID | SOLID;
66 if(reader.get("brick", value) && value)
68 if(reader.get("ice", value) && value)
70 if(reader.get("water", value) && value)
72 if(reader.get("spike", value) && value)
74 if(reader.get("fullbox", value) && value)
75 attributes |= FULLBOX;
76 if(reader.get("coin", value) && value)
78 if(reader.get("goal", value) && value)
81 if(reader.get("north", value) && value)
82 data |= WORLDMAP_NORTH;
83 if(reader.get("south", value) && value)
84 data |= WORLDMAP_SOUTH;
85 if(reader.get("west", value) && value)
86 data |= WORLDMAP_WEST;
87 if(reader.get("east", value) && value)
88 data |= WORLDMAP_EAST;
89 if(reader.get("stop", value) && value)
90 data |= WORLDMAP_STOP;
92 reader.get("data", data);
93 reader.get("anim-fps", anim_fps);
95 if(reader.get("slope-type", data)) {
96 attributes |= SOLID | SLOPE;
99 const lisp::Lisp* images = reader.get_lisp("images");
101 parse_images(*images);
102 reader.get("editor-images", editor_imagefile);
106 Tile::parse_images(const lisp::Lisp& images_lisp)
108 const lisp::Lisp* list = &images_lisp;
110 const lisp::Lisp* cur = list->get_car();
111 if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
114 imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
115 } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
116 cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL) {
117 const lisp::Lisp* ptr = cur->get_cdr();
120 float x = 0, y = 0, w = 0, h = 0;
121 ptr->get_car()->get(file); ptr = ptr->get_cdr();
122 ptr->get_car()->get(x); ptr = ptr->get_cdr();
123 ptr->get_car()->get(y); ptr = ptr->get_cdr();
124 ptr->get_car()->get(w); ptr = ptr->get_cdr();
125 ptr->get_car()->get(h);
126 imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
128 std::cerr << "Expected string or list in images tag.\n";
132 list = list->get_cdr();
137 Tile::load_images(const std::string& tilesetpath)
139 assert(images.size() == 0);
140 for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
141 imagespecs.end(); ++i) {
142 const ImageSpec& spec = *i;
144 std::string file = tilesetpath + spec.file;
145 if(spec.rect.get_width() <= 0) {
146 surface = new Surface(file);
148 surface = new Surface(file,
149 (int) spec.rect.p1.x,
150 (int) spec.rect.p1.y,
151 (int) spec.rect.get_width(),
152 (int) spec.rect.get_height());
154 images.push_back(surface);
156 if(editor_imagefile != "") {
157 editor_image = new Surface(tilesetpath + editor_imagefile);
162 Tile::get_editor_image() const
166 if(images.size() > 0)
173 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
175 if(images.size() > 1) {
176 size_t frame = size_t(game_time * anim_fps) % images.size();
177 context.draw_surface(images[frame], pos, layer);
178 } else if (images.size() == 1) {
179 context.draw_surface(images[0], pos, layer);