This repository has been archived on 2024-01-18. You can view files and clone it, but cannot push or open issues or pull requests.
DamesEtCo/src/Ecran.cpp

84 lines
1.9 KiB
C++

#include "../includes/Ecran.hpp"
#include <iomanip>
#include <iostream>
sf::RenderWindow Ecran::window;
std::string Ecran::message = "";
bool Ecran::fps = false;
int Ecran::count = 0;
Ecran::Ecran(const uint w, const uint h, const std::string n, const bool pf) {
if (count >= 1) {
throw std::logic_error("Uniquement 1 écran supporté.");
}
++count;
// Création de la fenêtre SFML
window.create(sf::VideoMode(w, h + bottomTxtPadding), n,
sf::Style::Titlebar | sf::Style::Close);
fps = pf;
}
Ecran::~Ecran() {
--count;
}
void Ecran::afficher(
const std::function<void()> dessin,
const std::function<void(const int, const int)> onLeftClick) const {
sf::Font font;
font.loadFromFile("assets/open-sans-latin-400-normal.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(24);
text.setPosition(3, hauteur());
text.setFillColor(sf::Color::White);
sf::Clock fpsClock;
sf::Clock printFpsclock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
// Fermeture de la fenêtre
if (event.type == sf::Event::Closed) {
window.close();
}
// Récupération des coordonnées du clic de souris
if (event.type == sf::Event::MouseButtonPressed &&
event.mouseButton.button == sf::Mouse::Left) {
onLeftClick(event.mouseButton.x, event.mouseButton.y);
}
}
window.clear();
// Appel d'une fonction dessin
dessin();
// Ecriture du message
text.setString(message);
window.draw(text);
window.display();
// Affichage des FPS
if (fps) {
const float currentTime = fpsClock.restart().asSeconds();
// Toutes les 2 secondes
if (printFpsclock.getElapsedTime().asSeconds() >= 2.f) {
std::cout << " " << std::fixed << std::setprecision(2)
<< 1.f / currentTime << std::endl;
printFpsclock.restart();
}
}
}
}