]> Dogcows Code - chaz/yoink/blobdiff - src/tilemap.cc
big batch of progress
[chaz/yoink] / src / tilemap.cc
diff --git a/src/tilemap.cc b/src/tilemap.cc
new file mode 100644 (file)
index 0000000..0852f98
--- /dev/null
@@ -0,0 +1,204 @@
+
+/*******************************************************************************
+
+ 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 <cassert>
+
+#include "mippleton.hh"
+#include "serializable.hh"
+#include "deserializer.hh"
+
+#include "opengl.hh"
+
+#include "tilemap.hh"
+
+
+namespace dc {
+
+
+class tilemap::tilemap_impl : public mippleton<tilemap_impl>
+{
+public:
+       tilemap_impl(const std::string& name) :
+               mippleton<tilemap_impl>(name),
+               tilesU_(1),
+               tilesV_(1),
+               minFilter_(GL_NEAREST),
+               maxFilter_(GL_NEAREST),
+               wrapU_(GL_CLAMP),
+               wrapV_(GL_CLAMP)
+       {
+               loadFromFile();
+       }
+
+       void loadFromFile()
+       {
+               deserializer in(tilemap::getPathToResource(getName()));
+
+               serializable_ptr root = in.deserialize();
+
+               if (root)
+               {
+                       std::map<std::string,serializable_ptr> rootMap;
+
+                       if (root->get(rootMap))
+                       {
+                               std::map<std::string,serializable_ptr>::iterator it;
+
+                               if ((it = rootMap.find("TilesU")) != rootMap.end())
+                               {
+                                       long value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               tilesU_ = unsigned(value);
+                                       }
+                               }
+                               if ((it = rootMap.find("TilesV")) != rootMap.end())
+                               {
+                                       long value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               tilesV_ = unsigned(value);
+                                       }
+                               }
+                               if ((it = rootMap.find("MinFilter")) != rootMap.end())
+                               {
+                                       std::string value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               if (value == "Linear")
+                                               {
+                                                       minFilter_ = GL_LINEAR;
+                                               }
+                                       }
+                               }
+                               if ((it = rootMap.find("MaxFilter")) != rootMap.end())
+                               {
+                                       std::string value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               if (value == "Linear")
+                                               {
+                                                       maxFilter_ = GL_LINEAR;
+                                               }
+                                       }
+                               }
+                               if ((it = rootMap.find("WrapU")) != rootMap.end())
+                               {
+                                       std::string value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               if (value == "Repeat")
+                                               {
+                                                       wrapU_ = GL_REPEAT;
+                                               }
+                                       }
+                               }
+                               if ((it = rootMap.find("WrapV")) != rootMap.end())
+                               {
+                                       std::string value;
+                                       if ((*it).second->get(value))
+                                       {
+                                               if (value == "Repeat")
+                                               {
+                                                       wrapV_ = GL_REPEAT;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       unsigned tilesU_;
+       unsigned tilesV_;
+       GLuint minFilter_;
+       GLuint maxFilter_;
+       GLuint wrapU_;
+       GLuint wrapV_;
+};
+
+
+tilemap::tilemap(const std::string& name) :
+       texture(name),
+       impl(tilemap::tilemap_impl::retain(name), &tilemap::tilemap_impl::release)
+{
+       setMinFilter(impl->minFilter_);
+       setMaxFilter(impl->maxFilter_);
+       setWrapU(impl->wrapU_);
+       setWrapV(impl->wrapV_);
+       applyChanges();
+}
+
+
+void tilemap::getTileCoords(unsigned index, scalar coords[8])
+{
+       assert(index < impl->tilesU_ * impl->tilesV_);
+
+       scalar w = 1.0 / scalar(impl->tilesU_);
+       scalar h = 1.0 / scalar(impl->tilesV_);
+
+       coords[0] = scalar(index % impl->tilesU_) * w;
+       coords[1] = (scalar(impl->tilesV_ - 1) - scalar(index / impl->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];
+}
+
+void tilemap::getTileCoords(unsigned index, scalar coords[8], orientation what)
+{
+       getTileCoords(index, coords);
+
+       if (what & flip)
+       {
+               coords[1] = coords[5];
+               coords[5] = coords[3];
+               coords[3] = coords[7];
+               coords[7] = coords[5];
+       }
+       if (what & reverse)
+       {
+               coords[0] = coords[2];
+               coords[2] = coords[6];
+               coords[4] = coords[6];
+               coords[6] = coords[0];
+       }
+}
+
+
+std::string tilemap::getPathToResource(const std::string& name)
+{
+       return resource::getPathToResource("tilemaps/" + name + ".json");
+}
+
+
+} // namespace dc
+
+/** vim: set ts=4 sw=4 tw=80: *************************************************/
+
This page took 0.023865 seconds and 4 git commands to generate.