delta-time: title.cpp

This commit is contained in:
2025-09-16 20:23:10 +02:00
parent 470a07d28c
commit 49e30f947a
9 changed files with 114 additions and 46 deletions

View File

@@ -77,10 +77,10 @@ Title::~Title() {
Options::gamepad_manager.clearPlayers();
}
// Actualiza las variables del objeto
// Actualiza las variables del objeto (frame-based)
void Title::update() {
if (SDL_GetTicks() - ticks_ > param.game.speed) {
ticks_ = SDL_GetTicks();
if (SDL_GetTicks() - last_time_ > param.game.speed) {
last_time_ = SDL_GetTicks();
Screen::get()->update();
updateFade();
@@ -92,6 +92,28 @@ void Title::update() {
Audio::update();
}
// Actualiza las variables del objeto (time-based)
void Title::update(float deltaTime) {
Screen::get()->update();
updateFade();
updateState(deltaTime);
updateStartPrompt();
for (auto& player : players_) {
player->update(deltaTime); // deltaTime ya está en segundos
}
Audio::update();
}
// Calcula el tiempo transcurrido desde el último frame
float Title::calculateDeltaTime() {
const Uint64 current_time = SDL_GetTicks();
const float delta_time = static_cast<float>(current_time - last_time_);
last_time_ = current_time;
return delta_time;
}
// Dibuja el objeto en pantalla
void Title::render() {
static auto* const SCREEN = Screen::get();
@@ -136,7 +158,7 @@ void Title::handleDebugColorKeys(SDL_Keycode key) {
adjustColorComponent(key, color_);
counter_ = 0;
counter_time_ = 0.0f;
tiled_bg_->setColor(color_);
printColorValue(color_);
}
@@ -290,21 +312,25 @@ void Title::processPlayer2Start() {
void Title::activatePlayerAndSetState(Player::Id player_id) {
getPlayer(player_id)->setPlayingState(Player::State::TITLE_ANIMATION);
setState(TitleState::START_HAS_BEEN_PRESSED);
counter_ = 0;
counter_time_ = 0.0f;
}
// Bucle para el titulo del juego
void Title::run() {
last_time_ = SDL_GetTicks();
while (Section::name == Section::Name::TITLE) {
const float delta_time = calculateDeltaTime();
checkInput();
update();
update(delta_time);
checkEvents(); // Tiene que ir antes del render
render();
}
}
// Reinicia el contador interno
void Title::resetCounter() { counter_ = 0; }
void Title::resetCounter() { counter_time_ = 0.0f; }
// Intercambia la asignación de mandos a los jugadores
void Title::swapControllers() {
@@ -370,18 +396,55 @@ void Title::updateState() {
// Establece la lógica según el estado
switch (state_) {
case TitleState::LOGO_ANIMATING: {
game_logo_->update();
game_logo_->update(); // Mantener frame-based para consistencia del estado
if (game_logo_->hasFinished()) {
setState(TitleState::LOGO_FINISHED);
}
break;
}
case TitleState::LOGO_FINISHED: {
++counter_; // Incrementa el contador
game_logo_->update(); // Actualiza el logo con el título del juego
tiled_bg_->update(); // Actualiza el mosaico de fondo
// Ya no se usa counter_ aquí, se usa updateState(deltaTime)
game_logo_->update();
tiled_bg_->update();
if (counter_ == param.title.title_duration) {
// Esta lógica se movió a updateState(deltaTime)
break;
}
case TitleState::START_HAS_BEEN_PRESSED: {
// Ya no se usa counter_ aquí, se usa updateState(deltaTime)
game_logo_->update();
tiled_bg_->update();
// Esta lógica se movió a updateState(deltaTime)
break;
}
default:
break;
}
}
// Actualiza el estado (time-based)
void Title::updateState(float deltaTime) {
// deltaTime ya está en segundos desde calculateDeltaTime()
game_logo_->update(deltaTime);
tiled_bg_->update(deltaTime);
// Establece la lógica según el estado
switch (state_) {
case TitleState::LOGO_ANIMATING: {
if (game_logo_->hasFinished()) {
setState(TitleState::LOGO_FINISHED);
}
break;
}
case TitleState::LOGO_FINISHED: {
counter_time_ += deltaTime; // deltaTime está en milisegundos
// param.title.title_duration está en frames (60fps), convertir a ms: frames * (1000ms/60fps)
float duration_ms = static_cast<float>(param.title.title_duration) * (1000.0f / 60.0f);
if (counter_time_ >= duration_ms) {
// El menu ha hecho time out
fade_->setPostDuration(0);
fade_->activate();
@@ -390,11 +453,10 @@ void Title::updateState() {
break;
}
case TitleState::START_HAS_BEEN_PRESSED: {
++counter_; // Incrementa el contador
game_logo_->update(); // Actualiza el logo con el título del juego
tiled_bg_->update(); // Actualiza el mosaico de fondo
if (counter_ == 100) {
counter_time_ += deltaTime; // deltaTime está en milisegundos
// 100 frames a 60fps convertir a ms: 100 * (1000/60) = 1666.67 ms
if (counter_time_ >= (100.0f * 1000.0f / 60.0f)) {
fade_->activate();
}
break;
@@ -544,7 +606,7 @@ void Title::initPlayers() {
}
}
// Actualza los jugadores
// Actualiza los jugadores
void Title::updatePlayers() {
for (auto& player : players_) {
player->update();