From 9cf909f72a8a4b7265c3864894886f16cbd320bb Mon Sep 17 00:00:00 2001 From: Aman Menda Date: Wed, 12 Jun 2024 17:17:37 +0100 Subject: [PATCH] fix: reverse issue with gameData vector --- Stuckfish.vcxproj | 1 + include/App/ChessBoard.hpp | 13 +++---- include/UI/HomePage.hpp | 11 ++---- include/Utils.h | 9 +++++ src/App/Chessboard.cpp | 65 +++++++++++++++++++++++++++++++++++ src/UI/HomePage.cpp | 69 +++----------------------------------- 6 files changed, 89 insertions(+), 79 deletions(-) create mode 100644 include/Utils.h diff --git a/Stuckfish.vcxproj b/Stuckfish.vcxproj index 31c0433..b5a0ebb 100644 --- a/Stuckfish.vcxproj +++ b/Stuckfish.vcxproj @@ -40,6 +40,7 @@ + diff --git a/include/App/ChessBoard.hpp b/include/App/ChessBoard.hpp index a758153..0af25e6 100644 --- a/include/App/ChessBoard.hpp +++ b/include/App/ChessBoard.hpp @@ -8,6 +8,7 @@ #include "../Imgui.hpp" #include +#include "../Utils.h" namespace Stuckfish { @@ -16,14 +17,14 @@ namespace Stuckfish public: ImTextureID LoadTextureFromFile(const char* filename); void LoadPiecesTexture(); + const std::string& getStartingPosition() - { - return _startingPositionFen; - } + { return _startingPositionFen; } const std::unordered_map& getPiecesTextures() - { - return _pieceTextures; - } + { return _pieceTextures; } + + void draw(void); + void drawPieces(void); private: const std::string _startingPositionFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; diff --git a/include/UI/HomePage.hpp b/include/UI/HomePage.hpp index 78f6373..a7d04a8 100644 --- a/include/UI/HomePage.hpp +++ b/include/UI/HomePage.hpp @@ -8,10 +8,7 @@ #include "Page.hpp" #include "../Imgui.hpp" #include "../App/ChessBoard.hpp" - -#define CHESSBOARD_SIZE 8 -#define TILE_SIZE 70.0f -#define LABEL_SIZE 20.0f +#include "../Utils.h" namespace Stuckfish { @@ -45,18 +42,16 @@ 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; ChessBoard _board; + std::shared_ptr _selectedGameData; std::vector _gamesData; std::shared_ptr _userData; + bool _hasDisplayedGames = false; bool _hasRetrievedGames = false; }; } diff --git a/include/Utils.h b/include/Utils.h new file mode 100644 index 0000000..639052f --- /dev/null +++ b/include/Utils.h @@ -0,0 +1,9 @@ +#pragma once + +#define CHESSBOARD_SIZE 8 +#define TILE_SIZE 70.0f +#define LABEL_SIZE 20.0f + +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); diff --git a/src/App/Chessboard.cpp b/src/App/Chessboard.cpp index 0a7534f..ac2d70c 100644 --- a/src/App/Chessboard.cpp +++ b/src/App/Chessboard.cpp @@ -46,4 +46,69 @@ namespace Stuckfish _pieceTextures['k'] = LoadTextureFromFile("assets/pieces/black_king.png"); } + void ChessBoard::draw(void) + { + ImDrawList* drawList = ImGui::GetWindowDrawList(); + 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 - 300); + 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) + 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()); + } + } + } + } + + void ChessBoard::drawPieces(void) + { + ImDrawList* drawList = ImGui::GetWindowDrawList(); + int pieceRow = 0; + int pieceCol = 0; + ImVec2 boardPos = ImGui::GetCursorScreenPos(); + + for (char c : _startingPositionFen) { + if (c == '/') { + pieceRow++; + pieceCol = 0; + } + else if (isdigit(c)) { + pieceCol += c - '0'; + } + else { + ImVec2 piecePos = ImVec2(boardPos.x + pieceCol * TILE_SIZE, boardPos.y + pieceRow * TILE_SIZE - 300); + ImVec2 pieceEndPos = ImVec2(piecePos.x + TILE_SIZE, piecePos.y + TILE_SIZE); + + if (_pieceTextures.find(c) != _pieceTextures.end()) { + ImTextureID pieceTexture = _pieceTextures[c]; + drawList->AddImage(pieceTexture, piecePos, pieceEndPos); + } + pieceCol++; + } + } + } } diff --git a/src/UI/HomePage.cpp b/src/UI/HomePage.cpp index a36615b..81f4784 100644 --- a/src/UI/HomePage.cpp +++ b/src/UI/HomePage.cpp @@ -115,6 +115,7 @@ void HomePage::RenderPopup(void) ImVec2 buttonSize(confirmButtonSizeX * 3, confirmButtonSizeY); ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); + if (!_hasRetrievedGames) { // just use the current month automatically for now; @@ -145,9 +146,9 @@ void HomePage::RenderPopup(void) _gamesData = res.gamesData; _hasRetrievedGames = true; + std::reverse(_gamesData.begin(), _gamesData.end()); } - std::reverse(_gamesData.begin(), _gamesData.end()); for (const auto& game : _gamesData) { std::string buttonLabel = game.whiteUsername + "(" + game.whiteRating + ") vs " + game.blackUsername + "(" + game.blackRating + ") - " + @@ -195,70 +196,8 @@ void HomePage::RenderChessBoard(void) texturesLoaded = true; } - ImDrawList* drawList = ImGui::GetWindowDrawList(); - ImVec2 boardPos = ImGui::GetCursorScreenPos(); - - int pieceRow = 0; - int pieceCol = 0; - std::unordered_map piecesTextures = _board.getPiecesTextures(); - - /*for (auto [a, b] : piecesTextures) - { - std::cout << a << b << '\n'; - }*/ - 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 - 300); - 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()); - } - } - } - - // Draw pieces - for (char c : _board.getStartingPosition()) { - if (c == '/') { - pieceRow++; - pieceCol = 0; - } - else if (isdigit(c)) { - pieceCol += c - '0'; - } - else { - ImVec2 piecePos = ImVec2(boardPos.x + pieceCol * TILE_SIZE, boardPos.y + pieceRow * TILE_SIZE - 300); - ImVec2 pieceEndPos = ImVec2(piecePos.x + TILE_SIZE, piecePos.y + TILE_SIZE); - - if (piecesTextures.find(c) != piecesTextures.end()) { - ImTextureID pieceTexture = piecesTextures[c]; - drawList->AddImage(pieceTexture, piecePos, pieceEndPos); - } - pieceCol++; - } - } + _board.draw(); + _board.drawPieces(); } } \ No newline at end of file