Movidos los efectos shake y flash de la clase game a la clase screen

This commit is contained in:
2024-06-15 12:14:30 +02:00
parent d452f84482
commit a5386a606a
6 changed files with 77 additions and 132 deletions

View File

@@ -20,20 +20,20 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
borderHeight = options->video.border.height * 2;
dest = {0, 0, 0, 0};
borderColor = {0, 0, 0};
fade.enabled = false;
fade.counter = 0;
fade.lenght = 0;
fade.color = {0xFF, 0xFF, 0xFF};
flash.enabled = false;
flash.counter = 0;
flash.lenght = 0;
flash.color = {0xFF, 0xFF, 0xFF};
shake.desp = 1;
shake.delay = 3;
shake.counter = 0;
shake.lenght = 8;
shake.remaining = 0;
shake.origin = 0;
fadeEffect.enabled = false;
fadeEffect.counter = 0;
fadeEffect.lenght = 0;
fadeEffect.color = {0xFF, 0xFF, 0xFF};
flashEffect.enabled = false;
flashEffect.counter = 0;
flashEffect.lenght = 0;
flashEffect.color = {0xFF, 0xFF, 0xFF};
shakeEffect.desp = 1;
shakeEffect.delay = 3;
shakeEffect.counter = 0;
shakeEffect.lenght = 8;
shakeEffect.remaining = 0;
shakeEffect.origin = 0;
iniFade();
@@ -178,8 +178,8 @@ void Screen::setVideoMode(int videoMode)
options->video.window.width = windowWidth;
options->video.window.height = windowHeight;
// Recalcula los valores de los efectos
setShake();
// Actualiza variables
shakeEffect.origin = dest.x;
}
// Camibia entre pantalla completa y ventana
@@ -252,13 +252,13 @@ void Screen::switchBorder()
// Activa el fade
void Screen::setFade()
{
fade.enabled = true;
fadeEffect.enabled = true;
}
// Comprueba si ha terminado el fade
bool Screen::fadeEnded()
{
if (fade.enabled || fade.counter > 0)
if (fadeEffect.enabled || fadeEffect.counter > 0)
{
return false;
}
@@ -269,21 +269,21 @@ bool Screen::fadeEnded()
// Inicializa las variables para el fade
void Screen::iniFade()
{
fade.enabled = false;
fade.counter = 0;
fade.lenght = 200;
fadeEffect.enabled = false;
fadeEffect.counter = 0;
fadeEffect.lenght = 200;
}
// Actualiza el fade
void Screen::updateFade()
{
if (!fade.enabled)
if (!fadeEffect.enabled)
{
return;
}
fade.counter++;
if (fade.counter > fade.lenght)
fadeEffect.counter++;
if (fadeEffect.counter > fadeEffect.lenght)
{
iniFade();
}
@@ -292,14 +292,14 @@ void Screen::updateFade()
// Dibuja el fade
void Screen::renderFade()
{
if (!fade.enabled)
if (!fadeEffect.enabled)
{
return;
}
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
color_t color = {0, 0, 0};
const float step = (float)fade.counter / (float)fade.lenght;
const float step = (float)fadeEffect.counter / (float)fadeEffect.lenght;
const int alpha = 0 + (255 - 0) * step;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
SDL_RenderFillRect(renderer, &rect);
@@ -325,65 +325,65 @@ void Screen::update()
}
// Agita la pantalla
void Screen::setShake()
void Screen::shake()
{
shake.remaining = shake.lenght;
shake.counter = shake.delay;
shake.origin = dest.x;
shakeEffect.remaining = shakeEffect.lenght;
shakeEffect.counter = shakeEffect.delay;
shakeEffect.origin = dest.x;
}
// Actualiza la logica para agitar la pantalla
void Screen::updateShake()
{
if (shake.remaining > 0)
if (shakeEffect.remaining > 0)
{
if (shake.counter > 0)
if (shakeEffect.counter > 0)
{
shake.counter--;
shakeEffect.counter--;
}
else
{
shake.counter = shake.delay;
const int desp = shake.remaining % 2 == 0 ? shake.desp * (-1) : shake.desp;
dest.x = shake.origin + desp;
shake.remaining--;
shakeEffect.counter = shakeEffect.delay;
const int desp = shakeEffect.remaining % 2 == 0 ? shakeEffect.desp * (-1) : shakeEffect.desp;
dest.x = shakeEffect.origin + desp;
shakeEffect.remaining--;
}
}
else
{
dest.x = shake.origin;
dest.x = shakeEffect.origin;
}
}
// Pone la pantalla de color
void Screen::setFlash(color_t color, int lenght)
void Screen::flash(color_t color, int lenght)
{
flash.enabled = true;
flash.counter = 0;
flash.lenght = lenght;
flash.color = color;
flashEffect.enabled = true;
flashEffect.counter = 0;
flashEffect.lenght = lenght;
flashEffect.color = color;
}
// Actualiza y dibuja el efecto de flash en la pantalla
void Screen::doFlash()
{
if (flash.enabled)
if (flashEffect.enabled)
{
// Dibuja el color del flash en la textura
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, gameCanvas);
SDL_SetRenderDrawColor(renderer, flash.color.r, flash.color.g, flash.color.b, 0xFF);
SDL_SetRenderDrawColor(renderer, flashEffect.color.r, flashEffect.color.g, flashEffect.color.b, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderTarget(renderer, temp);
// Actualiza la lógica del efecto
if (flash.counter < flash.lenght)
if (flashEffect.counter < flashEffect.lenght)
{
flash.counter++;
flashEffect.counter++;
}
else
{
flash.enabled = false;
flashEffect.enabled = false;
}
}
}

View File

@@ -41,8 +41,8 @@ private:
};
// Variables - Efectos
effect_t fade; // Variable para gestionar el efecto de fade
effect_t flash; // Variable para gestionar el efecto de flash
effect_t fadeEffect; // Variable para gestionar el efecto de fade
effect_t flashEffect; // Variable para gestionar el efecto de flash
struct shake_t
{
@@ -52,7 +52,7 @@ private:
int lenght; // Cantidad de desplazamientos a realizar
int remaining; // Cantidad de desplazamientos pendientes a realizar
int origin; // Valor inicial de la pantalla para dejarla igual tras el desplazamiento
} shake;
} shakeEffect;
// Inicializa las variables para el fade
void iniFade();
@@ -132,10 +132,10 @@ public:
bool fadeEnded();
// Agita la pantalla
void setShake();
void shake();
// Pone la pantalla de color
void setFlash(color_t color, int lenght);
void flash(color_t color, int lenght);
};
#endif

View File

@@ -72,5 +72,6 @@ const color_t scoreboardColor = {46, 63, 71};
const color_t difficultyEasyColor = {75, 105, 47};
const color_t difficultyNormalColor = {255, 122, 0};
const color_t difficultyHardColor = {118, 66, 138};
const color_t flashColor = {0xFF, 0xFF, 0xFF};
#endif

View File

@@ -244,9 +244,6 @@ void Game::init()
lastEnemyDeploy = 0;
enemyDeployCounter = 0;
enemySpeed = defaultEnemySpeed;
effect.flash = false;
effect.shake = false;
effect.shakeCounter = SHAKE_COUNTER;
helper.needCoffee = false;
helper.needCoffeeMachine = false;
helper.needPowerBall = false;
@@ -1620,8 +1617,8 @@ void Game::updateStage()
stageBitmapCounter = 0;
enemySpeed = defaultEnemySpeed;
setBalloonSpeed(enemySpeed);
effect.flash = true;
effect.shake = true;
screen->flash(flashColor, 5);
screen->shake();
}
// Incrementa el contador del bitmap que aparece mostrando el cambio de fase
@@ -1999,8 +1996,8 @@ void Game::destroyAllBalloons()
enemyDeployCounter = 255;
JA_PlaySound(powerBallSound);
effect.flash = true;
effect.shake = true;
screen->flash(flashColor, 5);
screen->shake();
}
// Detiene todos los globos
@@ -2263,7 +2260,7 @@ void Game::updateItems()
if (item->isOnFloor())
{
JA_PlaySound(coffeeMachineSound);
effect.shake = true;
screen->shake();
}
}
}
@@ -2409,36 +2406,6 @@ void Game::freeSmartSprites()
}
}
// Dibuja el efecto de flash
void Game::renderFlashEffect()
{
if (effect.flash)
{
// Pantallazo blanco
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
effect.flash = false;
}
}
// Actualiza el efecto de agitar la pantalla
void Game::updateShakeEffect()
{
if (effect.shake)
{
if (effect.shakeCounter > 0)
{
effect.shakeCounter--;
}
else
{
effect.shake = false;
effect.shakeCounter = SHAKE_COUNTER;
}
}
}
// Crea un SmartSprite para arrojar el item café al recibir un impacto
void Game::throwCoffee(int x, int y)
{
@@ -2491,13 +2458,14 @@ void Game::killPlayer(Player *player)
player->removeExtraHit();
throwCoffee(player->getPosX() + (player->getWidth() / 2), player->getPosY() + (player->getHeight() / 2));
JA_PlaySound(coffeeOutSound);
screen->shake();
}
else
{
JA_PauseMusic();
stopAllBalloons(10);
JA_PlaySound(playerCollisionSound);
shakeScreen();
screen->shake();
JA_PlaySound(coffeeOutSound);
player->setAlive(false);
if (allPlayersAreDead())
@@ -2629,7 +2597,6 @@ void Game::update()
// Actualiza los contadores de estado y efectos
updateTimeStoppedCounter();
updateEnemyDeployCounter();
updateShakeEffect();
// Actualiza el ayudante
updateHelper();
@@ -2718,31 +2685,33 @@ void Game::fillCanvas()
// Dibuja el juego
void Game::render()
{
// Dibuja la zona de juego y el marcador
// Dibuja los graficos de la zona de juego en la textura
fillCanvas();
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean(bgColor);
// Copia la textura con la zona de juego a la pantalla
SDL_RenderCopy(renderer, canvas, nullptr, &playArea);
// Dibuja el marcador
scoreboard->render();
// Dibuja el separador del marcador de la zona de juego
renderSeparator();
// Dibuja el sprite del jugador al morir
if ((deathCounter <= 150) && !players[0]->isAlive())
{
renderDeathFade(150 - deathCounter);
}
// Dibuja el fade de fin de juego
if ((gameCompleted) && (gameCompletedCounter >= GAME_COMPLETED_START_FADE))
{
renderDeathFade(gameCompletedCounter - GAME_COMPLETED_START_FADE);
}
renderFlashEffect();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
@@ -3060,12 +3029,6 @@ void Game::disableTimeStopItem()
}
}
// Agita la pantalla
void Game::shakeScreen()
{
screen->setShake();
}
// Bucle para el juego
void Game::run()
{
@@ -3208,8 +3171,6 @@ void Game::renderPausedGame()
{
renderDeathFade(gameCompletedCounter - GAME_COMPLETED_START_FADE);
}
renderFlashEffect();
}
if (leavingPauseMenu)

View File

@@ -87,13 +87,6 @@ private:
Uint8 number; // Numero de fase
};
struct effect_t
{
bool flash; // Indica si se ha de pintar la pantalla de blanco
bool shake; // Indica si se ha de agitar la pantalla
Uint8 shakeCounter; // Contador para medir el tiempo que dura el efecto
};
struct helper_t
{
bool needCoffee; // Indica si se necesitan cafes
@@ -209,7 +202,6 @@ private:
int enemyDeployCounter; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
float enemySpeed; // Velocidad a la que se mueven los enemigos
float defaultEnemySpeed; // Velocidad base de los enemigos, sin incrementar
effect_t effect; // Variable para gestionar los efectos visuales
helper_t helper; // Variable para gestionar las ayudas
bool powerBallEnabled; // Indica si hay una powerball ya activa
Uint8 powerBallCounter; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
@@ -382,12 +374,6 @@ private:
// Vacia el vector de smartsprites
void freeSmartSprites();
// Dibuja el efecto de flash
void renderFlashEffect();
// Actualiza el efecto de agitar la pantalla
void updateShakeEffect();
// Crea un SmartSprite para arrojar el item café al recibir un impacto
void throwCoffee(int x, int y);
@@ -442,9 +428,6 @@ private:
// Deshabilita el efecto del item de detener el tiempo
void disableTimeStopItem();
// Agita la pantalla
void shakeScreen();
// Actualiza las variables del menu de pausa del juego
void updatePausedGame();

View File

@@ -135,7 +135,7 @@ void GameLogo::update()
status = shaking;
// Pantallazo blanco
screen->setFlash({0xFF, 0xFF, 0xFF}, 5);
screen->flash(flashColor, 5);
// Reproduce el efecto sonoro
JA_PlaySound(crashSound);