Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / tile.hpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef HEADER_SUPERTUX_SUPERTUX_TILE_HPP
19 #define HEADER_SUPERTUX_SUPERTUX_TILE_HPP
20
21 #include <stdint.h>
22 #include <vector>
23
24 #include "math/rect.hpp"
25 #include "video/surface.hpp"
26 #include "util/reader_fwd.hpp"
27
28 class TileSet;
29 class DrawingContext;
30
31 /**
32    Tile Class
33 */
34 class Tile
35 {
36 public:
37   /// bitset for tile attributes
38   enum {
39     /** solid tile that is indestructible by Tux */
40     SOLID     = 0x0001,
41     /** uni-directional solid tile */
42     UNISOLID  = 0x0002,
43     /** a brick that can be destroyed by jumping under it */
44     BRICK     = 0x0004,
45     /** the level should be finished when touching a goaltile.
46      * if data is 0 then the endsequence should be triggered, if data is 1
47      * then we can finish the level instantly.
48      */
49     GOAL      = 0x0008,
50     /** slope tile */
51     SLOPE     = 0x0010,
52     /** Bonusbox, content is stored in \a data */
53     FULLBOX   = 0x0020,
54     /** Tile is a coin */
55     COIN      = 0x0040,
56
57     /* interesting flags (the following are passed to gameobjects) */
58     FIRST_INTERESTING_FLAG = 0x0100,
59
60     /** an ice brick that makes tux sliding more than usual */
61     ICE       = 0x0100,
62     /** a water tile in which tux starts to swim */
63     WATER     = 0x0200,
64     /** a tile that hurts the player if he touches it */
65     HURTS     = 0x0400,
66     /** for lava: WATER, HURTS, FIRE */
67     FIRE      = 0x0800
68   };
69
70   /// worldmap flags
71   enum {
72     WORLDMAP_NORTH = 0x0001,
73     WORLDMAP_SOUTH = 0x0002,
74     WORLDMAP_EAST  = 0x0004,
75     WORLDMAP_WEST  = 0x0008,
76     WORLDMAP_DIR_MASK = 0x000f,
77
78     WORLDMAP_STOP  = 0x0010,
79
80     // convenience values ("C" stands for crossroads)
81     WORLDMAP_CNSE  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST,
82     WORLDMAP_CNSW  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_WEST,
83     WORLDMAP_CNEW  = WORLDMAP_NORTH | WORLDMAP_EAST  | WORLDMAP_WEST,
84     WORLDMAP_CSEW  = WORLDMAP_SOUTH | WORLDMAP_EAST  | WORLDMAP_WEST,
85     WORLDMAP_CNSEW = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST | WORLDMAP_WEST
86   };
87
88   struct ImageSpec {
89     ImageSpec(const std::string& newfile, const Rect& newrect)
90       : file(newfile), rect(newrect)
91     { }
92
93     std::string file;
94     Rect rect;
95   };
96
97 private:
98   const TileSet         *tileset;
99   std::vector<ImageSpec> imagespecs;
100   std::vector<Surface*>  images;
101
102   /// tile attributes
103   uint32_t attributes;
104
105   /** General purpose data attached to a tile (content of a box, type of coin)*/
106   int data;
107
108   float anim_fps;
109
110 public:
111   ~Tile();
112
113   /** Draw a tile on the screen */
114   void draw(DrawingContext& context, const Vector& pos, int z_pos) const;
115
116   uint32_t getAttributes() const
117   { return attributes; }
118
119   int getData() const
120   { return data; }
121
122   /// returns the width of the tile in pixels
123   int getWidth() const
124   {
125     if(!images.size())
126       return 0;
127     return (int) images[0]->get_width();
128   }
129
130   /// returns the height of the tiles in pixels
131   int getHeight() const
132   {
133     if(!images.size())
134       return 0;
135     return (int) images[0]->get_height();
136   }
137
138 protected:
139   friend class TileSet;
140   Tile(const TileSet *tileset);
141   Tile(const TileSet *tileset, std::vector<std::string> images, Rect rect,
142        uint32_t attributes = 0, uint32_t data = 0, float animfps = 1.0);
143
144   void load_images();
145
146   /// parses the tile and returns it's id number
147   uint32_t parse(const Reader& reader);
148   void parse_images(const Reader& cur);
149
150   //Correct small oddities in attributes that naive people
151   //might miss (and rebuke them for it)
152   void correct_attributes();
153
154 private:
155   Tile(const Tile&);
156   Tile& operator=(const Tile&);
157 };
158
159 #endif
160
161 /* EOF */