Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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), editor_image(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_), editor_image(0), 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   delete editor_image;
55 }
56
57 void
58 Tile::parse(const lisp::Lisp& reader)
59 {
60   if(!reader.get("id", id)) {
61     throw std::runtime_error("Missing tile-id.");
62   }
63   
64   bool value = false;
65   if(reader.get("solid", value) && value)
66     attributes |= SOLID;
67   if(reader.get("unisolid", value) && value)
68     attributes |= UNISOLID | SOLID;
69   if(reader.get("brick", value) && value)
70     attributes |= BRICK;
71   if(reader.get("ice", value) && value)
72     attributes |= ICE;
73   if(reader.get("water", value) && value)
74     attributes |= WATER;
75   if(reader.get("hurts", value) && value)
76     attributes |= HURTS;
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 = reader.get_lisp("images");
103   if(images)
104     parse_images(*images);
105   reader.get("editor-images", editor_imagefile);
106 }
107
108 void
109 Tile::parse_images(const lisp::Lisp& images_lisp)
110 {
111   const lisp::Lisp* list = &images_lisp;
112   while(list) {
113     const lisp::Lisp* cur = list->get_car();
114     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
115       std::string file;
116       cur->get(file);
117       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
118     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS && 
119         cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL) {
120       const lisp::Lisp* ptr = cur->get_cdr();
121
122       std::string file;
123       float x = 0, y = 0, w = 0, h = 0;
124       ptr->get_car()->get(file); ptr = ptr->get_cdr();
125       ptr->get_car()->get(x); ptr = ptr->get_cdr();
126       ptr->get_car()->get(y); ptr = ptr->get_cdr();
127       ptr->get_car()->get(w); ptr = ptr->get_cdr();
128       ptr->get_car()->get(h);
129       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
130     } else {
131       log_warning << "Expected string or list in images tag" << std::endl;
132       continue;
133     }
134     
135     list = list->get_cdr();
136   }
137 }
138
139 void
140 Tile::load_images(const std::string& tilesetpath)
141 {
142   assert(images.size() == 0);
143   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
144       imagespecs.end(); ++i) {
145     const ImageSpec& spec = *i;
146     Surface* surface;
147     std::string file = tilesetpath + spec.file;
148     if(spec.rect.get_width() <= 0) {
149       surface = new Surface(file);
150     } else {
151       surface = new Surface(file,
152           (int) spec.rect.p1.x,
153           (int) spec.rect.p1.y,
154           (int) spec.rect.get_width(),
155           (int) spec.rect.get_height());
156     }
157     images.push_back(surface);
158   }
159   if(editor_imagefile != "") {
160     editor_image = new Surface(tilesetpath + editor_imagefile);
161   }
162 }
163
164 Surface*
165 Tile::get_editor_image() const
166 {
167   if(editor_image)
168     return editor_image;
169   if(images.size() > 0)
170     return images[0];
171
172   return 0;
173 }
174
175 void
176 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
177 {
178   if(images.size() > 1) {
179     size_t frame = size_t(game_time * anim_fps) % images.size();
180     context.draw_surface(images[frame], pos, layer);
181   } else if (images.size() == 1) {
182     context.draw_surface(images[0], pos, layer);
183   }
184 }
185