Skip to content

Commit

Permalink
feat: render chess board without pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed May 14, 2024
1 parent 5c4bb78 commit 9a4dc73
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/UI/HomePage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions src/UI/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void HomePage::Render(void)
| ImGuiWindowFlags_NoSavedSettings);

RenderUsernameInputBox(isDisabled);
RenderChessBoard();

if (_event == HomePageEvent::ON_GAME_SELECTION)
RenderPopup();
Expand Down Expand Up @@ -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());
}
}
}
}

}

0 comments on commit 9a4dc73

Please sign in to comment.