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;