Unified Messaging Subsystem
[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 #include "msg.hpp"
34
35
36 Tile::Tile()
37   : id(0), editor_image(0), attributes(0), data(0), anim_fps(1)
38 {
39 }
40
41 Tile::Tile(unsigned int id_, Uint32 attributes_, const ImageSpec& imagespec)
42   : id(id_), editor_image(0), attributes(attributes_), data(0), anim_fps(1)
43 {
44   imagespecs.push_back(imagespec);
45 }
46
47 Tile::~Tile()
48 {
49   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
50       ++i) {
51     delete *i;
52   }
53   delete editor_image;
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   reader.get("editor-images", editor_imagefile);
105 }
106
107 void
108 Tile::parse_images(const lisp::Lisp& images_lisp)
109 {
110   const lisp::Lisp* list = &images_lisp;
111   while(list) {
112     const lisp::Lisp* cur = list->get_car();
113     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
114       std::string file;
115       cur->get(file);
116       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
117     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS && 
118         cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL) {
119       const lisp::Lisp* ptr = cur->get_cdr();
120
121       std::string file;
122       float x = 0, y = 0, w = 0, h = 0;
123       ptr->get_car()->get(file); ptr = ptr->get_cdr();
124       ptr->get_car()->get(x); ptr = ptr->get_cdr();
125       ptr->get_car()->get(y); ptr = ptr->get_cdr();
126       ptr->get_car()->get(w); ptr = ptr->get_cdr();
127       ptr->get_car()->get(h);
128       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
129     } else {
130       msg_warning("Expected string or list in images tag");
131       continue;
132     }
133     
134     list = list->get_cdr();
135   }
136 }
137
138 void
139 Tile::load_images(const std::string& tilesetpath)
140 {
141   assert(images.size() == 0);
142   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
143       imagespecs.end(); ++i) {
144     const ImageSpec& spec = *i;
145     Surface* surface;
146     std::string file = tilesetpath + spec.file;
147     if(spec.rect.get_width() <= 0) {
148       surface = new Surface(file);
149     } else {
150       surface = new Surface(file,
151           (int) spec.rect.p1.x,
152           (int) spec.rect.p1.y,
153           (int) spec.rect.get_width(),
154           (int) spec.rect.get_height());
155     }
156     images.push_back(surface);
157   }
158   if(editor_imagefile != "") {
159     editor_image = new Surface(tilesetpath + editor_imagefile);
160   }
161 }
162
163 Surface*
164 Tile::get_editor_image() const
165 {
166   if(editor_image)
167     return editor_image;
168   if(images.size() > 0)
169     return images[0];
170
171   return 0;
172 }
173
174 void
175 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
176 {
177   if(images.size() > 1) {
178     size_t frame = size_t(game_time * anim_fps) % images.size();
179     context.draw_surface(images[frame], pos, layer);
180   } else if (images.size() == 1) {
181     context.draw_surface(images[0], pos, layer);
182   }
183 }
184