29575a04eb942a88b5aabd5f3b8b8bd4561ff519
[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
21 #include <config.h>
22
23 #include <cmath>
24 #include <cassert>
25 #include <iostream>
26 #include <stdexcept>
27
28 #include "app/globals.h"
29 #include "tile.h"
30 #include "scene.h"
31 #include "utils/lispreader.h"
32 #include "math/vector.h"
33 #include "video/drawing_context.h"
34
35 /** Dirty little helper to create a surface from a snipped of lisp:
36  *
37  *  "filename"
38  *  (region "filename" x y w h)
39  */
40 static
41 Surface* create_surface(lisp_object_t* cur)
42 {
43   if (lisp_string_p(cur))
44     {
45       return new Surface(datadir + "/images/tilesets/" + lisp_string(cur),
46                          true);
47     }
48   else if (lisp_cons_p(cur) && lisp_symbol_p(lisp_car(cur)))
49     {
50       lisp_object_t* sym  = lisp_car(cur);
51       lisp_object_t* data = lisp_cdr(cur);
52       
53       if (strcmp(lisp_symbol(sym), "region") == 0)
54         {
55           if (lisp_list_length(data) == 5) // (image-region filename x y w h)
56             {
57               return new Surface(datadir + "/images/tilesets/" + lisp_string(lisp_car(data)), 
58                                  lisp_integer(lisp_list_nth(data, 1)),
59                                  lisp_integer(lisp_list_nth(data, 2)),
60                                  lisp_integer(lisp_list_nth(data, 3)),
61                                  lisp_integer(lisp_list_nth(data, 4)),
62                                  true);
63             }
64           else
65             {
66               std::cout << "Tile: Type mispatch, should be '(region \"somestring\" x y w h)'" << std::endl;
67               return 0;
68             }
69         }
70       else
71         {
72           std::cout << "Tile: Unhandled tag: " << lisp_symbol(sym) << std::endl;
73           return 0;
74         }
75     }
76
77   std::cout << "Tile: unhandled element" << std::endl;
78   return 0;  
79 }
80
81 /** Create a vector of surfaces (aka Sprite) from a piece of lisp:
82     ((image "bla.png") (image-region "bla.png") ...)
83  */
84 static 
85 std::vector<Surface*> create_surfaces(lisp_object_t* cur)
86 {
87   std::vector<Surface*> surfs;
88
89   while(cur)
90     {
91       Surface* surface = create_surface(lisp_car(cur));
92       if (surface)
93         surfs.push_back(surface); 
94       else
95         std::cout << "Tile: Couldn't create image" << std::endl;
96         
97       cur = lisp_cdr(cur);
98     }
99   
100   return surfs;
101 }
102
103 Tile::Tile()
104   : id(0), attributes(0), data(0), next_tile(0), anim_speed(25)
105 {
106 }
107
108 Tile::~Tile()
109 {
110   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
111       ++i) {
112     delete *i;
113   }
114   for(std::vector<Surface*>::iterator i = editor_images.begin();
115       i != editor_images.end(); ++i) {
116     delete *i;                                                                
117   }
118 }
119
120 void
121 Tile::read(LispReader& reader)
122 {
123   if(!reader.read_uint("id", id)) {
124     throw std::runtime_error("Missing tile-id.");
125   }
126   
127   bool value;
128   if(reader.read_bool("solid", value) && value)
129     attributes |= SOLID;
130   if(reader.read_bool("unisolid", value) && value)
131     attributes |= UNISOLID | SOLID;
132   if(reader.read_bool("brick", value) && value)
133     attributes |= BRICK;
134   if(reader.read_bool("ice", value) && value)
135     attributes |= ICE;
136   if(reader.read_bool("water", value) && value)
137     attributes |= WATER;
138   if(reader.read_bool("spike", value) && value)
139     attributes |= SPIKE;
140   if(reader.read_bool("fullbox", value) && value)
141     attributes |= FULLBOX;
142   if(reader.read_bool("distro", value) && value)
143     attributes |= COIN;
144   if(reader.read_bool("coin", value) && value)
145     attributes |= COIN;
146   if(reader.read_bool("goal", value) && value)
147     attributes |= GOAL;
148
149   reader.read_int("data", data);
150   reader.read_int("anim-speed", anim_speed);
151   reader.read_int("next-tile", next_tile);
152
153   if(reader.read_int("slope-type", data)) {
154     attributes |= SOLID | SLOPE;
155   }
156
157   images        = create_surfaces(reader.read_lisp("images"));
158   editor_images = create_surfaces(reader.read_lisp("editor-images"));
159 }
160
161 void
162 Tile::draw(DrawingContext& context, const Vector& pos, int layer) const
163 {
164   if(images.size() > 1) {
165     size_t frame = ((global_frame_counter*25) / anim_speed) % images.size();
166     context.draw_surface(images[frame], pos, layer);
167   } else if (images.size() == 1) {
168     context.draw_surface(images[0], pos, layer);
169   }
170 }
171