[ Patch #1793 ] Turn physic into POD
[supertux.git] / src / tile.hpp
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 #ifndef TILE_H
22 #define TILE_H
23
24 #include <vector>
25 #include <SDL.h>
26 #include <stdint.h>
27 #include "video/surface.hpp"
28 #include "math/rect.hpp"
29 #include "lisp/lisp.hpp"
30
31 class DrawingContext;
32
33 /**
34 Tile Class
35 */
36 class Tile
37 {
38 public:
39   /// bitset for tile attributes
40   enum {
41     /** solid tile that is indestructable by Tux */
42     SOLID     = 0x0001,
43     /** uni-directional solid tile */
44     UNISOLID  = 0x0002,
45     /** a brick that can be destroyed by jumping under it */
46     BRICK     = 0x0004,
47     /** the level should be finished when touching a goaltile.
48      * if data is 0 then the endsequence should be triggered, if data is 1
49      * then we can finish the level instantly.
50      */
51     GOAL      = 0x0008,
52     /** slope tile */
53     SLOPE     = 0x0010,
54     /** Bonusbox, content is stored in \a data */
55     FULLBOX   = 0x0020,
56     /** Tile is a coin */
57     COIN      = 0x0040,
58
59     /* interesting flags (the following are passed to gameobjects) */
60     FIRST_INTERESTING_FLAG = 0x0100,
61
62     /** an ice brick that makes tux sliding more than usual */
63     ICE       = 0x0100,
64     /** a water tile in which tux starts to swim */
65     WATER     = 0x0200,
66     /** a tile that hurts the player if he touches it */
67     HURTS     = 0x0400,
68     /** for lava: WATER, HURTS, FIRE */
69     FIRE      = 0x0800
70   };
71
72   /// worldmap flags
73   enum {
74     WORLDMAP_NORTH = 0x0001,
75     WORLDMAP_SOUTH = 0x0002,
76     WORLDMAP_EAST  = 0x0004,
77     WORLDMAP_WEST  = 0x0008,
78
79     WORLDMAP_STOP  = 0x0010,
80
81     // convenience values ("C" stands for crossroads)
82     WORLDMAP_CNSE  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST,
83     WORLDMAP_CNSW  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_WEST,
84     WORLDMAP_CNEW  = WORLDMAP_NORTH | WORLDMAP_EAST  | WORLDMAP_WEST,
85     WORLDMAP_CSEW  = WORLDMAP_SOUTH | WORLDMAP_EAST  | WORLDMAP_WEST,
86     WORLDMAP_CNSEW = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST | WORLDMAP_WEST
87   };
88
89   struct ImageSpec {
90     ImageSpec(const std::string& newfile, const Rect& newrect)
91       : file(newfile), rect(newrect)
92     { }
93
94     std::string file;
95     Rect rect;
96   };
97
98 private:
99   unsigned int id;
100
101   std::vector<ImageSpec> imagespecs;
102   std::vector<Surface*> images;
103
104   /// tile attributes
105   uint32_t attributes;
106
107   /** General purpose data attached to a tile (content of a box, type of coin)*/
108   int data;
109
110   float anim_fps;
111
112 public:
113   ~Tile();
114
115   /** Draw a tile on the screen */
116   void draw(DrawingContext& context, const Vector& pos, int z_pos) const;
117
118   unsigned int getID() const
119   { return id; }
120
121   uint32_t getAttributes() const
122   { return attributes; }
123
124   int getData() const
125   { return data; }
126
127   /// returns the width of the tile in pixels
128   int getWidth() const
129   {
130     if(!images.size())
131       return 0;
132     return (int) images[0]->get_width();
133   }
134
135   /// returns the height of the tiles in pixels
136   int getHeight() const
137   {
138     if(!images.size())
139       return 0;
140     return (int) images[0]->get_height();
141   }
142
143 protected:
144   friend class TileManager;
145   Tile();
146   Tile(unsigned int id, Uint32 attributes, const ImageSpec& imagespec);
147
148   void load_images(const std::string& tilesetpath);
149
150   /// parses the tile and returns it's id number
151   void parse(const lisp::Lisp& reader);
152   void parse_images(const lisp::Lisp& cur);
153 };
154
155 #endif