Skip to content

Commit

Permalink
feat: draw pieces on the board
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanMenda committed May 31, 2024
1 parent 2e6d21a commit b68c49b
Show file tree
Hide file tree
Showing 36 changed files with 8,136 additions and 7 deletions.
15 changes: 15 additions & 0 deletions Stuckfish.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\App\Chessboard.cpp" />
<ClCompile Include="thirdparty\imgui\imgui.cpp" />
<ClCompile Include="thirdparty\imgui\imgui_demo.cpp" />
<ClCompile Include="thirdparty\imgui\imgui_draw.cpp" />
Expand All @@ -37,6 +38,7 @@
<ClCompile Include="src\UI\HomePage.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\App\ChessBoard.hpp" />
<ClInclude Include="include\Imgui.hpp" />
<ClInclude Include="thirdparty\imgui\imgui.h" />
<ClInclude Include="thirdparty\imgui\imgui_impl_glfw.h" />
Expand All @@ -52,11 +54,24 @@
<ClInclude Include="include\UI\Page.hpp" />
<ClInclude Include="include\App\Stuckfish.hpp" />
<ClInclude Include="include\UI\HomePage.hpp" />
<ClInclude Include="thirdparty\stb_image.h" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Roboto-Regular" />
</ItemGroup>
<ItemGroup>
<Image Include="assets\pieces\black_bishop.png" />
<Image Include="assets\pieces\black_king.png" />
<Image Include="assets\pieces\black_knight.png" />
<Image Include="assets\pieces\black_pawn.png" />
<Image Include="assets\pieces\black_queen.png" />
<Image Include="assets\pieces\black_rook.png" />
<Image Include="assets\pieces\white_bishop.png" />
<Image Include="assets\pieces\white_king.png" />
<Image Include="assets\pieces\white_knight.png" />
<Image Include="assets\pieces\white_pawn.png" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
Expand Down
Binary file added assets/analysis/best.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/blunder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/book.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/brilliant.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/excellent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/forced.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/good.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/great.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/inaccuracy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/analysis/mistake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/audio/capture.mp3
Binary file not shown.
Binary file added assets/audio/castle.mp3
Binary file not shown.
Binary file added assets/audio/check.mp3
Binary file not shown.
Binary file added assets/audio/game_end.mp3
Binary file not shown.
Binary file added assets/audio/move.mp3
Binary file not shown.
Binary file added assets/audio/promote.mp3
Binary file not shown.
Binary file added assets/pieces/black_bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/black_king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/black_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/black_pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/black_queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/black_rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pieces/white_rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions include/App/ChessBoard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif

#include <GLFW/glfw3.h>
#include "../Imgui.hpp"

#include <unordered_map>

namespace Stuckfish
{
class ChessBoard
{
public:
ImTextureID LoadTextureFromFile(const char* filename);
void LoadPiecesTexture();
const std::string& getStartingPosition()
{
return _startingPositionFen;
}
const std::unordered_map<char, ImTextureID>& getPiecesTextures()
{
return _pieceTextures;
}

private:
const std::string _startingPositionFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
const std::unordered_map<std::string, std::string> _moveClassificationColor = {
{ "brilliant", "#1baaa6" },
{ "great", "#5b8baf" },
{ "best", "#98bc49" },
{ "excellent", "#98bc49" },
{ "good", "#97af8b" },
{ "inaccuracy", "#f4bf44" },
{ "mistake", "#e28c28" },
{ "blunder", "#c93230" },
{ "forced", "#97af8b" },
{ "book", "#a88764" },
};
std::unordered_map<char, ImTextureID> _pieceTextures;
};
}

1 change: 1 addition & 0 deletions include/App/Logic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ namespace Stuckfish
};
}

// class to fetch the games data will be named differently
2 changes: 1 addition & 1 deletion include/Imgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "imgui_stdlib.h"
#include "imgui_stdlib.h"
3 changes: 3 additions & 0 deletions include/UI/HomePage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Page.hpp"
#include "../Imgui.hpp"
#include "../App/ChessBoard.hpp"

#define CHESSBOARD_SIZE 8
#define TILE_SIZE 70.0f
Expand Down Expand Up @@ -47,9 +48,11 @@ namespace Stuckfish
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;
Expand Down
49 changes: 49 additions & 0 deletions src/App/Chessboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "../../include/App/ChessBoard.hpp"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>

namespace Stuckfish
{
ImTextureID ChessBoard::LoadTextureFromFile(const char* filename)
{
int width, height, channels;
unsigned char* data = stbi_load(filename, &width, &height, &channels, 4); // Load image with 4 channels (RGBA)
if (data == nullptr) {
printf("Failed to load texture from file: %s\n", filename);
return nullptr;
}

printf("Loaded texture: %s, Width: %d, Height: %d, Channels: %d\n", filename, width, height, channels);

GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

stbi_image_free(data);

return (ImTextureID)(intptr_t)textureID;
}

void ChessBoard::LoadPiecesTexture()
{
_pieceTextures['P'] = LoadTextureFromFile("assets/pieces/white_pawn.png");
_pieceTextures['p'] = LoadTextureFromFile("assets/pieces/black_pawn.png");
_pieceTextures['R'] = LoadTextureFromFile("assets/pieces/white_rook.png");
_pieceTextures['r'] = LoadTextureFromFile("assets/pieces/black_rook.png");
_pieceTextures['N'] = LoadTextureFromFile("assets/pieces/white_knight.png");
_pieceTextures['n'] = LoadTextureFromFile("assets/pieces/black_knight.png");
_pieceTextures['B'] = LoadTextureFromFile("assets/pieces/white_bishop.png");
_pieceTextures['b'] = LoadTextureFromFile("assets/pieces/black_bishop.png");
_pieceTextures['Q'] = LoadTextureFromFile("assets/pieces/white_queen.png");
_pieceTextures['q'] = LoadTextureFromFile("assets/pieces/black_queen.png");
_pieceTextures['K'] = LoadTextureFromFile("assets/pieces/white_king.png");
_pieceTextures['k'] = LoadTextureFromFile("assets/pieces/black_king.png");

}
}
43 changes: 37 additions & 6 deletions src/UI/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ void HomePage::RenderPopup(void)
_hasRetrievedGames = true;
}


// reverse the list of games
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 @@ -190,19 +189,30 @@ void HomePage::RenderUsernameInputBox(bool isDisabled)

void HomePage::RenderChessBoard(void)
{
// Get the ImGui window draw list
ImDrawList* drawList = ImGui::GetWindowDrawList();
static bool texturesLoaded = false;
if (!texturesLoaded) {
_board.LoadPiecesTexture();
texturesLoaded = true;
}

// Get the top-left corner of the window where the board should start
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);
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);

Expand All @@ -228,6 +238,27 @@ void HomePage::RenderChessBoard(void)
}
}
}

// 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++;
}
}
}

}
Loading

0 comments on commit b68c49b

Please sign in to comment.