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

grid rendering code

parent 52342ac7
No related branches found
No related tags found
No related merge requests found
......@@ -40,15 +40,53 @@ namespace demo::hexboard {
}
}
// see https://www.redblobgames.com/grids/hexagons/#hex-to-pixel
const fggl::math::mat2 HEX_BASIS{
std::sqrt(3.0F), std::sqrt(3.0F) / 2.0F,
0.0F, 3.0F / 2.0F
};
static inline fggl::math::vec2 hexToScreen(fggl::grid::IntHex hexPos, float size, fggl::math::vec2 offset = {0,0}) {
return size * fggl::math::vec2{hexPos.q(), hexPos.r()} * HEX_BASIS + offset;
}
void Scene::render(fggl::gfx::Graphics &gfx) {
// if the board is not set, abort
if ( m_board == nullptr ){
/*if ( m_board == nullptr ){
return;
}
}*/
// draw the grid
// FIXME don't hard-code the screen size
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) ));
const fggl::math::vec2 offset{
( 1920 - ( (float)gridWidth * hexRadius * std::sqrt(3.0F))) - (hexRadius / 2.0F),
( (1080 - hexRadius / 2.0F) - ( (float)gridHeight * hexRadius * (3.0F / 2.0F) )),
};
fggl::gfx::Paint paint;
fggl::grid::IntHex hexPos{0, 0};
auto rowBasis = hexPos;
for(auto i=0; i<gridHeight; ++i) {
for (auto j=0; j<gridWidth; ++j) {
auto pos = hexToScreen(hexPos, hexRadius, offset);
auto hex = fggl::gfx::make_shape(pos, hexRadius, 6);
paint.stroke(hex);
pos.x += hexRadius;
hexPos = hexPos.neighbour( fggl::grid::HexDirPointy::RIGHT );
}
rowBasis = i % 2 == 0 ? rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_RIGHT) : rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_LEFT);
hexPos = rowBasis;
}
gfx.draw2D(paint);
}
} // namespace demo::hexboard
\ No newline at end of file
......@@ -72,11 +72,13 @@ namespace fggl::grid {
}
inline HexPointT neighbour(HexDirPointy dir) {
return this + HexPointT<T>(HEX_DIRECTIONS.at((int)dir));
auto& offset = HEX_DIRECTIONS.at( (int)dir );
return { m_pos[0] + offset[0], m_pos[1] + offset[1] };
}
inline HexPointT neighbour(HexDirFlat dir) {
return this + HexPointT<T>(HEX_DIAGONALS.at((int)dir));
auto& offset = HEX_DIAGONALS.at( (int)dir );
return { m_pos[0] + offset[0], m_pos[1] + offset[1] };
}
HexPointT operator+(const HexPointT<T>& other) const {
......
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