From d8557d02dfb17c9fd6c5b2a2a6391f939dfd20a3 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 6 Feb 2025 19:11:30 +0100 Subject: [PATCH] now showing target squares of legal moves --- cpp/src/view/gui.cpp | 40 +++++++++++++++++++++++++++------------- cpp/src/view/gui.hpp | 5 ++--- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/cpp/src/view/gui.cpp b/cpp/src/view/gui.cpp index 0c6c321..d5ddb67 100644 --- a/cpp/src/view/gui.cpp +++ b/cpp/src/view/gui.cpp @@ -1,5 +1,7 @@ #include "gui.hpp" +#include "../model/utils/utils.hpp" + #include #include @@ -75,25 +77,37 @@ void GUI::draw_annotation(int file, int rank) { } } -void GUI::draw_board(int selected_square, std::vector legal_moves) { - // sf::Color alt_colours[2] = { - // sf::Color(0xF6EB72), - // sf::Color(0xDCC34B) - // }; // when selected - // sf::Color circle_colours[2] = { - // sf::Color(0xCCB897), - // sf::Color(0x9E7454) - // }; // legal moves - +void GUI::draw_board(int selected_square, std::vector moves) { sf::RectangleShape square(sf::Vector2f(TILE_SIZE, TILE_SIZE)); for (int rank = 0; rank < 8; ++rank) { for (int file = 0; file < 8; ++file) { + int8_t index = Coords{file, rank}.to_index(); square.setPosition(file * TILE_SIZE, (7 - rank) * TILE_SIZE); - square.setFillColor( - (file + rank) % 2 == 0 ? colours[0] : colours[1] - ); + if (index == selected_square) + square.setFillColor( + (file + rank) % 2 == 0 ? alt_colours[0] : alt_colours[1] + ); + else + square.setFillColor( + (file + rank) % 2 == 0 ? colours[0] : colours[1] + ); window.draw(square); draw_annotation(file, rank); + + std::vector targets = to_target_square(moves); + if (std::find(targets.begin(), targets.end(), index) + != targets.end()) { + float r = .15 * TILE_SIZE; + sf::CircleShape circle{r}; + sf::Color c(0x00000055); + circle.setFillColor(c); + circle.setOrigin(r, r); + circle.setPosition( + (file + .5) * TILE_SIZE, + (7 - rank + .5) * TILE_SIZE + ); + window.draw(circle); + } } } } diff --git a/cpp/src/view/gui.hpp b/cpp/src/view/gui.hpp index ce0fdaa..718b0a2 100644 --- a/cpp/src/view/gui.hpp +++ b/cpp/src/view/gui.hpp @@ -24,9 +24,8 @@ class GUI : public View { sf::Texture textures[6][2]; sf::Sprite pieces[64]; sf::Font font; - sf::Color colours[2] = { - sf::Color(0xB88762FF), sf::Color(0xEDD6B0FF) - }; // Light and dark squares + sf::Color colours[2] = {sf::Color(0xB88762FF), sf::Color(0xEDD6B0FF)}; + sf::Color alt_colours[2] = {sf::Color(0xDCC34BFF), sf::Color(0xF6EB72FF)}; void load_textures(); void handle_events();