time-based: nou DeltaTime + migrada escena Logo (constants en segons, fora counters)

This commit is contained in:
2026-05-18 21:57:31 +02:00
parent 081a7e02c7
commit f1a6636222
4 changed files with 70 additions and 33 deletions
+21 -24
View File
@@ -11,12 +11,16 @@
#include "core/rendering/screen.h" // for Screen
#include "core/rendering/sprite.h" // for Sprite
#include "core/resources/resource.h"
#include "game/defaults.hpp" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG...
#include "utils/utils.h" // for Section, Color
#include "core/system/delta_time.hpp" // for DeltaTime::reset / tick
#include "game/defaults.hpp" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG...
#include "utils/utils.h" // for Section, Color
// Valores de inicialización y fin
constexpr int INIT_FADE = 100;
constexpr int END_LOGO = 200;
// Durades de l'escena (segons). Time-based: ja no comptem frames. Valors
// equivalents al comportament anterior (frame counter a 15ms): 100 i 200
// frames ⇒ 1.5s i 3.0s; fi a 220 frames ⇒ 3.3s.
constexpr float FADE_START_S = 1.5F;
constexpr float FADE_END_S = 3.0F;
constexpr float SCENE_END_S = 3.3F;
// Constructor
Logo::Logo(SDL_Renderer *renderer, Section *section) {
@@ -30,13 +34,14 @@ Logo::Logo(SDL_Renderer *renderer, Section *section) {
sprite_ = new Sprite(14, 75, 226, 44, texture_, renderer);
// Inicializa variables
counter_ = 0;
section->name = SECTION_PROG_LOGO;
section->subsection = 0;
ticks_ = 0;
ticks_speed_ = 15;
Audio::get()->stopMusic();
// Reset del rellotge: la primera crida a tick() retornarà ~0 i no un
// delta gegant arrossegat des del boot o l'escena anterior.
DeltaTime::reset();
}
// Destructor
@@ -48,7 +53,7 @@ Logo::~Logo() {
// Comprueba si ha terminado el logo
void Logo::checkLogoEnd() {
if (counter_ >= END_LOGO + 20) {
if (elapsed_time_s_ >= SCENE_END_S) {
section_->name = SECTION_PROG_INTRO;
section_->subsection = 0;
}
@@ -68,9 +73,8 @@ void Logo::checkInput() {
// Dibuja el fade
void Logo::renderFade() {
// Dibuja el fade
if (counter_ >= INIT_FADE) {
const float STEP = (float)(counter_ - INIT_FADE) / (float)(END_LOGO - INIT_FADE);
if (elapsed_time_s_ >= FADE_START_S) {
const float STEP = (elapsed_time_s_ - FADE_START_S) / (FADE_END_S - FADE_START_S);
const int ALPHA = std::min((int)(255 * STEP), 255);
SDL_SetRenderDrawColor(renderer_, BG_COLOR.r, BG_COLOR.g, BG_COLOR.b, ALPHA);
SDL_RenderFillRect(renderer_, nullptr);
@@ -78,20 +82,12 @@ void Logo::renderFade() {
}
// Actualiza las variables del objeto
void Logo::update() {
void Logo::update(float delta_time_s) {
Audio::update();
checkInput();
if (SDL_GetTicks() - ticks_ > ticks_speed_) {
// Actualiza el contador de ticks
ticks_ = SDL_GetTicks();
// Actualiza el contador
counter_++;
// Comprueba si ha terminado el logo
checkLogoEnd();
}
elapsed_time_s_ += delta_time_s;
checkLogoEnd();
}
// Dibuja el objeto en pantalla
@@ -123,7 +119,8 @@ void Logo::run() {
// Ejecuta un frame
void Logo::iterate() {
update();
const float DELTA_TIME_S = DeltaTime::tick();
update(DELTA_TIME_S);
render();
}