Skip to content

Commit

Permalink
fix: reverse issue with gameData vector
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed Jun 12, 2024
1 parent b68c49b commit 9cf909f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 79 deletions.
1 change: 1 addition & 0 deletions Stuckfish.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<ItemGroup>
<ClInclude Include="include\App\ChessBoard.hpp" />
<ClInclude Include="include\Imgui.hpp" />
<ClInclude Include="include\Utils.h" />
<ClInclude Include="thirdparty\imgui\imgui.h" />
<ClInclude Include="thirdparty\imgui\imgui_impl_glfw.h" />
<ClInclude Include="thirdparty\imgui\imgui_impl_opengl3.h" />
Expand Down
13 changes: 7 additions & 6 deletions include/App/ChessBoard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../Imgui.hpp"

#include <unordered_map>
#include "../Utils.h"

namespace Stuckfish
{
Expand All @@ -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<char, ImTextureID>& getPiecesTextures()
{
return _pieceTextures;
}
{ return _pieceTextures; }

void draw(void);
void drawPieces(void);

private:
const std::string _startingPositionFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
Expand Down
11 changes: 3 additions & 8 deletions include/UI/HomePage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<GamesData> _selectedGameData;
std::vector<GamesData> _gamesData;
std::shared_ptr<UserData> _userData;

bool _hasDisplayedGames = false;
bool _hasRetrievedGames = false;
};
}
Expand Down
9 changes: 9 additions & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
@@ -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);
65 changes: 65 additions & 0 deletions src/App/Chessboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
}
}
69 changes: 4 additions & 65 deletions src/UI/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + ") - " +
Expand Down Expand Up @@ -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<char, ImTextureID> 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();
}

}

0 comments on commit 9cf909f

Please sign in to comment.