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