1+ #include " BoardRenderer.hpp"
2+ #include < SFML/Graphics.hpp>
3+
4+ void drawBoardAndUI (
5+ sf::RenderWindow& window,
6+ sf::RectangleShape& tile,
7+ sf::Color& lightColor,
8+ sf::Color& darkColor,
9+ sf::Color& checkedKingTileColor,
10+ std::optional<sf::Vector2i>& selectedPiecePos,
11+ std::vector<sf::Vector2i>& possibleMoves,
12+ sf::Text& whiteTimerText,
13+ sf::Text& blackTimerText,
14+ sf::Text& messageText,
15+ std::string& gameMessageStr,
16+ sf::Text& popupMessageText,
17+ sf::RectangleShape& popupBackground,
18+ sf::RectangleShape& homeButtonShape,
19+ sf::Text& homeButtonText,
20+ GameState& currentGameState,
21+ PieceColor& currentTurn,
22+ sf::Vector2i checkedKingCurrentPos,
23+ std::array<std::array<std::optional<Piece>, 8 >, 8 >& board_state,
24+ sf::Text& chooseSidePromptText,
25+ sf::RectangleShape& whiteStartButton,
26+ sf::Text& whiteStartText,
27+ sf::RectangleShape& blackStartButton,
28+ sf::Text& blackStartText
29+ ) {
30+ window.clear (sf::Color (240 ,240 ,240 ));
31+
32+ window.clear (sf::Color (240 ,240 ,240 ));
33+
34+ if (currentGameState == GameState::ChoosingPlayer) {
35+ window.draw (chooseSidePromptText);
36+ window.draw (whiteStartButton); window.draw (whiteStartText);
37+ window.draw (blackStartButton); window.draw (blackStartText);
38+ }
39+ else {
40+ for (int r = 0 ; r < 8 ; ++r) for (int c = 0 ; c < 8 ; ++c) {
41+ bool isLight = (r + c) % 2 == 0 ;
42+ tile.setFillColor (isLight ? lightColor : darkColor);
43+ if (checkedKingCurrentPos.x == c && checkedKingCurrentPos.y == r)
44+ tile.setFillColor (checkedKingTileColor);
45+ else if (selectedPiecePos && selectedPiecePos->x == c && selectedPiecePos->y == r)
46+ tile.setFillColor (sf::Color (255 , 255 , 0 , 150 ));
47+ else {
48+ for (const auto & move : possibleMoves) {
49+ if (move.x == c && move.y == r) {
50+ if (board_state[r][c] && board_state[r][c]->color != currentTurn)
51+ tile.setFillColor (sf::Color (50 , 150 , 250 , 150 ));
52+ else
53+ tile.setFillColor (sf::Color (100 , 250 , 50 , 150 ));
54+ break ;
55+ }
56+ }
57+ }
58+ tile.setPosition ({c * static_cast <float >(TILE_SIZE), r * static_cast <float >(TILE_SIZE)});
59+ window.draw (tile);
60+ }
61+
62+ for (int r = 0 ; r < 8 ; ++r) for (int c = 0 ; c < 8 ; ++c) {
63+ if (board_state[r][c]) {
64+ Piece piece = board_state[r][c].value ();
65+ bool isLosingKing = (currentGameState == GameState::GameOver &&
66+ piece.type == PieceType::King &&
67+ piece.color == currentTurn);
68+ piece.sprite .setColor (isLosingKing ? sf::Color::Red : sf::Color::White);
69+ window.draw (piece.sprite );
70+ }
71+ }
72+
73+ window.draw (whiteTimerText);
74+ window.draw (blackTimerText);
75+
76+ if (!gameMessageStr.empty ()) {
77+ messageText.setString (gameMessageStr);
78+ sf::FloatRect msgBounds = messageText.getLocalBounds ();
79+ messageText.setPosition ({BOARD_WIDTH + (BUTTON_PANEL_WIDTH - msgBounds.size .x ) / 2 .f - msgBounds.position .x ,
80+ (WINDOW_HEIGHT - msgBounds.size .y ) / 2 .f - msgBounds.position .y });
81+ messageText.setFillColor (sf::Color::Black);
82+ messageText.setStyle (sf::Text::Regular);
83+ if (currentGameState == GameState::GameOver || gameMessageStr.find (" wins by" ) != std::string::npos)
84+ messageText.setFillColor (sf::Color::Red), messageText.setStyle (sf::Text::Bold);
85+ else if (gameMessageStr.find (" Check!" ) != std::string::npos)
86+ messageText.setFillColor (sf::Color (200 ,0 ,0 )), messageText.setStyle (sf::Text::Bold);
87+
88+ window.draw (messageText);
89+ }
90+
91+ if (currentGameState == GameState::GameOver) {
92+ window.draw (popupBackground);
93+ popupMessageText.setString (gameMessageStr);
94+ sf::FloatRect popupMsgBounds = popupMessageText.getLocalBounds ();
95+ popupMessageText.setPosition ({popupBackground.getPosition ().x - popupMsgBounds.size .x / 2 .f - popupMsgBounds.position .x ,
96+ popupBackground.getPosition ().y - popupBackground.getSize ().y / 2 .f + 30 .f - popupMsgBounds.position .y });
97+ window.draw (popupMessageText);
98+
99+ homeButtonShape.setPosition ({popupBackground.getPosition ().x ,
100+ popupBackground.getPosition ().y + popupBackground.getSize ().y / 2 .f - 40 .f - homeButtonShape.getSize ().y / 2 .f });
101+ window.draw (homeButtonShape);
102+
103+ sf::FloatRect homeTextBounds = homeButtonText.getLocalBounds ();
104+ homeButtonText.setPosition ({homeButtonShape.getPosition ().x - homeTextBounds.size .x / 2 .f - homeTextBounds.position .x ,
105+ homeButtonShape.getPosition ().y - homeTextBounds.size .y / 2 .f - homeTextBounds.position .y });
106+ window.draw (homeButtonText);
107+ }
108+ }
109+
110+ window.display ();
111+ }
0 commit comments