summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-01-28 20:56:41 +0100
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-01-28 20:57:58 +0100
commit264be2108c51837fa92085f6c839e66aebbcfc9e (patch)
treee1f535d4cda5f399e5e622f6154690a0a4e08c4b /src/resources
parentd86a1df562e00a6e930683534b9d001f45a951ff (diff)
downloadMana-264be2108c51837fa92085f6c839e66aebbcfc9e.tar.gz
Mana-264be2108c51837fa92085f6c839e66aebbcfc9e.tar.bz2
Mana-264be2108c51837fa92085f6c839e66aebbcfc9e.tar.xz
Mana-264be2108c51837fa92085f6c839e66aebbcfc9e.zip
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
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/ambientlayer.cpp16
-rw-r--r--src/resources/ambientlayer.h27
-rw-r--r--src/resources/mapreader.cpp20
-rw-r--r--src/resources/resource.h68
4 files changed, 104 insertions, 27 deletions
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<Image> 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<typename RESOURCE>
+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