2023-11-24 20:13:20 +01:00
|
|
|
#include "../includes/Ecran.hpp"
|
|
|
|
|
2023-12-01 16:36:50 +01:00
|
|
|
sf::RenderWindow Ecran::window;
|
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
Ecran::Ecran(const uint w, const uint h, const std::string n) {
|
|
|
|
// Création de la fenêtre SFML
|
|
|
|
window.create(sf::VideoMode(w, h), n);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ecran::~Ecran() {}
|
|
|
|
|
2023-12-27 23:59:59 +01:00
|
|
|
void Ecran::afficher(
|
|
|
|
const std::function<void()> dessin,
|
2023-12-28 00:27:13 +01:00
|
|
|
const std::function<void(const int, const int)> onClick) const {
|
2023-12-28 00:01:59 +01:00
|
|
|
// Flag qui track si le clic gauche est pressé
|
2023-12-27 23:59:59 +01:00
|
|
|
bool mouseButtonPressed = false;
|
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
while (window.isOpen()) {
|
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
if (event.type == sf::Event::Closed) {
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:28:34 +01:00
|
|
|
/****************
|
|
|
|
* Evenements *
|
|
|
|
***************/
|
2023-11-24 20:13:20 +01:00
|
|
|
|
2023-12-27 23:59:59 +01:00
|
|
|
// Récupération des coordonnées du clic de souris
|
|
|
|
if (!mouseButtonPressed) {
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed &&
|
|
|
|
event.mouseButton.button == sf::Mouse::Left) {
|
2023-12-28 00:27:13 +01:00
|
|
|
onClick(event.mouseButton.x, event.mouseButton.y);
|
2023-12-27 23:59:59 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:28:34 +01:00
|
|
|
/****************
|
|
|
|
* Dessin *
|
|
|
|
***************/
|
|
|
|
|
|
|
|
window.clear();
|
|
|
|
|
2023-12-28 00:27:29 +01:00
|
|
|
// Appel d'une fonction dessin
|
|
|
|
dessin();
|
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
}
|