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

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

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

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