GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/action.cpp Lines: 20 36 55.6 %
Date: 2017-11-29 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-2017  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
2318
Action::Action(const std::string &name) noexcept2 :
33
    MemoryCounter(),
34
    mAnimations(),
35
    mCounterName(name),
36
6954
    mNumber(100)
37
{
38
2318
}
39
40
11590
Action::~Action()
41
{
42
4636
    delete_all(mAnimations);
43
4636
}
44
45
2316
const Animation *Action::getAnimation(SpriteDirection::Type direction)
46
                                      const noexcept2
47
{
48
4632
    Animations::const_iterator i = mAnimations.find(direction);
49
50
4632
    if (i == mAnimations.end())
51
    {
52
2316
        if (direction == SpriteDirection::UPLEFT
53
2316
            || direction == SpriteDirection::UPRIGHT)
54
        {
55
            direction = SpriteDirection::UP;
56
        }
57
2316
        else if (direction == SpriteDirection::DOWNLEFT
58
2316
                 || direction == SpriteDirection::DOWNRIGHT)
59
        {
60
            direction = SpriteDirection::DOWN;
61
        }
62
4632
        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
4632
        if (i == mAnimations.end())
67
4632
            i = mAnimations.begin();
68
    }
69
70
6948
    return (i == mAnimations.end()) ? nullptr : i->second;
71
}
72
73
2318
void Action::setAnimation(const SpriteDirection::Type direction,
74
                          Animation *const animation) noexcept2
75
{
76
2318
    mAnimations[direction] = animation;
77
2318
}
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
}