From 15321d003203e28bd6f9d448c15461838625e885 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 27 Dec 2023 23:59:59 +0100 Subject: [PATCH] =?UTF-8?q?fonction=20qui=20g=C3=A8re=20les=20click=20de?= =?UTF-8?q?=20souris,=20je=20sais=20pas=20trop=20si=20=C3=A7a=20sera=20uti?= =?UTF-8?q?le=20mais=20au=20moins=20c'est=20l=C3=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/Ecran.hpp | 9 ++++++++- src/Ecran.cpp | 22 +++++++++++++++++++++- src/main.cpp | 8 ++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/includes/Ecran.hpp b/includes/Ecran.hpp index ee139df..2ed94dc 100644 --- a/includes/Ecran.hpp +++ b/includes/Ecran.hpp @@ -11,5 +11,12 @@ struct Ecran { const std::string name = "Projet"); // constructor ~Ecran(); // destructor - void afficher(const std::function f = {[]() {}}) const; + void afficher( + /* Function called at each frame */ + const std::function drawEachFrame = {[]() {}}, + + /* Take 2 arguments: position X, position Y + * -> called each time user left-click */ + const std::function doOnClick = { + [](const int, const int) {}}) const; }; diff --git a/src/Ecran.cpp b/src/Ecran.cpp index d9bf46a..5bcd884 100644 --- a/src/Ecran.cpp +++ b/src/Ecran.cpp @@ -9,7 +9,12 @@ Ecran::Ecran(const uint w, const uint h, const std::string n) { Ecran::~Ecran() {} -void Ecran::afficher(const std::function dessin) const { +void Ecran::afficher( + const std::function dessin, + const std::function onclick) const { + // Boolean flag to track whether the mouse button is pressed + bool mouseButtonPressed = false; + while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -23,6 +28,21 @@ void Ecran::afficher(const std::function dessin) const { // Appel d'une fonction dessin dessin(); + // Récupération des coordonnées du clic de souris + if (!mouseButtonPressed) { + if (event.type == sf::Event::MouseButtonPressed && + event.mouseButton.button == sf::Mouse::Left) { + onclick(event.mouseButton.x, event.mouseButton.y); + mouseButtonPressed = true; + } + } + + // Réinitialisation du flag lorsque le bouton est relâché + if (event.type == sf::Event::MouseButtonReleased && + event.mouseButton.button == sf::Mouse::Left) { + mouseButtonPressed = false; + } + window.display(); } } diff --git a/src/main.cpp b/src/main.cpp index bfdc6de..0ab90ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,11 @@ #include "../includes/Ecran.hpp" void draw_debug(PlateauDames &p) { - p.afficherPlateau(std::cout, true); + p.afficherPlateau(std::cout, false); +} + +void click_debug(const int x, const int y) { + std::cout << "Clic souris @ (" << x << ", " << y << ")\n"; } int main() { @@ -10,7 +14,7 @@ int main() { Joueur j2; PlateauDames p(j1, j2); Ecran e; - // e.afficher(std::bind(&draw_debug, p)); + // e.afficher(std::bind(&draw_debug, p), click_debug); std::cout << "Good bye!" << std::endl; return EXIT_SUCCESS;