GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/map/mapitem.cpp Lines: 29 58 50.0 %
Date: 2021-03-17 Branches: 15 36 41.7 %

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/mapitem.h"
23
24
#include "enums/resources/map/mapitemtype.h"
25
26
#include "gui/gui.h"
27
#include "gui/userpalette.h"
28
29
#include "gui/fonts/font.h"
30
31
#include "resources/image/image.h"
32
33
#include "resources/loaders/imageloader.h"
34
35
#include "render/graphics.h"
36
37
#include "debug.h"
38
39
MapItem::MapItem() :
40
    mImage(nullptr),
41
    mComment(),
42
    mName(),
43
    mType(MapItemType::EMPTY),
44
    mX(-1),
45
    mY(-1)
46
{
47
    setType(MapItemType::EMPTY);
48
}
49
50
10079
MapItem::MapItem(const int type) :
51
    mImage(nullptr),
52
    mComment(),
53
    mName(),
54
    mType(type),
55
    mX(-1),
56
30237
    mY(-1)
57
{
58
10079
    setType(type);
59
10079
}
60
61
MapItem::MapItem(const int type,
62
                 const std::string &comment) :
63
    mImage(nullptr),
64
    mComment(comment),
65
    mName(),
66
    mType(type),
67
    mX(-1),
68
    mY(-1)
69
{
70
    setType(type);
71
}
72
73
MapItem::MapItem(const int type,
74
                 const std::string &comment,
75
                 const int x,
76
                 const int y) :
77
    mImage(nullptr),
78
    mComment(comment),
79
    mName(),
80
    mType(type),
81
    mX(x),
82
    mY(y)
83
{
84
    setType(type);
85
}
86
87
40316
MapItem::~MapItem()
88
{
89
10079
    if (mImage != nullptr)
90
    {
91
10066
        mImage->decRef();
92
10066
        mImage = nullptr;
93
    }
94
10079
}
95
96
10079
void MapItem::setType(const int type)
97
{
98
20158
    std::string name;
99
10079
    mType = type;
100
10079
    if (mImage != nullptr)
101
        mImage->decRef();
102
103

10079
    switch (type)
104
    {
105
        case MapItemType::ARROW_UP:
106
            name = "graphics/sprites/arrow_up.png";
107
            break;
108
        case MapItemType::ARROW_DOWN:
109
            name = "graphics/sprites/arrow_down.png";
110
            break;
111
        case MapItemType::ARROW_LEFT:
112
            name = "graphics/sprites/arrow_left.png";
113
            break;
114
        case MapItemType::ARROW_RIGHT:
115
            name = "graphics/sprites/arrow_right.png";
116
            break;
117
        default:
118
            break;
119
    }
120
121
10079
    if (!name.empty())
122
10066
        mImage = Loader::getImage(name);
123
    else
124
13
        mImage = nullptr;
125
10079
}
126
127
10079
void MapItem::setPos(const int x, const int y)
128
{
129
10079
    mX = x;
130
10079
    mY = y;
131
10079
}
132
133
44
void MapItem::draw(Graphics *const graphics, const int x, const int y,
134
                   const int dx, const int dy) const
135
{
136
    BLOCK_START("MapItem::draw")
137
44
    if (mImage != nullptr)
138
44
        graphics->drawImage(mImage, x, y);
139
140
44
    switch (mType)
141
    {
142
        case MapItemType::ROAD:
143
        case MapItemType::CROSS:
144
            graphics->setColor(userPalette->getColorWithAlpha(
145
                               UserColorId::ROAD_POINT));
146
            graphics->fillRectangle(Rect(x + dx / 3, y + dy / 3,
147
                                    dx / 3, dy / 3));
148
            break;
149
        case MapItemType::HOME:
150
        {
151
            graphics->setColor(userPalette->getColorWithAlpha(
152
                               UserColorId::HOME_PLACE));
153
            graphics->fillRectangle(Rect(x, y, dx, dy));
154
            graphics->setColor(userPalette->getColorWithAlpha(
155
                               UserColorId::HOME_PLACE_BORDER));
156
            graphics->drawRectangle(Rect(x, y, dx, dy));
157
            break;
158
        }
159
        default:
160
            break;
161
    }
162
132
    if (!mName.empty()
163
        && mType != MapItemType::PORTAL
164

44
        && mType != MapItemType::EMPTY)
165
    {
166
        Font *const font = gui->getFont();
167
        const Color &color = userPalette->getColor(UserColorId::BEING, 255U);
168
        font->drawString(graphics,
169
            color,
170
            color,
171
            mName,
172
            x, y);
173
    }
174
    BLOCK_END("MapItem::draw")
175
44
}