From fcccdc297dce961b7dccac4260d88ce32b2fa619 Mon Sep 17 00:00:00 2001 From: Alfred Burgess <alfred.burgess95@gmail.com> Date: Sat, 9 Dec 2023 23:31:18 +0800 Subject: [PATCH] Added a method to draw hexagons --- include/window.h | 1 + src/window.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/window.h b/include/window.h index d1acdcf..9c99ba2 100644 --- a/include/window.h +++ b/include/window.h @@ -64,6 +64,7 @@ class Window // void _drawLine( Vec2 from, Vec2 to ); void _drawPoint( Vec2 point ); // void _drawPoint( int16_t x, int16_t y ); + void _drawHex( Vec2 point, uint16_t radius ); SDL_Rect _createViewport( int16_t x, int16_t y, int16_t w, int16_t h ); diff --git a/src/window.cpp b/src/window.cpp index 944591c..118b728 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -93,7 +93,9 @@ void Window::update() { SDL_Rect bg = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); this->_background->render( {0,0}, this->_renderer ); this->_character->render( {SCREEN_HEIGHT/3,SCREEN_HEIGHT/3}, this->_renderer ); + SDL_Rect core = this->_createViewport( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + // Render Example Primatives this->_drawRect( SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); this->_drawRect( 0, 0, 10, 10 ); @@ -103,7 +105,22 @@ void Window::update() { this->_drawPoint( { SCREEN_WIDTH / 2, i } ); } - // SDL_Rect overlay = this->_createViewport( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + SDL_Rect overlay = this->_createViewport( 0, 0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 ); + const int hexRad = 10; + const int hexSpacing = 1; + const int drawHexRad = hexRad + hexSpacing; + for( int i = 0; i < 20; i++ ) + { + for( int j = 0; j < 20; j++ ) + { + this->_drawHex( { + i * ( drawHexRad * sqrt(3) ) + ( j % 2 == 0 ? 0 : ( drawHexRad * sqrt(3) ) / 2 ), + j * ( drawHexRad * 3 / 2 ) + } , hexRad ); + + } + + } // SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); @@ -131,6 +148,18 @@ void Window::_drawPoint( Vec2 point ) SDL_SetRenderDrawColor( this->_renderer, 0xFF, 0xFF, 0x00, 0xFF ); SDL_RenderDrawPoint( this->_renderer, point.x, point.y ); } +void Window::_drawHex( Vec2 point, uint16_t radius ) +{ + const float halfWidth = (sqrt(3) * radius) / 2.0; + const float quaterHeight = radius / 2.0; + this->_drawLine( (int)point.x, (int)point.y + radius, (int)point.x + halfWidth, (int)point.y + quaterHeight ); + this->_drawLine( (int)point.x + halfWidth, (int)point.y + quaterHeight, (int)point.x + halfWidth, (int)point.y - quaterHeight ); + this->_drawLine( (int)point.x + halfWidth, (int)point.y - quaterHeight, (int)point.x, (int)point.y - radius ); + + this->_drawLine( (int)point.x, (int)point.y + radius, (int)point.x - halfWidth, (int)point.y + quaterHeight ); + this->_drawLine( (int)point.x - halfWidth, (int)point.y + quaterHeight, (int)point.x - halfWidth, (int)point.y - quaterHeight ); + this->_drawLine( (int)point.x - halfWidth, (int)point.y - quaterHeight, (int)point.x, (int)point.y - radius ); +} void Window::spawn() -- GitLab