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