diff options
author | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:15 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:15 +0200 |
commit | eea082ffd1a3ecf73b7c464cc28da5ef74d3a30f (patch) | |
tree | 0da31817b587ae244d9cf3a186014601ca5e6e48 /src | |
parent | be49084ba59b89c14f8b9f9e1274b013428d7205 (diff) |
Add grid<> template class for representing 2D grids
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/grid.hpp | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7d64a66f..bf970c59 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -122,6 +122,7 @@ SET(SRCS_TESTS ../tests/harness.cc ../tests/lua_get_level.cc ../tests/flag_set.cc + ../tests/grid.cc ) ADD_LIBRARY(game diff --git a/src/grid.hpp b/src/grid.hpp new file mode 100644 index 00000000..4708811d --- /dev/null +++ b/src/grid.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include <boost/multi_array.hpp> + +/** + * 2D grid of T's. + */ +template <typename T> +struct grid { + +private: + boost::multi_array<T, 2> impl; + +public: + /** + * Get reference to tile. + */ + T &operator ()(std::size_t x, std::size_t y) + { + return impl[y][x]; + } + + /** + * Get const reference to tile. + */ + T const &operator ()(std::size_t x, std::size_t y) const + { + return impl[y][x]; + } + + /** + * Resize grid. There is no guarantee whether + * existing elements will be preserved or not. + */ + void resize(std::size_t width, std::size_t height) + { + impl.resize(boost::extents[height][width]); + } + + /** + * Get current width. + */ + std::size_t width() const + { + return impl.shape()[1]; + } + + /** + * Get current height. + */ + std::size_t height() const + { + return impl.shape()[0]; + } + + /** + * Set width. Same caveats apply as for resize(). + */ + void width(std::size_t width) + { + resize(width, height()); + } + + /** + * Set height. Same caveats apply as for resize(). + */ + void height(std::size_t height) + { + resize(width(), height); + } + +}; |