From 9e4e0b1d6abfb141c2e368cebf40bdb53697c045 Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Sun, 18 Dec 2022 15:52:07 +0000 Subject: [PATCH] cleanup some of the hexagon code --- demo/demo/hexboard/board.cpp | 49 ++++++++++++++---------------------- include/fggl/grid/layout.hpp | 21 +++++++++++----- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/demo/demo/hexboard/board.cpp b/demo/demo/hexboard/board.cpp index 9c12a82..ad9d434 100644 --- a/demo/demo/hexboard/board.cpp +++ b/demo/demo/hexboard/board.cpp @@ -35,10 +35,17 @@ namespace demo::hexboard { grass.data->name = "grass"; grass.data->colour = {0.0F, 1.0F, 0.0}; - fggl::grid::IntHex islandPoint{3,3}; - auto island = islandPoint.hexesInRange(2); - for ( auto& hex : island) { - m_board->setTerrain(hex, grass); + std::array<fggl::grid::IntHex, 4> islands {{ + {3, 3}, + {7, 7}, + {10, 10}, + {7, 3} + }}; + for (auto islandPoint : islands){ + auto island = islandPoint.hexesInRange(2); + for (auto &hex : island) { + m_board->setTerrain(hex, grass); + } } } @@ -59,12 +66,10 @@ namespace demo::hexboard { // check if a button was pressed auto& input = this->input(); { - fggl::math::vec2 screenPos( - input.mouse.axis(fggl::input::MouseAxis::X), - input.mouse.axis(fggl::input::MouseAxis::Y) + const fggl::math::vec2 screenPos( + fggl::math::rescale_ndc(input.mouse.axis(fggl::input::MouseAxis::X), 0, 1920), + fggl::math::rescale_ndc(input.mouse.axis(fggl::input::MouseAxis::Y), 0, 1080) ); - screenPos.x = fggl::math::rescale_ndc(screenPos.x, 0, 1920); - screenPos.y = fggl::math::rescale_ndc(screenPos.y, 0, 1080); m_selections->hover = fggl::grid::round2( m_layout->toGrid(screenPos) ); if (input.mouse.pressed(fggl::input::MouseButton::LEFT)) { @@ -74,29 +79,13 @@ namespace demo::hexboard { } void Scene::drawGrid(fggl::gfx::Paint& paint) { - const float hexRadius = 64.0F; - const auto gridWidth = (int)( (1920 - hexRadius) / (hexRadius * std::sqrt(3.0F)) ); - const auto gridHeight = (int)( (1080 - hexRadius) / (hexRadius * (3.0F / 2.0F) )); - auto tiles = m_board->getAllTiles(); - - fggl::grid::IntHex hexPos{0, 0}; - auto rowBasis = hexPos; - for(auto i=0; i<gridHeight; ++i) { - for (auto j=0; j<gridWidth; ++j) { - - auto terrain = m_board->getTerrain(hexPos); - if ( terrain.has_value() ) { - const auto& value = terrain.value(); - m_layout->paintHex(paint, hexPos, value.colour); - } - - // next hexagon - hexPos = hexPos.neighbour(fggl::grid::HexDirPointy::RIGHT); + for ( const auto& tile : tiles ) { + auto terrain = m_board->getTerrain(tile); + if ( terrain.has_value() ) { + const auto& terrainData = terrain.value(); + m_layout->paintHex(paint, tile, terrainData.colour); } - - rowBasis = i % 2 == 0 ? rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_RIGHT) : rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_LEFT); - hexPos = rowBasis; } } diff --git a/include/fggl/grid/layout.hpp b/include/fggl/grid/layout.hpp index 96cfa7e..55a878e 100644 --- a/include/fggl/grid/layout.hpp +++ b/include/fggl/grid/layout.hpp @@ -38,14 +38,18 @@ namespace fggl::grid { - const math::mat2 MAT_HEX_POINTY { - std::sqrt(3.0F), 0.0F, - std::sqrt(3.0F) / 2.0F, 3.0F/2.0F + // factor out the call to sqrt so the matrices can be constexpr + constexpr float M_SQRT_3 = 1.73205080757F; + constexpr float M_SQRT_3_OVER_2 = M_SQRT_3 / 2.0F; + + constexpr math::mat2 MAT_HEX_POINTY{ + M_SQRT_3, 0.0F, + M_SQRT_3_OVER_2, 3.F / 2.F }; - const math::mat2 MAT_HEX_FLAT { - 3.F/2.F, std::sqrt(3.F)/2.F, - 0.0F, std::sqrt(3.F) + constexpr math::mat2 MAT_HEX_FLAT{ + 3.F / 2.F, M_SQRT_3_OVER_2, + 0.0F, M_SQRT_3 }; struct Orientation { @@ -73,6 +77,11 @@ namespace fggl::grid { Layout(Orientation orientation, math::vec2 size, math::vec2 origin) : m_orientation(orientation), m_size(size), m_origin(origin){} Layout(Orientation orientation, float size) : m_orientation(orientation), m_size(size, size), m_origin() {} + inline void translate(float dx, float dy){ + m_origin.x += dx; + m_origin.y += dy; + } + [[nodiscard]] inline math::vec2 origin() const { return m_origin; -- GitLab