]> Dogcows Code - chaz/yoink/blobdiff - src/tilemap.hh
new classes: resource, tilemap, animation
[chaz/yoink] / src / tilemap.hh
diff --git a/src/tilemap.hh b/src/tilemap.hh
new file mode 100644 (file)
index 0000000..2121953
--- /dev/null
@@ -0,0 +1,157 @@
+
+/*******************************************************************************
+
+ Copyright (c) 2009, Charles McGarvey
+ All rights reserved.
+ Redistribution   and   use  in  source  and  binary  forms,  with  or  without
+ modification, are permitted provided that the following conditions are met:
+   * Redistributions  of  source  code  must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+   * Redistributions  in binary form must reproduce the above copyright notice,
+     this  list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+ THIS  SOFTWARE  IS  PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND  ANY  EXPRESS  OR  IMPLIED  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED.  IN  NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES  (INCLUDING,  BUT  NOT  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES;  LOSS  OF  USE,  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*******************************************************************************/
+
+#ifndef _TILEMAP_HH_
+#define _TILEMAP_HH_
+
+/**
+ * @file tilemap.hh
+ * Small subclass to give some basic tile-mapping functionality to textures.
+ */
+
+#include <cassert>
+
+#include <boost/shared_ptr.hpp>
+
+#include "opengl.hh"
+#include "texture.hh"
+
+
+namespace dc {
+
+
+namespace tile {
+
+/**
+ * Possible orientations for texture coordinates.
+ */
+
+typedef enum
+{
+       normal =                        0,                      ///< Normal orientation.
+       flip =                          1,                      ///< Flip over a horizontal axis.
+       reverse =                       2,                      ///< Flip over a vertical axis.
+       flip_and_reverse =      3                       ///< Flip over both.
+} orientation;
+
+} // namespace tile
+
+
+/**
+ * A tilemap is a texture which is meant to be divided into smaller sprites.
+ * This class provides all the functionality of a texture and adds some methods
+ * to get texture coordinates to individual tiles within the tilemap.  For
+ * simplicity, this class assumes square tiles.
+ */
+
+class tilemap : public texture
+{
+public:
+       tilemap(const std::string& filePath, bool keepInMemory = false,
+                       unsigned tilesU = 1, unsigned tilesV = 1)
+               : texture(filePath, keepInMemory), tilesU_(tilesU), tilesV_(tilesV) {}
+
+
+       /**
+        * Set the number of rows and columns of square tiles.
+        * @param tilesU Columns of tiles.
+        * @param tilesV Rows of tiles.
+        */
+
+       void setTileDimensions(unsigned tilesU, unsigned tilesV)
+       {
+               tilesU_ = tilesU;
+               tilesV_ = tilesV;
+       }
+
+
+       /**
+        * Calculate texture coordinates for a tile at a certain index.  Tiles are
+        * indexed start with zero as the to-left tile and moving across, then down.
+        * @param index The tile index.
+        * @param coords An array of floats where the texture coordinates will be
+        * stored after this call.  The first coordinate (u,v) will be in the first
+        * two places and so on until all four coordinates are stored, therefore
+        * requiring enough room for an array of eight floats.  The winding of the
+        * coordinates is always counter-clockwise (the GL default).
+        */
+
+       void getTileCoords(unsigned index, float coords[8])
+       {
+               assert(index < tilesU_ * tilesV_);
+
+               float w = 1.0 / float(tilesU_);
+               float h = 1.0 / float(tilesV_);
+
+               coords[0] = float(index % tilesU_) * w;
+               coords[1] = (float(tilesV_ - 1) - float(index / tilesU_)) * h;
+               coords[2] = coords[0] + w;
+               coords[3] = coords[1];
+               coords[4] = coords[2];
+               coords[5] = coords[1] + h;
+               coords[6] = coords[0];
+               coords[7] = coords[5];
+       }
+
+       /**
+        * This version let's you specify an orientation that will be reflected in
+        * the texture coordinates.  This allows you to easily map a texture
+        * backwards or upside-down.
+        * @param what The orientation; can be flip, reverse, or flip_and_reverse.
+        */
+
+       void getTileCoords(unsigned index, float coords[8], tile::orientation what)
+       {
+               getTileCoords(index, coords);
+
+               if (what & tile::flip)
+               {
+                       coords[1] = coords[5];
+                       coords[5] = coords[3];
+                       coords[3] = coords[7];
+                       coords[7] = coords[5];
+               }
+               if (what & tile::reverse)
+               {
+                       coords[0] = coords[2];
+                       coords[2] = coords[6];
+                       coords[4] = coords[6];
+                       coords[6] = coords[0];
+               }
+       }
+
+private:
+       unsigned tilesU_;
+       unsigned tilesV_;
+};
+
+
+} // namespace dc
+
+#endif // _TILEMAP_HH_
+
This page took 0.023388 seconds and 4 git commands to generate.