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.

References mCache, mHeight, mTiles, and mWidth.

Referenced by calcMemoryLocal().

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 }
MapItem ** mTiles
Definition: speciallayer.h:79
const std::string mName
Definition: speciallayer.h:78

◆ ~SpecialLayer()

SpecialLayer::~SpecialLayer ( )

Definition at line 48 of file speciallayer.cpp.

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

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

Member Function Documentation

◆ addRoad()

void SpecialLayer::addRoad ( const Path road)

Definition at line 103 of file speciallayer.cpp.

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

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

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 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
int y
Definition: position.h:45
void setTile(const int x, const int y, MapItem *const item)
void updateCache()
MapItem * getTile(const int x, const int y) const
void setType(const int type)
Definition: mapitem.cpp:95
int x
Definition: position.h:44

◆ calcMemoryLocal()

int SpecialLayer::calcMemoryLocal ( ) const
virtual

Reimplemented from MemoryCounter.

Definition at line 162 of file speciallayer.cpp.

References mHeight, mWidth, and SpecialLayer().

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

◆ clean()

void SpecialLayer::clean ( )

Definition at line 117 of file speciallayer.cpp.

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

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

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 }
MapItem ** mTiles
Definition: speciallayer.h:79
void updateCache()
void setType(const int type)
Definition: mapitem.cpp:95

◆ 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.

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

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 }
#define BLOCK_START(name)
Definition: perfomance.h:78
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
MapItem ** mTiles
Definition: speciallayer.h:79
static const int mapTileSize
Definition: map.h:26
void draw(Graphics *const graphics, const int x, const int y, const int dx, const int dy) const
Definition: mapitem.cpp:132

◆ getCounterName()

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

Reimplemented from MemoryCounter.

Definition at line 64 of file speciallayer.h.

References mCache, mName, mTiles, and updateCache().

65  { return mName; }
const std::string mName
Definition: speciallayer.h:78

◆ getTile()

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

Definition at line 56 of file speciallayer.cpp.

References mHeight, mTiles, and mWidth.

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

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

◆ setTile() [1/2]

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

Definition at line 66 of file speciallayer.cpp.

References ServerCommandType::item, mHeight, mTiles, mWidth, and MapItem::setPos().

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

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 }
MapItem ** mTiles
Definition: speciallayer.h:79
void setPos(const int x, const int y)
Definition: mapitem.cpp:126

◆ setTile() [2/2]

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

Definition at line 81 of file speciallayer.cpp.

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

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 }
MapItem ** mTiles
Definition: speciallayer.h:79
void setType(const int type)
Definition: mapitem.cpp:95
void setPos(const int x, const int y)
Definition: mapitem.cpp:126

◆ updateCache()

void SpecialLayer::updateCache ( )

Definition at line 168 of file speciallayer.cpp.

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

Referenced by Map::addPortalTile(), addRoad(), clean(), getCounterName(), PopupMenu::handleLink(), LocalPlayer::setHome(), LocalPlayer::setRealPos(), and Map::updatePortalTile().

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 }
int mType
Definition: mapitem.h:87
MapItem ** mTiles
Definition: speciallayer.h:79

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

◆ 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: