GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/action.cpp Lines: 20 35 57.1 %
Date: 2018-09-20 Branches: 5 16 31.3 %

Line Branch Exec Source
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-2018  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
25
#include "resources/animation/animation.h"
26
27
#include "utils/dtor.h"
28
#include "utils/foreach.h"
29
30
#include "debug.h"
31
32
1297
Action::Action(const std::string &name) noexcept2 :
33
    MemoryCounter(),
34
    mAnimations(),
35
    mCounterName(name),
36
3891
    mNumber(100)
37
{
38
1297
}
39
40
6485
Action::~Action()
41
{
42
2594
    delete_all(mAnimations);
43
2594
}
44
45
1296
const Animation *Action::getAnimation(SpriteDirection::Type direction)
46
                                      const noexcept2
47
{
48
2592
    Animations::const_iterator i = mAnimations.find(direction);
49
50
2592
    if (i == mAnimations.end())
51
    {
52
1296
        if (direction == SpriteDirection::UPLEFT
53
1296
            || direction == SpriteDirection::UPRIGHT)
54
        {
55
            direction = SpriteDirection::UP;
56
        }
57
1296
        else if (direction == SpriteDirection::DOWNLEFT
58
1296
                 || direction == SpriteDirection::DOWNRIGHT)
59
        {
60
            direction = SpriteDirection::DOWN;
61
        }
62
2592
        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
2592
        if (i == mAnimations.end())
67
2592
            i = mAnimations.begin();
68
    }
69
70
3888
    return (i == mAnimations.end()) ? nullptr : i->second;
71
}
72
73
1297
void Action::setAnimation(const SpriteDirection::Type direction,
74
                          Animation *const animation) noexcept2
75
{
76
1297
    mAnimations[direction] = animation;
77
1297
}
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
90
int Action::calcMemoryLocal() const
91
{
92
    return sizeof(Action);
93
}
94
95
int Action::calcMemoryChilds(const int level) const
96
{
97
    int sz = 0;
98
    FOR_EACH (AnimationCIter, it, mAnimations)
99
    {
100
        sz += sizeof(SpriteDirection::Type);
101
        const Animation *const animation = (*it).second;
102
        sz += animation->calcMemory(level + 1);
103
    }
104
    return sz;
105
}