Skip to content

Commit 0e3e81b

Browse files
feat: GampLoop 함수 분리 및 서버 메세지 수신 테스트 로직 추가
1 parent c508bf0 commit 0e3e81b

13 files changed

Lines changed: 521 additions & 210 deletions

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ add_executable(${PROJECT_NAME} src/main.cpp
2424
src/GameLogic.cpp
2525
src/GameLoop.hpp
2626
src/GameLoop.cpp
27+
src/NetworkClient.hpp
28+
src/NetworkClient.cpp
29+
src/BoardRenderer.hpp
30+
src/BoardRenderer.cpp
31+
src/GameStateUpdater.hpp
32+
src/GameStateUpdater.cpp
33+
src/InputHandler.hpp
34+
src/InputHandler.cpp
2735
)
2836

37+
2938
# C++ 표준 설정
3039
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
3140

src/BoardRenderer.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

src/BoardRenderer.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include <SFML/Graphics.hpp>
3+
#include "GameData.hpp"
4+
5+
void drawBoardAndUI(
6+
sf::RenderWindow& window,
7+
sf::RectangleShape& tile,
8+
sf::Color& lightColor,
9+
sf::Color& darkColor,
10+
sf::Color& checkedKingTileColor,
11+
std::optional<sf::Vector2i>& selectedPiecePos,
12+
std::vector<sf::Vector2i>& possibleMoves,
13+
sf::Text& whiteTimerText,
14+
sf::Text& blackTimerText,
15+
sf::Text& messageText,
16+
std::string& gameMessageStr,
17+
sf::Text& popupMessageText,
18+
sf::RectangleShape& popupBackground,
19+
sf::RectangleShape& homeButtonShape,
20+
sf::Text& homeButtonText,
21+
GameState& currentGameState,
22+
PieceColor& currentTurn,
23+
sf::Vector2i checkedKingCurrentPos,
24+
std::array<std::array<std::optional<Piece>, 8>, 8>& board_state,
25+
sf::Text& chooseSidePromptText,
26+
sf::RectangleShape& whiteStartButton,
27+
sf::Text& whiteStartText,
28+
sf::RectangleShape& blackStartButton,
29+
sf::Text& blackStartText
30+
31+
);

src/ChessUtils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "ChessUtils.hpp"
2+
3+
std::string toChessNotation(int col, int row) {
4+
char file = 'a' + col;
5+
char rank = '8' - row;
6+
return std::string{file} + rank;
7+
}
8+
9+
std::string pieceTypeToString(PieceType type) {
10+
switch (type) {
11+
case PieceType::King: return "king";
12+
case PieceType::Queen: return "queen";
13+
case PieceType::Rook: return "rook";
14+
case PieceType::Bishop: return "bishop";
15+
case PieceType::Knight: return "knight";
16+
case PieceType::Pawn: return "pawn";
17+
default: return "unknown";
18+
}
19+
}

src/ChessUtils.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
#include <string>
3+
#include "GameData.hpp" // PieceType 정의가 필요
4+
5+
// 보드 좌표를 체스 표기법 (예: e2)으로 변환
6+
std::string toChessNotation(int col, int row);
7+
8+
// 말 타입을 문자열로 변환 (예: "king", "queen")
9+
std::string pieceTypeToString(PieceType type);

0 commit comments

Comments
 (0)