From 16652236a09e8e703eec1cf0e040a16ab9350c40 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 30 Dec 2023 22:52:38 +0100 Subject: [PATCH] Affichage des FPS --- includes/Ecran.hpp | 12 ++++++++++-- src/Ecran.cpp | 24 +++++++++++++++++++++++- src/main.cpp | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/includes/Ecran.hpp b/includes/Ecran.hpp index 3890e90..63acb1d 100644 --- a/includes/Ecran.hpp +++ b/includes/Ecran.hpp @@ -8,13 +8,16 @@ class Ecran { static std::string message; + static bool fps; + public: // Fenêtre static sf::RenderWindow window; Ecran(const uint width = 800, const uint height = 800, - const std::string name = "Projet"); // constructor - ~Ecran(); // destructor + const std::string name = "Projet", + const bool printFps = false); // constructor + ~Ecran(); // destructor void afficher( /* Fonction appellée à chaque image */ @@ -44,4 +47,9 @@ public: static void cleanMessage() { message = ""; } + + // Change l'état de l'affichage des FPS + static void toggleFps() { + fps = !fps; + } }; diff --git a/src/Ecran.cpp b/src/Ecran.cpp index 3f9af0b..b2f0312 100644 --- a/src/Ecran.cpp +++ b/src/Ecran.cpp @@ -1,13 +1,20 @@ #include "../includes/Ecran.hpp" +#include +#include + sf::RenderWindow Ecran::window; std::string Ecran::message = ""; -Ecran::Ecran(const uint w, const uint h, const std::string n) { +bool Ecran::fps = false; + +Ecran::Ecran(const uint w, const uint h, const std::string n, const bool pf) { // 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() {} @@ -24,6 +31,9 @@ void Ecran::afficher( 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)) { @@ -49,5 +59,17 @@ void Ecran::afficher( window.draw(text); window.display(); + + // Affichage des FPS + if (fps) { + 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(); + } + } } } diff --git a/src/main.cpp b/src/main.cpp index 27e1ccf..f21266e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ void help(char const *progName) { int main(int argc, char const *argv[]) { Ecran e; + Ecran::toggleFps(); // Interface cli if (argc >= 2) {