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