From c1f0f90b161c8e3566e9732f731228eb650fd583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Thu, 27 Oct 2022 19:24:41 +0200 Subject: [PATCH] Arreglado un bug en el cambio de paleta durante el juego --- source/common/resource.cpp | 21 +++++++++++++++++++-- source/common/resource.h | 6 ++++++ source/director.cpp | 12 ++++++------ source/game.cpp | 1 - source/room.cpp | 11 +++++++++-- source/title.cpp | 8 ++++++-- 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/source/common/resource.cpp b/source/common/resource.cpp index fa56b09..198e1b7 100644 --- a/source/common/resource.cpp +++ b/source/common/resource.cpp @@ -27,8 +27,8 @@ void Resource::loadAnimations(std::vector list) for (auto l : list) { animation_t as; - as.name = l+".ani"; - as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(l+".png"), asset->get(l+".ani"))); + as.name = l + ".ani"; + as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(l + ".png"), asset->get(l + ".ani"))); animations.push_back(as); } } @@ -52,6 +52,23 @@ void Resource::freeTextures() textures.clear(); } +// Libera las animaciones +void Resource::freeAnimations() +{ + for (auto a : animations) + { + delete a.animation; + } + animations.clear(); +} + +// Libera todos los recursos +void Resource::free() +{ + freeTextures(); + freeAnimations(); +} + // Obtiene una textura Texture *Resource::getTexture(std::string name) { diff --git a/source/common/resource.h b/source/common/resource.h index 5e6679e..1760c27 100644 --- a/source/common/resource.h +++ b/source/common/resource.h @@ -51,6 +51,12 @@ public: // Libera las texturas void freeTextures(); + // Libera las animaciones + void freeAnimations(); + + // Libera todos los recursos + void free(); + // Obtiene una textura Texture *getTexture(std::string name); diff --git a/source/director.cpp b/source/director.cpp index aec301e..c63c7f0 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -845,7 +845,7 @@ void Director::runLogo() logo = new Logo(renderer, screen, resource, asset, options, section.subsection); setSection(logo->run()); delete logo; - resource->freeTextures(); + resource->free(); } // Ejecuta la seccion de juego de la introducción @@ -857,7 +857,7 @@ void Director::runIntro() intro = new Intro(renderer, screen, resource, asset, options); setSection(intro->run()); delete intro; - resource->freeTextures(); + resource->free(); } // Ejecuta la seccion de juego con el titulo y los menus @@ -873,7 +873,7 @@ void Director::runTitle() title = new Title(renderer, screen, resource, asset, options); setSection(title->run()); delete title; - resource->freeTextures(); + resource->free(); } // Ejecuta la seccion de los creditos del juego @@ -885,7 +885,7 @@ void Director::runCredits() credits = new Credits(renderer, screen, resource, asset, options); setSection(credits->run()); delete credits; - resource->freeTextures(); + resource->free(); } // Ejecuta la seccion de la demo, donde se ven pantallas del juego @@ -897,7 +897,7 @@ void Director::runDemo() demo = new Demo(renderer, screen, resource, asset, options, debug); setSection(demo->run()); delete demo; - resource->freeTextures(); + resource->free(); } // Ejecuta la seccion de juego donde se juega @@ -910,7 +910,7 @@ void Director::runGame() game = new Game(renderer, screen, resource, asset, options, input, debug); setSection(game->run()); delete game; - resource->freeTextures(); + resource->free(); } void Director::run() diff --git a/source/game.cpp b/source/game.cpp index 238bc7e..b87c7a6 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -158,7 +158,6 @@ void Game::checkEventHandler() case SDL_SCANCODE_F5: switchPalette(); - //reLoadTextures(); break; default: diff --git a/source/room.cpp b/source/room.cpp index 57550ae..610291d 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -778,11 +778,15 @@ void Room::reLoadTexture() { if (options->palette == p_zxspectrum) { - texture->loadFromFile(asset->get("standard.png"), renderer); + //texture->loadFromFile(asset->get("standard.png"), renderer); + texture = resource->getTexture("standard.png"); + texture->reLoad(); } else if (options->palette == p_zxarne) { - texture->loadFromFile(asset->get("standard_zxarne.png"), renderer); + //texture->loadFromFile(asset->get("standard_zxarne.png"), renderer); + texture = resource->getTexture("standard_zxarne.png"); + texture->reLoad(); } fillMapTexture(); @@ -815,6 +819,9 @@ void Room::reLoadPalette() // Establece el color del borde screen->setBorderColor(stringToColor(options->palette, borderColor)); + + // Recarga las texturas + reLoadTexture(); } // Obten el tamaño del tile diff --git a/source/title.cpp b/source/title.cpp index 267c398..89963c0 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -232,12 +232,16 @@ void Title::reLoadTextures() // Carga la textura adecuada if (options->palette == p_zxspectrum) { - texture->loadFromFile(asset->get("loading_screen_color.png"), renderer); + // texture->loadFromFile(asset->get("loading_screen_color.png"), renderer); + texture = resource->getTexture("loading_screen_color.png"); } else if (options->palette == p_zxarne) { - texture->loadFromFile(asset->get("loading_screen_color_zxarne.png"), renderer); + // texture->loadFromFile(asset->get("loading_screen_color_zxarne.png"), renderer); + texture = resource->getTexture("loading_screen_color_zxarne.png"); } + + texture->reLoad(); } // Cambia la paleta