Mana
Loading...
Searching...
No Matches
sprite.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "sprite.h"
23
24#include "graphics.h"
25
26#include "resources/action.h"
27#include "resources/animation.h"
28#include "resources/image.h"
30
31#include <cassert>
32
34 mSprite(sprite)
35{
36 assert(mSprite);
37
38 // Play the stand animation by default
39 play(SpriteAction::STAND);
40}
41
42Sprite *Sprite::load(const std::string &filename, int variant)
43{
45 auto spriteDef = resman->getSprite(filename, variant);
46 if (!spriteDef)
47 return nullptr;
48 return new Sprite(spriteDef);
49}
50
51Sprite::~Sprite() = default;
52
54{
55 bool ret = mFrameIndex !=0 || mFrameTime != 0;
56
57 mFrameIndex = 0;
58 mFrameTime = 0;
59
60 if (mAnimation)
62 else
63 mFrame = nullptr;
64 return ret;
65}
66
67bool Sprite::play(const std::string &spriteAction)
68{
69 Action *action = mSprite->getAction(spriteAction);
70 if (!action)
71 return false;
72
73 mAction = action;
75
76 if (animation && animation != mAnimation && animation->getLength() > 0)
77 {
78 mAnimation = animation;
79 reset();
80
81 return true;
82 }
83
84 return false;
85}
86
87bool Sprite::update(int dt)
88{
89 if (!mAnimation)
90 return false;
91
92 Animation *animation = mAnimation;
93 Frame *frame = mFrame;
94
96 {
97 // Animation finished, reset to default
98 play(SpriteAction::STAND);
99 }
100
101 // Make sure something actually changed
102 return animation != mAnimation || frame != mFrame;
103}
104
106{
108 return false;
109
110 mFrameTime += dt;
111
112 while (mFrameTime > mFrame->delay && mFrame->delay > 0)
113 {
115 mFrameIndex++;
116
118 mFrameIndex = 0;
119
121
123 {
124 mAnimation = nullptr;
125 mFrame = nullptr;
126 return false;
127 }
128 }
129
130 return true;
131}
132
133bool Sprite::draw(Graphics *graphics, int posX, int posY) const
134{
135 if (!mFrame)
136 return false;
137
138 if (!mFrame->image)
139 return false;
140
141 if (mFrame->image->getAlpha() != mAlpha)
143
145 posX + mFrame->offsetX,
146 posY + mFrame->offsetY);
147}
148
150{
151 if (mDirection != direction)
152 {
153 mDirection = direction;
154
155 if (!mAction)
156 return false;
157
159
160 if (animation && animation != mAnimation && animation->getLength() > 0)
161 {
162 mAnimation = animation;
163 reset();
164 }
165
166 return true;
167 }
168
169 return false;
170}
171
173{
174 if (mAnimation)
175 return mAnimation->getDuration();
176 return 0;
177}
178
180{
181 if (mFrame && mFrame->image)
182 return mFrame->image->getWidth();
183 return 0;
184}
185
187{
188 if (mFrame && mFrame->image)
189 return mFrame->image->getHeight();
190 return 0;
191}
192
194{
195 return mFrame ? mFrame->offsetX : 0;
196}
197
199{
200 return mFrame ? mFrame->offsetY : 0;
201}
202
204{
205 return mFrame ? mFrame->image : nullptr;
206}
An action consists of several animations, one for each direction.
Definition action.h:32
Animation * getAnimation(int direction) const
Definition action.cpp:35
An animation consists of several frames, each with their own delay and offset.
Definition animation.h:47
Frame * getFrame(int index)
Returns the frame at the specified index.
Definition animation.h:65
static bool isTerminator(const Frame &phase)
Determines whether the given animation frame is a terminator.
Definition animation.cpp:45
int getLength() const
Returns the length of this animation in frames.
Definition animation.h:70
int getDuration() const
Returns the duration of this animation in milliseconds.
Definition animation.h:75
A central point of control for graphics.
Definition graphics.h:78
bool drawImage(const Image *image, int x, int y)
Blits an image onto the screen.
Definition graphics.cpp:36
Defines a class for loading and storing images.
Definition image.h:45
int getHeight() const
Returns the height of the image.
Definition image.h:89
int getWidth() const
Returns the width of the image.
Definition image.h:83
void setAlpha(float alpha)
Sets the alpha value of this image.
Definition image.cpp:176
float getAlpha() const
Returns the alpha value of this image.
Definition image.h:106
A class for loading and managing resources.
ResourceRef< SpriteDef > getSprite(const std::string &path, int variant=0)
Loads a SpriteDef based on a given path and the supplied variant.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
Defines a class to load an animation.
Definition spritedef.h:87
Action * getAction(const std::string &action) const
Returns the specified action.
Definition spritedef.cpp:41
Animates a sprite by adding playback state.
Definition sprite.h:37
int getOffsetY() const
Gets the vertical offset that the sprite will be drawn at.
Definition sprite.cpp:198
int getDuration() const
Returns the duration of the current sprite animation in milliseconds.
Definition sprite.cpp:172
int mFrameIndex
The index of the current frame.
Definition sprite.h:138
int mFrameTime
The time since start of frame.
Definition sprite.h:139
Sprite(SpriteDef *sprite)
Constructor.
Definition sprite.cpp:33
bool draw(Graphics *graphics, int posX, int posY) const
Draw the current animation frame at the coordinates given in screen pixels.
Definition sprite.cpp:133
Animation * mAnimation
The currently active animation.
Definition sprite.h:143
bool updateCurrentAnimation(int dt)
Definition sprite.cpp:105
int getWidth() const
Gets the width in pixels of the image.
Definition sprite.cpp:179
SpriteDirection mDirection
The sprite direction.
Definition sprite.h:136
bool update(int time)
Inform the animation of the passed time so that it can output the correct animation frame.
Definition sprite.cpp:87
Action * mAction
The currently active action.
Definition sprite.h:142
const Image * getImage() const
Returns a reference to the current image being drawn.
Definition sprite.cpp:203
static Sprite * load(const std::string &filename, int variant=0)
An helper function, which will request the sprite to animate from the resource manager.
Definition sprite.cpp:42
float mAlpha
The alpha opacity used to draw.
Definition sprite.h:134
int getOffsetX() const
Gets the horizontal offset that the sprite will be drawn at.
Definition sprite.cpp:193
bool setDirection(SpriteDirection direction)
Sets the direction.
Definition sprite.cpp:149
ResourceRef< SpriteDef > mSprite
The sprite definition.
Definition sprite.h:141
Frame * mFrame
The currently active frame.
Definition sprite.h:144
bool reset()
Resets the sprite.
Definition sprite.cpp:53
int getHeight() const
Gets the height in pixels of the image.
Definition sprite.cpp:186
bool play(const std::string &action)
Plays an action using the current direction.
Definition sprite.cpp:67
Graphics * graphics
Definition client.cpp:104
SpriteDirection
Definition spritedef.h:74
A single frame in an animation, with a delay and an offset.
Definition animation.h:35
Image * image
Definition animation.h:36
int delay
Definition animation.h:37
int offsetX
Definition animation.h:38
int offsetY
Definition animation.h:39