Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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 "lisp/lisp.hpp"
21 #include "supertux/tile_set.hpp"
22 #include "supertux/timer.hpp"
23 #include "video/drawing_context.hpp"
24
25 Tile::Tile(const TileSet *new_tileset) :
26   tileset(new_tileset), 
27   attributes(0), 
28   data(0), 
29   anim_fps(1)
30 {
31 }
32
33 Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect, 
34            uint32_t attributes, uint32_t data, float animfps) :
35   tileset(new_tileset),
36   attributes(attributes), 
37   data(data), 
38   anim_fps(animfps)
39 {
40   for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
41     imagespecs.push_back(ImageSpec(*i, rect));
42   }
43   correct_attributes();
44 }
45
46 Tile::~Tile()
47 {
48   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
49       ++i) {
50     delete *i;
51   }
52 }
53
54 uint32_t
55 Tile::parse(const Reader& reader)
56 {
57   uint32_t id;
58   if(!reader.get("id", id)) {
59     throw std::runtime_error("Missing tile-id.");
60   }
61
62   bool value = false;
63   if(reader.get("solid", value) && value)
64     attributes |= SOLID;
65   if(reader.get("unisolid", value) && value)
66     attributes |= UNISOLID | SOLID;
67   if(reader.get("brick", value) && value)
68     attributes |= BRICK;
69   if(reader.get("ice", value) && value)
70     attributes |= ICE;
71   if(reader.get("water", value) && value)
72     attributes |= WATER;
73   if(reader.get("hurts", value) && value)
74     attributes |= HURTS;
75   if(reader.get("fire", value) && value)
76     attributes |= FIRE;
77   if(reader.get("fullbox", value) && value)
78     attributes |= FULLBOX;
79   if(reader.get("coin", value) && value)
80     attributes |= COIN;
81   if(reader.get("goal", value) && value)
82     attributes |= GOAL;
83
84   if(reader.get("north", value) && value)
85     data |= WORLDMAP_NORTH;
86   if(reader.get("south", value) && value)
87     data |= WORLDMAP_SOUTH;
88   if(reader.get("west", value) && value)
89     data |= WORLDMAP_WEST;
90   if(reader.get("east", value) && value)
91     data |= WORLDMAP_EAST;
92   if(reader.get("stop", value) && value)
93     data |= WORLDMAP_STOP;
94
95   reader.get("data", data);
96   reader.get("anim-fps", anim_fps);
97
98   if(reader.get("slope-type", data)) {
99     attributes |= SOLID | SLOPE;
100   }
101
102   const lisp::Lisp* images;
103 #ifdef DEBUG
104   images = reader.get_lisp("editor-images");
105   if(images)
106     parse_images(*images);
107   else {
108 #endif /*DEBUG*/
109     images = reader.get_lisp("images");
110     if(images)
111       parse_images(*images);
112 #ifdef DEBUG
113   }
114 #endif /*DEBUG*/
115
116   correct_attributes();
117   return id;
118 }
119
120 void
121 Tile::parse_images(const Reader& images_lisp)
122 {
123   const lisp::Lisp* list = &images_lisp;
124   while(list) {
125     const lisp::Lisp* cur = list->get_car();
126     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
127       std::string file;
128       cur->get(file);
129       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
130     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
131               cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
132               cur->get_car()->get_symbol() == "region") {
133       const lisp::Lisp* ptr = cur->get_cdr();
134
135       std::string file;
136       float x = 0, y = 0, w = 0, h = 0;
137       ptr->get_car()->get(file); ptr = ptr->get_cdr();
138       ptr->get_car()->get(x); ptr = ptr->get_cdr();
139       ptr->get_car()->get(y); ptr = ptr->get_cdr();
140       ptr->get_car()->get(w); ptr = ptr->get_cdr();
141       ptr->get_car()->get(h);
142       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
143     } else {
144       log_warning << "Expected string or list in images tag" << std::endl;
145       continue;
146     }
147
148     list = list->get_cdr();
149   }
150 }
151
152 void
153 Tile::load_images()
154 {
155   const std::string& tiles_path = tileset->tiles_path;
156
157   assert(images.size() == 0);
158   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
159         imagespecs.end(); ++i) {
160     const ImageSpec& spec = *i;
161     Surface* surface;
162     std::string file = tiles_path + spec.file;
163     if(spec.rect.get_width() <= 0) {
164       surface = new Surface(file);
165     } else {
166       surface = new Surface(file,
167                             (int) spec.rect.p1.x,
168                             (int) spec.rect.p1.y,
169                             (int) spec.rect.get_width(),
170                             (int) spec.rect.get_height());
171     }
172     images.push_back(surface);
173   }
174 }
175
176 void
177 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
178 {
179   if(images.size() > 1) {
180     size_t frame = size_t(game_time * anim_fps) % images.size();
181     context.draw_surface(images[frame], pos, z_pos);
182   } else if (images.size() == 1) {
183     context.draw_surface(images[0], pos, z_pos);
184   }
185 }
186
187 void Tile::correct_attributes()
188 {
189   //Fix little oddities in attributes (not many, currently...)
190   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
191     attributes |= SOLID;
192     //But still be vocal about it
193     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
194   }
195 }
196
197 /* EOF */