9e0d39bdd27d6a0fcc30799d66813be05987ff96
[supertux.git] / src / tile.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21
22 #include <config.h>
23
24 #include "tile.hpp"
25
26 #include <math.h>
27 #include <assert.h>
28 #include <iostream>
29 #include <stdexcept>
30
31 #include "lisp/lisp.hpp"
32 #include "tile_set.hpp"
33 #include "timer.hpp"
34 #include "math/vector.hpp"
35 #include "video/drawing_context.hpp"
36 #include "log.hpp"
37
38
39 Tile::Tile(const TileSet *new_tileset)
40   : tileset(new_tileset), attributes(0), data(0), anim_fps(1)
41 {
42 }
43
44 Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect, Uint32 attributes, Uint32 data, float animfps)
45   : tileset(new_tileset), attributes(attributes), data(data), anim_fps(animfps)
46 {
47   for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
48       imagespecs.push_back(ImageSpec(*i, rect));
49   }
50   correct_attributes();
51 }
52
53 Tile::~Tile()
54 {
55   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
56       ++i) {
57     delete *i;
58   }
59 }
60
61 uint32_t
62 Tile::parse(const lisp::Lisp& reader)
63 {
64   uint32_t id;
65   if(!reader.get("id", id)) {
66     throw std::runtime_error("Missing tile-id.");
67   }
68
69   bool value = false;
70   if(reader.get("solid", value) && value)
71     attributes |= SOLID;
72   if(reader.get("unisolid", value) && value)
73     attributes |= UNISOLID | SOLID;
74   if(reader.get("brick", value) && value)
75     attributes |= BRICK;
76   if(reader.get("ice", value) && value)
77     attributes |= ICE;
78   if(reader.get("water", value) && value)
79     attributes |= WATER;
80   if(reader.get("hurts", value) && value)
81     attributes |= HURTS;
82   if(reader.get("fire", value) && value)
83     attributes |= FIRE;
84   if(reader.get("fullbox", value) && value)
85     attributes |= FULLBOX;
86   if(reader.get("coin", value) && value)
87     attributes |= COIN;
88   if(reader.get("goal", value) && value)
89     attributes |= GOAL;
90
91   if(reader.get("north", value) && value)
92     data |= WORLDMAP_NORTH;
93   if(reader.get("south", value) && value)
94     data |= WORLDMAP_SOUTH;
95   if(reader.get("west", value) && value)
96     data |= WORLDMAP_WEST;
97   if(reader.get("east", value) && value)
98     data |= WORLDMAP_EAST;
99   if(reader.get("stop", value) && value)
100     data |= WORLDMAP_STOP;
101
102   reader.get("data", data);
103   reader.get("anim-fps", anim_fps);
104
105   if(reader.get("slope-type", data)) {
106     attributes |= SOLID | SLOPE;
107   }
108
109   const lisp::Lisp* images;
110 #ifdef DEBUG
111   images = reader.get_lisp("editor-images");
112   if(images)
113     parse_images(*images);
114   else {
115 #endif /*DEBUG*/
116     images = reader.get_lisp("images");
117     if(images)
118       parse_images(*images);
119 #ifdef DEBUG
120   }
121 #endif /*DEBUG*/
122
123   correct_attributes();
124   return id;
125 }
126
127 void
128 Tile::parse_images(const lisp::Lisp& images_lisp)
129 {
130   const lisp::Lisp* list = &images_lisp;
131   while(list) {
132     const lisp::Lisp* cur = list->get_car();
133     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
134       std::string file;
135       cur->get(file);
136       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
137     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
138               cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
139               cur->get_car()->get_symbol() == "region") {
140       const lisp::Lisp* ptr = cur->get_cdr();
141
142       std::string file;
143       float x = 0, y = 0, w = 0, h = 0;
144       ptr->get_car()->get(file); ptr = ptr->get_cdr();
145       ptr->get_car()->get(x); ptr = ptr->get_cdr();
146       ptr->get_car()->get(y); ptr = ptr->get_cdr();
147       ptr->get_car()->get(w); ptr = ptr->get_cdr();
148       ptr->get_car()->get(h);
149       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
150     } else {
151       log_warning << "Expected string or list in images tag" << std::endl;
152       continue;
153     }
154
155     list = list->get_cdr();
156   }
157 }
158
159 void
160 Tile::load_images()
161 {
162   const std::string& tiles_path = tileset->tiles_path;
163
164   assert(images.size() == 0);
165   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
166       imagespecs.end(); ++i) {
167     const ImageSpec& spec = *i;
168     Surface* surface;
169     std::string file = tiles_path + spec.file;
170     if(spec.rect.get_width() <= 0) {
171       surface = new Surface(file);
172     } else {
173       surface = new Surface(file,
174           (int) spec.rect.p1.x,
175           (int) spec.rect.p1.y,
176           (int) spec.rect.get_width(),
177           (int) spec.rect.get_height());
178     }
179     images.push_back(surface);
180   }
181 }
182
183 void
184 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
185 {
186   if(images.size() > 1) {
187     size_t frame = size_t(game_time * anim_fps) % images.size();
188     context.draw_surface(images[frame], pos, z_pos);
189   } else if (images.size() == 1) {
190     context.draw_surface(images[0], pos, z_pos);
191   }
192 }
193
194 void Tile::correct_attributes()
195 {
196   //Fix little oddities in attributes (not many, currently...)
197   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
198     attributes |= SOLID;
199     //But still be vocal about it
200     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
201   }
202 }