delta-time: tiled_bg.cpp
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
#include <SDL3/SDL.h> // Para SDL_SetRenderTarget, SDL_CreateTexture, SDL_DestroyTexture, SDL_FRect, SDL_GetRenderTarget, SDL_RenderTexture, SDL_PixelFormat, SDL_TextureAccess
|
#include <SDL3/SDL.h> // Para SDL_SetRenderTarget, SDL_CreateTexture, SDL_DestroyTexture, SDL_FRect, SDL_GetRenderTarget, SDL_RenderTexture, SDL_PixelFormat, SDL_TextureAccess
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath> // Para sin
|
#include <cmath> // Para sin, pow
|
||||||
#include <cstdlib> // Para rand
|
#include <cstdlib> // Para rand
|
||||||
#include <memory> // Para allocator, unique_ptr, make_unique
|
#include <memory> // Para allocator, unique_ptr, make_unique
|
||||||
#include <numbers> // Para pi
|
#include <numbers> // Para pi
|
||||||
@@ -81,15 +81,9 @@ void TiledBG::render() {
|
|||||||
SDL_RenderTexture(renderer_, canvas_, &window_, &pos_);
|
SDL_RenderTexture(renderer_, canvas_, &window_, &pos_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza la lógica de la clase (compatibilidad)
|
// Actualiza la lógica de la clase (frame-based)
|
||||||
void TiledBG::update() {
|
void TiledBG::update() {
|
||||||
constexpr float FRAME_TIME_MS = 1000.0f / 60.0f; // 16.67ms por frame a 60 FPS
|
updateDesp();
|
||||||
update(FRAME_TIME_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Actualiza la lógica de la clase
|
|
||||||
void TiledBG::update(float delta_time) {
|
|
||||||
updateDesp(delta_time);
|
|
||||||
updateStop();
|
updateStop();
|
||||||
|
|
||||||
switch (mode_) {
|
switch (mode_) {
|
||||||
@@ -113,7 +107,33 @@ void TiledBG::update(float delta_time) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detiene el desplazamiento de forma ordenada
|
// Actualiza la lógica de la clase (time-based)
|
||||||
|
void TiledBG::update(float delta_time) {
|
||||||
|
updateDesp(delta_time);
|
||||||
|
updateStop(delta_time);
|
||||||
|
|
||||||
|
switch (mode_) {
|
||||||
|
case TiledBGMode::DIAGONAL: {
|
||||||
|
// El tileado de fondo se desplaza en diagonal
|
||||||
|
window_.x = static_cast<int>(desp_) % TILE_WIDTH;
|
||||||
|
window_.y = static_cast<int>(desp_) % TILE_HEIGHT;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TiledBGMode::CIRCLE: {
|
||||||
|
// El tileado de fondo se desplaza en circulo
|
||||||
|
const int INDEX = static_cast<int>(desp_) % 360;
|
||||||
|
|
||||||
|
window_.x = 128 + (static_cast<int>(sin_[(INDEX + 270) % 360] * 128));
|
||||||
|
window_.y = 128 + (static_cast<int>(sin_[(360 - INDEX) % 360] * 96));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detiene el desplazamiento de forma ordenada (frame-based)
|
||||||
void TiledBG::updateStop() {
|
void TiledBG::updateStop() {
|
||||||
if (stopping_) {
|
if (stopping_) {
|
||||||
const int UMBRAL = 20 * speed_; // Ajusta este valor según la precisión deseada
|
const int UMBRAL = 20 * speed_; // Ajusta este valor según la precisión deseada
|
||||||
@@ -126,6 +146,29 @@ void TiledBG::updateStop() {
|
|||||||
speed_ = std::max(speed_, 0.1F);
|
speed_ = std::max(speed_, 0.1F);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Si estamos en 0, detener
|
||||||
|
if (window_.x == 0) {
|
||||||
|
speed_ = 0.0F;
|
||||||
|
stopping_ = false; // Desactivamos el estado de "stopping"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detiene el desplazamiento de forma ordenada (time-based)
|
||||||
|
void TiledBG::updateStop(float delta_time) {
|
||||||
|
if (stopping_) {
|
||||||
|
const int UMBRAL = 20 * speed_; // Ajusta este valor según la precisión deseada
|
||||||
|
|
||||||
|
// Desacelerar si estamos cerca de completar el ciclo (ventana a punto de regresar a 0)
|
||||||
|
if (window_.x >= TILE_WIDTH - UMBRAL) {
|
||||||
|
// Convertir 1.05F por frame a por segundo: (1.05^60) per second
|
||||||
|
float deceleration_factor = std::pow(1.05F, 60.0F * delta_time);
|
||||||
|
speed_ /= deceleration_factor;
|
||||||
|
|
||||||
|
// Asegura que no baje demasiado
|
||||||
|
speed_ = std::max(speed_, 0.1F);
|
||||||
|
}
|
||||||
|
|
||||||
// Si estamos en 0, detener
|
// Si estamos en 0, detener
|
||||||
if (window_.x == 0) {
|
if (window_.x == 0) {
|
||||||
speed_ = 0.0F;
|
speed_ = 0.0F;
|
||||||
|
|||||||
@@ -55,8 +55,9 @@ class TiledBG {
|
|||||||
bool stopping_ = false; // Indica si se está deteniendo
|
bool stopping_ = false; // Indica si se está deteniendo
|
||||||
|
|
||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
void fillTexture(); // Rellena la textura con el contenido
|
void fillTexture(); // Rellena la textura con el contenido
|
||||||
void updateDesp() { desp_ += speed_; } // Actualiza el desplazamiento (compatibilidad)
|
void updateDesp() { desp_ += speed_; } // Actualiza el desplazamiento (frame-based)
|
||||||
void updateDesp(float delta_time) { desp_ += speed_ * delta_time / (1000.0f / 60.0f); } // Actualiza el desplazamiento
|
void updateDesp(float delta_time) { desp_ += speed_ * 60.0f * delta_time; } // Actualiza el desplazamiento (time-based)
|
||||||
void updateStop(); // Detiene el desplazamiento de forma ordenada
|
void updateStop(); // Detiene el desplazamiento de forma ordenada (frame-based)
|
||||||
|
void updateStop(float delta_time); // Detiene el desplazamiento de forma ordenada (time-based)
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user