Skip to content

Commit e6c7082

Browse files
Merge pull request #168 from Longwater1234/dev
Now clicked cell is also highlighted
2 parents bcfc711 + b020b34 commit e6c7082

File tree

12 files changed

+100
-62
lines changed

12 files changed

+100
-62
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- [ ] (Online mode) Display countdown timer (40 s), waiting for player's move. Reset it when they make a move.
44
- [ ] Record and list all previous moves and captures (for current match), in a scroll panel.
5-
- [ ] highlight cell color blue after clicking a piece, reset when click srcCell = null
5+
- [x] highlight cell color blue after clicking a piece, reset when click srcCell = null
66
- [ ] write unit tests using googletests
77

88
### (Extras) Standard CJK font paths

src/Cell.hpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,46 @@ namespace chk
1414
class Cell final : public sf::Drawable
1515
{
1616
public:
17-
explicit Cell(int idx, const sf::RectangleShape &rec, const sf::Vector2f &pos, const sf::Font &font);
17+
explicit Cell(int idx, const sf::RectangleShape &rec, const sf::Font &font);
1818
Cell() = delete;
1919
Cell &operator=(const Cell &) = delete;
2020
Cell(const Cell &) = delete;
2121
bool containsPoint(const sf::Vector2i &pos) const;
2222
bool isAtPosition(const sf::Vector2f &pos) const;
2323
const sf::Vector2f &getPos() const;
2424
int getIndex() const;
25-
void setEvenRow(bool is_even);
25+
void setEvenRow(bool val);
2626
bool getIsEvenRow() const;
27+
void highlightActive();
28+
void resetColor();
2729

2830
private:
2931
sf::RectangleShape rec;
30-
int index; // Darker cells have index in range [1~32]. Lighter cells are all -1
32+
sf::Color DARK_BROWN = sf::Color{82, 55, 27};
33+
sf::Color BABY_BLUE = sf::Color{98, 174, 239};
34+
int index = -1; // Darker cells have index in range [1~32]. Lighter cells are all -1
3135
bool isEvenRow = false;
3236
sf::Vector2f cell_pos;
3337
sf::Text sfText;
3438
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
3539
};
3640

37-
inline Cell::Cell(const int idx, const sf::RectangleShape &rect, const sf::Vector2f &pos, const sf::Font &font)
41+
inline Cell::Cell(const int idx, const sf::RectangleShape &rect, const sf::Font &font)
3842
{
3943
this->rec = rect;
4044
this->index = idx;
41-
this->cell_pos = pos;
45+
this->cell_pos = rec.getPosition();
4246

4347
sf::Text text;
4448
text.setFont(font);
4549
text.setFillColor(sf::Color{255, 255, 255, 100});
4650
text.setString(std::to_string(this->index));
47-
text.setPosition(pos);
51+
text.setPosition(this->cell_pos);
4852
this->sfText = text;
4953
}
5054

5155
inline void Cell::draw(sf::RenderTarget &target, sf::RenderStates states) const
5256
{
53-
5457
target.draw(rec, states);
5558
if (this->index != -1)
5659
{
@@ -69,7 +72,8 @@ inline bool Cell::getIsEvenRow() const
6972
}
7073

7174
/**
72-
* Check whether mouse cursor is anywhere within this Cell region
75+
* Check whether mouse cursor is anywhere INSIDE this Cell region
76+
*
7377
* @param pos position x,y (int) relative to main Window
7478
* @return TRUE or FALSE
7579
*/
@@ -80,6 +84,7 @@ inline bool Cell::containsPoint(const sf::Vector2i &pos) const
8084

8185
/**
8286
* Check whether given object's position is EQUAL to this Cell's position
87+
*
8388
* @param pos position x,y (float) relative to main window
8489
* @return TRUE or FALSE
8590
*/
@@ -99,11 +104,11 @@ inline int Cell::getIndex() const
99104

100105
/**
101106
* Set whether this cell's row is even
102-
* @param is_even TRUE or FALSE
107+
* @param val TRUE or FALSE
103108
*/
104-
inline void Cell::setEvenRow(bool is_even)
109+
inline void Cell::setEvenRow(bool val)
105110
{
106-
this->isEvenRow = is_even;
111+
this->isEvenRow = val;
107112
}
108113

109114
/**
@@ -115,4 +120,20 @@ const inline sf::Vector2f &Cell::getPos() const
115120
return this->cell_pos;
116121
}
117122

123+
/**
124+
* (ONLY FOR PLAYABLE CELLS) Highlight the currently clicked cell with a piece with BLUE
125+
*/
126+
inline void Cell::highlightActive()
127+
{
128+
this->rec.setFillColor(BABY_BLUE);
129+
}
130+
131+
/**
132+
* Restore the original color
133+
*/
134+
inline void Cell::resetColor()
135+
{
136+
this->rec.setFillColor(DARK_BROWN);
137+
}
138+
118139
} // namespace chk

src/GameManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GameManager
5454
bool playerRedTurn = true;
5555
// bottom display message
5656
mutable std::string currentMsg;
57-
// whether match is over
57+
// whether match is over (for offline play only)
5858
bool gameOver = false;
5959
// used for atomic updates
6060
std::mutex my_mutex;

src/MainMenu.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace chk
2020
{
2121
constexpr auto ICON_PATH = "win-icon-16.png";
2222
constexpr auto FONT_PATH = "notosans-regular.ttf";
23+
constexpr unsigned FONT_SIZE{16};
2324

2425
enum class UserChoice
2526
{

src/Piece.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#include <SFML/Graphics/RenderStates.hpp>
66
#include <SFML/Graphics/RenderTarget.hpp>
77
#include <SFML/Graphics/Texture.hpp>
8+
#include <iostream>
89

910
namespace chk
1011
{
1112
enum class PieceType
1213
{
13-
Red = 69995,
14+
Red = 67895,
1415
Black,
1516
};
1617

src/WsClient.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ chk::WsClient::WsClient()
1616
this->webSocketPtr->setPingInterval(30);
1717
ix::SocketTLSOptions tlsOptions;
1818
#ifndef _WIN32
19-
// Currently system CAs are not supported on non-Windows platforms with mbedtls
19+
// Currently system CAs are not supported on non-Windows platforms with
20+
// mbedtls
2021
tlsOptions.caFile = "NONE";
2122
#endif // _WIN32
2223
this->webSocketPtr->setTLSOptions(tlsOptions);
@@ -200,9 +201,9 @@ void WsClient::runMainLoop()
200201

201202
else {
202203
// already connected
203-
this->runServerLoop();
204+
this->runServerLoop();
204205
}
205-
206+
206207
// some error happened 🙁
207208
if (this->isDead) {
208209
if (this->_onDeathCallback != nullptr) {
@@ -212,11 +213,10 @@ void WsClient::runMainLoop()
212213
} else if (this->haveWinner) {
213214
this->showWinnerPopup();
214215
}
215-
// clang-format on
216216
}
217217

218218
/**
219-
* Try to connect to Server
219+
* Try to connect to Server.
220220
* @param address server IP or URI
221221
*/
222222
void WsClient::tryConnect(std::string_view address)
@@ -282,7 +282,7 @@ void WsClient::setOnReadyConnectedCallback(const onConnectedServer &callback)
282282
}
283283

284284
/**
285-
* Set the callback to handle starting game after signal from server
285+
* Set the callback to handle starting game after signal from server.
286286
* @param callback the callback function
287287
*/
288288
void WsClient::setOnReadyStartGameCallback(const onReadyStartGame &callback)
@@ -291,7 +291,7 @@ void WsClient::setOnReadyStartGameCallback(const onReadyStartGame &callback)
291291
}
292292

293293
/**
294-
* Set the callback to handle connection failures or server kickouts
294+
* Set the callback to handle connection failures or server kickouts.
295295
* @param callback the callback function
296296
*/
297297
void WsClient::setOnDeathCallback(const onDeathCallback &callback)
@@ -300,7 +300,7 @@ void WsClient::setOnDeathCallback(const onDeathCallback &callback)
300300
}
301301

302302
/**
303-
* Set the callback for handling Opponent moving their piece
303+
* Set the callback for handling Opponent moving their piece.
304304
* @param callback the callback function
305305
*/
306306
void WsClient::setOnMovePieceCallback(const onMovePieceCallback &callback)
@@ -309,7 +309,7 @@ void WsClient::setOnMovePieceCallback(const onMovePieceCallback &callback)
309309
}
310310

311311
/**
312-
* Set the callback for handling Opponent capturing my Piece
312+
* Set the callback for handling Opponent capturing my Piece.
313313
* @param callback the callback function
314314
*/
315315
void WsClient::setOnCapturePieceCallback(const onCaptureCallback &callback)
@@ -318,7 +318,7 @@ void WsClient::setOnCapturePieceCallback(const onCaptureCallback &callback)
318318
}
319319

320320
/**
321-
* Set the callback for handling Winner or Loser of match
321+
* Set the callback for handling Winner or Loser of match.
322322
* @param callback the callback function
323323
*/
324324
void WsClient::setOnWinLoseCallback(const onWinLoseCallback &callback)
@@ -327,7 +327,8 @@ void WsClient::setOnWinLoseCallback(const onWinLoseCallback &callback)
327327
}
328328

329329
/**
330-
* Send Protobuf response back to server
330+
* Send Protobuf response back to server.
331+
*
331332
* @param payload the request body
332333
* @return TRUE if sent successfully, else FALSE
333334
*/
@@ -347,7 +348,8 @@ bool WsClient::replyServer(const chk::payload::BasePayload &payload) const
347348
}
348349

349350
/**
350-
* Exchange messages with the server and update the game accordingly. if any error happen, close connection
351+
* Exchange messages with the server and update the game accordingly. if any
352+
* error happens, close connection
351353
*/
352354
void WsClient::runServerLoop()
353355
{
@@ -360,15 +362,14 @@ void WsClient::runServerLoop()
360362
chk::payload::BasePayload basePayload;
361363
if (!basePayload.ParseFromString(msg))
362364
{
363-
std::scoped_lock lg(this->mut);
365+
std::scoped_lock lg{this->mut};
364366
this->deathNote = "Profobuf: Could not parse payload";
365367
this->isDead = true;
366368
return;
367369
}
368370

369371
if (basePayload.has_welcome())
370372
{
371-
/* code */
372373
chk::payload::WelcomePayload welcome = basePayload.welcome();
373374
if (this->_onReadyConnected != nullptr)
374375
{
@@ -414,7 +415,7 @@ void WsClient::runServerLoop()
414415
if (this->_onWinLoseCallback != nullptr)
415416
{
416417
this->_onWinLoseCallback(basePayload.notice());
417-
std::scoped_lock lg(this->mut);
418+
std::scoped_lock lg{this->mut};
418419
this->deathNote = basePayload.notice();
419420
this->haveWinner = true;
420421
}

src/WsClient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class WsClient final
5656
std::atomic_bool haveWinner{false}; // whether server returned Winner or Loser
5757
std::atomic_bool isConnected{false}; // if done connected to server (else, show loading)
5858
chk::CircularBuffer<std::string> msgBuffer{1}; // keep only recent 1 incoming message
59-
mutable std::string deathNote; // reason from server for disconnected (KICKED or WIN or LOSE)
59+
mutable std::string deathNote; // reason from server for disconnecting
6060
mutable std::string protoBucket; // REUSABLE container to store OUTGOING protobuf
6161
std::atomic_bool connClicked = false; // if 'connect' button clicked
6262
std::vector<chk::ServerLocation> publicServers; // list of public servers (fetched from CDN)

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main()
2323
std::unique_ptr<chk::GameManager> manager = nullptr;
2424

2525
// SHOW MAIN MENU
26-
chk::MainMenu homeMenu(&window);
26+
chk::MainMenu homeMenu{&window};
2727
const auto userChoice = homeMenu.runMainLoop();
2828
if (userChoice == chk::UserChoice::ONLINE_PLAY)
2929
{
@@ -37,7 +37,7 @@ int main()
3737
// LOAD FONT for IMGUI
3838
ImGuiIO &io = ImGui::GetIO();
3939
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
40-
ImFont *imfont = io.Fonts->AddFontFromFileTTF(chk::getResourcePath(chk::FONT_PATH).c_str(), 16);
40+
ImFont *imfont = io.Fonts->AddFontFromFileTTF(chk::getResourcePath(chk::FONT_PATH).c_str(), chk::FONT_SIZE);
4141
IM_ASSERT(imfont != nullptr);
4242
ImGui::SFML::UpdateFontTexture();
4343

@@ -66,7 +66,7 @@ int main()
6666
chk::CircularBuffer<short> circularBuffer{1};
6767

6868
// THE STATUS TEXT
69-
sf::Text txtPanel{"Space Checkers", font, 16};
69+
sf::Text txtPanel{"Space Checkers", font, chk::FONT_SIZE};
7070
txtPanel.setFillColor(sf::Color::White);
7171
txtPanel.setPosition(sf::Vector2f{10.0f, 8.5 * chk::SIZE_CELL});
7272
manager->updateMessage("Welcome to Space Checkers");

0 commit comments

Comments
 (0)