ManaPlus
action.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "resources/action.h"
24 
26 
27 #include "utils/dtor.h"
28 #include "utils/foreach.h"
29 
30 #include "debug.h"
31 
32 Action::Action(const std::string &name) noexcept2 :
33  MemoryCounter(),
34  mAnimations(),
35  mCounterName(name),
36  mNumber(100)
37 {
38 }
39 
41 {
43 }
44 
46  const noexcept2
47 {
48  Animations::const_iterator i = mAnimations.find(direction);
49 
50  if (i == mAnimations.end())
51  {
52  if (direction == SpriteDirection::UPLEFT
53  || direction == SpriteDirection::UPRIGHT)
54  {
55  direction = SpriteDirection::UP;
56  }
57  else if (direction == SpriteDirection::DOWNLEFT
58  || direction == SpriteDirection::DOWNRIGHT)
59  {
60  direction = SpriteDirection::DOWN;
61  }
62  i = mAnimations.find(direction);
63 
64  // When the given direction is not available, return the first one.
65  // (either DEFAULT, or more usually DOWN).
66  if (i == mAnimations.end())
67  i = mAnimations.begin();
68  }
69 
70  return (i == mAnimations.end()) ? nullptr : i->second;
71 }
72 
74  Animation *const animation) noexcept2
75 {
76  mAnimations[direction] = animation;
77 }
78 
79 void Action::setLastFrameDelay(const int delay) noexcept2
80 {
81  FOR_EACH (AnimationIter, it, mAnimations)
82  {
83  Animation *const animation = (*it).second;
84  if (animation == nullptr)
85  continue;
86  animation->setLastFrameDelay(delay);
87  }
88 }
89 
91 {
92  return sizeof(Action);
93 }
94 
95 int Action::calcMemoryChilds(const int level) const
96 {
97  int sz = 0;
99  {
100  sz += sizeof(SpriteDirection::Type);
101  const Animation *const animation = (*it).second;
102  sz += animation->calcMemory(level + 1);
103  }
104  return sz;
105 }
animation.h
SpriteDirection::DOWN
@ DOWN
Definition: spritedirection.h:32
Action::mAnimations
Animations mAnimations
Definition: action.h:74
Animation::setLastFrameDelay
void setLastFrameDelay(const int delay)
Definition: animation.cpp:84
SpriteDirection::UPLEFT
@ UPLEFT
Definition: spritedirection.h:35
MemoryCounter
Definition: memorycounter.h:28
Action::~Action
~Action()
Definition: action.cpp:40
SpriteDirection::Type
Type
Definition: spritedirection.h:28
Action::setAnimation
void setAnimation(const SpriteDirection::Type direction, Animation *const animation)
Definition: action.cpp:73
Action::AnimationIter
Animations::iterator AnimationIter
Definition: action.h:71
SpriteDirection::DOWNLEFT
@ DOWNLEFT
Definition: spritedirection.h:37
action.h
noexcept2
#define noexcept2
Definition: localconsts.h:49
Action::getAnimation
const Animation * getAnimation(SpriteDirection::Type direction) const
Definition: action.cpp:45
Action::setLastFrameDelay
void setLastFrameDelay(const int delay)
Definition: action.cpp:79
nullptr
#define nullptr
Definition: localconsts.h:44
SpriteDirection::DOWNRIGHT
@ DOWNRIGHT
Definition: spritedirection.h:38
MemoryCounter::calcMemory
int calcMemory(const int level) const
Definition: memorycounter.cpp:36
SpriteDirection::UP
@ UP
Definition: spritedirection.h:31
Action::AnimationCIter
Animations::const_iterator AnimationCIter
Definition: action.h:72
Animation
Definition: animation.h:40
Action::calcMemoryLocal
int calcMemoryLocal() const
Definition: action.cpp:90
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
foreach.h
SpriteDirection::UPRIGHT
@ UPRIGHT
Definition: spritedirection.h:36
Action::calcMemoryChilds
int calcMemoryChilds(const int level) const
Definition: action.cpp:95
dtor.h
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
debug.h
Action::Action
Action(const std::string &name)
Definition: action.cpp:32