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
28 #include "app/globals.h"
31 #include "utils/lispreader.h"
32 #include "math/vector.h"
33 #include "video/drawing_context.h"
35 /** Dirty little helper to create a surface from a snipped of lisp:
38 * (region "filename" x y w h)
41 Surface* create_surface(lisp_object_t* cur)
43 if (lisp_string_p(cur))
45 return new Surface(datadir + "/images/tilesets/" + lisp_string(cur),
48 else if (lisp_cons_p(cur) && lisp_symbol_p(lisp_car(cur)))
50 lisp_object_t* sym = lisp_car(cur);
51 lisp_object_t* data = lisp_cdr(cur);
53 if (strcmp(lisp_symbol(sym), "region") == 0)
55 if (lisp_list_length(data) == 5) // (image-region filename x y w h)
57 return new Surface(datadir + "/images/tilesets/" + lisp_string(lisp_car(data)),
58 lisp_integer(lisp_list_nth(data, 1)),
59 lisp_integer(lisp_list_nth(data, 2)),
60 lisp_integer(lisp_list_nth(data, 3)),
61 lisp_integer(lisp_list_nth(data, 4)),
66 std::cout << "Tile: Type mispatch, should be '(region \"somestring\" x y w h)'" << std::endl;
72 std::cout << "Tile: Unhandled tag: " << lisp_symbol(sym) << std::endl;
77 std::cout << "Tile: unhandled element" << std::endl;
81 /** Create a vector of surfaces (aka Sprite) from a piece of lisp:
82 ((image "bla.png") (image-region "bla.png") ...)
85 std::vector<Surface*> create_surfaces(lisp_object_t* cur)
87 std::vector<Surface*> surfs;
91 Surface* surface = create_surface(lisp_car(cur));
93 surfs.push_back(surface);
95 std::cout << "Tile: Couldn't create image" << std::endl;
104 : id(0), attributes(0), data(0), next_tile(0), anim_speed(25)
110 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
114 for(std::vector<Surface*>::iterator i = editor_images.begin();
115 i != editor_images.end(); ++i) {
121 Tile::read(LispReader& reader)
123 if(!reader.read_uint("id", id)) {
124 throw std::runtime_error("Missing tile-id.");
128 if(reader.read_bool("solid", value) && value)
130 if(reader.read_bool("unisolid", value) && value)
131 attributes |= UNISOLID | SOLID;
132 if(reader.read_bool("brick", value) && value)
134 if(reader.read_bool("ice", value) && value)
136 if(reader.read_bool("water", value) && value)
138 if(reader.read_bool("spike", value) && value)
140 if(reader.read_bool("fullbox", value) && value)
141 attributes |= FULLBOX;
142 if(reader.read_bool("distro", value) && value)
144 if(reader.read_bool("coin", value) && value)
146 if(reader.read_bool("goal", value) && value)
149 reader.read_int("data", data);
150 reader.read_int("anim-speed", anim_speed);
151 reader.read_int("next-tile", next_tile);
153 if(reader.read_int("slope-type", data)) {
154 attributes |= SOLID | SLOPE;
157 images = create_surfaces(reader.read_lisp("images"));
158 editor_images = create_surfaces(reader.read_lisp("editor-images"));
162 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
164 if(images.size() > 1) {
165 size_t frame = ((global_frame_counter*25) / anim_speed) % images.size();
166 context.draw_surface(images[frame], pos, layer);
167 } else if (images.size() == 1) {
168 context.draw_surface(images[0], pos, layer);