Affichage des FPS

This commit is contained in:
Mylloon 2023-12-30 22:52:38 +01:00
parent 735464f3ba
commit 16652236a0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 34 additions and 3 deletions

View file

@ -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;
}
};

View file

@ -1,13 +1,20 @@
#include "../includes/Ecran.hpp"
#include <iomanip>
#include <iostream>
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();
}
}
}
}

View file

@ -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) {