From a510742984143a95bf11a9ffb38152d162c70f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Wed, 2 Nov 2022 22:50:25 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20el=20esqueleto=20para=20dos=20se?= =?UTF-8?q?cciones=20nuevas:=20ending=20y=20game=5Fover?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/const.h | 4 +- source/director.cpp | 46 +++++++++++++++ source/director.h | 10 ++++ source/ending.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++ source/ending.h | 59 +++++++++++++++++++ source/game_over.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++ source/game_over.h | 59 +++++++++++++++++++ 7 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 source/ending.cpp create mode 100644 source/ending.h create mode 100644 source/game_over.cpp create mode 100644 source/game_over.h diff --git a/source/const.h b/source/const.h index 6055566..ca9043b 100644 --- a/source/const.h +++ b/source/const.h @@ -54,7 +54,9 @@ const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3; #define SECTION_PROG_CREDITS 3 #define SECTION_PROG_GAME 4 #define SECTION_PROG_DEMO 5 -#define SECTION_PROG_QUIT 6 +#define SECTION_PROG_GAME_OVER 6 +#define SECTION_PROG_ENDING 7 +#define SECTION_PROG_QUIT 8 // Subsecciones #define SUBSECTION_LOGO_TO_INTRO 0 diff --git a/source/director.cpp b/source/director.cpp index 90a7622..6aec1a8 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -298,6 +298,24 @@ void Director::loadResources(section_t section) resource->loadOffsets(offsetsList); } + else if (section.name == SECTION_PROG_ENDING) + { + std::vector textureList; + textureList.push_back("jailgames.png"); + textureList.push_back("since_1998.png"); + + resource->loadTextures(textureList); + } + + else if (section.name == SECTION_PROG_GAME_OVER) + { + std::vector textureList; + textureList.push_back("jailgames.png"); + textureList.push_back("since_1998.png"); + + resource->loadTextures(textureList); + } + else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO) { // Texturas @@ -1166,6 +1184,34 @@ void Director::runDemo() resource->free(); } +// Ejecuta la seccion del final del juego +void Director::runEnding() +{ + if (options->console) + { + std::cout << "\n* SECTION: ENDING" << std::endl; + } + loadResources(section); + ending = new Ending(renderer, screen, resource, asset, options); + setSection(ending->run()); + delete ending; + resource->free(); +} + +// Ejecuta la seccion del final de la partida +void Director::runGameOver() +{ + if (options->console) + { + std::cout << "\n* SECTION: GAME OVER" << std::endl; + } + loadResources(section); + gameOver = new GameOver(renderer, screen, resource, asset, options); + setSection(gameOver->run()); + delete gameOver; + resource->free(); +} + // Ejecuta la seccion de juego donde se juega void Director::runGame() { diff --git a/source/director.h b/source/director.h index 9f94d1f..8ccb4cc 100644 --- a/source/director.h +++ b/source/director.h @@ -16,6 +16,8 @@ #include "intro.h" #include "logo.h" #include "title.h" +#include "game_over.h" +#include "ending.h" #ifndef DIRECTOR_H #define DIRECTOR_H @@ -36,6 +38,8 @@ private: Intro *intro; // Objeto para gestionar la introducción del juego Credits *credits; // Objeto para gestionar los creditos del juego Demo *demo; // Objeto para gestionar el modo demo, en el que se ven pantallas del juego + Ending *ending; // Objeto para gestionar el final del juego + GameOver *gameOver; // Objeto para gestionar el final de la partida Debug *debug; // Objeto para getsionar la información de debug struct options_t *options; // Variable con todas las opciones del programa @@ -98,6 +102,12 @@ private: // Ejecuta la seccion de la demo, donde se ven pantallas del juego void runDemo(); + // Ejecuta la seccion del final del juego + void runEnding(); + + // Ejecuta la seccion del final de la partida + void runGameOver(); + // Ejecuta la seccion de juego donde se juega void runGame(); diff --git a/source/ending.cpp b/source/ending.cpp new file mode 100644 index 0000000..68e5907 --- /dev/null +++ b/source/ending.cpp @@ -0,0 +1,133 @@ +#include "ending.h" + +// Constructor +Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options) +{ + // Copia los punteros + this->renderer = renderer; + this->screen = screen; + this->resource = resource; + this->asset = asset; + this->options = options; + + // Reserva memoria para los punteros a objetos + eventHandler = new SDL_Event(); + + // Inicializa variables + counter = 0; + section.name = SECTION_PROG_ENDING; + section.subsection = 0; + ticks = 0; + ticksSpeed = 15; +} + +// Destructor +Ending::~Ending() +{ + // Libera la memoria de los objetos + delete eventHandler; +} + +// Actualiza el objeto +void Ending::update() +{ + // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego + if (SDL_GetTicks() - ticks > ticksSpeed) + { + // Actualiza el contador de ticks + ticks = SDL_GetTicks(); + + // Comprueba el manejador de eventos + checkEventHandler(); + } +} + +// Dibuja el final en pantalla +void Ending::render() +{ + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(); + + // Vuelca el contenido del renderizador en pantalla + screen->blit(); +} + +// Comprueba el manejador de eventos +void Ending::checkEventHandler() +{ + // Comprueba los eventos que hay en la cola + while (SDL_PollEvent(eventHandler) != 0) + { + // Evento de salida de la aplicación + if (eventHandler->type == SDL_QUIT) + { + section.name = SECTION_PROG_QUIT; + break; + } + + // Comprueba las teclas que se han pulsado + if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN)) + { + switch (eventHandler->key.keysym.scancode) + { + case SDL_SCANCODE_ESCAPE: + section.name = SECTION_PROG_QUIT; + break; + + case SDL_SCANCODE_B: + screen->switchBorder(); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F: + screen->switchVideoMode(); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F1: + screen->setWindowSize(1); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F2: + screen->setWindowSize(2); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F3: + screen->setWindowSize(3); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F4: + screen->setWindowSize(4); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F5: + // switchPalette(); + break; + + default: + // section.name = SECTION_PROG_TITLE; + // section.subsection = 0; + break; + } + } + } +} + +// Bucle principal +section_t Ending::run() +{ + while (section.name == SECTION_PROG_ENDING) + { + update(); + render(); + } + + return section; +} \ No newline at end of file diff --git a/source/ending.h b/source/ending.h new file mode 100644 index 0000000..ca8ab45 --- /dev/null +++ b/source/ending.h @@ -0,0 +1,59 @@ +#pragma once +#include +#include "common/animatedsprite.h" +#include "common/asset.h" +#include "common/jail_audio.h" +#include "common/resource.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/texture.h" +#include "common/utils.h" +#include "const.h" +#include + +#ifndef ENDING_H +#define ENDING_H + +class Ending +{ +private: + // Objetos y punteros + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + Resource *resource; // Objeto con los recursos + Asset *asset; // Objeto con los ficheros de recursos + options_t *options; // Puntero a las opciones del juego + SDL_Event *eventHandler; // Manejador de eventos + Text *text; // Objeto para escribir texto en pantalla + SDL_Texture *textTexture; // Textura para dibujar el texto + SDL_Texture *coverTexture; // Textura para cubrir el texto + AnimatedSprite *sprite; // Sprite para el brillo del corazón + + // Variables + int counter; // Contador + section_t section; // Estado del bucle principal para saber si continua o se sale + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + + // Actualiza el objeto + void update(); + + // Dibuja el final en pantalla + void render(); + + // Comprueba el manejador de eventos + void checkEventHandler(); + +public: + // Constructor + Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options); + + // Destructor + ~Ending(); + + // Bucle principal + section_t run(); +}; + +#endif diff --git a/source/game_over.cpp b/source/game_over.cpp new file mode 100644 index 0000000..84606d0 --- /dev/null +++ b/source/game_over.cpp @@ -0,0 +1,133 @@ +#include "game_over.h" + +// Constructor +GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options) +{ + // Copia los punteros + this->renderer = renderer; + this->screen = screen; + this->resource = resource; + this->asset = asset; + this->options = options; + + // Reserva memoria para los punteros a objetos + eventHandler = new SDL_Event(); + + // Inicializa variables + counter = 0; + section.name = SECTION_PROG_GAME_OVER; + section.subsection = 0; + ticks = 0; + ticksSpeed = 15; +} + +// Destructor +GameOver::~GameOver() +{ + // Libera la memoria de los objetos + delete eventHandler; +} + +// Actualiza el objeto +void GameOver::update() +{ + // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego + if (SDL_GetTicks() - ticks > ticksSpeed) + { + // Actualiza el contador de ticks + ticks = SDL_GetTicks(); + + // Comprueba el manejador de eventos + checkEventHandler(); + } +} + +// Dibuja el final en pantalla +void GameOver::render() +{ + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(); + + // Vuelca el contenido del renderizador en pantalla + screen->blit(); +} + +// Comprueba el manejador de eventos +void GameOver::checkEventHandler() +{ + // Comprueba los eventos que hay en la cola + while (SDL_PollEvent(eventHandler) != 0) + { + // Evento de salida de la aplicación + if (eventHandler->type == SDL_QUIT) + { + section.name = SECTION_PROG_QUIT; + break; + } + + // Comprueba las teclas que se han pulsado + if ((eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) || (eventHandler->type == SDL_JOYBUTTONDOWN)) + { + switch (eventHandler->key.keysym.scancode) + { + case SDL_SCANCODE_ESCAPE: + section.name = SECTION_PROG_QUIT; + break; + + case SDL_SCANCODE_B: + screen->switchBorder(); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F: + screen->switchVideoMode(); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F1: + screen->setWindowSize(1); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F2: + screen->setWindowSize(2); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F3: + screen->setWindowSize(3); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F4: + screen->setWindowSize(4); + resource->reLoadTextures(); + break; + + case SDL_SCANCODE_F5: + // switchPalette(); + break; + + default: + // section.name = SECTION_PROG_TITLE; + // section.subsection = 0; + break; + } + } + } +} + +// Bucle principal +section_t GameOver::run() +{ + while (section.name == SECTION_PROG_GAME_OVER) + { + update(); + render(); + } + + return section; +} \ No newline at end of file diff --git a/source/game_over.h b/source/game_over.h new file mode 100644 index 0000000..e22ce06 --- /dev/null +++ b/source/game_over.h @@ -0,0 +1,59 @@ +#pragma once +#include +#include "common/animatedsprite.h" +#include "common/asset.h" +#include "common/jail_audio.h" +#include "common/resource.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "common/text.h" +#include "common/texture.h" +#include "common/utils.h" +#include "const.h" +#include + +#ifndef GAME_OVER_H +#define GAME_OVER_H + +class GameOver +{ +private: + // Objetos y punteros + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + Resource *resource; // Objeto con los recursos + Asset *asset; // Objeto con los ficheros de recursos + options_t *options; // Puntero a las opciones del juego + SDL_Event *eventHandler; // Manejador de eventos + Text *text; // Objeto para escribir texto en pantalla + SDL_Texture *textTexture; // Textura para dibujar el texto + SDL_Texture *coverTexture; // Textura para cubrir el texto + AnimatedSprite *sprite; // Sprite para el brillo del corazón + + // Variables + int counter; // Contador + section_t section; // Estado del bucle principal para saber si continua o se sale + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + + // Actualiza el objeto + void update(); + + // Dibuja el final en pantalla + void render(); + + // Comprueba el manejador de eventos + void checkEventHandler(); + +public: + // Constructor + GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options); + + // Destructor + ~GameOver(); + + // Bucle principal + section_t run(); +}; + +#endif