* Tiles now have an alpha parameter for semi-transparency (useful in levels like...
[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.h"
28 #include "tile.h"
29 #include "resources.h"
30 #include "timer.h"
31 #include "math/vector.h"
32 #include "video/drawing_context.h"
33
34 Tile::Tile()
35   : id(0), editor_image(0), attributes(0), data(0), anim_fps(1)
36 {
37 }
38
39 Tile::~Tile()
40 {
41   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
42       ++i) {
43     delete *i;
44   }
45   delete editor_image;
46 }
47
48 void
49 Tile::parse(const lisp::Lisp& reader)
50 {
51   if(!reader.get("id", id)) {
52     throw std::runtime_error("Missing tile-id.");
53   }
54   
55   bool value;
56   if(reader.get("solid", value) && value)
57     attributes |= SOLID;
58   if(reader.get("unisolid", value) && value)
59     attributes |= UNISOLID | SOLID;
60   if(reader.get("brick", value) && value)
61     attributes |= BRICK;
62   if(reader.get("ice", value) && value)
63     attributes |= ICE;
64   if(reader.get("water", value) && value)
65     attributes |= WATER;
66   if(reader.get("spike", value) && value)
67     attributes |= SPIKE;
68   if(reader.get("fullbox", value) && value)
69     attributes |= FULLBOX;
70   if(reader.get("coin", value) && value)
71     attributes |= COIN;
72   if(reader.get("goal", value) && value)
73     attributes |= GOAL;
74
75   if(reader.get("north", value) && value)
76     data |= WORLDMAP_NORTH;
77   if(reader.get("south", value) && value)
78     data |= WORLDMAP_SOUTH;
79   if(reader.get("west", value) && value)
80     data |= WORLDMAP_WEST;
81   if(reader.get("east", value) && value) 
82     data |= WORLDMAP_EAST;
83   if(reader.get("stop", value) && value)
84     data |= WORLDMAP_STOP;                      
85
86   reader.get("data", data);
87   reader.get("anim-fps", anim_fps);
88
89   if(reader.get("slope-type", data)) {
90     attributes |= SOLID | SLOPE;
91   }
92   
93   if (!reader.get("alpha", alpha)) {
94         alpha = 255;
95   }
96
97   const lisp::Lisp* images = reader.get_lisp("images");
98   if(images)
99     parse_images(*images);
100   reader.get("editor-images", editor_imagefile);
101 }
102
103 void
104 Tile::parse_images(const lisp::Lisp& images_lisp)
105 {
106   const lisp::Lisp* list = &images_lisp;
107   while(list) {
108     const lisp::Lisp* cur = list->get_car();
109     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
110       std::string file;
111       cur->get(file);
112       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
113     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS && 
114         cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL) {
115       const lisp::Lisp* ptr = cur->get_cdr();
116
117       std::string file;
118       float x, y, w, h;
119       ptr->get_car()->get(file); ptr = ptr->get_cdr();
120       ptr->get_car()->get(x); ptr = ptr->get_cdr();
121       ptr->get_car()->get(y); ptr = ptr->get_cdr();
122       ptr->get_car()->get(w); ptr = ptr->get_cdr();
123       ptr->get_car()->get(h);
124       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
125     } else {
126       std::cerr << "Expected string or list in images tag.\n";
127       continue;
128     }
129     
130     list = list->get_cdr();
131   }
132 }
133
134 void
135 Tile::load_images(const std::string& tilesetpath)
136 {
137   assert(images.size() == 0);
138   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
139       imagespecs.end(); ++i) {
140     const ImageSpec& spec = *i;
141     Surface* surface;
142     std::string file 
143       = get_resource_filename(tilesetpath + spec.file);
144     if(spec.rect.get_width() <= 0) {
145       surface = new Surface(file, true);
146     } else {
147       surface = new Surface(file,
148           (int) spec.rect.p1.x,
149           (int) spec.rect.p1.y,
150           (int) spec.rect.get_width(),
151           (int) spec.rect.get_height(), true);
152     }
153     images.push_back(surface);
154   }
155   if(editor_imagefile != "") {
156     editor_image = new Surface(
157         get_resource_filename(tilesetpath + editor_imagefile), true);
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   context.set_alpha(this->alpha);
176   
177   if(images.size() > 1) {
178     size_t frame = size_t(global_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   context.set_alpha(255);
185 }
186