diff --git a/data/ending/ending4.png b/data/ending/ending4.png index c7cb6f7..80fb8d9 100644 Binary files a/data/ending/ending4.png and b/data/ending/ending4.png differ diff --git a/source/ending.cpp b/source/ending.cpp index 1c09a94..5fc0fbb 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -13,12 +13,12 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset // Reserva memoria para los punteros a objetos eventHandler = new SDL_Event(); text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); - texture = resource->getTexture("ending1.png"); - sprite = new Sprite({0, 0, texture->getWidth(), texture->getHeight()}, texture, renderer); music = JA_LoadMusic(asset->get("ending1.ogg").c_str()); // Inicializa variables - counter = 0; + counter = -1; + preCounter = 0; + coverCounter = 0; section.name = SECTION_PROG_ENDING; section.subsection = 0; ticks = 0; @@ -37,17 +37,6 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset // Cambia el color del borde screen->setBorderColor(stringToColor(options->palette, "black")); - // Crea la textura para el texto que se escribe en pantalla - canvasTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); - if (canvasTexture == nullptr) - { - if (options->console) - { - std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - SDL_SetTextureBlendMode(canvasTexture, SDL_BLENDMODE_BLEND); - // Crea la textura para cubrir el rexto coverTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT + 8); if (coverTexture == nullptr) @@ -58,6 +47,9 @@ Ending::Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset } } SDL_SetTextureBlendMode(coverTexture, SDL_BLENDMODE_BLEND); + + // Rellena la textura para la cortinilla + fillCoverTexture(); } // Destructor @@ -66,8 +58,6 @@ Ending::~Ending() // Libera la memoria de los objetos delete eventHandler; delete text; - delete sprite; - SDL_DestroyTexture(canvasTexture); SDL_DestroyTexture(coverTexture); for (auto st : spriteTexts) @@ -103,7 +93,7 @@ void Ending::update() checkEventHandler(); // Actualiza el contador - updateCounter(); + updateCounters(); // Actualiza las cortinillas de los elementos updateSpriteCovers(); @@ -136,6 +126,9 @@ void Ending::render() } } + // Dibuja la cortinilla de cambio de escena + renderCoverTexture(); + // text->write(0, 0, std::to_string(counter)); // Vuelca el contenido del renderizador en pantalla @@ -221,8 +214,8 @@ void Ending::iniTexts() // Escena #0 texts.push_back({"HE FINALLY MANAGED", 32}); - texts.push_back({"TO GET TO THE JAIL", 43}); - texts.push_back({"WITH ALL HIS PROJECTS", 141}); + texts.push_back({"TO GET TO THE JAIL", 42}); + texts.push_back({"WITH ALL HIS PROJECTS", 142}); texts.push_back({"READY TO BE RELEASED", 152}); // Escena #1 @@ -237,15 +230,15 @@ void Ending::iniTexts() // Escena #2 texts.push_back({"BUT SUDDENLY SOMETHING", 19}); - texts.push_back({"CAUGHT HIS ATTENTION", 31}); + texts.push_back({"CAUGHT HIS ATTENTION", 29}); // Escena #3 texts.push_back({"A PILE OF JUNK!", 36}); - texts.push_back({"FULL OF NON WORKING THINGS!!", 49}); + texts.push_back({"FULL OF NON WORKING THINGS!!", 46}); // Escena #4 - texts.push_back({"AND THEN,", 26}); - texts.push_back({"FOURTY NEW PROJECTS", 36}); + texts.push_back({"AND THEN,", 36}); + texts.push_back({"FOURTY NEW PROJECTS", 46}); texts.push_back({"WERE BORN...", 158}); // Crea los sprites @@ -435,7 +428,7 @@ void Ending::iniScenes() sc.textIndex.push_back({5, trigger}); trigger += lapse; sc.textIndex.push_back({6, trigger}); - trigger += lapse * 2; + trigger += lapse * 3; sc.textIndex.push_back({7, trigger}); trigger += lapse; sc.textIndex.push_back({8, trigger}); @@ -447,8 +440,8 @@ void Ending::iniScenes() sc.counterEnd = 1000; sc.pictureIndex = 2; sc.textIndex.clear(); - trigger = 148 * 2; - //trigger += lapse / 2; + trigger = 148 / 2; + trigger += lapse; sc.textIndex.push_back({10, trigger}); trigger += lapse; sc.textIndex.push_back({11, trigger}); @@ -458,10 +451,10 @@ void Ending::iniScenes() sc.counterEnd = 800; sc.pictureIndex = 3; sc.textIndex.clear(); - trigger = 87 * 2; - //trigger += lapse; + trigger = 87 / 2; + trigger += lapse; sc.textIndex.push_back({12, trigger}); - trigger += lapse/2; + trigger += lapse / 2; sc.textIndex.push_back({13, trigger}); scenes.push_back(sc); @@ -495,11 +488,23 @@ section_t Ending::run() return section; } -// Actualiza el contador -void Ending::updateCounter() +// Actualiza los contadores +void Ending::updateCounters() { // Incrementa el contador - counter++; + if (preCounter < 200) + { + preCounter++; + } + else + { + counter++; + } + + if (counter > scenes.at(scene).counterEnd - 100) + { + coverCounter++; + } } // Actualiza las cortinillas de los elementos @@ -552,9 +557,57 @@ void Ending::checkChangeScene() { scene++; counter = 0; + coverCounter = 0; if (scene == 5) { + scene = 4; section.name = SECTION_PROG_QUIT; } } +} + +// Rellena la textura para la cortinilla +void Ending::fillCoverTexture() +{ + // Rellena la textura que cubre el texto con color transparente + SDL_SetRenderTarget(renderer, coverTexture); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderClear(renderer); + + // Los primeros 8 pixels crea una malla + const color_t c = stringToColor(options->palette, "brack"); + SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, 0xFF); + for (int i = 0; i < 256; i += 2) + { + SDL_RenderDrawPoint(renderer, i + 0, GAMECANVAS_HEIGHT + 0); + SDL_RenderDrawPoint(renderer, i + 1, GAMECANVAS_HEIGHT + 1); + SDL_RenderDrawPoint(renderer, i + 0, GAMECANVAS_HEIGHT + 2); + SDL_RenderDrawPoint(renderer, i + 1, GAMECANVAS_HEIGHT + 3); + + SDL_RenderDrawPoint(renderer, i, GAMECANVAS_HEIGHT + 4); + SDL_RenderDrawPoint(renderer, i, GAMECANVAS_HEIGHT + 6); + } + + // El resto se rellena de color sólido + SDL_Rect rect = {0, 0, 256, GAMECANVAS_HEIGHT}; + SDL_RenderFillRect(renderer, &rect); + + SDL_SetRenderTarget(renderer, nullptr); +} + +// Dibuja la cortinilla de cambio de escena +void Ending::renderCoverTexture() +{ + if (coverCounter > 0) + { // Dibuja la textura que cubre el texto + // const int offset = std::min(coverCounter, 200 / 2); + // SDL_Rect srcRect = {0, 0, 256, 200 - (offset * 2)}; + // SDL_Rect dstRect = {0, offset * 2, 256, 200 - (offset * 2)}; + // SDL_RenderCopy(renderer, coverTexture, &srcRect, &dstRect); + + const int offset = std::min(coverCounter, 100); + SDL_Rect srcRect = {0, 200 - (coverCounter * 2), 256, offset * 2}; + SDL_Rect dstRect = {0, 0, 256, offset * 2}; + SDL_RenderCopy(renderer, coverTexture, &srcRect, &dstRect); + } } \ No newline at end of file diff --git a/source/ending.h b/source/ending.h index eced76c..d0cb336 100644 --- a/source/ending.h +++ b/source/ending.h @@ -50,20 +50,19 @@ 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 *canvasTexture; // Textura para dibujar el texto y los dibujos - SDL_Texture *coverTexture; // Textura para cubrir el texto - Texture *texture; // Textura con los graficos - Sprite *sprite; // Sprite para dibujar las imagenes + 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 *coverTexture; // Textura para cubrir el texto // Variables int counter; // Contador + int preCounter; // Contador previo + int coverCounter; // Contador para la cortinilla 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 @@ -91,8 +90,8 @@ private: // Inicializa las escenas void iniScenes(); - // Actualiza el contador - void updateCounter(); + // Actualiza los contadores + void updateCounters(); // Actualiza las cortinillas de los elementos void updateSpriteCovers(); @@ -100,6 +99,12 @@ private: // Comprueba si se ha de cambiar de escena void checkChangeScene(); + // Rellena la textura para la cortinilla + void fillCoverTexture(); + + // Dibuja la cortinilla de cambio de escena + void renderCoverTexture(); + public: // Constructor Ending(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options);