GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/map/mapitem.cpp Lines: 32 63 50.8 %
Date: 2017-11-29 Branches: 14 34 41.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/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
20158
MapItem::MapItem(const int type) :
50
    mImage(nullptr),
51
    mComment(),
52
    mName(),
53
    mType(type),
54
    mX(-1),
55
60474
    mY(-1)
56
{
57
20158
    setType(type);
58
20158
}
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
80632
MapItem::~MapItem()
87
{
88
20158
    if (mImage != nullptr)
89
    {
90
20132
        mImage->decRef();
91
20132
        mImage = nullptr;
92
    }
93
20158
}
94
95
20158
void MapItem::setType(const int type)
96
{
97
40316
    std::string name;
98
20158
    mType = type;
99
20158
    if (mImage != nullptr)
100
        mImage->decRef();
101
102

20158
    switch (type)
103
    {
104
20096
        case MapItemType::ARROW_UP:
105
            name = "graphics/sprites/arrow_up.png";
106
            break;
107
26
        case MapItemType::ARROW_DOWN:
108
            name = "graphics/sprites/arrow_down.png";
109
            break;
110
10
        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
20158
    if (!name.empty())
121
20132
        mImage = Loader::getImage(name);
122
    else
123
26
        mImage = nullptr;
124
20158
}
125
126
20158
void MapItem::setPos(const int x, const int y)
127
{
128
20158
    mX = x;
129
20158
    mY = y;
130
20158
}
131
132
88
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
88
    if (mImage != nullptr)
137
88
        graphics->drawImage(mImage, x, y);
138
139
88
    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
176
    if (!mName.empty()
162
        && mType != MapItemType::PORTAL
163

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