]> Dogcows Code - chaz/yoink/blob - src/tilemap.hh
new classes: resource, tilemap, animation
[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 tilesU_ = tilesU;
89 tilesV_ = tilesV;
90 }
91
92
93 /**
94 * Calculate texture coordinates for a tile at a certain index. Tiles are
95 * indexed start with zero as the to-left tile and moving across, then down.
96 * @param index The tile index.
97 * @param coords An array of floats where the texture coordinates will be
98 * stored after this call. The first coordinate (u,v) will be in the first
99 * two places and so on until all four coordinates are stored, therefore
100 * requiring enough room for an array of eight floats. The winding of the
101 * coordinates is always counter-clockwise (the GL default).
102 */
103
104 void getTileCoords(unsigned index, float coords[8])
105 {
106 assert(index < tilesU_ * tilesV_);
107
108 float w = 1.0 / float(tilesU_);
109 float h = 1.0 / float(tilesV_);
110
111 coords[0] = float(index % tilesU_) * w;
112 coords[1] = (float(tilesV_ - 1) - float(index / tilesU_)) * h;
113 coords[2] = coords[0] + w;
114 coords[3] = coords[1];
115 coords[4] = coords[2];
116 coords[5] = coords[1] + h;
117 coords[6] = coords[0];
118 coords[7] = coords[5];
119 }
120
121 /**
122 * This version let's you specify an orientation that will be reflected in
123 * the texture coordinates. This allows you to easily map a texture
124 * backwards or upside-down.
125 * @param what The orientation; can be flip, reverse, or flip_and_reverse.
126 */
127
128 void getTileCoords(unsigned index, float coords[8], tile::orientation what)
129 {
130 getTileCoords(index, coords);
131
132 if (what & tile::flip)
133 {
134 coords[1] = coords[5];
135 coords[5] = coords[3];
136 coords[3] = coords[7];
137 coords[7] = coords[5];
138 }
139 if (what & tile::reverse)
140 {
141 coords[0] = coords[2];
142 coords[2] = coords[6];
143 coords[4] = coords[6];
144 coords[6] = coords[0];
145 }
146 }
147
148 private:
149 unsigned tilesU_;
150 unsigned tilesV_;
151 };
152
153
154 } // namespace dc
155
156 #endif // _TILEMAP_HH_
157
This page took 0.040882 seconds and 4 git commands to generate.