* Small subclass to give some basic tile-mapping functionality to textures.
*/
-#include <cassert>
-
#include <boost/shared_ptr.hpp>
-#include "opengl.hh"
#include "texture.hh"
+#include "math.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
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.
+ * Possible orientations for texture coordinates.
*/
- void setTileDimensions(unsigned tilesU, unsigned tilesV)
+ typedef enum
{
- assert(tilesU != 0 && tilesV != 0);
- tilesU_ = tilesU;
- tilesV_ = tilesV;
- }
+ 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;
+
+ tilemap(const std::string& name);
/**
* 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
+ * @param coords An array of scalars 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
+ * requiring enough room for an array of eight scalars. The winding of the
* coordinates is always counter-clockwise (the GL default).
+ * @return True if index is valid, false otherwise.
*/
- void getTileCoords(unsigned index, float coords[8])
- {
- assert(index < tilesU_ * tilesV_);
-
- float w = 1.0 / float(tilesU_);
- float h = 1.0 / float(tilesV_);
+ bool getTileCoords(unsigned index, scalar coords[8]);
- 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.
+ * @return True if index is valid, false otherwise.
*/
- 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];
- }
- }
+ bool getTileCoords(unsigned index, scalar coords[8], orientation what);
+
+
+ static std::string getPathToResource(const std::string& name);
private:
- unsigned tilesU_;
- unsigned tilesV_;
+ class tilemap_impl;
+ boost::shared_ptr<tilemap_impl> impl;
};
#endif // _TILEMAP_HH_
+/** vim: set ts=4 sw=4 tw=80: *************************************************/
+