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