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 42 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 43 of file simpleanimation.cpp.

43  :
44  mAnimation(animation),
45  mAnimationTime(0),
46  mAnimationPhase(0),
47  mCurrentFrame(mAnimation != nullptr ? &mAnimation->mFrames[0] : nullptr),
48  mInitialized(true),
49  mImageSet(nullptr)
50 {
51 }
Frames mFrames
Definition: animation.h:99
Animation * mAnimation
const Frame * mCurrentFrame
ImageSet * mImageSet

◆ 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 53 of file simpleanimation.cpp.

54  :
55  mAnimation(new Animation("simple animation")),
56  mAnimationTime(0),
57  mAnimationPhase(0),
58  mCurrentFrame(nullptr),
59  mInitialized(false),
60  mImageSet(nullptr)
61 {
62  initializeAnimation(animationNode, dyePalettes);
63  if (mAnimation != nullptr)
65  else
66  mCurrentFrame = nullptr;
67 }
void initializeAnimation(const xmlNodePtr animationNode, const std::string &dyePalettes)

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

◆ ~SimpleAnimation()

SimpleAnimation::~SimpleAnimation ( )

Definition at line 69 of file simpleanimation.cpp.

70 {
72  if (mImageSet != nullptr)
73  {
74  mImageSet->decRef();
75  mImageSet = nullptr;
76  }
77 }
virtual void decRef()
Definition: resource.cpp:50
#define delete2(var)
Definition: delete2.h:25

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 79 of file simpleanimation.cpp.

81 {
82  FUNC_BLOCK("SimpleAnimation::draw", 1)
83  if ((mCurrentFrame == nullptr) || (mCurrentFrame->image == nullptr))
84  return;
85 
86  graphics->drawImage(mCurrentFrame->image,
87  posX + mCurrentFrame->offsetX,
88  posY + mCurrentFrame->offsetY);
89 }
if(!vert) return
void drawImage(const Image *restrict const image, int dstX, int dstY) restrict2 override final
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:81

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 141 of file simpleanimation.cpp.

142 {
143  if (mCurrentFrame != nullptr)
144  return mCurrentFrame->image;
145  return nullptr;
146 }
Image * image
Definition: frame.h:42

References Frame::image, and mCurrentFrame.

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

◆ getLength()

int SimpleAnimation::getLength ( ) const

Definition at line 133 of file simpleanimation.cpp.

134 {
135  if (mAnimation == nullptr)
136  return 0;
137 
138  return CAST_S32(mAnimation->getLength());
139 }
#define CAST_S32
Definition: cast.h:30
size_t getLength() const
Definition: animation.h:70

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 148 of file simpleanimation.cpp.

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

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 91 of file simpleanimation.cpp.

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

References mAnimationPhase, and mAnimationTime.

◆ setFrame()

void SimpleAnimation::setFrame ( int  frame)

Definition at line 97 of file simpleanimation.cpp.

98 {
99  if (mAnimation == nullptr)
100  return;
101 
102  if (frame < 0)
103  frame = 0;
104  const unsigned int len = CAST_U32(mAnimation->getLength());
105  if (CAST_U32(frame) >= len)
106  frame = len - 1;
107  mAnimationPhase = frame;
108  mCurrentFrame = &mAnimation->mFrames[frame];
109 }
#define CAST_U32
Definition: cast.h:31

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 111 of file simpleanimation.cpp.

112 {
113  if ((mCurrentFrame == nullptr) || (mAnimation == nullptr) || !mInitialized)
114  return false;
115 
116  bool updated(false);
117  mAnimationTime += timePassed;
118 
120  {
121  updated = true;
123  mAnimationPhase++;
124 
126  mAnimationPhase = 0;
127 
129  }
130  return updated;
131 }
#define CAST_SIZE
Definition: cast.h:34
int delay
Definition: frame.h:43

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

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

Field Documentation

◆ mAnimation

Animation* SimpleAnimation::mAnimation
private

The hosted animation.

Definition at line 82 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 88 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 85 of file simpleanimation.h.

Referenced by reset(), and update().

◆ mCurrentFrame

const Frame* SimpleAnimation::mCurrentFrame
private

Current animation phase.

Definition at line 91 of file simpleanimation.h.

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

◆ mImageSet

ImageSet* SimpleAnimation::mImageSet
private

Definition at line 96 of file simpleanimation.h.

Referenced by ~SimpleAnimation().

◆ mInitialized

bool SimpleAnimation::mInitialized
private

Tell whether the animation is ready

Definition at line 94 of file simpleanimation.h.

Referenced by initializeAnimation(), and update().


The documentation for this class was generated from the following files: