ManaPlus
objectslayer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "utils/cast.h"
25 
27 
28 #include "debug.h"
29 
30 ObjectsLayer::ObjectsLayer(const unsigned width,
31  const unsigned height) :
32  MemoryCounter(),
33  mTiles(new MapObjectList*[width * height]),
34  mWidth(width),
35  mHeight(height)
36 {
37  std::fill_n(mTiles, width * height, static_cast<MapObjectList*>(nullptr));
38 }
39 
41 {
42  const unsigned size = mWidth * mHeight;
43  for (unsigned f = 0; f < size; f ++)
44  delete mTiles[f];
45 
46  delete [] mTiles;
47  mTiles = nullptr;
48 }
49 
50 void ObjectsLayer::addObject(const std::string &name, const int type,
51  const unsigned x, const unsigned y,
52  unsigned dx, unsigned dy)
53 {
54  if (mTiles == nullptr)
55  return;
56 
57  if (x + dx > mWidth)
58  dx = mWidth - x;
59  if (y + dy > mHeight)
60  dy = mHeight - y;
61 
62  for (unsigned y1 = y; y1 < y + dy; y1 ++)
63  {
64  const unsigned idx1 = x + y1 * mWidth;
65  const unsigned idx2 = idx1 + dx;
66 
67  for (unsigned i = idx1; i < idx2; i ++)
68  {
69  if (mTiles[i] == nullptr)
70  mTiles[i] = new MapObjectList;
71  mTiles[i]->objects.push_back(MapObject(type, name));
72  }
73  }
74 }
75 
76 MapObjectList *ObjectsLayer::getAt(const unsigned x, const unsigned y) const
77 {
78  if (x >= mWidth || y >= mHeight)
79  return nullptr;
80  return mTiles[x + y * mWidth];
81 }
82 
84 {
85  return CAST_S32(sizeof(ObjectsLayer) +
86  (sizeof(MapObjectList) + sizeof(MapObjectList*)) * mWidth * mHeight);
87 }
#define CAST_S32
Definition: cast.h:30
std::vector< MapObject > objects
Definition: mapobjectlist.h:43
int calcMemoryLocal() const
ObjectsLayer(const unsigned width, const unsigned height)
MapObjectList * getAt(const unsigned x, const unsigned y) const
unsigned mWidth
Definition: objectslayer.h:54
void addObject(const std::string &name, const int type, const unsigned x, const unsigned y, unsigned dx, unsigned dy)
MapObjectList ** mTiles
Definition: objectslayer.h:53
unsigned mHeight
Definition: objectslayer.h:55
#define new
Definition: debug_new.h:147
int size()
Definition: emotedb.cpp:306