2023-11-24 20:13:20 +01:00
|
|
|
#include "../includes/Ecran.hpp"
|
|
|
|
|
2023-12-30 22:52:38 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
2024-01-06 15:54:21 +01:00
|
|
|
using namespace sf;
|
|
|
|
|
|
|
|
RenderWindow Ecran::window;
|
2023-12-01 16:36:50 +01:00
|
|
|
|
2023-12-28 17:25:36 +01:00
|
|
|
std::string Ecran::message = "";
|
|
|
|
|
2023-12-30 22:52:38 +01:00
|
|
|
bool Ecran::fps = false;
|
|
|
|
|
2023-12-30 23:02:45 +01:00
|
|
|
int Ecran::count = 0;
|
|
|
|
|
2023-12-30 22:52:38 +01:00
|
|
|
Ecran::Ecran(const uint w, const uint h, const std::string n, const bool pf) {
|
2023-12-30 23:02:45 +01:00
|
|
|
if (count >= 1) {
|
|
|
|
throw std::logic_error("Uniquement 1 écran supporté.");
|
|
|
|
}
|
|
|
|
++count;
|
|
|
|
|
2024-01-06 21:07:28 +01:00
|
|
|
sf::ContextSettings settings;
|
|
|
|
settings.antialiasingLevel = 8;
|
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
// Création de la fenêtre SFML
|
2024-01-06 15:54:21 +01:00
|
|
|
window.create(VideoMode(w, h + bottomTxtPadding), n,
|
2024-01-06 21:07:28 +01:00
|
|
|
Style::Titlebar | Style::Close, settings);
|
2023-12-30 22:52:38 +01:00
|
|
|
|
|
|
|
fps = pf;
|
2023-11-24 20:13:20 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 23:02:45 +01:00
|
|
|
Ecran::~Ecran() {
|
|
|
|
--count;
|
|
|
|
}
|
2023-11-24 20:13:20 +01:00
|
|
|
|
2023-12-27 23:59:59 +01:00
|
|
|
void Ecran::afficher(
|
|
|
|
const std::function<void()> dessin,
|
2023-12-28 00:30:44 +01:00
|
|
|
const std::function<void(const int, const int)> onLeftClick) const {
|
2024-01-06 15:54:21 +01:00
|
|
|
Font font;
|
2023-12-28 17:09:00 +01:00
|
|
|
font.loadFromFile("assets/open-sans-latin-400-normal.ttf");
|
|
|
|
|
2024-01-06 15:54:21 +01:00
|
|
|
Text text;
|
2023-12-28 17:09:00 +01:00
|
|
|
text.setFont(font);
|
|
|
|
text.setCharacterSize(24);
|
2023-12-28 18:01:28 +01:00
|
|
|
text.setPosition(3, hauteur());
|
2024-01-06 15:54:21 +01:00
|
|
|
text.setFillColor(Color::White);
|
2023-12-28 17:09:00 +01:00
|
|
|
|
2024-01-06 15:54:21 +01:00
|
|
|
Clock fpsClock;
|
|
|
|
Clock printFpsclock;
|
2023-12-30 22:52:38 +01:00
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
while (window.isOpen()) {
|
2024-01-06 15:54:21 +01:00
|
|
|
Event event;
|
2023-11-24 20:13:20 +01:00
|
|
|
while (window.pollEvent(event)) {
|
2023-12-28 00:30:44 +01:00
|
|
|
// Fermeture de la fenêtre
|
2024-01-06 15:54:21 +01:00
|
|
|
if (event.type == Event::Closed) {
|
2023-11-24 20:13:20 +01:00
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:30:44 +01:00
|
|
|
// Récupération des coordonnées du clic de souris
|
2024-01-06 15:54:21 +01:00
|
|
|
if (event.type == Event::MouseButtonPressed &&
|
|
|
|
event.mouseButton.button == Mouse::Left) {
|
2023-12-28 00:30:44 +01:00
|
|
|
onLeftClick(event.mouseButton.x, event.mouseButton.y);
|
2023-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:28:34 +01:00
|
|
|
window.clear();
|
|
|
|
|
2023-12-28 00:27:29 +01:00
|
|
|
// Appel d'une fonction dessin
|
|
|
|
dessin();
|
|
|
|
|
2023-12-28 17:09:00 +01:00
|
|
|
// Ecriture du message
|
2024-01-07 02:30:03 +01:00
|
|
|
text.setString(String::fromUtf8(message.begin(), message.end()));
|
2023-12-28 17:09:00 +01:00
|
|
|
window.draw(text);
|
|
|
|
|
2023-11-24 20:13:20 +01:00
|
|
|
window.display();
|
2023-12-30 22:52:38 +01:00
|
|
|
|
|
|
|
// Affichage des FPS
|
|
|
|
if (fps) {
|
2023-12-31 04:59:45 +01:00
|
|
|
const float currentTime = fpsClock.restart().asSeconds();
|
2023-12-30 22:52:38 +01:00
|
|
|
|
|
|
|
// Toutes les 2 secondes
|
|
|
|
if (printFpsclock.getElapsedTime().asSeconds() >= 2.f) {
|
|
|
|
std::cout << " " << std::fixed << std::setprecision(2)
|
|
|
|
<< 1.f / currentTime << std::endl;
|
|
|
|
printFpsclock.restart();
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 20:13:20 +01:00
|
|
|
}
|
|
|
|
}
|