1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* The ManaPlus Client
* Copyright (C) 2011-2015 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "resources/map/speciallayer.h"
#include "enums/resources/map/mapitemtype.h"
#include "resources/map/mapconsts.h"
#include "resources/map/mapitem.h"
#include "utils/delete2.h"
#include "debug.h"
SpecialLayer::SpecialLayer(const int width, const int height) :
mWidth(width),
mHeight(height),
mTiles(new MapItem*[mWidth * mHeight])
{
std::fill_n(mTiles, mWidth * mHeight, static_cast<MapItem*>(nullptr));
}
SpecialLayer::~SpecialLayer()
{
for (int f = 0; f < mWidth * mHeight; f ++)
delete2(mTiles[f])
delete [] mTiles;
}
MapItem* SpecialLayer::getTile(const int x, const int y) const
{
if (x < 0 || x >= mWidth ||
y < 0 || y >= mHeight)
{
return nullptr;
}
return mTiles[x + y * mWidth];
}
void SpecialLayer::setTile(const int x, const int y, MapItem *const item)
{
if (x < 0 || x >= mWidth ||
y < 0 || y >= mHeight)
{
return;
}
const int idx = x + y * mWidth;
delete mTiles[idx];
if (item)
item->setPos(x, y);
mTiles[idx] = item;
}
void SpecialLayer::setTile(const int x, const int y, const int type)
{
if (x < 0 || x >= mWidth ||
y < 0 || y >= mHeight)
{
return;
}
const int idx = x + y * mWidth;
MapItem *const tile = mTiles[idx];
if (tile)
{
tile->setType(type);
tile->setPos(x, y);
}
else
{
mTiles[idx] = new MapItem(type);
mTiles[idx]->setPos(x, y);
}
}
void SpecialLayer::addRoad(const Path &road)
{
FOR_EACH (Path::const_iterator, i, road)
{
const Position &pos = (*i);
MapItem *const item = getTile(pos.x, pos.y);
if (!item)
setTile(pos.x, pos.y, new MapItem(MapItemType::ROAD));
else
item->setType(MapItemType::ROAD);
}
}
void SpecialLayer::clean() const
{
if (!mTiles)
return;
for (int f = 0; f < mWidth * mHeight; f ++)
{
MapItem *const item = mTiles[f];
if (item)
item->setType(MapItemType::EMPTY);
}
}
void SpecialLayer::draw(Graphics *const graphics, int startX, int startY,
int endX, int endY,
const int scrollX, const int scrollY) const
{
BLOCK_START("SpecialLayer::draw")
if (startX < 0)
startX = 0;
if (startY < 0)
startY = 0;
if (endX > mWidth)
endX = mWidth;
if (endY > mHeight)
endY = mHeight;
for (int y = startY; y < endY; y ++)
{
const int py = y * mapTileSize - scrollY;
const int y2 = y * mWidth;
for (int x = startX; x < endX; x ++)
{
const MapItem *const item = mTiles[x + y2];
if (item)
{
item->draw(graphics, x * mapTileSize - scrollX, py,
mapTileSize, mapTileSize);
}
}
}
BLOCK_END("SpecialLayer::draw")
}
|