From b7cda085cce6d24ca524102b1c8be91fcd85e0f2 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 31 May 2024 21:43:20 +0200 Subject: [PATCH] =?UTF-8?q?shakeScreen=20ya=20no=20detiene=20la=20ejecuci?= =?UTF-8?q?=C3=B3n=20del=20programa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/common/screen.cpp | 77 +++++++++++++++++++++++++++------------- source/common/screen.h | 64 ++++++++++++++++++--------------- source/game.cpp | 57 +++++++++-------------------- source/game.h | 3 ++ source/player.cpp | 2 +- source/scoreboard.cpp | 4 --- todo.txt | 4 +++ 7 files changed, 113 insertions(+), 98 deletions(-) create mode 100644 todo.txt diff --git a/source/common/screen.cpp b/source/common/screen.cpp index 8502e9b..9ccf9dc 100644 --- a/source/common/screen.cpp +++ b/source/common/screen.cpp @@ -5,18 +5,30 @@ // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) { - // Inicializa variables + // Copia punteros this->window = window; this->renderer = renderer; this->options = options; this->asset = asset; + // Inicializa variables + windowWidth = 0; + windowHeight = 0; gameCanvasWidth = options->video.gameWidth; gameCanvasHeight = options->video.gameHeight; borderWidth = options->video.border.width * 2; borderHeight = options->video.border.height * 2; - notificationLogicalWidth = gameCanvasWidth; - notificationLogicalHeight = gameCanvasHeight; + dest = {0, 0, 0, 0}; + borderColor = {0, 0, 0}; + fade = 0; + fadeCounter = 0; + fadeLenght = 0; + shake.desp = 1; + shake.delay = 3; + shake.counter = 0; + shake.lenght = 8; + shake.remaining = 0; + shake.origin = 0; iniFade(); @@ -28,9 +40,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options // Establece el modo de video setVideoMode(options->video.mode); - - // Inicializa variables - notifyActive = false; } // Destructor @@ -82,7 +91,7 @@ void Screen::setVideoMode(int videoMode) SDL_ShowCursor(SDL_ENABLE); // Esconde la ventana - //SDL_HideWindow(window); + // SDL_HideWindow(window); if (options->video.border.enabled) { @@ -246,23 +255,6 @@ bool Screen::fadeEnded() return true; } -// Activa el spectrum fade -void Screen::setspectrumFade() -{ - spectrumFade = true; -} - -// Comprueba si ha terminado el spectrum fade -bool Screen::spectrumFadeEnded() -{ - if (spectrumFade || spectrumFadeCounter > 0) - { - return false; - } - - return true; -} - // Inicializa las variables para el fade void Screen::iniFade() { @@ -312,4 +304,41 @@ void Screen::updateFX() void Screen::renderFX() { renderFade(); +} + +// Actualiza la lógica de la clase +void Screen::update() +{ + updateShake(); +} + +// Agita la pantalla +void Screen::startShake() +{ + shake.remaining = shake.lenght; + shake.counter = shake.delay; + shake.origin = dest.x; +} + +// Actualiza la logica para agitar la pantalla +void Screen::updateShake() +{ + if (shake.remaining > 0) + { + if (shake.counter > 0) + { + shake.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--; + } + } + else + { + dest.x = shake.origin; + } } \ No newline at end of file diff --git a/source/common/screen.h b/source/common/screen.h index c1f06cc..8565c4a 100644 --- a/source/common/screen.h +++ b/source/common/screen.h @@ -23,26 +23,29 @@ private: options_t *options; // Variable con todas las opciones del programa // Variables - int windowWidth; // Ancho de la pantalla o ventana - int windowHeight; // Alto de la pantalla o ventana - int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego - int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego - int borderWidth; // Anchura del borde - int borderHeight; // Anltura del borde - SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana - color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla - bool notifyActive; // Indica si hay notificaciones activas - int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla - int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla + int windowWidth; // Ancho de la pantalla o ventana + int windowHeight; // Alto de la pantalla o ventana + int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego + int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego + int borderWidth; // Anchura del borde + int borderHeight; // Anltura del borde + SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana + color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla // Variables - Efectos - bool fade; // Indica si esta activo el efecto de fade - int fadeCounter; // Temporizador para el efecto de fade - int fadeLenght; // Duración del fade - bool spectrumFade; // Indica si esta activo el efecto de fade spectrum - int spectrumFadeCounter; // Temporizador para el efecto de fade spectrum - int spectrumFadeLenght; // Duración del fade spectrum - std::vector spectrumColor; // Colores para el fade spectrum + bool fade; // Indica si esta activo el efecto de fade + int fadeCounter; // Temporizador para el efecto de fade + int fadeLenght; // Duración del fade + + struct shake_t + { + int desp; // Pixels de desplazamiento para agitar la pantalla en el eje x + int delay; // Retraso entre cada desplazamiento de la pantalla al agitarse + int counter; // Contador para el retraso + 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; // Inicializa las variables para el fade void iniFade(); @@ -53,6 +56,15 @@ private: // Dibuja el fade void renderFade(); + // Actualiza los efectos + void updateFX(); + + // Dibuja los efectos + void renderFX(); + + // Actualiza la logica para agitar la pantalla + void updateShake(); + public: // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options); @@ -60,6 +72,9 @@ public: // Destructor ~Screen(); + // Actualiza la lógica de la clase + void update(); + // Limpia la pantalla void clean(color_t color = {0x00, 0x00, 0x00}); @@ -106,17 +121,8 @@ public: // Comprueba si ha terminado el fade bool fadeEnded(); - // Activa el spectrum fade - void setspectrumFade(); - - // Comprueba si ha terminado el spectrum fade - bool spectrumFadeEnded(); - - // Actualiza los efectos - void updateFX(); - - // Dibuja los efectos - void renderFX(); + // Agita la pantalla + void startShake(); }; #endif diff --git a/source/game.cpp b/source/game.cpp index 58e4e70..4ccfe7a 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -215,7 +215,7 @@ void Game::init() players.push_back(player2); } - // Inicializa las variables + // Variables relacionadas con la dificultad switch (difficulty) { case DIFFICULTY_EASY: @@ -246,18 +246,18 @@ void Game::init() break; } - // Marcador - scoreboard->setPos({10, 10, 256-20, 32}); + // Variables para el marcador + scoreboard->setPos({0, 160, 256, 32}); if (difficulty == DIFFICULTY_NORMAL) { scoreboard->setColor({46, 63, 71}); - scoreboard->setColor({255, 63, 71}); } else { scoreboard->setColor(difficultyColor); } + // Resto de variables gameCompleted = false; gameCompletedCounter = 0; section->name = SECTION_PROG_GAME; @@ -2545,7 +2545,6 @@ void Game::killPlayer(Player *player) stopAllBalloons(10); JA_PlaySound(playerCollisionSound); shakeScreen(); - SDL_Delay(500); JA_PlaySound(coffeeOutSound); player->setAlive(false); if (allPlayersAreDead()) @@ -2641,6 +2640,9 @@ void Game::update() // Actualiza el contador de juego counter++; + // Actualiza el objeto screen + screen->update(); + // Comprueba el teclado/mando checkGameInput(); @@ -2798,6 +2800,14 @@ void Game::renderBackground() grassSprite->render(); } +// Dibuja la linea que separa la zona ade juego del marcador +void Game::renderSeparator() +{ + // Dibuja la linea que separa el marcador de la zona de juego + SDL_SetRenderDrawColor(renderer, 13, 26, 43, 255); + SDL_RenderDrawLine(renderer, 0, 160, 255, 160); +} + // Dibuja el juego void Game::render() { @@ -2815,6 +2825,7 @@ void Game::render() renderItems(); renderSmartSprites(); scoreboard->render(); + renderSeparator(); renderPlayers(); if ((deathCounter <= 150) && !players[0]->isAlive()) @@ -3149,41 +3160,7 @@ void Game::disableTimeStopItem() // Agita la pantalla void Game::shakeScreen() { - const int v[] = {-1, 1, -1, 1, -1, 1, -1, 0}; - for (int n = 0; n < 8; ++n) - { - // Prepara para empezar a dibujar en la textura de juego - screen->start(); - - // Limpia la pantalla - screen->clean(bgColor); - - // Dibuja los objetos - buildingsSprite->setPosX(0); - buildingsSprite->setWidth(1); - buildingsSprite->setSpriteClip(0, 0, 1, 160); - renderBackground(); - - buildingsSprite->setPosX(255); - buildingsSprite->setSpriteClip(255, 0, 1, 160); - buildingsSprite->render(); - - buildingsSprite->setPosX(v[n]); - buildingsSprite->setWidth(256); - buildingsSprite->setSpriteClip(0, 0, 256, 160); - buildingsSprite->render(); - - grassSprite->render(); - renderBalloons(); - renderBullets(); - renderItems(); - renderPlayers(); - scoreboard->render(); - - // Vuelca el contenido del renderizador en pantalla - screen->blit(); - SDL_Delay(50); - } + screen->startShake(); } // Bucle para el juego diff --git a/source/game.h b/source/game.h index 85b8d41..2074380 100644 --- a/source/game.h +++ b/source/game.h @@ -508,6 +508,9 @@ private: // Actualiza el marcador void updateScoreboard(); + // Dibuja la linea que separa la zona ade juego del marcador + void renderSeparator(); + public: // Constructor Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, options_t *options, section_t *section); diff --git a/source/player.cpp b/source/player.cpp index c9af1d3..6e01811 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -372,7 +372,7 @@ void Player::setAlive(bool value) { alive = value; - if (!value) + if (!alive) { deathSprite->setPosX(headSprite->getRect().x); deathSprite->setPosY(headSprite->getRect().y); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 28d6416..79ea9c1 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -216,10 +216,6 @@ void Scoreboard::fillBackgroundTexture() SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255); SDL_RenderFillRect(renderer, nullptr); - // Dibuja la linea que separa el marcador de la zona de juego - // SDL_SetRenderDrawColor(renderer, 13, 26, 43, 255); - // SDL_RenderDrawLine(renderer, 0, 160, 255, 160); - // PLAYER1 - SCORE textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53)); textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1)); diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..b988fee --- /dev/null +++ b/todo.txt @@ -0,0 +1,4 @@ +[] Cargar todos los graficos al inicio +[] Independizar el pintado del fondo del juego +[] Cambiar la resolución a 320x240 +[] Permitir entrar al segundo jugador en caliente \ No newline at end of file