GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/particle/particlevector.cpp Lines: 11 44 25.0 %
Date: 2021-03-17 Branches: 1 34 2.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-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 "particle/particle.h"
25
#include "particle/particlevector.h"
26
27
#include "debug.h"
28
29
103
ParticleVector::ParticleVector(ParticleContainer *const parent,
30
103
                               const bool delParent) :
31
    ParticleContainer(parent, delParent),
32
206
    mIndexedElements()
33
103
{}
34
35
206
ParticleVector::~ParticleVector()
36
103
{}
37
38
void ParticleVector::setLocally(const int index, Particle *const particle)
39
{
40
    if (index < 0)
41
        return;
42
43
    delLocally(index);
44
45
    if (mIndexedElements.size() <= CAST_SIZE(index))
46
        mIndexedElements.resize(index + 1, nullptr);
47
48
    if (particle != nullptr)
49
        particle->disableAutoDelete();
50
    mIndexedElements[index] = particle;
51
}
52
53
void ParticleVector::delLocally(const int index)
54
{
55
    if (index < 0)
56
        return;
57
58
    if (mIndexedElements.size() <= CAST_SIZE(index))
59
        return;
60
61
    Particle *const p = mIndexedElements[index];
62
    if (p != nullptr)
63
    {
64
        mIndexedElements[index] = nullptr;
65
        p->kill();
66
    }
67
}
68
69
206
void ParticleVector::clearLocally()
70
{
71
412
    for (unsigned int i = 0;
72
412
         i < CAST_U32(mIndexedElements.size());
73
         i++)
74
    {
75
        delLocally(i);
76
    }
77
206
}
78
79
void ParticleVector::moveTo(const float x, const float y)
80
{
81
    ParticleContainer::moveTo(x, y);
82
83
    for (STD_VECTOR<Particle *>::iterator it = mIndexedElements.begin();
84
         it != mIndexedElements.end(); ++it)
85
    {
86
        Particle *const p = *it;
87
        if (p != nullptr)
88
        {
89
            p->moveTo(x, y);
90
91
            if (p->isExtinct())
92
            {
93
                p->kill();
94
                *it = nullptr;
95
            }
96
        }
97
    }
98
}
99
100
size_t ParticleVector::usedSize() const
101
{
102
    size_t cnt = 0;
103
    for (STD_VECTOR<Particle *>::const_iterator it = mIndexedElements.begin();
104
         it != mIndexedElements.end(); ++it)
105
    {
106
        if (*it != nullptr)
107
            cnt ++;
108
    }
109
    return cnt;
110
2
}