Skip to content
Snippets Groups Projects
Commit 568cfa83 authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

stronger bounds checks

parent 1cf8f72e
No related branches found
No related tags found
No related merge requests found
...@@ -34,17 +34,22 @@ namespace fggl::entity::grid { ...@@ -34,17 +34,22 @@ namespace fggl::entity::grid {
template<typename T, uint32_t width, uint32_t height> template<typename T, uint32_t width, uint32_t height>
struct Grid { struct Grid {
public: public:
constexpr static std::array<math::vec2i, 4> DIRECTIONS{{ {-1, 0}, {0, -1}, {1, 0}, {0, 1} }};
Grid() = default; Grid() = default;
inline T& get(GridPos pos) { inline T& get(GridPos pos) {
assert(inBounds(pos));
return m_cells[getCellIndex(pos)]; return m_cells[getCellIndex(pos)];
} }
const T& get(GridPos pos) const { const T& get(GridPos pos) const {
assert(inBounds(pos));
return m_cells[getCellIndex(pos)]; return m_cells[getCellIndex(pos)];
} }
inline void set(GridPos pos, T value) { inline void set(GridPos pos, T value) {
assert(inBounds(pos));
m_cells[getCellIndex(pos)] = value; m_cells[getCellIndex(pos)] = value;
} }
...@@ -57,7 +62,6 @@ namespace fggl::entity::grid { ...@@ -57,7 +62,6 @@ namespace fggl::entity::grid {
constexpr static math::vec2i size = math::vec2i(width, height); constexpr static math::vec2i size = math::vec2i(width, height);
std::array<T, size.x * size.y> m_cells; std::array<T, size.x * size.y> m_cells;
inline uint32_t getCellIndex(GridPos pos) const { inline uint32_t getCellIndex(GridPos pos) const {
assert( inBounds(pos)); assert( inBounds(pos));
return pos.y * size.x + pos.x; return pos.y * size.x + pos.x;
...@@ -98,6 +102,7 @@ namespace fggl::entity::grid { ...@@ -98,6 +102,7 @@ namespace fggl::entity::grid {
clear(); clear();
} }
[[nodiscard]]
inline bool inBounds(GridPos pos) const { inline bool inBounds(GridPos pos) const {
return 0 <= pos.x && pos.x <= width return 0 <= pos.x && pos.x <= width
&& 0 <= pos.y && pos.y <= height; && 0 <= pos.y && pos.y <= height;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment