Some selfmade noise.
[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 <math.h>
24 #include <assert.h>
25 #include <iostream>
26 #include <stdexcept>
27
28 #include "lisp/lisp.hpp"
29 #include "tile.hpp"
30 #include "tile_set.hpp"
31 #include "resources.hpp"
32 #include "timer.hpp"
33 #include "math/vector.hpp"
34 #include "video/drawing_context.hpp"
35 #include "log.hpp"
36
37
38 Tile::Tile(const TileSet *new_tileset)
39   : tileset(new_tileset), attributes(0), data(0), anim_fps(1)
40 {
41 }
42
43 Tile::Tile(const TileSet *new_tileset, Uint32 attributes, const ImageSpec& imagespec)
44   : tileset(new_tileset), attributes(attributes), data(0), anim_fps(1)
45 {
46   imagespecs.push_back(imagespec);
47 }
48
49 Tile::~Tile()
50 {
51   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
52       ++i) {
53     delete *i;
54   }
55 }
56
57 uint32_t
58 Tile::parse(const lisp::Lisp& reader)
59 {
60   uint32_t id;
61   if(!reader.get("id", id)) {
62     throw std::runtime_error("Missing tile-id.");
63   }
64
65   bool value = false;
66   if(reader.get("solid", value) && value)
67     attributes |= SOLID;
68   if(reader.get("unisolid", value) && value)
69     attributes |= UNISOLID | SOLID;
70   if(reader.get("brick", value) && value)
71     attributes |= BRICK;
72   if(reader.get("ice", value) && value)
73     attributes |= ICE;
74   if(reader.get("water", value) && value)
75     attributes |= WATER;
76   if(reader.get("hurts", value) && value)
77     attributes |= HURTS;
78   if(reader.get("fire", value) && value)
79     attributes |= FIRE;
80   if(reader.get("fullbox", value) && value)
81     attributes |= FULLBOX;
82   if(reader.get("coin", value) && value)
83     attributes |= COIN;
84   if(reader.get("goal", value) && value)
85     attributes |= GOAL;
86
87   if(reader.get("north", value) && value)
88     data |= WORLDMAP_NORTH;
89   if(reader.get("south", value) && value)
90     data |= WORLDMAP_SOUTH;
91   if(reader.get("west", value) && value)
92     data |= WORLDMAP_WEST;
93   if(reader.get("east", value) && value)
94     data |= WORLDMAP_EAST;
95   if(reader.get("stop", value) && value)
96     data |= WORLDMAP_STOP;
97
98   reader.get("data", data);
99   reader.get("anim-fps", anim_fps);
100
101   if(reader.get("slope-type", data)) {
102     attributes |= SOLID | SLOPE;
103   }
104
105   const lisp::Lisp* images = reader.get_lisp("images");
106   if(images)
107     parse_images(*images);
108
109   return id;
110 }
111
112 void
113 Tile::parse_images(const lisp::Lisp& images_lisp)
114 {
115   const lisp::Lisp* list = &images_lisp;
116   while(list) {
117     const lisp::Lisp* cur = list->get_car();
118     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
119       std::string file;
120       cur->get(file);
121       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
122     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
123               cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
124               cur->get_car()->get_symbol() == "region") {
125       const lisp::Lisp* ptr = cur->get_cdr();
126
127       std::string file;
128       float x = 0, y = 0, w = 0, h = 0;
129       ptr->get_car()->get(file); ptr = ptr->get_cdr();
130       ptr->get_car()->get(x); ptr = ptr->get_cdr();
131       ptr->get_car()->get(y); ptr = ptr->get_cdr();
132       ptr->get_car()->get(w); ptr = ptr->get_cdr();
133       ptr->get_car()->get(h);
134       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
135     } else {
136       log_warning << "Expected string or list in images tag" << std::endl;
137       continue;
138     }
139
140     list = list->get_cdr();
141   }
142 }
143
144 void
145 Tile::load_images()
146 {
147   const std::string& tiles_path = tileset->tiles_path;
148
149   assert(images.size() == 0);
150   for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
151       imagespecs.end(); ++i) {
152     const ImageSpec& spec = *i;
153     Surface* surface;
154     std::string file = tiles_path + spec.file;
155     if(spec.rect.get_width() <= 0) {
156       surface = new Surface(file);
157     } else {
158       surface = new Surface(file,
159           (int) spec.rect.p1.x,
160           (int) spec.rect.p1.y,
161           (int) spec.rect.get_width(),
162           (int) spec.rect.get_height());
163     }
164     images.push_back(surface);
165   }
166 }
167
168 void
169 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
170 {
171   if(images.size() > 1) {
172     size_t frame = size_t(game_time * anim_fps) % images.size();
173     context.draw_surface(images[frame], pos, z_pos);
174   } else if (images.size() == 1) {
175     context.draw_surface(images[0], pos, z_pos);
176   }
177 }