ManaPlus
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes
SpriteDef Class Reference

#include <spritedef.h>

Inheritance diagram for SpriteDef:
Resource MemoryCounter

Public Member Functions

const ActiongetAction (const std::string &action, const unsigned num) const
 
unsigned findNumber (const unsigned num) const
 
void addAction (const unsigned hp, const std::string &name, Action *const action)
 
int calcMemoryLocal () const
 
int calcMemoryChilds (const int level) const
 
bool addSequence (const int start, const int end, const int delay, const int offsetX, const int offsetY, const int variant_offset, int repeat, const int rand, const ImageSet *const imageSet, Animation *const animation) const
 
- Public Member Functions inherited from Resource
 Resource ()
 
 ~Resource ()
 
virtual void incRef ()
 
virtual void decRef ()
 
std::string getCounterName () const
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 

Static Public Member Functions

static SpriteDefload (const std::string &file, const int variant, const bool prot)
 
static SpriteDirection::Type makeSpriteDirection (const std::string &direction)
 

Private Types

typedef std::map< std::string, ImageSet * > ImageSets
 
typedef ImageSets::iterator ImageSetIterator
 
typedef ImageSets::const_iterator ImageSetCIterator
 
typedef std::map< std::string, Action * > ActionMap
 
typedef std::map< unsigned, ActionMap * > Actions
 
typedef Actions::const_iterator ActionsConstIter
 
typedef Actions::iterator ActionsIter
 
typedef Actions::const_iterator ActionsCIter
 

Private Member Functions

 SpriteDef ()
 
 ~SpriteDef ()
 
void loadSprite (const xmlNodePtr spriteNode, const int variant, const std::string &palettes)
 
void loadImageSet (const xmlNodePtr node, const std::string &palettes)
 
void loadAction (const xmlNodePtr node, const int variant_offset)
 
void loadAnimation (const xmlNodePtr animationNode, Action *const action, const ImageSet *const imageSet, const int variant_offset) const
 
void includeSprite (const xmlNodePtr includeNode, const int variant)
 
const ImageSetgetImageSet (const std::string &imageSetName) const
 
void substituteActions ()
 
void fixDeadAction ()
 
void substituteAction (const std::string &complete, const std::string &with)
 

Private Attributes

ImageSets mImageSets
 
Actions mActions
 
std::set< std::string > mProcessedFiles
 

Additional Inherited Members

- Data Fields inherited from Resource
time_t mTimeStamp
 
std::string mIdPath
 
std::string mSource
 
unsigned int mRefCount
 
bool mProtected
 
bool mNotCount
 

Detailed Description

Defines a class to load an animation.

Definition at line 41 of file spritedef.h.

Member Typedef Documentation

◆ ActionMap

typedef std::map<std::string, Action*> SpriteDef::ActionMap
private

Definition at line 156 of file spritedef.h.

◆ Actions

typedef std::map<unsigned, ActionMap*> SpriteDef::Actions
private

Definition at line 157 of file spritedef.h.

◆ ActionsCIter

typedef Actions::const_iterator SpriteDef::ActionsCIter
private

Definition at line 160 of file spritedef.h.

◆ ActionsConstIter

typedef Actions::const_iterator SpriteDef::ActionsConstIter
private

Definition at line 158 of file spritedef.h.

◆ ActionsIter

typedef Actions::iterator SpriteDef::ActionsIter
private

Definition at line 159 of file spritedef.h.

◆ ImageSetCIterator

typedef ImageSets::const_iterator SpriteDef::ImageSetCIterator
private

Definition at line 155 of file spritedef.h.

◆ ImageSetIterator

typedef ImageSets::iterator SpriteDef::ImageSetIterator
private

Definition at line 154 of file spritedef.h.

◆ ImageSets

typedef std::map<std::string, ImageSet*> SpriteDef::ImageSets
private

Definition at line 153 of file spritedef.h.

Constructor & Destructor Documentation

◆ SpriteDef()

SpriteDef::SpriteDef ( )
inlineprivate

Constructor.

Definition at line 89 of file spritedef.h.

89  :
90  Resource(),
91  mImageSets(),
92  mActions(),
94  { }
Resource()
Definition: resource.h:40
std::set< std::string > mProcessedFiles
Definition: spritedef.h:164
ImageSets mImageSets
Definition: spritedef.h:162
Actions mActions
Definition: spritedef.h:163

Referenced by calcMemoryLocal(), and load().

◆ ~SpriteDef()

SpriteDef::~SpriteDef ( )
private

Destructor.

Definition at line 524 of file spritedef.cpp.

525 {
526  // Actions are shared, so ensure they are deleted only once.
527  std::set<Action*> actions;
528  FOR_EACH (Actions::iterator, i, mActions)
529  {
530  FOR_EACHP (ActionMap::iterator, it, (*i).second)
531  actions.insert(it->second);
532  delete (*i).second;
533  }
534 
535  FOR_EACH (std::set<Action*>::const_iterator, i, actions)
536  delete *i;
537 
538  mActions.clear();
539 
541  {
542  if (i->second != nullptr)
543  {
544  i->second->decRef();
545  i->second = nullptr;
546  }
547  }
548  mImageSets.clear();
549 }
static const BeingActionT actions[]
Definition: action.h:41
ImageSets::iterator ImageSetIterator
Definition: spritedef.h:154
#define FOR_EACHP(type, iter, array)
Definition: foreach.h:31
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
void clear()
Definition: playerinfo.cpp:452

References actions, FOR_EACH, FOR_EACHP, mActions, and mImageSets.

Member Function Documentation

◆ addAction()

void SpriteDef::addAction ( const unsigned  hp,
const std::string &  name,
Action *const  action 
)

Definition at line 576 of file spritedef.cpp.

578 {
579  const Actions::const_iterator i = mActions.find(hp);
580  if (i == mActions.end())
581  mActions[hp] = new ActionMap;
582 
583  (*mActions[hp])[name] = action;
584 }
std::map< std::string, Action * > ActionMap
Definition: spritedef.h:156

References mActions.

Referenced by loadAction().

◆ addSequence()

bool SpriteDef::addSequence ( const int  start,
const int  end,
const int  delay,
const int  offsetX,
const int  offsetY,
const int  variant_offset,
int  repeat,
const int  rand,
const ImageSet *const  imageSet,
Animation *const  animation 
) const

Definition at line 586 of file spritedef.cpp.

596 {
597  if ((imageSet == nullptr) || (animation == nullptr))
598  return true;
599 
600  if (start < 0 || end < 0)
601  {
602  reportAlways("%s: No valid value for 'start' or 'end'",
603  mSource.c_str())
604  return true;
605  }
606 
607  if (start <= end)
608  {
609  while (repeat > 0)
610  {
611  int pos = start;
612  while (end >= pos)
613  {
614  Image *const img = imageSet->get(pos + variant_offset);
615 
616  if (img == nullptr)
617  {
618  reportAlways("%s: No image at index %d",
619  mSource.c_str(),
620  pos + variant_offset)
621  pos ++;
622  continue;
623  }
624 
625  animation->addFrame(img, delay,
626  offsetX, offsetY, rand);
627  pos ++;
628  }
629  repeat --;
630  }
631  }
632  else
633  {
634  while (repeat > 0)
635  {
636  int pos = start;
637  while (end <= pos)
638  {
639  Image *const img = imageSet->get(pos + variant_offset);
640 
641  if (img == nullptr)
642  {
643  reportAlways("%s: No image at index %d",
644  mSource.c_str(),
645  pos + variant_offset)
646  pos ++;
647  continue;
648  }
649 
650  animation->addFrame(img, delay,
651  offsetX, offsetY, rand);
652  pos --;
653  }
654  repeat --;
655  }
656  }
657  return false;
658 }
#define reportAlways(...)
Definition: checkutils.h:253
void addFrame(Image *const image, const int delay, const int offsetX, const int offsetY, const int rand)
Definition: animation.cpp:46
Image * get(const size_type i) const
Definition: imageset.cpp:67
std::string mSource
Definition: resource.h:85
if(!vert) return

References Animation::addFrame(), ImageSet::get(), Resource::mSource, reportAlways, and anonymous_namespace{stringutils.cpp}::start.

Referenced by loadAnimation().

◆ calcMemoryChilds()

int SpriteDef::calcMemoryChilds ( const int  level) const
virtual

Reimplemented from MemoryCounter.

Definition at line 674 of file spritedef.cpp.

675 {
676  int sz = 0;
677  FOR_EACH (ImageSets::const_iterator, it, mImageSets)
678  {
679  sz += static_cast<int>((*it).first.capacity());
680  const ImageSet *const imageSet = (*it).second;
681  sz += imageSet->calcMemory(level + 1);
682  }
684  {
685  sz += sizeof(unsigned);
686  const ActionMap *const actionMap = (*it).second;
687  FOR_EACHP (ActionMap::const_iterator, it2, actionMap)
688  {
689  sz += static_cast<int>((*it2).first.capacity());
690  const Action *const action = (*it2).second;
691  sz += action->calcMemory(level + 1);
692  }
693  }
694  return sz;
695 }
int calcMemory(const int level) const
Actions::const_iterator ActionsCIter
Definition: spritedef.h:160

References MemoryCounter::calcMemory(), FOR_EACH, FOR_EACHP, mActions, and mImageSets.

◆ calcMemoryLocal()

int SpriteDef::calcMemoryLocal ( ) const
virtual

Reimplemented from Resource.

Definition at line 660 of file spritedef.cpp.

661 {
662  int sz = static_cast<int>(sizeof(SpriteDef) +
663  sizeof(ImageSets) +
664  sizeof(Actions) +
665  sizeof(std::set<std::string>)) +
667  FOR_EACH (std::set<std::string>::const_iterator, it, mProcessedFiles)
668  {
669  sz += static_cast<int>((*it).capacity());
670  }
671  return sz;
672 }
int calcMemoryLocal() const
Definition: resource.cpp:76
std::map< std::string, ImageSet * > ImageSets
Definition: spritedef.h:153
SpriteDef()
Definition: spritedef.h:89
std::map< unsigned, ActionMap * > Actions
Definition: spritedef.h:157

References Resource::calcMemoryLocal(), FOR_EACH, mProcessedFiles, and SpriteDef().

◆ findNumber()

unsigned SpriteDef::findNumber ( const unsigned  num) const

Definition at line 76 of file spritedef.cpp.

77 {
78  unsigned min = 101;
79  FOR_EACH (Actions::const_iterator, it, mActions)
80  {
81  const unsigned n = (*it).first;
82  if (n >= num && n < min)
83  min = n;
84  }
85  if (min == 101)
86  return 0;
87  return min;
88 }

References FOR_EACH, and mActions.

◆ fixDeadAction()

void SpriteDef::fixDeadAction ( )
private

Fix bad timeout in last dead action frame

Definition at line 136 of file spritedef.cpp.

137 {
139  {
140  ActionMap *const d = (*it).second;
141  if (d == nullptr)
142  continue;
143  const ActionMap::iterator i = d->find(SpriteAction::DEAD);
144  const ActionMap::iterator i2 = d->find(SpriteAction::STAND);
145  // search dead action and check what it not same with stand action
146  if (i != d->end() &&
147  i->second != nullptr &&
148  (i2 == d->end() || i->second != i2->second))
149  {
150  (i->second)->setLastFrameDelay(0);
151  }
152  }
153 }
Actions::iterator ActionsIter
Definition: spritedef.h:159
static const std::string STAND("stand")
static const std::string DEAD("dead")

References SpriteAction::DEAD(), FOR_EACH, mActions, and SpriteAction::STAND().

Referenced by load().

◆ getAction()

const Action * SpriteDef::getAction ( const std::string &  action,
const unsigned  num 
) const

Returns the specified action.

Definition at line 52 of file spritedef.cpp.

54 {
55  Actions::const_iterator i = mActions.find(num);
56  if (i == mActions.end() && num != 100)
57  i = mActions.find(100);
58 
59  if (i == mActions.end() || ((*i).second == nullptr))
60  return nullptr;
61 
62  const ActionMap *const actMap = (*i).second;
63  if (actMap == nullptr)
64  return nullptr;
65  const ActionMap::const_iterator it = actMap->find(action);
66 
67  if (it == actMap->end())
68  {
69  logger->log("Warning: no action \"%s\" defined!", action.c_str());
70  return nullptr;
71  }
72 
73  return (*it).second;
74 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
Logger * logger
Definition: logger.cpp:89

References Logger::log(), logger, and mActions.

◆ getImageSet()

const ImageSet * SpriteDef::getImageSet ( const std::string &  imageSetName) const
private

Definition at line 269 of file spritedef.cpp.

270 {
271  const ImageSetCIterator si = mImageSets.find(imageSetName);
272  if (si == mImageSets.end())
273  {
274  reportAlways("%s: Imageset \"%s\" not defined in %s",
275  mSource.c_str(),
276  imageSetName.c_str(),
277  mIdPath.c_str())
278  return nullptr;
279  }
280  return si->second;
281 }
std::string mIdPath
Definition: resource.h:84
ImageSets::const_iterator ImageSetCIterator
Definition: spritedef.h:155

References Resource::mIdPath, mImageSets, Resource::mSource, and reportAlways.

Referenced by loadAction(), and loadAnimation().

◆ includeSprite()

void SpriteDef::includeSprite ( const xmlNodePtr  includeNode,
const int  variant 
)
private

Include another sprite into this one.

Definition at line 487 of file spritedef.cpp.

488 {
489  std::string filename = XML::getProperty(includeNode, "file", "");
490 
491  if (filename.empty())
492  return;
493  filename = pathJoin(paths.getStringValue("sprites"), filename);
494 
495  if (mProcessedFiles.find(filename) != mProcessedFiles.end())
496  {
497  reportAlways("%s: Tried to include %s which already is included.",
498  mSource.c_str(),
499  filename.c_str())
500  return;
501  }
502  mProcessedFiles.insert(filename);
503 
504  XML::Document *const doc = Loader::getXml(filename,
507  if (doc == nullptr)
508  return;
509  XmlNodeConstPtr rootNode = doc->rootNode();
510 
511  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "sprite"))
512  {
513  reportAlways("%s: No sprite root node in %s",
514  mSource.c_str(),
515  filename.c_str())
516  doc->decRef();
517  return;
518  }
519 
520  loadSprite(rootNode, variant, std::string());
521  doc->decRef();
522 }
std::string getStringValue(const std::string &key) const
virtual void decRef()
Definition: resource.cpp:50
void loadSprite(const xmlNodePtr spriteNode, const int variant, const std::string &palettes)
Definition: spritedef.cpp:205
Configuration paths
XML::Document * getXml(const std::string &idPath, const UseVirtFs useResman, const SkipError skipError)
Definition: xmlloader.cpp:56
Definition: libxml.cpp:86
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
const bool SkipError_false
Definition: skiperror.h:30
std::string pathJoin(std::string str1, const std::string &str2)
const bool UseVirtFs_true
Definition: usevirtfs.h:30

References Resource::decRef(), XML::getProperty(), Configuration::getStringValue(), Loader::getXml(), loadSprite(), mProcessedFiles, Resource::mSource, pathJoin(), paths, reportAlways, XML::Document::rootNode(), SkipError_false, and UseVirtFs_true.

Referenced by loadSprite().

◆ load()

SpriteDef * SpriteDef::load ( const std::string &  file,
const int  variant,
const bool  prot 
)
static

Loads a sprite definition file.

Definition at line 90 of file spritedef.cpp.

92 {
93  BLOCK_START("SpriteDef::load")
94  const size_t pos = animationFile.find('|');
95  std::string palettes;
96  if (pos != std::string::npos)
97  palettes = animationFile.substr(pos + 1);
98 
99  XML::Document *const doc = Loader::getXml(animationFile.substr(0, pos),
102  if (doc == nullptr)
103  return nullptr;
104  XmlNodePtrConst rootNode = doc->rootNode();
105 
106  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "sprite"))
107  {
108  reportAlways("Error, failed to parse sprite %s",
109  animationFile.c_str())
110  const std::string errorFile = pathJoin(paths.getStringValue("sprites"),
111  paths.getStringValue("spriteErrorFile"));
113  doc->decRef();
114  if (animationFile != errorFile)
115  return load(errorFile, 0, prot);
116  return nullptr;
117  }
118 
119  SpriteDef *const def = new SpriteDef;
120  def->mSource = animationFile;
121  def->mProcessedFiles.insert(animationFile);
122  def->loadSprite(rootNode, variant, palettes);
123  def->substituteActions();
124  if (settings.fixDeadAnimation)
125  def->fixDeadAction();
126  if (prot)
127  {
128  def->incRef();
129  def->mProtected = true;
130  }
131  doc->decRef();
132  BLOCK_END("SpriteDef::load")
133  return def;
134 }
void fixDeadAction()
Definition: spritedef.cpp:136
void substituteActions()
Definition: spritedef.cpp:172
static SpriteDef * load(const std::string &file, const int variant, const bool prot)
Definition: spritedef.cpp:90
#define new
Definition: debug_new.h:147
bool find(const std::string &key)
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
Settings settings
Definition: settings.cpp:32

References BLOCK_END, BLOCK_START, Resource::decRef(), fixDeadAction(), Settings::fixDeadAnimation, Configuration::getStringValue(), Loader::getXml(), Resource::incRef(), loadSprite(), mProcessedFiles, Resource::mProtected, Resource::mSource, pathJoin(), paths, reportAlways, XML::Document::rootNode(), settings, SkipError_false, SpriteDef(), substituteActions(), and UseVirtFs_true.

Referenced by anonymous_namespace{spritedefloader.cpp}::SpriteDefLoader::load().

◆ loadAction()

void SpriteDef::loadAction ( const xmlNodePtr  node,
const int  variant_offset 
)
private

Loads an action element.

Definition at line 283 of file spritedef.cpp.

285 {
286  if (node == nullptr)
287  return;
288 
289  const std::string actionName = XML::getProperty(node, "name", "");
290  const std::string imageSetName = XML::getProperty(node, "imageset", "");
291  const unsigned hp = XML::getProperty(node, "hp", 100);
292  const ImageSet *const imageSet = getImageSet(imageSetName);
293 
294  if (actionName == SpriteAction::INVALID)
295  {
296  reportAlways("%s: Unknown action \"%s\" defined in %s",
297  mSource.c_str(),
298  actionName.c_str(),
299  mIdPath.c_str())
300  return;
301  }
302  Action *const action = new Action(actionName);
303  action->setNumber(hp);
304  addAction(hp, actionName, action);
305 
306  // dirty hack to fix bad resources in tmw server
307  if (actionName == "attack_stab")
308  {
309  reportAlways("Found legacy attribute attack_stab in animation")
310  addAction(hp, "attack", action);
311  }
312 
313  // When first action, set it as default direction.
314  // i here always correct, because hp was added above.
315  const Actions::const_iterator i = mActions.find(hp);
316  if ((*i).second->size() == 1)
317  addAction(hp, SpriteAction::DEFAULT, action);
318 
319  // Load animations
320  for_each_xml_child_node(animationNode, node)
321  {
322  if (xmlNameEqual(animationNode, "animation"))
323  loadAnimation(animationNode, action, imageSet, variant_offset);
324  }
325 }
const ImageSet * getImageSet(const std::string &imageSetName) const
Definition: spritedef.cpp:269
void addAction(const unsigned hp, const std::string &name, Action *const action)
Definition: spritedef.cpp:576
void loadAnimation(const xmlNodePtr animationNode, Action *const action, const ImageSet *const imageSet, const int variant_offset) const
Definition: spritedef.cpp:327
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
bool attack(InputEvent &event)
Definition: actions.cpp:58
int size()
Definition: emotedb.cpp:306
static const std::string INVALID
Definition: spriteaction.h:72

References addAction(), SpriteAction::DEFAULT(), for_each_xml_child_node, getImageSet(), XML::getProperty(), SpriteAction::INVALID, loadAnimation(), mActions, Resource::mIdPath, Resource::mSource, reportAlways, and Action::setNumber().

Referenced by loadSprite().

◆ loadAnimation()

void SpriteDef::loadAnimation ( const xmlNodePtr  animationNode,
Action *const  action,
const ImageSet *const  imageSet,
const int  variant_offset 
) const
private

Loads an animation element.

Definition at line 327 of file spritedef.cpp.

331 {
332  if (action == nullptr ||
333  imageSet0 == nullptr ||
334  animationNode == nullptr)
335  {
336  return;
337  }
338 
339  const std::string directionName =
340  XML::getProperty(animationNode, "direction", "");
341  const SpriteDirection::Type directionType
342  = makeSpriteDirection(directionName);
343 
344  if (directionType == SpriteDirection::INVALID)
345  {
346  reportAlways("%s: Unknown direction \"%s\" used in %s",
347  mSource.c_str(),
348  directionName.c_str(),
349  mIdPath.c_str())
350  return;
351  }
352 
353  Animation *const animation = new Animation(directionName);
354  action->setAnimation(directionType, animation);
355 
356  // Get animation frames
357  for_each_xml_child_node(frameNode, animationNode)
358  {
359  const int delay = XML::getIntProperty(
360  frameNode, "delay", 0, 0, 100000);
361  const std::string imageSetName = XML::getProperty(frameNode,
362  "imageset",
363  "");
364  const ImageSet *imageSet = imageSet0;
365  if (!imageSetName.empty())
366  {
367  imageSet = getImageSet(imageSetName);
368  if (imageSet == nullptr)
369  imageSet = imageSet0;
370  }
371  const int offsetX = XML::getProperty(frameNode, "offsetX", 0)
372  + imageSet->getOffsetX() - imageSet->getWidth() / 2
373  + mapTileSize / 2;
374  const int offsetY = XML::getProperty(frameNode, "offsetY", 0)
375  + imageSet->getOffsetY() - imageSet->getHeight() + mapTileSize;
376  const int rand = XML::getIntProperty(frameNode, "rand", 100, 0, 100);
377 
378  if (xmlNameEqual(frameNode, "frame"))
379  {
380  const int index = XML::getProperty(frameNode, "index", -1);
381 
382  if (index < 0)
383  {
384  reportAlways(
385  "%s: No valid value for 'index' at direction '%s'",
386  mSource.c_str(),
387  directionName.c_str())
388  continue;
389  }
390 
391  Image *const img = imageSet->get(index + variant_offset);
392  if (img == nullptr)
393  {
394  reportAlways("%s: No image at index %d at direction '%s'",
395  mSource.c_str(),
396  index + variant_offset,
397  directionName.c_str())
398  continue;
399  }
400 
401  animation->addFrame(img, delay, offsetX, offsetY, rand);
402  }
403  else if (xmlNameEqual(frameNode, "sequence"))
404  {
405  const int start = XML::getProperty(frameNode, "start", -1);
406  const int end = XML::getProperty(frameNode, "end", -1);
407  const std::string value = XML::getProperty(frameNode, "value", "");
408  const int repeat = XML::getIntProperty(
409  frameNode, "repeat", 1, 0, 100);
410 
411  if (repeat < 1)
412  {
413  reportAlways("%s: No valid value for 'repeat' at direction %s",
414  mSource.c_str(),
415  directionName.c_str())
416  continue;
417  }
418 
419  if (value.empty())
420  {
421  if (addSequence(start, end, delay, offsetX, offsetY,
422  variant_offset, repeat, rand, imageSet, animation))
423  {
424  continue;
425  }
426  }
427  else
428  {
429  StringVect vals;
430  splitToStringVector(vals, value, ',');
431  FOR_EACH (StringVectCIter, it, vals)
432  {
433  const std::string str = *it;
434  const size_t idx = str.find('-');
435  if (str == "p")
436  {
437  animation->addPause(delay, rand);
438  }
439  else if (idx != std::string::npos)
440  {
441  const int v1 = atoi(str.substr(0, idx).c_str());
442  const int v2 = atoi(str.substr(idx + 1).c_str());
443  addSequence(v1, v2, delay, offsetX, offsetY,
444  variant_offset, repeat, rand, imageSet, animation);
445  }
446  else
447  {
448  Image *const img = imageSet->get(atoi(
449  str.c_str()) + variant_offset);
450  if (img != nullptr)
451  {
452  animation->addFrame(img, delay,
453  offsetX, offsetY, rand);
454  }
455  }
456  }
457  }
458  }
459  else if (xmlNameEqual(frameNode, "pause"))
460  {
461  animation->addPause(delay, rand);
462  }
463  else if (xmlNameEqual(frameNode, "end"))
464  {
465  animation->addTerminator(rand);
466  }
467  else if (xmlNameEqual(frameNode, "jump"))
468  {
469  animation->addJump(XML::getProperty(
470  frameNode, "action", ""), rand);
471  }
472  else if (xmlNameEqual(frameNode, "label"))
473  {
474  const std::string name = XML::getProperty(frameNode, "name", "");
475  if (!name.empty())
476  animation->addLabel(name);
477  }
478  else if (xmlNameEqual(frameNode, "goto"))
479  {
480  const std::string name = XML::getProperty(frameNode, "label", "");
481  if (!name.empty())
482  animation->addGoto(name, rand);
483  }
484  } // for frameNode
485 }
int getWidth() const
Definition: imageset.h:60
int getOffsetY() const
Definition: imageset.h:82
int getOffsetX() const
Definition: imageset.h:76
int getHeight() const
Definition: imageset.h:66
static SpriteDirection::Type makeSpriteDirection(const std::string &direction)
Definition: spritedef.cpp:551
bool addSequence(const int start, const int end, const int delay, const int offsetX, const int offsetY, const int variant_offset, int repeat, const int rand, const ImageSet *const imageSet, Animation *const animation) const
Definition: spritedef.cpp:586
static const int mapTileSize
Definition: map.h:27
AttributesT get(const std::string &key)
int getIntProperty(const xmlNodePtr node, const char *const name, int def, const int min, const int max)
Definition: libxml.cpp:190
std::string empty
Definition: podict.cpp:26
void splitToStringVector(StringVect &tokens, const std::string &text, const char separator)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29

References Animation::addFrame(), Animation::addGoto(), Animation::addJump(), Animation::addLabel(), Animation::addPause(), addSequence(), Animation::addTerminator(), ParticleType::Animation, FOR_EACH, for_each_xml_child_node, ImageSet::get(), ImageSet::getHeight(), getImageSet(), XML::getIntProperty(), ImageSet::getOffsetX(), ImageSet::getOffsetY(), XML::getProperty(), ImageSet::getWidth(), SpriteDirection::INVALID, makeSpriteDirection(), mapTileSize, Resource::mIdPath, Resource::mSource, reportAlways, Action::setAnimation(), splitToStringVector(), and anonymous_namespace{stringutils.cpp}::start.

Referenced by loadAction().

◆ loadImageSet()

void SpriteDef::loadImageSet ( const xmlNodePtr  node,
const std::string &  palettes 
)
private

Loads an imageset element.

Definition at line 238 of file spritedef.cpp.

240 {
241  const std::string name = XML::getProperty(node, "name", "");
242 
243  // We don't allow redefining image sets. This way, an included sprite
244  // definition will use the already loaded image set with the same name.
245  if (mImageSets.find(name) != mImageSets.end())
246  return;
247 
248  const int width = XML::getProperty(node, "width", 0);
249  const int height = XML::getProperty(node, "height", 0);
250  std::string imageSrc = XML::getProperty(node, "src", "");
251  Dye::instantiate(imageSrc, palettes);
252 
253  ImageSet *const imageSet = Loader::getImageSet(imageSrc,
254  width, height);
255 
256  if (imageSet == nullptr)
257  {
258  reportAlways("%s: Couldn't load imageset: %s",
259  mSource.c_str(),
260  imageSrc.c_str())
261  return;
262  }
263 
264  imageSet->setOffsetX(XML::getProperty(node, "offsetX", 0));
265  imageSet->setOffsetY(XML::getProperty(node, "offsetY", 0));
266  mImageSets[name] = imageSet;
267 }
static void instantiate(std::string &target, const std::string &palettes)
Definition: dye.cpp:97
ImageSet * getImageSet(const std::string &imagePath, const int w, const int h)

References Loader::getImageSet(), XML::getProperty(), Dye::instantiate(), mImageSets, Resource::mSource, reportAlways, ImageSet::setOffsetX(), and ImageSet::setOffsetY().

Referenced by loadSprite().

◆ loadSprite()

void SpriteDef::loadSprite ( const xmlNodePtr  spriteNode,
const int  variant,
const std::string &  palettes 
)
private

Loads a sprite element.

Definition at line 205 of file spritedef.cpp.

208 {
209  BLOCK_START("SpriteDef::loadSprite")
210  if (spriteNode == nullptr)
211  {
212  BLOCK_END("SpriteDef::loadSprite")
213  return;
214  }
215  // Get the variant
216  const int variantCount = XML::getProperty(spriteNode, "variants", 0);
217  int variant_offset = 0;
218 
219  if (variantCount > 0 && variant < variantCount)
220  {
221  variant_offset = variant * XML::getProperty(spriteNode,
222  "variant_offset",
223  0);
224  }
225 
226  for_each_xml_child_node(node, spriteNode)
227  {
228  if (xmlNameEqual(node, "imageset"))
229  loadImageSet(node, palettes);
230  else if (xmlNameEqual(node, "action"))
231  loadAction(node, variant_offset);
232  else if (xmlNameEqual(node, "include"))
233  includeSprite(node, variant);
234  }
235  BLOCK_END("SpriteDef::loadSprite")
236 }
void includeSprite(const xmlNodePtr includeNode, const int variant)
Definition: spritedef.cpp:487
void loadImageSet(const xmlNodePtr node, const std::string &palettes)
Definition: spritedef.cpp:238
void loadAction(const xmlNodePtr node, const int variant_offset)
Definition: spritedef.cpp:283

References BLOCK_END, BLOCK_START, for_each_xml_child_node, XML::getProperty(), includeSprite(), loadAction(), and loadImageSet().

Referenced by includeSprite(), and load().

◆ makeSpriteDirection()

SpriteDirection::Type SpriteDef::makeSpriteDirection ( const std::string &  direction)
static

Converts a string into a SpriteDirection enum.

Definition at line 551 of file spritedef.cpp.

553 {
554  if (direction.empty() || direction == "default")
556  else if (direction == "up")
557  return SpriteDirection::UP;
558  else if (direction == "left")
559  return SpriteDirection::LEFT;
560  else if (direction == "right")
561  return SpriteDirection::RIGHT;
562  else if (direction == "down")
563  return SpriteDirection::DOWN;
564  else if (direction == "upleft")
566  else if (direction == "upright")
568  else if (direction == "downleft")
570  else if (direction == "downright")
572  else
574 }

References SpriteDirection::DEFAULT, SpriteDirection::DOWN, SpriteDirection::DOWNLEFT, SpriteDirection::DOWNRIGHT, SpriteDirection::INVALID, SpriteDirection::LEFT, SpriteDirection::RIGHT, SpriteDirection::UP, SpriteDirection::UPLEFT, and SpriteDirection::UPRIGHT.

Referenced by loadAnimation().

◆ substituteAction()

void SpriteDef::substituteAction ( const std::string &  complete,
const std::string &  with 
)
private

When there are no animations defined for the action "complete", its animations become a copy of those of the action "with".

Definition at line 155 of file spritedef.cpp.

157 {
159  {
160  ActionMap *const d = (*it).second;
161  if (reportTrue(d == nullptr))
162  continue;
163  if (d->find(complete) == d->end())
164  {
165  const ActionMap::iterator i = d->find(with);
166  if (i != d->end())
167  (*d)[complete] = i->second;
168  }
169  }
170 }
#define reportTrue(val)
Definition: checkutils.h:252
Actions::const_iterator ActionsConstIter
Definition: spritedef.h:158

References FOR_EACH, mActions, and reportTrue.

Referenced by substituteActions().

◆ substituteActions()

void SpriteDef::substituteActions ( )
private

Complete missing actions by copying existing ones.

Definition at line 172 of file spritedef.cpp.

173 {
203 }
void substituteAction(const std::string &complete, const std::string &with)
Definition: spritedef.cpp:155
static const std::string ATTACKWATER("attackwater")
static const std::string STANDWATER("standwater")
static const std::string SPAWNRIDE("spawnride")
static const std::string ATTACKSKY("attacksky")
static const std::string STANDRIDE("standride")
static const std::string RIDE("ride")
static const std::string SIT("sit")
static const std::string SWIM("swim")
static const std::string FLY("fly")
static const std::string CASTSKY("castsky")
static const std::string DEFAULT("stand")
static const std::string SITRIDE("sitride")
static const std::string SITSKY("sitsky")
static const std::string MOVE("walk")
static const std::string CASTWATER("castwater")
static const std::string SPAWNWATER("spawnwater")
static const std::string DEADRIDE("deadride")
static const std::string SITWATER("sitwater")
static const std::string STANDSKY("standsky")
static const std::string DEADSKY("deadsky")
static const std::string CASTRIDE("castride")
static const std::string CAST("cast")
static const std::string DEADWATER("deadwater")
static const std::string ATTACKRIDE("attackride")
static const std::string SITTOP("sittop")
static const std::string SPAWN("spawn")
static const std::string ATTACK("attack")
static const std::string SPAWNSKY("spawnsky")

References SpriteAction::ATTACK(), SpriteAction::ATTACKRIDE(), SpriteAction::ATTACKSKY(), SpriteAction::ATTACKWATER(), SpriteAction::CAST(), SpriteAction::CASTRIDE(), SpriteAction::CASTSKY(), SpriteAction::CASTWATER(), SpriteAction::DEAD(), SpriteAction::DEADRIDE(), SpriteAction::DEADSKY(), SpriteAction::DEADWATER(), SpriteAction::DEFAULT(), SpriteAction::FLY(), SpriteAction::MOVE(), SpriteAction::RIDE(), SpriteAction::SIT(), SpriteAction::SITRIDE(), SpriteAction::SITSKY(), SpriteAction::SITTOP(), SpriteAction::SITWATER(), SpriteAction::SPAWN(), SpriteAction::SPAWNRIDE(), SpriteAction::SPAWNSKY(), SpriteAction::SPAWNWATER(), SpriteAction::STAND(), SpriteAction::STANDRIDE(), SpriteAction::STANDSKY(), SpriteAction::STANDWATER(), substituteAction(), and SpriteAction::SWIM().

Referenced by load().

Field Documentation

◆ mActions

Actions SpriteDef::mActions
private

◆ mImageSets

ImageSets SpriteDef::mImageSets
private

Definition at line 162 of file spritedef.h.

Referenced by calcMemoryChilds(), getImageSet(), loadImageSet(), and ~SpriteDef().

◆ mProcessedFiles

std::set<std::string> SpriteDef::mProcessedFiles
private

Definition at line 164 of file spritedef.h.

Referenced by calcMemoryLocal(), includeSprite(), and load().


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