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