diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-17 20:58:47 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-21 15:47:59 +0100 |
commit | 2f5c1467be6f46de659763e90c5d209c63e320b2 (patch) | |
tree | 41befd7edabc372a64639a76074f4f781dfb9037 /src | |
parent | b57e51ff732c93808aa844c511b87decc533237d (diff) | |
download | mana-2f5c1467be6f46de659763e90c5d209c63e320b2.tar.gz mana-2f5c1467be6f46de659763e90c5d209c63e320b2.tar.bz2 mana-2f5c1467be6f46de659763e90c5d209c63e320b2.tar.xz mana-2f5c1467be6f46de659763e90c5d209c63e320b2.zip |
Merged AnimatedSprite into Sprite
With AnimatedSprite being the only implementation of the Sprite
interface, there was no longer a point in separating these.
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/actorsprite.cpp | 6 | ||||
-rw-r--r-- | src/animatedsprite.h | 89 | ||||
-rw-r--r-- | src/being.cpp | 6 | ||||
-rw-r--r-- | src/gui/ministatuswindow.cpp | 6 | ||||
-rw-r--r-- | src/gui/ministatuswindow.h | 6 | ||||
-rw-r--r-- | src/simpleanimation.h | 2 | ||||
-rw-r--r-- | src/sprite.cpp (renamed from src/animatedsprite.cpp) | 34 | ||||
-rw-r--r-- | src/sprite.h | 72 | ||||
-rw-r--r-- | src/statuseffect.cpp | 4 | ||||
-rw-r--r-- | src/statuseffect.h | 4 |
11 files changed, 85 insertions, 147 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8add7f9a..2319610c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -399,8 +399,6 @@ set(SRCS actorsprite.h actorspritemanager.cpp actorspritemanager.h - animatedsprite.cpp - animatedsprite.h animationparticle.cpp animationparticle.h avatar.cpp @@ -489,6 +487,7 @@ set(SRCS simpleanimation.h sound.cpp sound.h + sprite.cpp sprite.h statuseffect.cpp statuseffect.h diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp index 444696c6..68803e26 100644 --- a/src/actorsprite.cpp +++ b/src/actorsprite.cpp @@ -20,13 +20,13 @@ #include "actorsprite.h" -#include "animatedsprite.h" #include "configuration.h" #include "event.h" #include "localplayer.h" #include "log.h" #include "particle.h" #include "simpleanimation.h" +#include "sprite.h" #include "resources/animation.h" #include "resources/imageset.h" @@ -134,13 +134,13 @@ void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display, for (const auto &sprite : display.sprites) { std::string file = paths.getStringValue("sprites") + sprite.sprite; - mSprites.add(AnimatedSprite::load(file, sprite.variant)); + mSprites.add(Sprite::load(file, sprite.variant)); } // Ensure that something is shown, if desired if (mSprites.size() == 0 && forceDisplay) { - mSprites.add(AnimatedSprite::load(paths.getStringValue("sprites") + mSprites.add(Sprite::load(paths.getStringValue("sprites") + paths.getStringValue("spriteErrorFile"))); } diff --git a/src/animatedsprite.h b/src/animatedsprite.h deleted file mode 100644 index d68031f2..00000000 --- a/src/animatedsprite.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * The Mana Client - * Copyright (C) 2004-2009 The Mana World Development Team - * Copyright (C) 2009-2012 The Mana Developers - * - * This file is part of The Mana Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#pragma once - -#include "sprite.h" - -#include <string> - -class Animation; -struct Frame; - -/** - * Animates a sprite by adding playback state. - */ -class AnimatedSprite final : public Sprite -{ - public: - /** - * Constructor. - * @param sprite the sprite to animate - */ - AnimatedSprite(SpriteDef *sprite); - - /** - * An helper function, which will request the sprite to animate - * from the resource manager. - * - * @param filename the file of the sprite to animate - * @param variant the sprite variant - */ - static AnimatedSprite *load(const std::string &filename, - int variant = 0); - - ~AnimatedSprite() override; - - bool reset() override; - - bool play(const std::string &action) override; - - bool update(int dt) override; - - bool draw(Graphics *graphics, int posX, int posY) const override; - - int getWidth() const override; - - int getHeight() const override; - - int getOffsetX() const override; - - int getOffsetY() const override; - - const Image *getImage() const override; - - bool setDirection(SpriteDirection direction) override; - - int getDuration() const override; - - private: - bool updateCurrentAnimation(int dt); - - SpriteDirection mDirection = DIRECTION_DOWN; /**< The sprite direction. */ - - int mFrameIndex = 0; /**< The index of the current frame. */ - int mFrameTime = 0; /**< The time since start of frame. */ - - ResourceRef<SpriteDef> mSprite; /**< The sprite definition. */ - Action *mAction = nullptr; /**< The currently active action. */ - Animation *mAnimation = nullptr; /**< The currently active animation. */ - Frame *mFrame = nullptr; /**< The currently active frame. */ -}; diff --git a/src/being.cpp b/src/being.cpp index 7de30d9b..ae3f13de 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -22,7 +22,6 @@ #include "being.h" #include "actorspritemanager.h" -#include "animatedsprite.h" #include "client.h" #include "configuration.h" #include "effectmanager.h" @@ -36,6 +35,7 @@ #include "party.h" #include "playerrelations.h" #include "sound.h" +#include "sprite.h" #include "statuseffect.h" #include "text.h" @@ -1264,14 +1264,14 @@ void Being::updatePlayerSprites() auto &itemInfo = itemDb->get(spriteState.visibleId); std::string filename = itemInfo.getSprite(mGender, mSubType); - AnimatedSprite *equipmentSprite = nullptr; + Sprite *equipmentSprite = nullptr; if (!filename.empty()) { if (!spriteState.color.empty()) filename += "|" + spriteState.color; - equipmentSprite = AnimatedSprite::load( + equipmentSprite = Sprite::load( paths.getStringValue("sprites") + filename); if (equipmentSprite) diff --git a/src/gui/ministatuswindow.cpp b/src/gui/ministatuswindow.cpp index 8172189b..534a5c42 100644 --- a/src/gui/ministatuswindow.cpp +++ b/src/gui/ministatuswindow.cpp @@ -21,10 +21,10 @@ #include "gui/ministatuswindow.h" -#include "animatedsprite.h" #include "configuration.h" #include "graphics.h" #include "playerinfo.h" +#include "sprite.h" #include "statuseffect.h" #include "gui/gui.h" @@ -95,7 +95,7 @@ MiniStatusWindow::MiniStatusWindow(): addMouseListener(this); } -void MiniStatusWindow::setIcon(int index, AnimatedSprite *sprite) +void MiniStatusWindow::setIcon(int index, Sprite *sprite) { if (index >= (int) mIcons.size()) mIcons.resize(index + 1); @@ -168,7 +168,7 @@ void MiniStatusWindow::event(Event::Channel channel, effect->deliverMessage(); effect->playSFX(); - AnimatedSprite *sprite = effect->getIcon(); + Sprite *sprite = effect->getIcon(); if (!sprite) { diff --git a/src/gui/ministatuswindow.h b/src/gui/ministatuswindow.h index 9400b661..b496fb00 100644 --- a/src/gui/ministatuswindow.h +++ b/src/gui/ministatuswindow.h @@ -27,7 +27,7 @@ #include <vector> -class AnimatedSprite; +class Sprite; class Graphics; class ProgressBar; class TextPopup; @@ -60,7 +60,7 @@ class MiniStatusWindow : public Popup, public EventListener /** * Sets one of the icons. */ - void setIcon(int index, AnimatedSprite *sprite); + void setIcon(int index, Sprite *sprite); void eraseIcon(int index); @@ -73,7 +73,7 @@ class MiniStatusWindow : public Popup, public EventListener TextPopup *mTextPopup; std::vector<int> mStatusEffectIcons; - std::vector<AnimatedSprite *> mIcons; + std::vector<Sprite *> mIcons; }; extern MiniStatusWindow *miniStatusWindow; diff --git a/src/simpleanimation.h b/src/simpleanimation.h index 63827fcd..94cc3a34 100644 --- a/src/simpleanimation.h +++ b/src/simpleanimation.h @@ -28,7 +28,7 @@ class Graphics; /** - * This class is a leightweight alternative to the AnimatedSprite class. + * This class is a leightweight alternative to the Sprite class. * It hosts a looping animation without actions and directions. */ class SimpleAnimation final diff --git a/src/animatedsprite.cpp b/src/sprite.cpp index 6491e696..9a329754 100644 --- a/src/animatedsprite.cpp +++ b/src/sprite.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "animatedsprite.h" +#include "sprite.h" #include "graphics.h" @@ -30,7 +30,7 @@ #include <cassert> -AnimatedSprite::AnimatedSprite(SpriteDef *sprite): +Sprite::Sprite(SpriteDef *sprite): mSprite(sprite) { assert(mSprite); @@ -39,20 +39,20 @@ AnimatedSprite::AnimatedSprite(SpriteDef *sprite): play(SpriteAction::STAND); } -AnimatedSprite *AnimatedSprite::load(const std::string &filename, int variant) +Sprite *Sprite::load(const std::string &filename, int variant) { ResourceManager *resman = ResourceManager::getInstance(); SpriteDef *s = resman->getSprite(filename, variant); if (!s) return nullptr; - auto *as = new AnimatedSprite(s); + auto *as = new Sprite(s); s->decRef(); return as; } -AnimatedSprite::~AnimatedSprite() = default; +Sprite::~Sprite() = default; -bool AnimatedSprite::reset() +bool Sprite::reset() { bool ret = mFrameIndex !=0 || mFrameTime != 0; @@ -62,7 +62,7 @@ bool AnimatedSprite::reset() return ret; } -bool AnimatedSprite::play(const std::string &spriteAction) +bool Sprite::play(const std::string &spriteAction) { Action *action = mSprite->getAction(spriteAction); if (!action) @@ -84,7 +84,7 @@ bool AnimatedSprite::play(const std::string &spriteAction) return false; } -bool AnimatedSprite::update(int dt) +bool Sprite::update(int dt) { if (!mAnimation) return false; @@ -102,7 +102,7 @@ bool AnimatedSprite::update(int dt) return animation != mAnimation || frame != mFrame; } -bool AnimatedSprite::updateCurrentAnimation(int dt) +bool Sprite::updateCurrentAnimation(int dt) { if (!mFrame || Animation::isTerminator(*mFrame)) return false; @@ -130,7 +130,7 @@ bool AnimatedSprite::updateCurrentAnimation(int dt) return true; } -bool AnimatedSprite::draw(Graphics *graphics, int posX, int posY) const +bool Sprite::draw(Graphics *graphics, int posX, int posY) const { if (!mFrame) return false; @@ -146,7 +146,7 @@ bool AnimatedSprite::draw(Graphics *graphics, int posX, int posY) const posY + mFrame->offsetY); } -bool AnimatedSprite::setDirection(SpriteDirection direction) +bool Sprite::setDirection(SpriteDirection direction) { if (mDirection != direction) { @@ -170,38 +170,38 @@ bool AnimatedSprite::setDirection(SpriteDirection direction) return false; } -int AnimatedSprite::getDuration() const +int Sprite::getDuration() const { if (mAnimation) return mAnimation->getDuration(); return 0; } -int AnimatedSprite::getWidth() const +int Sprite::getWidth() const { if (mFrame && mFrame->image) return mFrame->image->getWidth(); return 0; } -int AnimatedSprite::getHeight() const +int Sprite::getHeight() const { if (mFrame && mFrame->image) return mFrame->image->getHeight(); return 0; } -int AnimatedSprite::getOffsetX() const +int Sprite::getOffsetX() const { return mFrame ? mFrame->offsetX : 0; } -int AnimatedSprite::getOffsetY() const +int Sprite::getOffsetY() const { return mFrame ? mFrame->offsetY : 0; } -const Image *AnimatedSprite::getImage() const +const Image *Sprite::getImage() const { return mFrame ? mFrame->image : nullptr; } diff --git a/src/sprite.h b/src/sprite.h index 80076f99..b82bf7bb 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -22,30 +22,50 @@ #include "resources/spritedef.h" +class Animation; class Graphics; class Image; +struct Frame; // Default frame display delay in milliseconds const int DEFAULT_FRAME_DELAY = 75; +/** + * Animates a sprite by adding playback state. + */ class Sprite { public: - virtual ~Sprite() = default; + /** + * Constructor. + * @param sprite the sprite to animate + */ + Sprite(SpriteDef *sprite); + + /** + * An helper function, which will request the sprite to animate + * from the resource manager. + * + * @param filename the file of the sprite to animate + * @param variant the sprite variant + */ + static Sprite *load(const std::string &filename, int variant = 0); + + ~Sprite(); /** * Resets the sprite. * * @returns true if the sprite changed, false otherwise */ - virtual bool reset() = 0; + bool reset(); /** * Plays an action using the current direction. * * @returns true if the sprite changed, false otherwise */ - virtual bool play(const std::string &action) = 0; + bool play(const std::string &action); /** * Inform the animation of the passed time so that it can output the @@ -53,65 +73,73 @@ class Sprite * * @returns true if the sprite changed, false otherwise */ - virtual bool update(int time) = 0; + bool update(int time); /** * Draw the current animation frame at the coordinates given in screen * pixels. */ - virtual bool draw(Graphics *graphics, int posX, int posY) const = 0; + bool draw(Graphics *graphics, int posX, int posY) const; /** * Gets the width in pixels of the image */ - virtual int getWidth() const = 0; + int getWidth() const; /** * Gets the height in pixels of the image */ - virtual int getHeight() const = 0; + int getHeight() const; /** * Gets the horizontal offset that the sprite will be drawn at */ - virtual int getOffsetX() const - { return 0; } + int getOffsetX() const; /** * Gets the vertical offset that the sprite will be drawn at */ - virtual int getOffsetY() const - { return 0; } + int getOffsetY() const; /** * Returns a reference to the current image being drawn. */ - virtual const Image *getImage() const = 0; + const Image *getImage() const; /** * Sets the direction. * * @returns true if the sprite changed, false otherwise */ - virtual bool setDirection(SpriteDirection direction) = 0; + bool setDirection(SpriteDirection direction); /** - * Sets the alpha value of the animated sprite + * Sets the alpha value of the sprite. */ - virtual void setAlpha(float alpha) - { mAlpha = alpha; } + void setAlpha(float alpha) { mAlpha = alpha; } /** - * Returns the current alpha opacity of the animated sprite. + * Returns the current alpha opacity of the sprite. */ - virtual float getAlpha() const - { return mAlpha; } + float getAlpha() const { return mAlpha; } /** * Returns the duration of the current sprite animation in milliseconds. */ - virtual int getDuration() const = 0; + int getDuration() const; + + private: + bool updateCurrentAnimation(int dt); + + float mAlpha = 1.0f; /**< The alpha opacity used to draw */ + + SpriteDirection mDirection = DIRECTION_DOWN; /**< The sprite direction. */ + + int mFrameIndex = 0; /**< The index of the current frame. */ + int mFrameTime = 0; /**< The time since start of frame. */ - protected: - float mAlpha = 1.0f; /**< The alpha opacity used to draw */ + ResourceRef<SpriteDef> mSprite; /**< The sprite definition. */ + Action *mAction = nullptr; /**< The currently active action. */ + Animation *mAnimation = nullptr; /**< The currently active animation. */ + Frame *mFrame = nullptr; /**< The currently active frame. */ }; diff --git a/src/statuseffect.cpp b/src/statuseffect.cpp index f06ab827..a493b24f 100644 --- a/src/statuseffect.cpp +++ b/src/statuseffect.cpp @@ -54,12 +54,12 @@ Particle *StatusEffect::getParticle() return particleEngine->addEffect(mParticleEffect, 0, 0); } -AnimatedSprite *StatusEffect::getIcon() +Sprite *StatusEffect::getIcon() { if (mIcon.empty()) return nullptr; - AnimatedSprite *sprite = AnimatedSprite::load( + Sprite *sprite = Sprite::load( paths.getStringValue("sprites") + mIcon); if (false && sprite) { diff --git a/src/statuseffect.h b/src/statuseffect.h index e383a24b..d07609cc 100644 --- a/src/statuseffect.h +++ b/src/statuseffect.h @@ -22,7 +22,7 @@ #pragma once #include "particle.h" -#include "animatedsprite.h" +#include "sprite.h" #include "utils/xml.h" @@ -52,7 +52,7 @@ public: /** * Retrieves the status icon for this effect, if applicable */ - AnimatedSprite *getIcon(); + Sprite *getIcon(); /** * Retrieves an action to perform, or SpriteAction::INVALID |