From 264be2108c51837fa92085f6c839e66aebbcfc9e Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 28 Jan 2024 20:56:41 +0100 Subject: Added support for map/layer mask A custom "Mask" property on a layer or a "foregroundXmask" property on a map can now be used in combination with the SMSG_MAP_MASK to dynamically disable certain map layers from the server. Feature previously seen on ManaPlus and implemented for Mana client for compatibility. Also added a ResourceRef class for automating the Resource reference counting. Closes #44 --- src/map.cpp | 72 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 35 deletions(-) (limited to 'src/map.cpp') diff --git a/src/map.cpp b/src/map.cpp index 76ddc56a..1ba62fcd 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -255,61 +255,46 @@ void Map::initializeAmbientLayers() { ResourceManager *resman = ResourceManager::getInstance(); + auto addAmbientLayer = [=](const std::string &name, std::list &list) + { + if (Image *img = resman->getImage(getProperty(name + "image"))) + { + auto ambientLayer = new AmbientLayer(img); + ambientLayer->mParallax = getFloatProperty(name + "parallax"); + ambientLayer->mSpeedX = getFloatProperty(name + "scrollX"); + ambientLayer->mSpeedY = getFloatProperty(name + "scrollY"); + ambientLayer->mMask = getIntProperty(name + "mask", 1); + + list.push_back(ambientLayer); + + // The AmbientLayer takes control over the image. + img->decRef(); + } + }; + // search for "foreground*" or "overlay*" (old term) in map properties for (int i = 0; /* terminated by a break */; i++) { - std::string name; if (hasProperty("foreground" + toString(i) + "image")) { - name = "foreground" + toString(i); + addAmbientLayer("foreground" + toString(i), mForegrounds); } else if (hasProperty("overlay" + toString(i) + "image")) { - name = "overlay" + toString(i); + addAmbientLayer("overlay" + toString(i), mForegrounds); } else { break; // the FOR loop } - - Image *img = resman->getImage(getProperty(name + "image")); - const float speedX = getFloatProperty(name + "scrollX"); - const float speedY = getFloatProperty(name + "scrollY"); - const float parallax = getFloatProperty(name + "parallax"); - const bool keepRatio = getBoolProperty(name + "keepratio"); - - if (img) - { - mForegrounds.push_back( - new AmbientLayer(img, parallax, speedX, speedY, keepRatio)); - - // The AmbientLayer takes control over the image. - img->decRef(); - } } - // search for "background*" in map properties for (int i = 0; hasProperty("background" + toString(i) + "image"); i++) { - const std::string name = "background" + toString(i); - - Image *img = resman->getImage(getProperty(name + "image")); - const float speedX = getFloatProperty(name + "scrollX"); - const float speedY = getFloatProperty(name + "scrollY"); - const float parallax = getFloatProperty(name + "parallax"); - const bool keepRatio = getBoolProperty(name + "keepratio"); - - if (img) - { - mBackgrounds.push_back( - new AmbientLayer(img, parallax, speedX, speedY, keepRatio)); - - // The AmbientLayer takes control over the image. - img->decRef(); - } + addAmbientLayer("background" + toString(i), mBackgrounds); } } @@ -387,6 +372,9 @@ void Map::draw(Graphics *graphics, int scrollX, int scrollY) { for (; layeri != mLayers.end() && !overFringe; ++layeri) { + if (((*layeri)->getMask() & mMask) == 0) + continue; + if ((*layeri)->isFringeLayer() && (mDebugFlags & DEBUG_SPECIAL2)) overFringe = true; @@ -503,10 +491,16 @@ void Map::updateAmbientLayers(float scrollX, float scrollY) std::list::iterator i; for (i = mBackgrounds.begin(); i != mBackgrounds.end(); i++) { + if (((*i)->mMask & mMask) == 0) + continue; + (*i)->update(timePassed, dx, dy); } for (i = mForegrounds.begin(); i != mForegrounds.end(); i++) { + if (((*i)->mMask & mMask) == 0) + continue; + (*i)->update(timePassed, dx, dy); } mLastScrollX = scrollX; @@ -541,6 +535,9 @@ void Map::drawAmbientLayers(Graphics *graphics, LayerType type, for (auto i = layers->begin(); i != layers->end(); i++) { + if (((*i)->mMask & mMask) == 0) + continue; + (*i)->draw(graphics, graphics->getWidth(), graphics->getHeight()); // Detail 1: only one overlay, higher: all overlays @@ -1041,3 +1038,8 @@ TileAnimation *Map::getAnimationForGid(int gid) const auto i = mTileAnimations.find(gid); return (i == mTileAnimations.end()) ? NULL : i->second; } + +void Map::setMask(int mask) +{ + mMask = mask; +} -- cgit v1.2.3-60-g2f50