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