GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/map/objectslayer.cpp Lines: 12 31 38.7 %
Date: 2021-03-17 Branches: 4 22 18.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "resources/map/objectslayer.h"
23
24
#include "utils/cast.h"
25
26
#include "resources/map/mapobjectlist.h"
27
28
#include "debug.h"
29
30
47
ObjectsLayer::ObjectsLayer(const unsigned width,
31
47
                           const unsigned height) :
32
    MemoryCounter(),
33
47
    mTiles(new MapObjectList*[width * height]),
34
    mWidth(width),
35
94
    mHeight(height)
36
{
37
94
    std::fill_n(mTiles, width * height, static_cast<MapObjectList*>(nullptr));
38
47
}
39
40
141
ObjectsLayer::~ObjectsLayer()
41
{
42
47
    const unsigned size = mWidth * mHeight;
43
160220
    for (unsigned f = 0; f < size; f ++)
44
160173
        delete mTiles[f];
45
46
47
    delete [] mTiles;
47
    mTiles = nullptr;
48
94
}
49
50
void ObjectsLayer::addObject(const std::string &name, const int type,
51
                             const unsigned x, const unsigned y,
52
                             unsigned dx, unsigned dy)
53
{
54
    if (mTiles == nullptr)
55
        return;
56
57
    if (x + dx > mWidth)
58
        dx = mWidth - x;
59
    if (y + dy > mHeight)
60
        dy = mHeight - y;
61
62
    for (unsigned y1 = y; y1 < y + dy; y1 ++)
63
    {
64
        const unsigned idx1 = x + y1 * mWidth;
65
        const unsigned idx2 = idx1 + dx;
66
67
        for (unsigned i = idx1; i < idx2; i ++)
68
        {
69
            if (mTiles[i] == nullptr)
70
                mTiles[i] = new MapObjectList;
71
            mTiles[i]->objects.push_back(MapObject(type, name));
72
        }
73
    }
74
}
75
76
MapObjectList *ObjectsLayer::getAt(const unsigned x, const unsigned y) const
77
{
78
    if (x >= mWidth || y >= mHeight)
79
        return nullptr;
80
    return mTiles[x + y * mWidth];
81
}
82
83
int ObjectsLayer::calcMemoryLocal() const
84
{
85
    return CAST_S32(sizeof(ObjectsLayer) +
86
        (sizeof(MapObjectList) + sizeof(MapObjectList*)) * mWidth * mHeight);
87
}