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 34 of file speciallayer.h.

Constructor & Destructor Documentation

◆ SpecialLayer()

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

Definition at line 35 of file speciallayer.cpp.

37  :
38  MemoryCounter(),
39  mName(name),
40  mTiles(new MapItem*[width * height]),
41  mCache(new int[width * height]),
42  mWidth(width),
43  mHeight(height)
44 {
45  std::fill_n(mTiles, mWidth * mHeight, static_cast<MapItem*>(nullptr));
46  std::fill_n(mCache, mWidth * mHeight, 10000);
47 }
const std::string mName
Definition: speciallayer.h:79
MapItem ** mTiles
Definition: speciallayer.h:80

References mCache, mHeight, mTiles, and mWidth.

Referenced by calcMemoryLocal().

◆ ~SpecialLayer()

SpecialLayer::~SpecialLayer ( )

Definition at line 49 of file speciallayer.cpp.

50 {
51  for (int f = 0; f < mWidth * mHeight; f ++)
52  delete2(mTiles[f])
53  delete [] mTiles;
54  delete [] mCache;
55 }
#define delete2(var)
Definition: delete2.h:25

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

Member Function Documentation

◆ addRoad()

void SpecialLayer::addRoad ( const Path road)

Definition at line 104 of file speciallayer.cpp.

105 {
106  FOR_EACH (Path::const_iterator, i, road)
107  {
108  const Position &pos = *i;
109  MapItem *const item = getTile(pos.x, pos.y);
110  if (item == nullptr)
111  setTile(pos.x, pos.y, new MapItem(MapItemType::ROAD));
112  else
113  item->setType(MapItemType::ROAD);
114  }
115  updateCache();
116 }
void setType(const int type)
Definition: mapitem.cpp:96
MapItem * getTile(const int x, const int y) const
void setTile(const int x, const int y, MapItem *const item)
void updateCache()
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
int y
Definition: position.h:46
int x
Definition: position.h:45

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 163 of file speciallayer.cpp.

164 {
165  return static_cast<int>(sizeof(SpecialLayer) +
166  sizeof(MapItem) * mWidth * mHeight);
167 }
SpecialLayer(const std::string &name, const int width, const int height)

References mHeight, mWidth, and SpecialLayer().

◆ clean()

void SpecialLayer::clean ( )

Definition at line 118 of file speciallayer.cpp.

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

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 132 of file speciallayer.cpp.

135 {
136  BLOCK_START("SpecialLayer::draw")
137  if (startX < 0)
138  startX = 0;
139  if (startY < 0)
140  startY = 0;
141  if (endX > mWidth)
142  endX = mWidth;
143  if (endY > mHeight)
144  endY = mHeight;
145 
146  for (int y = startY; y < endY; y ++)
147  {
148  const int py = y * mapTileSize - scrollY;
149  const int y2 = y * mWidth;
150  for (int x = startX; x < endX; x ++)
151  {
152  const MapItem *const item = mTiles[x + y2];
153  if (item != nullptr)
154  {
155  item->draw(graphics, x * mapTileSize - scrollX, py,
157  }
158  }
159  }
160  BLOCK_END("SpecialLayer::draw")
161 }
void draw(Graphics *const graphics, const int x, const int y, const int dx, const int dy) const
Definition: mapitem.cpp:133
static const int mapTileSize
Definition: map.h:27
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79

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 65 of file speciallayer.h.

66  { return mName; }

References mName.

◆ getTile()

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

Definition at line 57 of file speciallayer.cpp.

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

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 82 of file speciallayer.cpp.

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

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 67 of file speciallayer.cpp.

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

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 169 of file speciallayer.cpp.

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

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 37 of file speciallayer.h.

◆ MapLayer

friend class MapLayer
friend

Definition at line 38 of file speciallayer.h.

Field Documentation

◆ mCache

int* SpecialLayer::mCache
private

Definition at line 81 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 79 of file speciallayer.h.

Referenced by getCounterName().

◆ mTiles

MapItem** SpecialLayer::mTiles
private

Definition at line 80 of file speciallayer.h.

Referenced by clean(), draw(), getTile(), setTile(), SpecialLayer(), updateCache(), and ~SpecialLayer().

◆ mWidth

int SpecialLayer::mWidth
private

The documentation for this class was generated from the following files: