From 6257c0b1e8343e143ec5cd1de2311497d22399d5 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 4 Nov 2022 18:55:36 +0100 Subject: [PATCH] Preparado para empezar con el ending2 --- source/const.h | 3 +- source/director.cpp | 33 +++++++++ source/director.h | 5 ++ source/ending.cpp | 2 +- source/ending.h | 4 +- source/ending2.cpp | 167 ++++++++++++++++++++++++++++++++++++++++++++ source/ending2.h | 62 ++++++++++++++++ 7 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 source/ending2.cpp create mode 100644 source/ending2.h diff --git a/source/const.h b/source/const.h index ca9043b..7de58b1 100644 --- a/source/const.h +++ b/source/const.h @@ -56,7 +56,8 @@ const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3; #define SECTION_PROG_DEMO 5 #define SECTION_PROG_GAME_OVER 6 #define SECTION_PROG_ENDING 7 -#define SECTION_PROG_QUIT 8 +#define SECTION_PROG_ENDING2 8 +#define SECTION_PROG_QUIT 9 // Subsecciones #define SUBSECTION_LOGO_TO_INTRO 0 diff --git a/source/director.cpp b/source/director.cpp index 5af94eb..f24ff57 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -318,6 +318,21 @@ void Director::loadResources(section_t section) resource->loadOffsets(offsetsList); } + else if (section.name == SECTION_PROG_ENDING2) + { + // Texturas + std::vector textureList; + textureList.push_back("smb2.png"); + + resource->loadTextures(textureList); + + // Offsets + std::vector offsetsList; + offsetsList.push_back("smb2.txt"); + + resource->loadOffsets(offsetsList); + } + else if (section.name == SECTION_PROG_GAME_OVER) { std::vector textureList; @@ -1218,6 +1233,20 @@ void Director::runEnding() resource->free(); } +// Ejecuta la seccion del final del juego +void Director::runEnding2() +{ + if (options->console) + { + std::cout << "\n* SECTION: ENDING2" << std::endl; + } + loadResources(section); + ending2 = new Ending2(renderer, screen, resource, asset, options); + setSection(ending2->run()); + delete ending2; + resource->free(); +} + // Ejecuta la seccion del final de la partida void Director::runGameOver() { @@ -1285,6 +1314,10 @@ void Director::run() case SECTION_PROG_ENDING: runEnding(); break; + + case SECTION_PROG_ENDING2: + runEnding2(); + break; } } } diff --git a/source/director.h b/source/director.h index 8ccb4cc..62871a8 100644 --- a/source/director.h +++ b/source/director.h @@ -18,6 +18,7 @@ #include "title.h" #include "game_over.h" #include "ending.h" +#include "ending2.h" #ifndef DIRECTOR_H #define DIRECTOR_H @@ -39,6 +40,7 @@ private: 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 + Ending2 *ending2; // 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 @@ -105,6 +107,9 @@ private: // Ejecuta la seccion del final del juego void runEnding(); + // Ejecuta la seccion del final del juego + void runEnding2(); + // Ejecuta la seccion del final de la partida void runGameOver(); diff --git a/source/ending.cpp b/source/ending.cpp index ca06ae6..1ca76b0 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -565,7 +565,7 @@ void Ending::checkChangeScene() if (scene == 5) { // Termina el bucle - section.name = SECTION_PROG_LOGO; + section.name = SECTION_PROG_ENDING2; // Mantiene los valores anteriores scene = 4; diff --git a/source/ending.h b/source/ending.h index ffe6ebd..278a3d1 100644 --- a/source/ending.h +++ b/source/ending.h @@ -1,17 +1,17 @@ #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/sprite.h" #include "common/text.h" #include "common/texture.h" #include "common/utils.h" #include "const.h" -#include #include +#include #ifndef ENDING_H #define ENDING_H diff --git a/source/ending2.cpp b/source/ending2.cpp new file mode 100644 index 0000000..7c98763 --- /dev/null +++ b/source/ending2.cpp @@ -0,0 +1,167 @@ +#include "ending2.h" + +// Constructor +Ending2::Ending2(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(); + text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); + music = JA_LoadMusic(asset->get("ending2.ogg").c_str()); + + // Inicializa variables + counter = -1; + preCounter = 0; + section.name = SECTION_PROG_ENDING2; + section.subsection = 0; + ticks = 0; + ticksSpeed = 15; + + // Cambia el color del borde + screen->setBorderColor(stringToColor(options->palette, "black")); +} + +// Destructor +Ending2::~Ending2() +{ + // Libera la memoria de los objetos + delete eventHandler; + delete text; + JA_DeleteMusic(music); +} + +// Actualiza el objeto +void Ending2::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(); + + // Actualiza los contadores + updateCounters(); + + if (counter > 400) + { + section.name = SECTION_PROG_LOGO; + } + } +} + +// Dibuja el final en pantalla +void Ending2::render() +{ + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(stringToColor(options->palette, "black")); + + text->write(0,0,std::to_string(counter)); + + // Vuelca el contenido del renderizador en pantalla + screen->blit(); +} + +// Comprueba el manejador de eventos +void Ending2::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: + break; + } + } + } +} + +// Bucle principal +section_t Ending2::run() +{ + JA_PlayMusic(music); + + while (section.name == SECTION_PROG_ENDING2) + { + update(); + render(); + } + + JA_StopMusic(); + + return section; +} + +// Actualiza los contadores +void Ending2::updateCounters() +{ + // Incrementa el contador + if (preCounter < 200) + { + preCounter++; + } + else + { + counter++; + } +} \ No newline at end of file diff --git a/source/ending2.h b/source/ending2.h new file mode 100644 index 0000000..79685b8 --- /dev/null +++ b/source/ending2.h @@ -0,0 +1,62 @@ +#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 +#include + +#ifndef ENDING2_H +#define ENDING2_H + +class Ending2 +{ +private: + // Objetos y punteros + Asset *asset; // Objeto con los ficheros de recursos + Resource *resource; // Objeto con los recursos + Screen *screen; // Objeto encargado de dibujar en pantalla + SDL_Event *eventHandler; // Manejador de eventos + SDL_Renderer *renderer; // El renderizador de la ventana + Text *text; // Objeto para escribir texto en pantalla + options_t *options; // Puntero a las opciones del juego + + // Variables + int counter; // Contador + int preCounter; // Contador previo + 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 + JA_Music music; // Musica que suena durante el final + + // Actualiza el objeto + void update(); + + // Dibuja el final en pantalla + void render(); + + // Comprueba el manejador de eventos + void checkEventHandler(); + + // Actualiza los contadores + void updateCounters(); + +public: + // Constructor + Ending2(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options); + + // Destructor + ~Ending2(); + + // Bucle principal + section_t run(); +}; + +#endif