X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FTilemap.cc;h=9f24b1bce91547a200bf7de7090f9ce6a8110ca8;hp=6accbd167d3b3fc638cf0443ee1facd59fb84fdd;hb=29e3d45f7bbbf31eadf793c41ff2b3d9c47b7539;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Tilemap.cc b/src/Moof/Tilemap.cc index 6accbd1..9f24b1b 100644 --- a/src/Moof/Tilemap.cc +++ b/src/Moof/Tilemap.cc @@ -38,15 +38,51 @@ namespace Mf { class Tilemap::TilemapImpl : public Mippleton { + 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(name), - tilesU_(1), - tilesV_(1), + 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(); } @@ -65,76 +101,64 @@ public: { std::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_; }; @@ -142,25 +166,26 @@ Tilemap::Tilemap(const std::string& name) : Texture(name), impl_(Tilemap::TilemapImpl::retain(name), &Tilemap::TilemapImpl::release) { + bind(); + 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 +197,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)) {