]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Tilemap.cc
considerable refactoring
[chaz/yoink] / src / Moof / Tilemap.cc
index 6accbd167d3b3fc638cf0443ee1facd59fb84fdd..4fe5e974597d19e2baacb1eb97c788be6f66df9f 100644 (file)
 namespace Mf {
 
 
-class Tilemap::TilemapImpl : public Mippleton<TilemapImpl>
+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:
-       TilemapImpl(const std::string& name) :
-               Mippleton<TilemapImpl>(name),
-               tilesU_(1),
-               tilesV_(1),
+       Impl(const std::string& name) :
+               Mippleton<Impl>(name),
+               magFilter_(GL_NEAREST),
                minFilter_(GL_NEAREST),
-               maxFilter_(GL_NEAREST),
-               wrapU_(GL_CLAMP),
-               wrapV_(GL_CLAMP)
+               nTilesS_(1),
+               nTilesT_(1),
+               wrapS_(GL_CLAMP),
+               wrapT_(GL_CLAMP)
        {
                loadFromFile();
        }
 
        void loadFromFile()
        {
-               Deserializer deserializer(Tilemap::getPathToResource(getName()));
+               Deserializer deserializer(Tilemap::getPath(getName()));
 
-               SerializablePtr root = deserializer.deserialize();
+               SerializableP root = deserializer.deserialize();
 
                if (root)
                {
-                       std::map<std::string,SerializablePtr> rootObj;
+                       Serializable::Map rootObj;
 
                        if (root->get(rootObj))
                        {
-                               std::map<std::string,SerializablePtr>::iterator it;
+                               Serializable::Map::iterator it;
 
-                               if ((it = rootObj.find("TilesU")) != rootObj.end())
+                               if ((it = rootObj.find("tiles_s")) != rootObj.end())
                                {
                                        long value;
                                        if ((*it).second->get(value))
                                        {
-                                               tilesU_ = unsigned(value);
+                                               nTilesS_ = unsigned(value);
                                        }
                                }
-                               if ((it = rootObj.find("TilesV")) != rootObj.end())
+                               if ((it = rootObj.find("tiles_t")) != rootObj.end())
                                {
                                        long value;
                                        if ((*it).second->get(value))
                                        {
-                                               tilesV_ = unsigned(value);
+                                               nTilesT_ = unsigned(value);
                                        }
                                }
-                               if ((it = rootObj.find("MinFilter")) != rootObj.end())
+                               if ((it = rootObj.find("min_filter")) != rootObj.end())
                                {
                                        std::string value;
                                        if ((*it).second->get(value))
                                        {
-                                               if (value == "Linear")
-                                               {
-                                                       minFilter_ = GL_LINEAR;
-                                               }
+                                               minFilter_ = filterFromString(value);
                                        }
                                }
-                               if ((it = rootObj.find("MaxFilter")) != rootObj.end())
+                               if ((it = rootObj.find("mag_filter")) != rootObj.end())
                                {
                                        std::string value;
                                        if ((*it).second->get(value))
                                        {
-                                               if (value == "Linear")
-                                               {
-                                                       maxFilter_ = GL_LINEAR;
-                                               }
+                                               magFilter_ = filterFromString(value);
                                        }
                                }
-                               if ((it = rootObj.find("WrapU")) != rootObj.end())
+                               if ((it = rootObj.find("wrap_s")) != rootObj.end())
                                {
                                        std::string value;
                                        if ((*it).second->get(value))
                                        {
-                                               if (value == "Repeat")
-                                               {
-                                                       wrapU_ = GL_REPEAT;
-                                               }
+                                               wrapS_ = wrapFromString(value);
                                        }
                                }
-                               if ((it = rootObj.find("WrapV")) != rootObj.end())
+                               if ((it = rootObj.find("wrap_t")) != rootObj.end())
                                {
                                        std::string value;
                                        if ((*it).second->get(value))
                                        {
-                                               if (value == "Repeat")
-                                               {
-                                                       wrapV_ = GL_REPEAT;
-                                               }
+                                               wrapT_ = wrapFromString(value);
                                        }
                                }
                        }
                }
        }
 
-       unsigned        tilesU_;
-       unsigned        tilesV_;
+       GLuint          magFilter_;
        GLuint          minFilter_;
-       GLuint          maxFilter_;
-       GLuint          wrapU_;
-       GLuint          wrapV_;
+       unsigned        nTilesS_;
+       unsigned        nTilesT_;
+       GLuint          wrapS_;
+       GLuint          wrapT_;
 };
 
 
 Tilemap::Tilemap(const std::string& name) :
        Texture(name),
-       impl_(Tilemap::TilemapImpl::retain(name), &Tilemap::TilemapImpl::release)
+       impl_(Tilemap::Impl::getInstance(name))
 {
        setMinFilter(impl_->minFilter_);
-       setMaxFilter(impl_->maxFilter_);
-       setWrapU(impl_->wrapU_);
-       setWrapV(impl_->wrapV_);
-       applyChanges();
+       setMagFilter(impl_->magFilter_);
+       setWrapS(impl_->wrapS_);
+       setWrapT(impl_->wrapT_);
 }
 
 
-bool Tilemap::getTileCoords(unsigned index, Scalar coords[8])
+bool Tilemap::getTileCoords(unsigned index, Scalar coords[8]) const
 {
        // make sure the index represents a real tile
-       if (index >= impl_->tilesU_ * impl_->tilesV_) return false;
+       if (index >= impl_->nTilesS_ * impl_->nTilesT_) return false;
 
-       Scalar w = 1.0 / Scalar(impl_->tilesU_);
-       Scalar h = 1.0 / Scalar(impl_->tilesV_);
+       Scalar w = 1.0 / Scalar(impl_->nTilesS_);
+       Scalar h = 1.0 / Scalar(impl_->nTilesT_);
 
-       coords[0] = Scalar(index % impl_->tilesU_) * w;
-       coords[1] = (Scalar(impl_->tilesV_ - 1) -
-                       Scalar(index / impl_->tilesU_)) * h;
+       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];
@@ -172,7 +195,7 @@ bool Tilemap::getTileCoords(unsigned index, Scalar coords[8])
 }
 
 bool Tilemap::getTileCoords(unsigned index, Scalar coords[8],
-               Orientation orientation)
+               Orientation orientation) const
 {
        if (getTileCoords(index, coords))
        {
@@ -200,9 +223,9 @@ bool Tilemap::getTileCoords(unsigned index, Scalar coords[8],
 }
 
 
-std::string Tilemap::getPathToResource(const std::string& name)
+std::string Tilemap::getPath(const std::string& name)
 {
-       return Resource::getPathToResource("tilemaps/" + name + ".json");
+       return Resource::getPath("tilemaps/" + name + ".json");
 }
 
 
This page took 0.024063 seconds and 4 git commands to generate.