afegit el fade RANDOM_SQAURE2
canviat els timings del fade a milisegons
This commit is contained in:
244
source/fade.cpp
244
source/fade.cpp
@@ -35,19 +35,22 @@ void Fade::init() {
|
||||
b_ = 0;
|
||||
a_ = 0;
|
||||
post_duration_ = 0;
|
||||
post_counter_ = 0;
|
||||
post_start_time_ = 0;
|
||||
pre_duration_ = 0;
|
||||
pre_counter_ = 0;
|
||||
pre_start_time_ = 0;
|
||||
num_squares_width_ = param.fade.num_squares_width;
|
||||
num_squares_height_ = param.fade.num_squares_height;
|
||||
fade_random_squares_delay_ = param.fade.random_squares_delay;
|
||||
fade_random_squares_mult_ = param.fade.random_squares_mult;
|
||||
random_squares_duration_ = param.fade.random_squares_duration_ms; // Usar como duración en ms
|
||||
square_transition_duration_ = random_squares_duration_ / 4; // 25% del tiempo total para la transición individual
|
||||
random_squares_start_time_ = 0;
|
||||
}
|
||||
|
||||
// Resetea algunas variables para volver a hacer el fade sin perder ciertos parametros
|
||||
void Fade::reset() {
|
||||
state_ = State::NOT_ENABLED;
|
||||
counter_ = 0;
|
||||
post_start_time_ = 0;
|
||||
pre_start_time_ = 0;
|
||||
}
|
||||
|
||||
// Pinta una transición en pantalla
|
||||
@@ -75,10 +78,11 @@ void Fade::update() {
|
||||
}
|
||||
|
||||
void Fade::updatePreState() {
|
||||
if (pre_counter_ == pre_duration_) {
|
||||
// Sistema basado en tiempo únicamente
|
||||
Uint32 elapsed_time = SDL_GetTicks() - pre_start_time_;
|
||||
|
||||
if (elapsed_time >= static_cast<Uint32>(pre_duration_)) {
|
||||
state_ = State::FADING;
|
||||
} else {
|
||||
pre_counter_++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +97,9 @@ void Fade::updateFadingState() {
|
||||
case Type::RANDOM_SQUARE:
|
||||
updateRandomSquareFade();
|
||||
break;
|
||||
case Type::RANDOM_SQUARE2:
|
||||
updateRandomSquare2Fade();
|
||||
break;
|
||||
case Type::VENETIAN:
|
||||
updateVenetianFade();
|
||||
break;
|
||||
@@ -102,13 +109,26 @@ void Fade::updateFadingState() {
|
||||
counter_++;
|
||||
}
|
||||
|
||||
void Fade::changeToPostState() {
|
||||
state_ = State::POST;
|
||||
post_start_time_ = SDL_GetTicks();
|
||||
}
|
||||
|
||||
void Fade::updatePostState() {
|
||||
if (post_counter_ == post_duration_) {
|
||||
// Sistema basado en tiempo únicamente
|
||||
Uint32 elapsed_time = SDL_GetTicks() - post_start_time_;
|
||||
|
||||
if (elapsed_time >= static_cast<Uint32>(post_duration_)) {
|
||||
state_ = State::FINISHED;
|
||||
} else {
|
||||
post_counter_++;
|
||||
}
|
||||
cleanBackbuffer(r_, g_, b_, a_);
|
||||
|
||||
// Mantener el alpha final correcto para cada tipo de fade
|
||||
Uint8 post_alpha = a_;
|
||||
if (type_ == Type::RANDOM_SQUARE2) {
|
||||
post_alpha = (mode_ == Mode::OUT) ? 255 : 0;
|
||||
}
|
||||
|
||||
cleanBackbuffer(r_, g_, b_, post_alpha);
|
||||
}
|
||||
|
||||
void Fade::updateFullscreenFade() {
|
||||
@@ -118,7 +138,7 @@ void Fade::updateFullscreenFade() {
|
||||
|
||||
// Comprueba si ha terminado
|
||||
if (counter_ >= 255 / 4) {
|
||||
state_ = State::POST;
|
||||
changeToPostState();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,8 +147,8 @@ void Fade::updateCenterFade() {
|
||||
|
||||
// Comprueba si ha terminado
|
||||
if ((counter_ * 4) > param.game.height) {
|
||||
state_ = State::POST;
|
||||
a_ = 255;
|
||||
changeToPostState();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,20 +171,99 @@ void Fade::drawCenterFadeRectangles() {
|
||||
}
|
||||
|
||||
void Fade::updateRandomSquareFade() {
|
||||
if (counter_ % fade_random_squares_delay_ == 0) {
|
||||
drawRandomSquares();
|
||||
}
|
||||
Uint32 elapsed_time = SDL_GetTicks() - random_squares_start_time_;
|
||||
float progress = static_cast<float>(elapsed_time) / random_squares_duration_;
|
||||
|
||||
value_ = calculateValue(0, (num_squares_width_ * num_squares_height_), (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_));
|
||||
// Calcula cuántos cuadrados deberían estar activos
|
||||
int total_squares = num_squares_width_ * num_squares_height_;
|
||||
int active_squares = static_cast<int>(progress * total_squares);
|
||||
active_squares = std::min(active_squares, total_squares);
|
||||
|
||||
// Dibuja los cuadrados activos
|
||||
drawRandomSquares(active_squares);
|
||||
|
||||
value_ = calculateValue(0, total_squares, active_squares);
|
||||
|
||||
// Comprueba si ha terminado
|
||||
if (counter_ * fade_random_squares_mult_ / fade_random_squares_delay_ >=
|
||||
num_squares_width_ * num_squares_height_) {
|
||||
state_ = State::POST;
|
||||
if (elapsed_time >= static_cast<Uint32>(random_squares_duration_)) {
|
||||
changeToPostState();
|
||||
}
|
||||
}
|
||||
|
||||
void Fade::drawRandomSquares() {
|
||||
void Fade::updateRandomSquare2Fade() {
|
||||
Uint32 elapsed_time = SDL_GetTicks() - random_squares_start_time_;
|
||||
|
||||
int total_squares = num_squares_width_ * num_squares_height_;
|
||||
|
||||
// Calcula el tiempo de activación: total - tiempo que necesitan los últimos cuadrados
|
||||
int activation_time = random_squares_duration_ - square_transition_duration_;
|
||||
activation_time = std::max(activation_time, square_transition_duration_); // Mínimo igual a la duración de transición
|
||||
|
||||
// Lógica diferente según el modo
|
||||
int squares_to_activate = 0;
|
||||
|
||||
if (mode_ == Mode::OUT) {
|
||||
// OUT: Activa cuadrados gradualmente
|
||||
if (elapsed_time < static_cast<Uint32>(activation_time)) {
|
||||
float activation_progress = static_cast<float>(elapsed_time) / activation_time;
|
||||
squares_to_activate = static_cast<int>(activation_progress * total_squares);
|
||||
} else {
|
||||
squares_to_activate = total_squares; // Activar todos
|
||||
}
|
||||
|
||||
// Activa nuevos cuadrados y guarda su tiempo de activación
|
||||
for (int i = 0; i < squares_to_activate && i < total_squares; ++i) {
|
||||
if (square_age_[i] == -1) {
|
||||
square_age_[i] = elapsed_time; // Guarda el tiempo de activación
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// IN: Todos los cuadrados empiezan activos desde el inicio
|
||||
squares_to_activate = total_squares;
|
||||
|
||||
// Activa cuadrados gradualmente con tiempo de inicio escalonado
|
||||
float activation_progress = static_cast<float>(elapsed_time) / activation_time;
|
||||
int squares_starting_transition = static_cast<int>(activation_progress * total_squares);
|
||||
|
||||
// Asegurar que al menos 1 cuadrado se active desde el primer frame
|
||||
squares_starting_transition = std::max(squares_starting_transition, 1);
|
||||
squares_starting_transition = std::min(squares_starting_transition, total_squares);
|
||||
|
||||
for (int i = 0; i < squares_starting_transition; ++i) {
|
||||
if (square_age_[i] == -1) {
|
||||
square_age_[i] = elapsed_time; // Empieza la transición a transparente
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drawRandomSquares2();
|
||||
|
||||
value_ = calculateValue(0, total_squares, squares_to_activate);
|
||||
|
||||
// Comprueba si ha terminado - todos los cuadrados han completado su transición
|
||||
bool all_completed = (squares_to_activate >= total_squares);
|
||||
if (all_completed) {
|
||||
// Verificar que todos han completado su transición individual
|
||||
for (int i = 0; i < total_squares; ++i) {
|
||||
if (square_age_[i] >= 0) { // Cuadrado activado
|
||||
Uint32 square_elapsed = elapsed_time - square_age_[i];
|
||||
if (square_elapsed < static_cast<Uint32>(square_transition_duration_)) {
|
||||
all_completed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (all_completed) {
|
||||
// Pintar textura final: OUT opaca, IN transparente
|
||||
Uint8 final_alpha = (mode_ == Mode::OUT) ? 255 : 0;
|
||||
cleanBackbuffer(r_, g_, b_, final_alpha);
|
||||
changeToPostState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Fade::drawRandomSquares(int active_count) {
|
||||
auto *temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, backbuffer_);
|
||||
|
||||
@@ -173,13 +272,56 @@ void Fade::drawRandomSquares() {
|
||||
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_NONE);
|
||||
SDL_SetRenderDrawColor(renderer_, r_, g_, b_, a_);
|
||||
|
||||
const int INDEX = std::min(counter_ / fade_random_squares_delay_,
|
||||
(num_squares_width_ * num_squares_height_) - 1);
|
||||
// Dibuja solo los cuadrados activos
|
||||
for (int i = 0; i < active_count && i < static_cast<int>(square_.size()); ++i) {
|
||||
SDL_RenderFillRect(renderer_, &square_[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < fade_random_squares_mult_; ++i) {
|
||||
const int INDEX2 = std::min((INDEX * fade_random_squares_mult_) + i,
|
||||
static_cast<int>(square_.size()) - 1);
|
||||
SDL_RenderFillRect(renderer_, &square_[INDEX2]);
|
||||
SDL_SetRenderDrawBlendMode(renderer_, blend_mode);
|
||||
SDL_SetRenderTarget(renderer_, temp);
|
||||
}
|
||||
|
||||
void Fade::drawRandomSquares2() {
|
||||
auto *temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, backbuffer_);
|
||||
|
||||
// CRÍTICO: Limpiar la textura antes de dibujar
|
||||
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 0);
|
||||
SDL_RenderClear(renderer_);
|
||||
|
||||
SDL_BlendMode blend_mode;
|
||||
SDL_GetRenderDrawBlendMode(renderer_, &blend_mode);
|
||||
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND); // Usar BLEND para alpha
|
||||
|
||||
Uint32 current_time = SDL_GetTicks() - random_squares_start_time_;
|
||||
|
||||
// Lógica unificada: sobre textura transparente, pintar cuadrados según su estado
|
||||
for (size_t i = 0; i < square_.size(); ++i) {
|
||||
Uint8 current_alpha = 0;
|
||||
|
||||
if (square_age_[i] == -1) {
|
||||
// Cuadrado no activado
|
||||
if (mode_ == Mode::OUT) {
|
||||
current_alpha = 0; // OUT: transparente si no activado
|
||||
} else {
|
||||
current_alpha = a_; // IN: opaco si no activado
|
||||
}
|
||||
} else {
|
||||
// Cuadrado activado - calculamos progreso
|
||||
Uint32 square_elapsed = current_time - square_age_[i];
|
||||
float progress = std::min(static_cast<float>(square_elapsed) / square_transition_duration_, 1.0f);
|
||||
|
||||
if (mode_ == Mode::OUT) {
|
||||
current_alpha = static_cast<Uint8>(progress * a_); // 0 → 255
|
||||
} else {
|
||||
current_alpha = static_cast<Uint8>((1.0f - progress) * a_); // 255 → 0
|
||||
}
|
||||
}
|
||||
|
||||
if (current_alpha > 0) {
|
||||
SDL_SetRenderDrawColor(renderer_, r_, g_, b_, current_alpha);
|
||||
SDL_RenderFillRect(renderer_, &square_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawBlendMode(renderer_, blend_mode);
|
||||
@@ -192,7 +334,7 @@ void Fade::updateVenetianFade() {
|
||||
updateVenetianRectangles();
|
||||
calculateVenetianProgress();
|
||||
} else {
|
||||
state_ = State::POST;
|
||||
changeToPostState();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,8 +381,7 @@ void Fade::activate() {
|
||||
|
||||
state_ = State::PRE;
|
||||
counter_ = 0;
|
||||
post_counter_ = 0;
|
||||
pre_counter_ = 0;
|
||||
pre_start_time_ = SDL_GetTicks();
|
||||
|
||||
switch (type_) {
|
||||
case Type::FULLSCREEN: {
|
||||
@@ -284,6 +425,49 @@ void Fade::activate() {
|
||||
// Deja el color listo para usar
|
||||
a_ = mode_ == Mode::OUT ? 255 : 0;
|
||||
|
||||
// Inicializa el tiempo de inicio
|
||||
random_squares_start_time_ = SDL_GetTicks();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case Type::RANDOM_SQUARE2: {
|
||||
rect1_ = {.x = 0, .y = 0, .w = static_cast<float>(param.game.width / num_squares_width_), .h = static_cast<float>(param.game.height / num_squares_height_)};
|
||||
square_.clear();
|
||||
square_age_.clear();
|
||||
|
||||
// Añade los cuadrados al vector
|
||||
for (int i = 0; i < num_squares_width_ * num_squares_height_; ++i) {
|
||||
rect1_.x = (i % num_squares_width_) * rect1_.w;
|
||||
rect1_.y = (i / num_squares_width_) * rect1_.h;
|
||||
square_.push_back(rect1_);
|
||||
square_age_.push_back(-1); // -1 indica cuadrado no activado aún
|
||||
}
|
||||
|
||||
// Desordena el vector de cuadrados y edades
|
||||
auto num = num_squares_width_ * num_squares_height_;
|
||||
while (num > 1) {
|
||||
auto num_arreu = rand() % num;
|
||||
SDL_FRect temp_rect = square_[num_arreu];
|
||||
int temp_age = square_age_[num_arreu];
|
||||
square_[num_arreu] = square_[num - 1];
|
||||
square_age_[num_arreu] = square_age_[num - 1];
|
||||
square_[num - 1] = temp_rect;
|
||||
square_age_[num - 1] = temp_age;
|
||||
num--;
|
||||
}
|
||||
|
||||
// Textura inicial: OUT transparente, IN opaca
|
||||
Uint8 initial_alpha = (mode_ == Mode::OUT) ? 0 : 255;
|
||||
cleanBackbuffer(r_, g_, b_, initial_alpha);
|
||||
|
||||
// Deja el color listo para usar (alpha target para los cuadrados)
|
||||
a_ = 255; // Siempre usar 255 como alpha target
|
||||
|
||||
// Inicializa el tiempo de inicio y recalcula la duración de transición
|
||||
random_squares_start_time_ = SDL_GetTicks();
|
||||
square_transition_duration_ = std::max(random_squares_duration_ / 4, 100); // Mínimo 100ms
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user