ManaPlus
Public Member Functions | Private Member Functions | Private Attributes
SimpleAnimation Class Reference

#include <simpleanimation.h>

Public Member Functions

 SimpleAnimation (Animation *const animation)
 
 SimpleAnimation (const xmlNodePtr animationNode, const std::string &dyePalettes)
 
 ~SimpleAnimation ()
 
void setFrame (int frame)
 
int getLength () const
 
bool update (const int timePassed)
 
void draw (Graphics *const graphics, const int posX, const int posY) const
 
void reset ()
 
ImagegetCurrentImage () const
 

Private Member Functions

void initializeAnimation (const xmlNodePtr animationNode, const std::string &dyePalettes)
 

Private Attributes

AnimationmAnimation
 
int mAnimationTime
 
int mAnimationPhase
 
const FramemCurrentFrame
 
bool mInitialized
 
ImageSetmImageSet
 

Detailed Description

This class is a leightweight alternative to the AnimatedSprite class. It hosts a looping animation without actions and directions.

Definition at line 41 of file simpleanimation.h.

Constructor & Destructor Documentation

◆ SimpleAnimation() [1/2]

SimpleAnimation::SimpleAnimation ( Animation *const  animation)
explicit

Creates a simple animation with an already created animation. Takes ownership over the given animation.

Definition at line 42 of file simpleanimation.cpp.

42  :
43  mAnimation(animation),
44  mAnimationTime(0),
45  mAnimationPhase(0),
46  mCurrentFrame(mAnimation != nullptr ? &mAnimation->mFrames[0] : nullptr),
47  mInitialized(true),
48  mImageSet(nullptr)
49 {
50 }

◆ SimpleAnimation() [2/2]

SimpleAnimation::SimpleAnimation ( const xmlNodePtr  animationNode,
const std::string &  dyePalettes 
)

Creates a simple animation that creates its animation from XML Data.

Definition at line 52 of file simpleanimation.cpp.

53  :
54  mAnimation(new Animation("simple animation")),
55  mAnimationTime(0),
56  mAnimationPhase(0),
57  mCurrentFrame(nullptr),
58  mInitialized(false),
59  mImageSet(nullptr)
60 {
61  initializeAnimation(animationNode, dyePalettes);
62  if (mAnimation != nullptr)
64  else
65  mCurrentFrame = nullptr;
66 }

References initializeAnimation(), mAnimation, mCurrentFrame, and Animation::mFrames.

◆ ~SimpleAnimation()

SimpleAnimation::~SimpleAnimation ( )

Definition at line 68 of file simpleanimation.cpp.

69 {
71  if (mImageSet != nullptr)
72  {
73  mImageSet->decRef();
74  mImageSet = nullptr;
75  }
76 }

References Resource::decRef(), delete2, mAnimation, and mImageSet.

Member Function Documentation

◆ draw()

void SimpleAnimation::draw ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const

Definition at line 78 of file simpleanimation.cpp.

80 {
81  FUNC_BLOCK("SimpleAnimation::draw", 1)
82  if ((mCurrentFrame == nullptr) || (mCurrentFrame->image == nullptr))
83  return;
84 
85  graphics->drawImage(mCurrentFrame->image,
86  posX + mCurrentFrame->offsetX,
87  posY + mCurrentFrame->offsetY);
88 }

References Graphics::drawImage(), FUNC_BLOCK, Frame::image, mCurrentFrame, Frame::offsetX, and Frame::offsetY.

Referenced by ProgressIndicator::draw(), and ProgressIndicator::safeDraw().

◆ getCurrentImage()

Image * SimpleAnimation::getCurrentImage ( ) const

Definition at line 140 of file simpleanimation.cpp.

141 {
142  if (mCurrentFrame != nullptr)
143  return mCurrentFrame->image;
144  return nullptr;
145 }

References Frame::image, and mCurrentFrame.

Referenced by TileAnimation::update(), and Particle::update().

◆ getLength()

int SimpleAnimation::getLength ( ) const

Definition at line 132 of file simpleanimation.cpp.

133 {
134  if (mAnimation == nullptr)
135  return 0;
136 
137  return CAST_S32(mAnimation->getLength());
138 }

References CAST_S32, Animation::getLength(), and mAnimation.

Referenced by Particle::update().

◆ initializeAnimation()

void SimpleAnimation::initializeAnimation ( const xmlNodePtr  animationNode,
const std::string &  dyePalettes 
)
private

Definition at line 147 of file simpleanimation.cpp.

149 {
150  mInitialized = false;
151 
152  if (animationNode == nullptr)
153  return;
154 
155  std::string imagePath = XML::getProperty(
156  animationNode, "imageset", "");
157 
158  // Instanciate the dye coloration.
159  if (!imagePath.empty() && !dyePalettes.empty())
160  Dye::instantiate(imagePath, dyePalettes);
161 
162  const ImageSet *const imageset = Loader::getImageSet(
163  XML::getProperty(animationNode, "imageset", ""),
164  XML::getProperty(animationNode, "width", 0),
165  XML::getProperty(animationNode, "height", 0));
166 
167  if (imageset == nullptr)
168  return;
169 
170  const int x1 = imageset->getWidth() / 2 - mapTileSize / 2;
171  const int y1 = imageset->getHeight() - mapTileSize;
172 
173  // Get animation frames
174  for_each_xml_child_node (frameNode, animationNode)
175  {
176  const int delay = XML::getIntProperty(
177  frameNode, "delay", 0, 0, 100000);
178  const int offsetX = XML::getProperty(frameNode, "offsetX", 0) - x1;
179  const int offsetY = XML::getProperty(frameNode, "offsetY", 0) - y1;
180  const int rand = XML::getIntProperty(frameNode, "rand", 100, 0, 100);
181 
182  if (xmlNameEqual(frameNode, "frame"))
183  {
184  const int index = XML::getProperty(frameNode, "index", -1);
185 
186  if (index < 0)
187  {
188  reportAlways("No valid value for 'index'")
189  continue;
190  }
191 
192  Image *const img = imageset->get(index);
193 
194  if (img == nullptr)
195  {
196  reportAlways("No image at index %d", index)
197  continue;
198  }
199 
200  if (mAnimation != nullptr)
201  mAnimation->addFrame(img, delay, offsetX, offsetY, rand);
202  }
203  else if (xmlNameEqual(frameNode, "sequence"))
204  {
205  int start = XML::getProperty(frameNode, "start", -1);
206  const int end = XML::getProperty(frameNode, "end", -1);
207 
208  if (start < 0 || end < 0)
209  {
210  reportAlways("No valid value for 'start' or 'end'")
211  continue;
212  }
213 
214  while (end >= start)
215  {
216  Image *const img = imageset->get(start);
217 
218  if (img == nullptr)
219  {
220  reportAlways("No image at index %d", start)
221  continue;
222  }
223 
224  if (mAnimation != nullptr)
225  mAnimation->addFrame(img, delay, offsetX, offsetY, rand);
226  start++;
227  }
228  }
229  else if (xmlNameEqual(frameNode, "end"))
230  {
231  if (mAnimation != nullptr)
232  mAnimation->addTerminator(rand);
233  }
234  }
235 
236  mInitialized = true;
237 }

References Animation::addFrame(), Animation::addTerminator(), for_each_xml_child_node, ImageSet::get(), ImageSet::getHeight(), Loader::getImageSet(), XML::getIntProperty(), XML::getProperty(), ImageSet::getWidth(), Dye::instantiate(), mAnimation, mapTileSize, mInitialized, reportAlways, and anonymous_namespace{stringutils.cpp}::start.

Referenced by SimpleAnimation().

◆ reset()

void SimpleAnimation::reset ( )

Resets the animation.

Definition at line 90 of file simpleanimation.cpp.

91 {
92  mAnimationTime = 0;
93  mAnimationPhase = 0;
94 }

References mAnimationPhase, and mAnimationTime.

◆ setFrame()

void SimpleAnimation::setFrame ( int  frame)

Definition at line 96 of file simpleanimation.cpp.

97 {
98  if (mAnimation == nullptr)
99  return;
100 
101  if (frame < 0)
102  frame = 0;
103  const unsigned int len = CAST_U32(mAnimation->getLength());
104  if (CAST_U32(frame) >= len)
105  frame = len - 1;
106  mAnimationPhase = frame;
107  mCurrentFrame = &mAnimation->mFrames[frame];
108 }

References CAST_U32, Animation::getLength(), mAnimation, mAnimationPhase, mCurrentFrame, and Animation::mFrames.

Referenced by Particle::update().

◆ update()

bool SimpleAnimation::update ( const int  timePassed)

Definition at line 110 of file simpleanimation.cpp.

111 {
112  if ((mCurrentFrame == nullptr) || (mAnimation == nullptr) || !mInitialized)
113  return false;
114 
115  bool updated(false);
116  mAnimationTime += timePassed;
117 
119  {
120  updated = true;
122  mAnimationPhase++;
123 
125  mAnimationPhase = 0;
126 
128  }
129  return updated;
130 }

References CAST_SIZE, Frame::delay, Animation::getLength(), mAnimation, mAnimationPhase, mAnimationTime, mCurrentFrame, Animation::mFrames, and mInitialized.

Referenced by ProgressIndicator::logic(), TileAnimation::update(), and Particle::update().

Field Documentation

◆ mAnimation

Animation* SimpleAnimation::mAnimation
private

The hosted animation.

Definition at line 81 of file simpleanimation.h.

Referenced by getLength(), initializeAnimation(), setFrame(), SimpleAnimation(), update(), and ~SimpleAnimation().

◆ mAnimationPhase

int SimpleAnimation::mAnimationPhase
private

Index of current animation phase.

Definition at line 87 of file simpleanimation.h.

Referenced by reset(), setFrame(), and update().

◆ mAnimationTime

int SimpleAnimation::mAnimationTime
private

Time in game ticks the current frame is shown.

Definition at line 84 of file simpleanimation.h.

Referenced by reset(), and update().

◆ mCurrentFrame

const Frame* SimpleAnimation::mCurrentFrame
private

Current animation phase.

Definition at line 90 of file simpleanimation.h.

Referenced by draw(), getCurrentImage(), setFrame(), SimpleAnimation(), and update().

◆ mImageSet

ImageSet* SimpleAnimation::mImageSet
private

Definition at line 95 of file simpleanimation.h.

Referenced by ~SimpleAnimation().

◆ mInitialized

bool SimpleAnimation::mInitialized
private

Tell whether the animation is ready

Definition at line 93 of file simpleanimation.h.

Referenced by initializeAnimation(), and update().


The documentation for this class was generated from the following files:
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
Animation::getLength
size_t getLength() const
Definition: animation.h:69
CAST_SIZE
#define CAST_SIZE
Definition: cast.h:33
anonymous_namespace{stringutils.cpp}::start
unsigned int start
Definition: stringutils.cpp:227
ParticleType::Animation
@ Animation
Definition: particletype.h:29
SimpleAnimation::mInitialized
bool mInitialized
Definition: simpleanimation.h:93
AttributesEnum::get
AttributesT get(const std::string &key)
Definition: attributesstrings.cpp:32
Loader::getImageSet
ImageSet * getImageSet(const std::string &imagePath, const int w, const int h)
Definition: imagesetloader.cpp:65
SimpleAnimation::mAnimationTime
int mAnimationTime
Definition: simpleanimation.h:84
Image
Definition: image.h:67
CAST_U32
#define CAST_U32
Definition: cast.h:30
SimpleAnimation::mCurrentFrame
const Frame * mCurrentFrame
Definition: simpleanimation.h:90
delete2
#define delete2(var)
Definition: delete2.h:24
XML::getIntProperty
int getIntProperty(const xmlNodePtr node, const char *const name, int def, const int min, const int max)
Definition: libxml.cpp:189
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
ImageSet::getWidth
int getWidth() const
Definition: imageset.h:59
SimpleAnimation::mAnimationPhase
int mAnimationPhase
Definition: simpleanimation.h:87
if
if(!vert) return
drawImage
void drawImage(const Image *restrict const image, int dstX, int dstY) restrict2 override final
ImageSet::get
Image * get(const size_type i) const
Definition: imageset.cpp:66
Frame::image
Image * image
Definition: frame.h:41
Animation::addTerminator
void addTerminator(const int rand)
Definition: animation.cpp:55
FUNC_BLOCK
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:80
SimpleAnimation::initializeAnimation
void initializeAnimation(const xmlNodePtr animationNode, const std::string &dyePalettes)
Definition: simpleanimation.cpp:147
ImageSet::getHeight
int getHeight() const
Definition: imageset.h:65
Resource::decRef
virtual void decRef()
Definition: resource.cpp:49
Dye::instantiate
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:96
CAST_S32
#define CAST_S32
Definition: cast.h:29
SimpleAnimation::mImageSet
ImageSet * mImageSet
Definition: simpleanimation.h:95
Animation::addFrame
void addFrame(Image *const image, const int delay, const int offsetX, const int offsetY, const int rand)
Definition: animation.cpp:45
SimpleAnimation::mAnimation
Animation * mAnimation
Definition: simpleanimation.h:81
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
Frame::delay
int delay
Definition: frame.h:42
ImageSet
Definition: imageset.h:37
mapTileSize
static const int mapTileSize
Definition: map.h:26
Animation::mFrames
Frames mFrames
Definition: animation.h:98