ScummVM API documentation
array2d.h
1 
2 /* ScummVM - Graphic Adventure Engine
3  *
4  * ScummVM is the legal property of its developers, whose names
5  * are too numerous to list here. Please refer to the COPYRIGHT
6  * file distributed with this source distribution.
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 3 of the License, or
11  * (at your option) 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  */
22 
23 #ifndef GAMOS_ARRAY2D_H
24 #define GAMOS_ARRAY2D_H
25 
26 #include "common/array.h"
27 #include "common/rect.h"
28 
29 template <class T>
30 class Array2D : protected Common::Array<T> {
31 private:
32  typedef Common::Array<T> __base;
33 public:
34  typedef uint16 size_type;
42 
43 public:
44 
45 
46  Array2D() = default;
47  Array2D(Array2D &&) = default;
48  Array2D(const Array2D &) = default;
49 
50  Array2D &operator=(const Array2D &) = default;
51  Array2D &operator=(Array2D &&) = default;
52 
53  Array2D(size_type w, size_type h) {
54  resize(w, h);
55  }
56 
57  Array2D(const Common::Point &sz) {
58  resize(sz);
59  }
60 
61  Array2D<T> *copy() {
62  Array2D<T> *tmp = new Array2D<T>;
63  *tmp = *this;
64  return tmp;
65  }
66 
67  void resize(const Common::Point &sz) {
68  _w = sz.x;
69  _h = sz.y;
70 
71  __base::resize(_w * _h);
72  }
73 
74  void resize(size_type w, size_type h) {
75  _w = w;
76  _h = h;
77 
78  __base::resize(_w * _h);
79  }
80 
81  T &at(size_type x, size_type y) {
82  return __base::operator[](x + y * _w);
83  }
84 
85  T &at(const Common::Point &p) {
86  return __base::operator[](p.x + p.y * _w);
87  }
88 
89  T &at(size_type n) {
90  return __base::operator[](n);
91  }
92 
93  T &operator()(size_type x, size_type y) {
94  return at(x + y * _w);
95  }
96 
97  T &operator()(const Common::Point &p) {
98  return at(p.x + p.y * _w);
99  }
100 
101  T *getLinePtr(size_type y) {
102  return &(at(y * _w));
103  }
104 
105  const T *getLinePtr(size_type y) const {
106  return &(at(y * _w));
107  }
108 
109  Common::Point sizes() const {
110  return Common::Point(_w, _h);
111  }
112 
113  size_type getIndex(size_type x, size_type y) const {
114  size_t n = x + y * _w;
115  if (n < size())
116  return n;
117  return -1;
118  }
119 
120  Common::Point indexToCoords(size_type id) const {
121  if (id >= 0 && id < size())
122  return Common::Point(id % _w, id / _w);
123  return Common::Point(-1, -1);
124  }
125 
126  size_type width() const {
127  return _w;
128  }
129 
130  size_type height() const {
131  return _h;
132  }
133 
134  void clear() {
135  _w = 0;
136  _h = 0;
138  }
139 
140  bool isNotNull() const {
141  return _w > 0 && _h > 0;
142  }
143 
144  bool isNull() const {
145  return _w == 0 || _h == 0;
146  }
147 
148  bool isOk() const {
149  return _w > 0 && _h > 0 && (size() == _w * _h);
150  }
151 
152 protected:
153  size_type _w = 0;
154  size_type _h = 0;
155 };
156 
157 
158 #endif
uint16 size_type
Definition: array2d.h:34
Definition: array.h:52
void clear()
Definition: array.h:321
T & operator[](size_type idx)
Definition: array.h:274
T y
Definition: rect.h:49
T x
Definition: rect.h:48
Definition: rect.h:144
size_type size() const
Definition: array.h:316
Definition: array2d.h:30
void resize(size_type newSize)
Definition: array.h:412