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