]> Dogcows Code - chaz/yoink/blob - src/tilemap.hh
new interface class: drawable
[chaz/yoink] / src / tilemap.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _TILEMAP_HH_
30 #define _TILEMAP_HH_
31
32 /**
33 * @file tilemap.hh
34 * Small subclass to give some basic tile-mapping functionality to textures.
35 */
36
37 #include <cassert>
38
39 #include <boost/shared_ptr.hpp>
40
41 #include "opengl.hh"
42 #include "texture.hh"
43
44
45 namespace dc {
46
47
48 namespace tile {
49
50 /**
51 * Possible orientations for texture coordinates.
52 */
53
54 typedef enum
55 {
56 normal = 0, ///< Normal orientation.
57 flip = 1, ///< Flip over a horizontal axis.
58 reverse = 2, ///< Flip over a vertical axis.
59 flip_and_reverse = 3 ///< Flip over both.
60 } orientation;
61
62 } // namespace tile
63
64
65 /**
66 * A tilemap is a texture which is meant to be divided into smaller sprites.
67 * This class provides all the functionality of a texture and adds some methods
68 * to get texture coordinates to individual tiles within the tilemap. For
69 * simplicity, this class assumes square tiles.
70 */
71
72 class tilemap : public texture
73 {
74 public:
75 tilemap(const std::string& filePath, bool keepInMemory = false,
76 unsigned tilesU = 1, unsigned tilesV = 1)
77 : texture(filePath, keepInMemory), tilesU_(tilesU), tilesV_(tilesV) {}
78
79
80 /**
81 * Set the number of rows and columns of square tiles.
82 * @param tilesU Columns of tiles.
83 * @param tilesV Rows of tiles.
84 */
85
86 void setTileDimensions(unsigned tilesU, unsigned tilesV)
87 {
88 assert(tilesU != 0 && tilesV != 0);
89 tilesU_ = tilesU;
90 tilesV_ = tilesV;
91 }
92
93
94 /**
95 * Calculate texture coordinates for a tile at a certain index. Tiles are
96 * indexed start with zero as the to-left tile and moving across, then down.
97 * @param index The tile index.
98 * @param coords An array of floats where the texture coordinates will be
99 * stored after this call. The first coordinate (u,v) will be in the first
100 * two places and so on until all four coordinates are stored, therefore
101 * requiring enough room for an array of eight floats. The winding of the
102 * coordinates is always counter-clockwise (the GL default).
103 */
104
105 void getTileCoords(unsigned index, float coords[8])
106 {
107 assert(index < tilesU_ * tilesV_);
108
109 float w = 1.0 / float(tilesU_);
110 float h = 1.0 / float(tilesV_);
111
112 coords[0] = float(index % tilesU_) * w;
113 coords[1] = (float(tilesV_ - 1) - float(index / tilesU_)) * h;
114 coords[2] = coords[0] + w;
115 coords[3] = coords[1];
116 coords[4] = coords[2];
117 coords[5] = coords[1] + h;
118 coords[6] = coords[0];
119 coords[7] = coords[5];
120 }
121
122 /**
123 * This version let's you specify an orientation that will be reflected in
124 * the texture coordinates. This allows you to easily map a texture
125 * backwards or upside-down.
126 * @param what The orientation; can be flip, reverse, or flip_and_reverse.
127 */
128
129 void getTileCoords(unsigned index, float coords[8], tile::orientation what)
130 {
131 getTileCoords(index, coords);
132
133 if (what & tile::flip)
134 {
135 coords[1] = coords[5];
136 coords[5] = coords[3];
137 coords[3] = coords[7];
138 coords[7] = coords[5];
139 }
140 if (what & tile::reverse)
141 {
142 coords[0] = coords[2];
143 coords[2] = coords[6];
144 coords[4] = coords[6];
145 coords[6] = coords[0];
146 }
147 }
148
149 private:
150 unsigned tilesU_;
151 unsigned tilesV_;
152 };
153
154
155 } // namespace dc
156
157 #endif // _TILEMAP_HH_
158
This page took 0.042557 seconds and 5 git commands to generate.