Replaced more lisp::Lisp/lisp::Writer with Reader/Writer
[supertux.git] / src / supertux / tile.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
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.
9 //
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.
14 //
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/>.
17
18 #include "supertux/tile.hpp"
19
20 #include "supertux/tile_set.hpp"
21 #include "supertux/timer.hpp"
22 #include "util/reader.hpp"
23 #include "video/drawing_context.hpp"
24
25 Tile::Tile(const TileSet *new_tileset) :
26   tileset(new_tileset), 
27   imagespecs(),
28   images(),
29   attributes(0), 
30   data(0), 
31   anim_fps(1)
32 {
33 }
34
35 Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect, 
36            uint32_t attributes, uint32_t data, float animfps) :
37   tileset(new_tileset),
38   imagespecs(),
39   images(),
40   attributes(attributes), 
41   data(data), 
42   anim_fps(animfps)
43 {
44   for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
45     imagespecs.push_back(ImageSpec(*i, rect));
46   }
47   correct_attributes();
48 }
49
50 Tile::~Tile()
51 {
52   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
53       ++i) {
54     delete *i;
55   }
56 }
57
58 uint32_t
59 Tile::parse(const Reader& reader)
60 {
61   uint32_t id;
62   if(!reader.get("id", id)) {
63     throw std::runtime_error("Missing tile-id.");
64   }
65
66   bool value = false;
67   if(reader.get("solid", value) && value)
68     attributes |= SOLID;
69   if(reader.get("unisolid", value) && value)
70     attributes |= UNISOLID | SOLID;
71   if(reader.get("brick", value) && value)
72     attributes |= BRICK;
73   if(reader.get("ice", value) && value)
74     attributes |= ICE;
75   if(reader.get("water", value) && value)
76     attributes |= WATER;
77   if(reader.get("hurts", value) && value)
78     attributes |= HURTS;
79   if(reader.get("fire", value) && value)
80     attributes |= FIRE;
81   if(reader.get("fullbox", value) && value)
82     attributes |= FULLBOX;
83   if(reader.get("coin", value) && value)
84     attributes |= COIN;
85   if(reader.get("goal", value) && value)
86     attributes |= GOAL;
87
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;
98
99   reader.get("data", data);
100   reader.get("anim-fps", anim_fps);
101
102   if(reader.get("slope-type", data)) {
103     attributes |= SOLID | SLOPE;
104   }
105
106   const lisp::Lisp* images;
107 #ifdef DEBUG
108   images = reader.get_lisp("editor-images");
109   if(images)
110     parse_images(*images);
111   else {
112 #endif /*DEBUG*/
113     images = reader.get_lisp("images");
114     if(images)
115       parse_images(*images);
116 #ifdef DEBUG
117   }
118 #endif /*DEBUG*/
119
120   correct_attributes();
121   return id;
122 }
123
124 void
125 Tile::parse_images(const Reader& images_lisp)
126 {
127   const lisp::Lisp* list = &images_lisp;
128   while(list) {
129     const lisp::Lisp* cur = list->get_car();
130     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
131       std::string file;
132       cur->get(file);
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();
138
139       std::string file;
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)));
147     } else {
148       log_warning << "Expected string or list in images tag" << std::endl;
149       continue;
150     }
151
152     list = list->get_cdr();
153   }
154 }
155
156 void
157 Tile::load_images()
158 {
159   const std::string& tiles_path = tileset->tiles_path;
160
161   assert(images.size() == 0);
162   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
163         imagespecs.end(); ++i) {
164     const ImageSpec& spec = *i;
165     Surface* surface;
166     std::string file = tiles_path + spec.file;
167     if(spec.rect.get_width() <= 0) {
168       surface = new Surface(file);
169     } else {
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());
175     }
176     images.push_back(surface);
177   }
178 }
179
180 void
181 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
182 {
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);
188   }
189 }
190
191 void Tile::correct_attributes()
192 {
193   //Fix little oddities in attributes (not many, currently...)
194   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
195     attributes |= SOLID;
196     //But still be vocal about it
197     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
198   }
199 }
200
201 /* EOF */