+++ /dev/null
-
-/*******************************************************************************
-
- 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.
-
-*******************************************************************************/
-
-#include "Deserializer.hh"
-#include "Mippleton.hh"
-#include "OpenGL.hh"
-#include "Serializable.hh"
-#include "Tilemap.hh"
-
-
-namespace Mf {
-
-
-class Tilemap::Impl : public Mippleton<Impl>
-{
- static GLint filterFromString(const std::string& filter)
- {
- if (filter == "linear")
- {
- return GL_LINEAR;
- }
- else if (filter == "nearest_mipmap_nearest")
- {
- return GL_NEAREST_MIPMAP_NEAREST;
- }
- else if (filter == "nearest_mipmap_linear")
- {
- return GL_NEAREST_MIPMAP_LINEAR;
- }
- else if (filter == "linear_mipmap_nearest")
- {
- return GL_LINEAR_MIPMAP_NEAREST;
- }
- else if (filter == "linear_mipmap_linear")
- {
- return GL_LINEAR_MIPMAP_LINEAR;
- }
-
- return GL_NEAREST;
- }
-
- static GLint wrapFromString(const std::string& wrap)
- {
- if (wrap == "repeat")
- {
- return GL_REPEAT;
- }
-
- return GL_CLAMP;
- }
-
-public:
- Impl(const std::string& name) :
- Mippleton<Impl>(name),
- magFilter_(GL_NEAREST),
- minFilter_(GL_NEAREST),
- nTilesS_(1),
- nTilesT_(1),
- wrapS_(GL_CLAMP),
- wrapT_(GL_CLAMP)
- {
- loadFromFile();
- }
-
- void loadFromFile()
- {
- Deserializer deserializer(Tilemap::getPath(getName()));
-
- SerializableP root = deserializer.deserialize();
-
- if (root)
- {
- Serializable::Map rootObj;
-
- if (root->get(rootObj))
- {
- Serializable::Map::iterator it;
-
- if ((it = rootObj.find("tiles_s")) != rootObj.end())
- {
- long value;
- if ((*it).second->get(value))
- {
- nTilesS_ = unsigned(value);
- }
- }
- if ((it = rootObj.find("tiles_t")) != rootObj.end())
- {
- long value;
- if ((*it).second->get(value))
- {
- nTilesT_ = unsigned(value);
- }
- }
- if ((it = rootObj.find("min_filter")) != rootObj.end())
- {
- std::string value;
- if ((*it).second->get(value))
- {
- minFilter_ = filterFromString(value);
- }
- }
- if ((it = rootObj.find("mag_filter")) != rootObj.end())
- {
- std::string value;
- if ((*it).second->get(value))
- {
- magFilter_ = filterFromString(value);
- }
- }
- if ((it = rootObj.find("wrap_s")) != rootObj.end())
- {
- std::string value;
- if ((*it).second->get(value))
- {
- wrapS_ = wrapFromString(value);
- }
- }
- if ((it = rootObj.find("wrap_t")) != rootObj.end())
- {
- std::string value;
- if ((*it).second->get(value))
- {
- wrapT_ = wrapFromString(value);
- }
- }
- }
- }
- }
-
- GLuint magFilter_;
- GLuint minFilter_;
- unsigned nTilesS_;
- unsigned nTilesT_;
- GLuint wrapS_;
- GLuint wrapT_;
-};
-
-
-Tilemap::Tilemap(const std::string& name) :
- Texture(name),
- impl_(Tilemap::Impl::getInstance(name))
-{
- setMinFilter(impl_->minFilter_);
- setMagFilter(impl_->magFilter_);
- setWrapS(impl_->wrapS_);
- setWrapT(impl_->wrapT_);
-}
-
-
-bool Tilemap::getTileCoords(unsigned index, Scalar coords[8]) const
-{
- // make sure the index represents a real tile
- if (index >= impl_->nTilesS_ * impl_->nTilesT_) return false;
-
- Scalar w = 1.0 / Scalar(impl_->nTilesS_);
- Scalar h = 1.0 / Scalar(impl_->nTilesT_);
-
- coords[0] = Scalar(index % impl_->nTilesS_) * w;
- coords[1] = (Scalar(impl_->nTilesT_ - 1) -
- Scalar(index / impl_->nTilesS_)) * 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];
-
- return true;
-}
-
-bool Tilemap::getTileCoords(unsigned index, Scalar coords[8],
- Orientation orientation) const
-{
- if (getTileCoords(index, coords))
- {
- if (orientation & FLIP)
- {
- // this looks kinda weird, but it's just swapping in a way that
- // doesn't require an intermediate variable
- coords[1] = coords[5];
- coords[5] = coords[3];
- coords[3] = coords[7];
- coords[7] = coords[5];
- }
- if (orientation & REVERSE)
- {
- coords[0] = coords[2];
- coords[2] = coords[6];
- coords[4] = coords[6];
- coords[6] = coords[0];
- }
-
- return true;
- }
-
- return false;
-}
-
-
-std::string Tilemap::getPath(const std::string& name)
-{
- return Resource::getPath("tilemaps/" + name + ".json");
-}
-
-
-} // namespace Mf
-
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-