Peoplemon  0.1.0
Peoplemon 3 game source documentation
Layer.hpp
Go to the documentation of this file.
1 #ifndef CORE_MAPS_LAYER_HPP
2 #define CORE_MAPS_LAYER_HPP
3 
4 #include <BLIB/Logging.hpp>
5 #include <BLIB/Serialization.hpp>
6 
7 #include <cstdint>
8 #include <vector>
9 
10 namespace core
11 {
12 namespace map
13 {
20 template<typename T>
21 class Layer {
22 public:
27  Layer();
28 
36  void create(unsigned int width, unsigned int height, const T& val = T());
37 
42  unsigned int width() const;
43 
48  unsigned int height() const;
49 
57  const T& get(unsigned int x, unsigned int y) const;
58 
66  T& getRef(unsigned int x, unsigned int y);
67 
75  void set(unsigned int x, unsigned int y, const T& value);
76 
77 private:
78  std::uint32_t w;
79  std::uint32_t h;
80  std::vector<T> data;
81 
82  friend struct bl::serial::SerializableObject<Layer<T>>;
83 };
84 
86 
87 template<typename T>
89 : w(0)
90 , h(0) {}
91 
92 template<typename T>
93 void Layer<T>::create(unsigned int width, unsigned int height, const T& val) {
94  w = width;
95  h = height;
96  data.clear();
97  data.resize(width * height, val);
98 }
99 
100 template<typename T>
101 unsigned int Layer<T>::width() const {
102  return w;
103 }
104 
105 template<typename T>
106 unsigned int Layer<T>::height() const {
107  return h;
108 }
109 
110 template<typename T>
111 const T& Layer<T>::get(unsigned int x, unsigned int y) const {
112  const unsigned int i = y * w + x;
113  if (i < data.size()) { return data[i]; }
114  else {
115  static const T null{};
116  BL_LOG_WARN << "Out of bounds layer access: pos=(" << x << "," << y << ") size=(" << w
117  << "," << h << ")";
118  return null;
119  }
120 }
121 
122 template<typename T>
123 T& Layer<T>::getRef(unsigned int x, unsigned int y) {
124  const unsigned int i = y * w + x;
125  if (i < data.size()) { return data[i]; }
126  else {
127  static T null{};
128  BL_LOG_WARN << "Out of bounds layer access: pos=(" << x << "," << y << ") size=(" << w
129  << "," << h << ")";
130  return null;
131  }
132 }
133 
134 template<typename T>
135 void Layer<T>::set(unsigned int x, unsigned int y, const T& value) {
136  const unsigned int i = y * w + x;
137  if (i < data.size()) { data[i] = value; }
138 }
139 
140 } // namespace map
141 } // namespace core
142 
143 namespace bl
144 {
145 namespace serial
146 {
147 template<typename T>
148 struct SerializableObject<core::map::Layer<T>> : public SerializableObjectBase {
150 
151  SerializableField<1, Layer, std::uint32_t> w;
152  SerializableField<2, Layer, std::uint32_t> h;
153  SerializableField<3, Layer, std::vector<T>> data;
154 
156  : SerializableObjectBase("MapLayer")
157  , w("w", *this, &Layer::w, SerializableFieldBase::Required{})
158  , h("h", *this, &Layer::h, SerializableFieldBase::Required{})
159  , data("data", *this, &Layer::data, SerializableFieldBase::Required{}) {}
160 };
161 
162 } // namespace serial
163 } // namespace bl
164 
165 #endif
Core classes and functionality for both the editor and game.
Generic map layer class. Can be used for any type of layer.
Definition: Layer.hpp:21
void create(unsigned int width, unsigned int height, const T &val=T())
Clears all stored data (if any) and creates the layer with the given size.
Definition: Layer.hpp:93
void set(unsigned int x, unsigned int y, const T &value)
Sets the object in the layer at the position to the given value.
Definition: Layer.hpp:135
unsigned int width() const
Returns the width of the layer in tiles.
Definition: Layer.hpp:101
unsigned int height() const
Returns the height of the layer in tiles.
Definition: Layer.hpp:106
T & getRef(unsigned int x, unsigned int y)
Returns the object in the layer at the given position.
Definition: Layer.hpp:123
Layer()
Construct an empty layer.
Definition: Layer.hpp:88
const T & get(unsigned int x, unsigned int y) const
Returns the object in the layer at the given position.
Definition: Layer.hpp:111
SerializableField< 1, Layer, std::uint32_t > w
Definition: Layer.hpp:151
SerializableField< 3, Layer, std::vector< T > > data
Definition: Layer.hpp:153
SerializableField< 2, Layer, std::uint32_t > h
Definition: Layer.hpp:152