From 9a4dc7311d39b3beac519348e36141ebdadf6fa5 Mon Sep 17 00:00:00 2001 From: Aman Menda Date: Tue, 14 May 2024 17:41:32 +0100 Subject: [PATCH] feat: render chess board without pieces --- include/UI/HomePage.hpp | 7 +++++++ src/UI/HomePage.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/UI/HomePage.hpp b/include/UI/HomePage.hpp index 6d6f2a5..bf72e4e 100644 --- a/include/UI/HomePage.hpp +++ b/include/UI/HomePage.hpp @@ -8,6 +8,10 @@ #include "Page.hpp" #include "../Imgui.hpp" +#define CHESSBOARD_SIZE 8 +#define TILE_SIZE 70.0f +#define LABEL_SIZE 20.0f + namespace Stuckfish { class Core; @@ -40,6 +44,9 @@ namespace Stuckfish void RenderUsernameInputBox(bool isDisabled); void RenderGameReview(void); + static inline ImU32 whiteTileColor = IM_COL32(240, 217, 181, 255); + static inline ImU32 blackTileColor = IM_COL32(181, 136, 99, 255); + static inline ImU32 coordinatetextColor = IM_COL32(0, 0, 0, 255); private: Core& _app; HomePageEvent _event; diff --git a/src/UI/HomePage.cpp b/src/UI/HomePage.cpp index 54c2170..b1e9cdd 100644 --- a/src/UI/HomePage.cpp +++ b/src/UI/HomePage.cpp @@ -60,6 +60,7 @@ void HomePage::Render(void) | ImGuiWindowFlags_NoSavedSettings); RenderUsernameInputBox(isDisabled); + RenderChessBoard(); if (_event == HomePageEvent::ON_GAME_SELECTION) RenderPopup(); @@ -187,4 +188,46 @@ void HomePage::RenderUsernameInputBox(bool isDisabled) ImGui::PopStyleVar(); } +void HomePage::RenderChessBoard(void) +{ + // Get the ImGui window draw list + ImDrawList* drawList = ImGui::GetWindowDrawList(); + + // Get the top-left corner of the window where the board should start + ImVec2 boardPos = ImGui::GetCursorScreenPos(); + + for (int row = 0; row < CHESSBOARD_SIZE; ++row) + { + for (int col = 0; col < CHESSBOARD_SIZE; ++col) + { + // Calculate the position of the current tile and draw the tile + ImU32 tileColor = ((row + col) % 2 == 0) ? whiteTileColor : blackTileColor; + ImVec2 tilePos = ImVec2(boardPos.x + col * TILE_SIZE, boardPos.y + row * TILE_SIZE); + ImVec2 tileEndPos = ImVec2(tilePos.x + TILE_SIZE, tilePos.y + TILE_SIZE); + drawList->AddRectFilled(tilePos, tileEndPos, tileColor); + + std::string coordText; + /*if (row == 0) { + // Bottom row (a to h) + coordText = std::string(1, 'h' - col); + }*/ + // Case where user has played as white. + if (row == 7) { + // Top row (h to a) + // Draw the letters coordinates text + coordText = std::string(1, 'a' + col); + ImVec2 letterPos = ImVec2(tilePos.x + TILE_SIZE - ImGui::CalcTextSize(coordText.c_str()).x - 2, tilePos.y + TILE_SIZE - ImGui::CalcTextSize(coordText.c_str()).y - 2); + drawList->AddText(letterPos, coordinatetextColor, coordText.c_str()); + } + if (col == 0) + { + // Draw the coordinates text + coordText = std::to_string(8 - row); + ImVec2 textPos = ImVec2(tilePos.x + 2, tilePos.y + 2); + drawList->AddText(textPos, coordinatetextColor, coordText.c_str()); + } + } + } +} + } \ No newline at end of file