ManaPlus
objectslayer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 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 
22 
23 #include "utils/cast.h"
24 
26 
27 #include "debug.h"
28 
29 ObjectsLayer::ObjectsLayer(const unsigned width,
30  const unsigned height) :
31  MemoryCounter(),
32  mTiles(new MapObjectList*[width * height]),
33  mWidth(width),
34  mHeight(height)
35 {
36  std::fill_n(mTiles, width * height, static_cast<MapObjectList*>(nullptr));
37 }
38 
40 {
41  const unsigned size = mWidth * mHeight;
42  for (unsigned f = 0; f < size; f ++)
43  delete mTiles[f];
44 
45  delete [] mTiles;
46  mTiles = nullptr;
47 }
48 
49 void ObjectsLayer::addObject(const std::string &name, const int type,
50  const unsigned x, const unsigned y,
51  unsigned dx, unsigned dy)
52 {
53  if (mTiles == nullptr)
54  return;
55 
56  if (x + dx > mWidth)
57  dx = mWidth - x;
58  if (y + dy > mHeight)
59  dy = mHeight - y;
60 
61  for (unsigned y1 = y; y1 < y + dy; y1 ++)
62  {
63  const unsigned idx1 = x + y1 * mWidth;
64  const unsigned idx2 = idx1 + dx;
65 
66  for (unsigned i = idx1; i < idx2; i ++)
67  {
68  if (mTiles[i] == nullptr)
69  mTiles[i] = new MapObjectList;
70  mTiles[i]->objects.push_back(MapObject(type, name));
71  }
72  }
73 }
74 
75 MapObjectList *ObjectsLayer::getAt(const unsigned x, const unsigned y) const
76 {
77  if (x >= mWidth || y >= mHeight)
78  return nullptr;
79  return mTiles[x + y * mWidth];
80 }
81 
83 {
84  return CAST_S32(sizeof(ObjectsLayer) +
85  (sizeof(MapObjectList) + sizeof(MapObjectList*)) * mWidth * mHeight);
86 }
unsigned mWidth
Definition: objectslayer.h:53
unsigned mHeight
Definition: objectslayer.h:54
std::vector< MapObject > objects
Definition: mapobjectlist.h:42
void addObject(const std::string &name, const int type, const unsigned x, const unsigned y, unsigned dx, unsigned dy)
MapObjectList * getAt(const unsigned x, const unsigned y) const
#define new
Definition: debug_new.h:147
#define CAST_S32
Definition: cast.h:29
int calcMemoryLocal() const
MapObjectList ** mTiles
Definition: objectslayer.h:52
ObjectsLayer(const unsigned width, const unsigned height)