ManaPlus
Public Member Functions | Private Attributes | Friends
SpecialLayer Class Reference

#include <speciallayer.h>

Inheritance diagram for SpecialLayer:
MemoryCounter

Public Member Functions

 SpecialLayer (const std::string &name, const int width, const int height)
 
 ~SpecialLayer ()
 
void draw (Graphics *const graphics, int startX, int startY, int endX, int endY, const int scrollX, const int scrollY) const
 
MapItemgetTile (const int x, const int y) const
 
void setTile (const int x, const int y, MapItem *const item)
 
void setTile (const int x, const int y, const int type)
 
void addRoad (const Path &road)
 
void clean ()
 
int calcMemoryLocal () const
 
std::string getCounterName () const
 
void updateCache ()
 
- Public Member Functions inherited from MemoryCounter
 MemoryCounter ()
 
virtual ~MemoryCounter ()
 
int calcMemory (const int level) const
 
virtual int calcMemoryChilds (const int level) const
 

Private Attributes

const std::string mName
 
MapItem ** mTiles
 
int * mCache
 
int mWidth
 
int mHeight
 

Friends

class Map
 
class MapLayer
 

Detailed Description

Definition at line 33 of file speciallayer.h.

Constructor & Destructor Documentation

◆ SpecialLayer()

SpecialLayer::SpecialLayer ( const std::string &  name,
const int  width,
const int  height 
)

Definition at line 34 of file speciallayer.cpp.

36  :
37  MemoryCounter(),
38  mName(name),
39  mTiles(new MapItem*[width * height]),
40  mCache(new int[width * height]),
41  mWidth(width),
42  mHeight(height)
43 {
44  std::fill_n(mTiles, mWidth * mHeight, static_cast<MapItem*>(nullptr));
45  std::fill_n(mCache, mWidth * mHeight, 10000);
46 }

References mCache, mHeight, mTiles, and mWidth.

Referenced by calcMemoryLocal().

◆ ~SpecialLayer()

SpecialLayer::~SpecialLayer ( )

Definition at line 48 of file speciallayer.cpp.

49 {
50  for (int f = 0; f < mWidth * mHeight; f ++)
51  delete2(mTiles[f])
52  delete [] mTiles;
53  delete [] mCache;
54 }

References delete2, mCache, mHeight, mTiles, and mWidth.

Member Function Documentation

◆ addRoad()

void SpecialLayer::addRoad ( const Path road)

Definition at line 103 of file speciallayer.cpp.

104 {
105  FOR_EACH (Path::const_iterator, i, road)
106  {
107  const Position &pos = *i;
108  MapItem *const item = getTile(pos.x, pos.y);
109  if (item == nullptr)
110  setTile(pos.x, pos.y, new MapItem(MapItemType::ROAD));
111  else
112  item->setType(MapItemType::ROAD);
113  }
114  updateCache();
115 }

References FOR_EACH, getTile(), MapItemType::ROAD, setTile(), MapItem::setType(), updateCache(), Position::x, and Position::y.

Referenced by LocalPlayer::navigateTo(), and LocalPlayer::updateCoords().

◆ calcMemoryLocal()

int SpecialLayer::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Definition at line 162 of file speciallayer.cpp.

163 {
164  return static_cast<int>(sizeof(SpecialLayer) +
165  sizeof(MapItem) * mWidth * mHeight);
166 }

References mHeight, mWidth, and SpecialLayer().

◆ clean()

void SpecialLayer::clean ( )

Definition at line 117 of file speciallayer.cpp.

118 {
119  if (mTiles == nullptr)
120  return;
121 
122  for (int f = 0; f < mWidth * mHeight; f ++)
123  {
124  MapItem *const item = mTiles[f];
125  if (item != nullptr)
127  }
128  updateCache();
129 }

References MapItemType::EMPTY, mHeight, mTiles, mWidth, MapItem::setType(), and updateCache().

Referenced by LocalPlayer::navigateClean(), and LocalPlayer::updateCoords().

◆ draw()

void SpecialLayer::draw ( Graphics *const  graphics,
int  startX,
int  startY,
int  endX,
int  endY,
const int  scrollX,
const int  scrollY 
) const

Definition at line 131 of file speciallayer.cpp.

134 {
135  BLOCK_START("SpecialLayer::draw")
136  if (startX < 0)
137  startX = 0;
138  if (startY < 0)
139  startY = 0;
140  if (endX > mWidth)
141  endX = mWidth;
142  if (endY > mHeight)
143  endY = mHeight;
144 
145  for (int y = startY; y < endY; y ++)
146  {
147  const int py = y * mapTileSize - scrollY;
148  const int y2 = y * mWidth;
149  for (int x = startX; x < endX; x ++)
150  {
151  const MapItem *const item = mTiles[x + y2];
152  if (item != nullptr)
153  {
154  item->draw(graphics, x * mapTileSize - scrollX, py,
156  }
157  }
158  }
159  BLOCK_END("SpecialLayer::draw")
160 }

References BLOCK_END, BLOCK_START, MapItem::draw(), mapTileSize, mHeight, mTiles, mWidth, x, and y.

◆ getCounterName()

std::string SpecialLayer::getCounterName ( ) const
inlinevirtual

Reimplemented from MemoryCounter.

Definition at line 64 of file speciallayer.h.

65  { return mName; }

References mName.

◆ getTile()

MapItem * SpecialLayer::getTile ( const int  x,
const int  y 
) const

Definition at line 56 of file speciallayer.cpp.

57 {
58  if (x < 0 || x >= mWidth ||
59  y < 0 || y >= mHeight)
60  {
61  return nullptr;
62  }
63  return mTiles[x + y * mWidth];
64 }

References mHeight, mTiles, mWidth, x, and y.

Referenced by RenameListener::action(), addRoad(), Map::saveExtraLayer(), LocalPlayer::setHome(), LocalPlayer::setRealPos(), and SocialNavigationTab::updateList().

◆ setTile() [1/2]

void SpecialLayer::setTile ( const int  x,
const int  y,
const int  type 
)

Definition at line 81 of file speciallayer.cpp.

82 {
83  if (x < 0 || x >= mWidth ||
84  y < 0 || y >= mHeight)
85  {
86  return;
87  }
88 
89  const int idx = x + y * mWidth;
90  MapItem *const tile = mTiles[idx];
91  if (tile != nullptr)
92  {
93  tile->setType(type);
94  tile->setPos(x, y);
95  }
96  else
97  {
98  mTiles[idx] = new MapItem(type);
99  mTiles[idx]->setPos(x, y);
100  }
101 }

References mHeight, mTiles, mWidth, MapItem::setPos(), MapItem::setType(), x, and y.

◆ setTile() [2/2]

void SpecialLayer::setTile ( const int  x,
const int  y,
MapItem *const  item 
)

Definition at line 66 of file speciallayer.cpp.

67 {
68  if (x < 0 || x >= mWidth ||
69  y < 0 || y >= mHeight)
70  {
71  return;
72  }
73 
74  const int idx = x + y * mWidth;
75  delete mTiles[idx];
76  if (item != nullptr)
77  item->setPos(x, y);
78  mTiles[idx] = item;
79 }

References mHeight, mTiles, mWidth, MapItem::setPos(), x, and y.

Referenced by addRoad(), PopupMenu::handleLink(), LocalPlayer::setHome(), and LocalPlayer::setRealPos().

◆ updateCache()

void SpecialLayer::updateCache ( )

Definition at line 168 of file speciallayer.cpp.

169 {
170  for (int y = 0; y < mHeight; y ++)
171  {
172  const int y2 = y * mWidth;
173  for (int x = 0; x < mWidth; x ++)
174  {
175  int c = 10000;
176  for (int f = x + 1; f < mWidth; f ++)
177  {
178  MapItem *const item = mTiles[f + y2];
179  if (item != nullptr &&
180  item->mType != MapItemType::EMPTY)
181  {
182  c = f - x - 1;
183  break;
184  }
185  }
186  mCache[x + y2] = c;
187  }
188  }
189 }

References MapItemType::EMPTY, mCache, mHeight, mTiles, MapItem::mType, mWidth, x, and y.

Referenced by addRoad(), clean(), PopupMenu::handleLink(), LocalPlayer::setHome(), and LocalPlayer::setRealPos().

Friends And Related Function Documentation

◆ Map

friend class Map
friend

Definition at line 36 of file speciallayer.h.

◆ MapLayer

friend class MapLayer
friend

Definition at line 37 of file speciallayer.h.

Field Documentation

◆ mCache

int* SpecialLayer::mCache
private

Definition at line 80 of file speciallayer.h.

Referenced by SpecialLayer(), updateCache(), and ~SpecialLayer().

◆ mHeight

int SpecialLayer::mHeight
private

◆ mName

const std::string SpecialLayer::mName
private

Definition at line 78 of file speciallayer.h.

Referenced by getCounterName().

◆ mTiles

MapItem** SpecialLayer::mTiles
private

◆ mWidth

int SpecialLayer::mWidth
private

The documentation for this class was generated from the following files:
SpecialLayer::getTile
MapItem * getTile(const int x, const int y) const
Definition: speciallayer.cpp:56
Position::x
int x
Definition: position.h:44
MapItem::setPos
void setPos(const int x, const int y)
Definition: mapitem.cpp:126
MapItem::mType
int mType
Definition: mapitem.h:87
MapItem
Definition: mapitem.h:31
delete2
#define delete2(var)
Definition: delete2.h:24
Position::y
int y
Definition: position.h:45
if
if(!vert) return
MemoryCounter::MemoryCounter
MemoryCounter()
Definition: memorycounter.cpp:27
MapItemType::ROAD
@ ROAD
Definition: mapitemtype.h:30
SpecialLayer::mHeight
int mHeight
Definition: speciallayer.h:82
MapItem::setType
void setType(const int type)
Definition: mapitem.cpp:95
SpecialLayer::updateCache
void updateCache()
Definition: speciallayer.cpp:168
x
x
Definition: graphics_calcImageRect.hpp:72
SpecialLayer::SpecialLayer
SpecialLayer(const std::string &name, const int width, const int height)
Definition: speciallayer.cpp:34
SpecialLayer::mCache
int * mCache
Definition: speciallayer.h:80
MapItemType::EMPTY
@ EMPTY
Definition: mapitemtype.h:28
MapItem::draw
void draw(Graphics *const graphics, const int x, const int y, const int dx, const int dy) const
Definition: mapitem.cpp:132
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
Position
Definition: position.h:34
SpecialLayer::setTile
void setTile(const int x, const int y, MapItem *const item)
Definition: speciallayer.cpp:66
y
y
Definition: graphics_calcImageRect.hpp:72
SpecialLayer::mWidth
int mWidth
Definition: speciallayer.h:81
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
SpecialLayer::mName
const std::string mName
Definition: speciallayer.h:78
mapTileSize
static const int mapTileSize
Definition: map.h:26
SpecialLayer::mTiles
MapItem ** mTiles
Definition: speciallayer.h:79