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/resources/ambientlayer.cpp | 16 +++------- src/resources/ambientlayer.h | 27 ++++++++--------- src/resources/mapreader.cpp | 20 +++++++++++++ src/resources/resource.h | 68 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 27 deletions(-) (limited to 'src/resources') diff --git a/src/resources/ambientlayer.cpp b/src/resources/ambientlayer.cpp index 301927f8..b71378cb 100644 --- a/src/resources/ambientlayer.cpp +++ b/src/resources/ambientlayer.cpp @@ -25,20 +25,12 @@ #include "resources/image.h" #include "resources/resourcemanager.h" -AmbientLayer::AmbientLayer(Image *img, float parallax, - float speedX, float speedY, bool keepRatio): - mImage(img), mParallax(parallax), - mPosX(0), mPosY(0), - mSpeedX(speedX), mSpeedY(speedY), - mKeepRatio(keepRatio) -{ - mImage->incRef(); -} +AmbientLayer::AmbientLayer(Image *img) + : mImage(img) +{} AmbientLayer::~AmbientLayer() -{ - mImage->decRef(); -} +{} void AmbientLayer::update(int timePassed, float dx, float dy) { diff --git a/src/resources/ambientlayer.h b/src/resources/ambientlayer.h index 9ccf5194..def2090b 100644 --- a/src/resources/ambientlayer.h +++ b/src/resources/ambientlayer.h @@ -21,6 +21,8 @@ #ifndef RESOURCES_AMBIENTOVERLAY_H #define RESOURCES_AMBIENTOVERLAY_H +#include "resource.h" + class Graphics; class Image; @@ -31,29 +33,24 @@ class AmbientLayer * Constructor. * * @param img the image this overlay displays - * @param parallax scroll factor based on camera position - * @param speedX scrolling speed in x-direction - * @param speedY scrolling speed in y-direction - * @param keepRatio rescale the image to keep - * the same ratio than in 800x600 resolution mode. */ - AmbientLayer(Image *img, float parallax, - float speedX, float speedY, bool keepRatio = false); - + AmbientLayer(Image *img); ~AmbientLayer(); void update(int timePassed, float dx, float dy); void draw(Graphics *graphics, int x, int y); + float mParallax = 0; /**< Scroll factor based on camera position. */ + float mSpeedX = 0; /**< Scrolling speed in X direction. */ + float mSpeedY = 0; /**< Scrolling speed in Y direction. */ + int mMask = 1; + bool mKeepRatio = false; /**< Keep overlay ratio on every resolution like in 800x600 */ + private: - Image *mImage; - float mParallax; - float mPosX; /**< Current layer X position. */ - float mPosY; /**< Current layer Y position. */ - float mSpeedX; /**< Scrolling speed in X direction. */ - float mSpeedY; /**< Scrolling speed in Y direction. */ - bool mKeepRatio; /**< Keep overlay ratio on every resolution */ + ResourceRef mImage; + float mPosX = 0; /**< Current layer X position. */ + float mPosY = 0; /**< Current layer Y position. */ }; #endif diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index af41da12..aba9f85d 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -276,6 +276,26 @@ void MapReader::readLayer(xmlNodePtr node, Map *map) // Load the tile data for_each_xml_child_node(childNode, node) { + if (xmlStrEqual(childNode->name, BAD_CAST "properties")) + { + for_each_xml_child_node(prop, childNode) + { + if (!xmlStrEqual(prop->name, BAD_CAST "property")) + continue; + + const std::string pname = XML::getProperty(prop, "name", ""); + const std::string value = XML::getProperty(prop, "value", ""); + + // TODO: Consider supporting "Hidden", "Version" and "NotVersion" + + if (pname == "Mask") + { + layer->setMask(atoi(value.c_str())); + } + } + continue; + } + if (!xmlStrEqual(childNode->name, BAD_CAST "data")) continue; diff --git a/src/resources/resource.h b/src/resources/resource.h index af688eb0..988b78a5 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -69,4 +69,72 @@ class Resource unsigned mRefCount; /**< Reference count. */ }; +/** + * Automatically counting Resource reference. + */ +template +class ResourceRef +{ +public: + // Allow implicit construction from RESOURCE * + ResourceRef(RESOURCE *resource = nullptr) + : mResource(resource) + { + if (mResource) + mResource->incRef(); + } + + // Copy constructor + ResourceRef(const ResourceRef &other) + : mResource(other.mResource) + { + if (mResource) + mResource->incRef(); + } + + // Move constructor + ResourceRef(ResourceRef &&other) + : mResource(other.mResource) + { + other.mResource = nullptr; + } + + // Destructor + ~ResourceRef() + { + if (mResource) + mResource->decRef(); + } + + // Assignment operator + ResourceRef &operator=(const ResourceRef &other) + { + if (this != &other) + { + if (mResource) + mResource->decRef(); + + mResource = other.mResource; + + if (mResource) + mResource->incRef(); + } + return *this; + } + + // Allow dereferencing + RESOURCE *operator->() const + { return mResource; } + + RESOURCE *get() const + { return mResource; } + + // Allow implicit conversion to RESOURCE * + operator RESOURCE *() const + { return mResource; } + +private: + RESOURCE *mResource; +}; + #endif -- cgit v1.2.3-70-g09d2