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