FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_matrix.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_MATRIX_HPP_INCLUDED
18#define _FGE_C_MATRIX_HPP_INCLUDED
19
20#include "FastEngine/C_vector.hpp"
21#include "FastEngine/fge_except.hpp"
22#include "json.hpp"
23#include <initializer_list>
24
25#define FGE_MATRIX_GET(dataType_, data_, sizeY_, px_, py_) \
26 (reinterpret_cast<dataType_*>(data_) + (py_) + ((px_) * (sizeY_)))
27
28namespace fge
29{
30
38template<class T>
39class Matrix
40{
41public:
42 using iterator = T*;
43 using const_iterator = T const*;
44
48 Matrix();
49
57 Matrix(std::initializer_list<std::initializer_list<T>> data);
58
65 template<class Tvec>
66 explicit Matrix(fge::Vector2<Tvec> const& msize);
73 Matrix(std::size_t sizex, std::size_t sizey);
74
82 template<class Tvec>
83 Matrix(fge::Vector2<Tvec> const& msize, T const& defaultValue);
91 Matrix(std::size_t sizex, std::size_t sizey, T const& defaultValue);
92
93 Matrix(fge::Matrix<T> const& m);
94 Matrix(fge::Matrix<T>&& m) noexcept;
95
96 ~Matrix() = default;
97
101 void clear();
102
103 fge::Matrix<T>& operator=(fge::Matrix<T> const& m);
104 fge::Matrix<T>& operator=(fge::Matrix<T>&& m) noexcept;
105
112 inline T* operator[](std::size_t x);
113 inline T const* operator[](std::size_t x) const;
114
122 inline T const& get(std::size_t x, std::size_t y) const;
130 template<class Tvec>
131 inline T const& get(fge::Vector2<Tvec> const& coord) const;
132 inline T& get(std::size_t x, std::size_t y);
133 template<class Tvec>
134 inline T& get(fge::Vector2<Tvec> const& coord);
135
144 bool get(std::size_t x, std::size_t y, T& buff) const;
153 template<class Tvec>
154 bool get(fge::Vector2<Tvec> const& coord, T& buff) const;
155
163 T* getPtr(std::size_t x, std::size_t y);
171 template<class Tvec>
172 T* getPtr(fge::Vector2<Tvec> const& coord);
173 T const* getPtr(std::size_t x, std::size_t y) const;
174 template<class Tvec>
175 T const* getPtr(fge::Vector2<Tvec> const& coord) const;
176
184 void set(std::size_t x, std::size_t y, T&& value);
192 template<class Tvec>
193 void set(fge::Vector2<Tvec> const& coord, T&& value);
201 void set(std::size_t x, std::size_t y, T const& value);
209 template<class Tvec>
210 void set(fge::Vector2<Tvec> const& coord, T const& value);
211
219 void set(std::initializer_list<std::initializer_list<T>> data);
220
226 [[nodiscard]] inline std::size_t getTotalSize() const;
232 [[nodiscard]] inline fge::Vector2<std::size_t> const& getSize() const;
238 [[nodiscard]] inline std::size_t getSizeX() const;
244 [[nodiscard]] inline std::size_t getSizeY() const;
245
251 [[nodiscard]] inline T const* get() const;
252 [[nodiscard]] inline T* get();
253
254 [[nodiscard]] fge::Matrix<T>::iterator begin();
255 [[nodiscard]] fge::Matrix<T>::iterator end();
256 [[nodiscard]] fge::Matrix<T>::const_iterator begin() const;
257 [[nodiscard]] fge::Matrix<T>::const_iterator end() const;
258
265 template<class Tvec>
266 void setSize(fge::Vector2<Tvec> const& msize);
273 void setSize(std::size_t sizex, std::size_t sizey);
274
280 void fill(T const& value);
281
285 void rotateClockwise();
295 void rotateClockwise(unsigned int n);
301 void rotateCounterClockwise(unsigned int n);
305 void flipHorizontally();
309 void flipVertically();
310
316 void toVector(std::vector<T>& buff) const;
317
318private:
319 std::unique_ptr<T[]> g_mdata;
320 fge::Vector2<std::size_t> g_msize;
321};
322
330template<class T>
331void to_json(nlohmann::json& j, fge::Matrix<T> const& r);
339template<class T>
340void from_json(nlohmann::json const& j, fge::Matrix<T>& r);
341
342} // namespace fge
343
344#include <FastEngine/C_matrix.inl>
345
346#endif // _FGE_C_MATRIX_HPP_INCLUDED
A container to store a 2D matrix of any type.
Definition C_matrix.hpp:40
void rotateCounterClockwise()
Rotate the matrix by 90 degrees counter-clockwise.
Definition C_matrix.inl:360
void clear()
Clear the matrix and set the size to 0,0.
Definition C_matrix.inl:78
void set(std::size_t x, std::size_t y, T &&value)
Set the specified value by moving it.
Definition C_matrix.inl:195
void fill(T const &value)
Fill the matrix by copying a value.
Definition C_matrix.inl:332
void setSize(fge::Vector2< Tvec > const &msize)
Set the size of the matrix.
Definition C_matrix.inl:309
T * operator[](std::size_t x)
Get the specified row.
Definition C_matrix.inl:106
std::size_t getTotalSize() const
Get the total number of elements in the matrix.
Definition C_matrix.inl:255
Matrix()
Construct a empty matrix.
Definition C_matrix.inl:21
void flipVertically()
Flip the matrix vertically.
Definition C_matrix.inl:415
fge::Vector2< std::size_t > const & getSize() const
Get the size of the matrix as a vector2.
Definition C_matrix.inl:260
std::size_t getSizeY() const
Get the y size of the matrix.
Definition C_matrix.inl:270
void rotateClockwise()
Rotate the matrix by 90 degrees clockwise.
Definition C_matrix.inl:344
T const & get(fge::Vector2< Tvec > const &coord) const
Get the specified value.
void toVector(std::vector< T > &buff) const
Insert all elements of the matrix in a 1D vector.
Definition C_matrix.inl:432
void flipHorizontally()
Flip the matrix horizontally.
Definition C_matrix.inl:398
std::size_t getSizeX() const
Get the x size of the matrix.
Definition C_matrix.inl:265
T const * get() const
Get the 2D array of the matrix.
Definition C_matrix.inl:276
T * getPtr(std::size_t x, std::size_t y)
Get the specified value as a pointer.
Definition C_matrix.inl:162