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

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