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 "app/globals.h"
30 #include "utils/lispreader.h"
31 #include "math/vector.h"
32 #include "video/drawing_context.h"
34 /** Dirty little helper to create a surface from a snipped of lisp:
37 * (region "filename" x y w h)
40 Surface* create_surface(lisp_object_t* cur)
42 if (lisp_string_p(cur))
44 return new Surface(datadir + "/images/tilesets/" + lisp_string(cur),
47 else if (lisp_cons_p(cur) && lisp_symbol_p(lisp_car(cur)))
49 lisp_object_t* sym = lisp_car(cur);
50 lisp_object_t* data = lisp_cdr(cur);
52 if (strcmp(lisp_symbol(sym), "region") == 0)
54 if (lisp_list_length(data) == 5) // (image-region filename x y w h)
56 return new Surface(datadir + "/images/tilesets/" + lisp_string(lisp_car(data)),
57 lisp_integer(lisp_list_nth(data, 1)),
58 lisp_integer(lisp_list_nth(data, 2)),
59 lisp_integer(lisp_list_nth(data, 3)),
60 lisp_integer(lisp_list_nth(data, 4)),
65 std::cout << "Tile: Type mispatch, should be '(region \"somestring\" x y w h)'" << std::endl;
71 std::cout << "Tile: Unhandled tag: " << lisp_symbol(sym) << std::endl;
76 std::cout << "Tile: unhandled element" << std::endl;
80 /** Create a vector of surfaces (aka Sprite) from a piece of lisp:
81 ((image "bla.png") (image-region "bla.png") ...)
84 std::vector<Surface*> create_surfaces(lisp_object_t* cur)
86 std::vector<Surface*> surfs;
90 Surface* surface = create_surface(lisp_car(cur));
92 surfs.push_back(surface);
94 std::cout << "Tile: Couldn't create image" << std::endl;
103 : id(0), attributes(0), data(0), next_tile(0), anim_speed(25)
109 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
113 for(std::vector<Surface*>::iterator i = editor_images.begin();
114 i != editor_images.end(); ++i) {
120 Tile::read(LispReader& reader)
122 if(!reader.read_uint("id", id)) {
123 throw std::runtime_error("Missing tile-id.");
127 if(reader.read_bool("solid", value) && value)
129 if(reader.read_bool("unisolid", value) && value)
130 attributes |= UNISOLID | SOLID;
131 if(reader.read_bool("brick", value) && value)
133 if(reader.read_bool("ice", value) && value)
135 if(reader.read_bool("water", value) && value)
137 if(reader.read_bool("spike", value) && value)
139 if(reader.read_bool("fullbox", value) && value)
140 attributes |= FULLBOX;
141 if(reader.read_bool("distro", value) && value)
143 if(reader.read_bool("coin", value) && value)
145 if(reader.read_bool("goal", value) && value)
148 reader.read_int("data", data);
149 reader.read_int("anim-speed", anim_speed);
150 reader.read_int("next-tile", next_tile);
152 if(reader.read_int("slope-type", data)) {
153 attributes |= SOLID | SLOPE;
156 images = create_surfaces(reader.read_lisp("images"));
157 editor_images = create_surfaces(reader.read_lisp("editor-images"));
161 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
163 if(images.size() > 1) {
164 size_t frame = ((global_frame_counter*25) / anim_speed) % images.size();
165 context.draw_surface(images[frame], pos, layer);
166 } else if (images.size() == 1) {
167 context.draw_surface(images[0], pos, layer);