From c26a4774a1cceb3971ef35ef13bd14889109cd67 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 2 Dec 2025 09:09:22 +0100 Subject: [PATCH] afegit comptador de frames per segon --- source/core/rendering/sdl_manager.cpp | 38 +++++++++++++++++++++++++++ source/core/rendering/sdl_manager.hpp | 12 +++++++++ source/game/escenes/escena_joc.cpp | 3 +++ source/game/escenes/escena_logo.cpp | 3 +++ source/game/escenes/escena_logo.hpp | 2 +- 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/source/core/rendering/sdl_manager.cpp b/source/core/rendering/sdl_manager.cpp index 1e3dada..114470b 100644 --- a/source/core/rendering/sdl_manager.cpp +++ b/source/core/rendering/sdl_manager.cpp @@ -15,6 +15,9 @@ SDLManager::SDLManager() : finestra_(nullptr), renderer_(nullptr), + fps_accumulator_(0.0f), + fps_frame_count_(0), + fps_display_(0), current_width_(Defaults::Window::WIDTH), current_height_(Defaults::Window::HEIGHT), is_fullscreen_(false), @@ -69,6 +72,9 @@ SDLManager::SDLManager() SDLManager::SDLManager(int width, int height, bool fullscreen) : finestra_(nullptr), renderer_(nullptr), + fps_accumulator_(0.0f), + fps_frame_count_(0), + fps_display_(0), current_width_(width), current_height_(height), is_fullscreen_(fullscreen), @@ -305,3 +311,35 @@ void SDLManager::updateColors(float delta_time) { // Actualitzar color global de línies Rendering::setLineColor(color_oscillator_.getCurrentLineColor()); } + +// [NUEVO] Actualitzar comptador de FPS +void SDLManager::updateFPS(float delta_time) { + // Acumular temps i frames + fps_accumulator_ += delta_time; + fps_frame_count_++; + + // Actualitzar display cada 0.5 segons + if (fps_accumulator_ >= 0.5f) { + fps_display_ = static_cast(fps_frame_count_ / fps_accumulator_); + fps_frame_count_ = 0; + fps_accumulator_ = 0.0f; + + // Actualitzar títol de la finestra + std::string title = std::format("{} v{} ({}) - {} FPS", + Project::LONG_NAME, + Project::VERSION, + Project::COPYRIGHT, + fps_display_); + + if (finestra_) { + SDL_SetWindowTitle(finestra_, title.c_str()); + } + } +} + +// [NUEVO] Actualitzar títol de la finestra +void SDLManager::setWindowTitle(const std::string& title) { + if (finestra_) { + SDL_SetWindowTitle(finestra_, title.c_str()); + } +} diff --git a/source/core/rendering/sdl_manager.hpp b/source/core/rendering/sdl_manager.hpp index 2a0ecb7..d49eac2 100644 --- a/source/core/rendering/sdl_manager.hpp +++ b/source/core/rendering/sdl_manager.hpp @@ -7,6 +7,7 @@ #include #include +#include #include "core/rendering/color_oscillator.hpp" @@ -35,13 +36,24 @@ class SDLManager { // [NUEVO] Actualització de colors (oscil·lació) void updateColors(float delta_time); + // [NUEVO] Actualitzar comptador de FPS + void updateFPS(float delta_time); + // Getters SDL_Renderer* obte_renderer() { return renderer_; } + // [NUEVO] Actualitzar títol de la finestra + void setWindowTitle(const std::string& title); + private: SDL_Window* finestra_; SDL_Renderer* renderer_; + // [NUEVO] Variables FPS + float fps_accumulator_; + int fps_frame_count_; + int fps_display_; + // [NUEVO] Estat de la finestra int current_width_; // Mida física actual int current_height_; diff --git a/source/game/escenes/escena_joc.cpp b/source/game/escenes/escena_joc.cpp index 59adc8a..bdff059 100644 --- a/source/game/escenes/escena_joc.cpp +++ b/source/game/escenes/escena_joc.cpp @@ -51,6 +51,9 @@ void EscenaJoc::executar() { delta_time = 0.05f; } + // Actualitzar comptador de FPS + sdl_.updateFPS(delta_time); + // Processar events SDL while (SDL_PollEvent(&event)) { // Manejo de finestra diff --git a/source/game/escenes/escena_logo.cpp b/source/game/escenes/escena_logo.cpp index 8f450e0..4d6a8f7 100644 --- a/source/game/escenes/escena_logo.cpp +++ b/source/game/escenes/escena_logo.cpp @@ -60,6 +60,9 @@ void EscenaLogo::executar() { delta_time = 0.05f; } + // Actualitzar comptador de FPS + sdl_.updateFPS(delta_time); + // Processar events SDL while (SDL_PollEvent(&event)) { // Manejo de finestra diff --git a/source/game/escenes/escena_logo.hpp b/source/game/escenes/escena_logo.hpp index db1e468..6cb5f06 100644 --- a/source/game/escenes/escena_logo.hpp +++ b/source/game/escenes/escena_logo.hpp @@ -56,7 +56,7 @@ class EscenaLogo { static constexpr float DURACIO_ZOOM = 4.0f; // Duració del zoom (segons) static constexpr float DURACIO_POST_ANIMATION = 3.0f; // Duració POST_ANIMATION (logo complet) static constexpr float DURACIO_POST_EXPLOSION = 3.0f; // Duració POST_EXPLOSION (espera final) - static constexpr float DELAY_ENTRE_EXPLOSIONS = 1.0f; // Temps entre explosions de lletres + static constexpr float DELAY_ENTRE_EXPLOSIONS = 0.15f; // Temps entre explosions de lletres static constexpr float VELOCITAT_EXPLOSIO = 80.0f; // Velocitat base fragments (px/s) static constexpr float ESCALA_INICIAL = 0.1f; // Escala inicial (10%) static constexpr float ESCALA_FINAL = 0.8f; // Escala final (80%)