From c3fd348a619c1877e40417762b308e7fb51b6ebf Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 4 Oct 2022 12:13:47 +0200 Subject: [PATCH] Optimizada la carga de las animaciones de los globos. Cacheada --- source/animatedsprite.cpp | 38 ++++++++++++++++++-------------------- source/animatedsprite.h | 6 +++--- source/balloon.cpp | 4 ++-- source/balloon.h | 2 +- source/game.cpp | 32 ++++++++++++++------------------ source/game.h | 14 +++++++------- 6 files changed, 45 insertions(+), 51 deletions(-) diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 76008aa..9cb2ae6 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -1,7 +1,7 @@ #include "animatedsprite.h" // Constructor -AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file, std::stringstream *stream) +AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file, std::vector *buffer) { // Copia los punteros setTexture(texture); @@ -13,9 +13,9 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::s loadFromFile(file); } - else if (stream) + else if (buffer) { - loadFromStream(stream); + loadFromVector(buffer); } // Inicializa variables @@ -310,31 +310,26 @@ bool AnimatedSprite::loadFromFile(std::string filePath) return success; } -// Carga la animación desde un stream -bool AnimatedSprite::loadFromStream(std::stringstream *stream) +// Carga la animación desde un vector +bool AnimatedSprite::loadFromVector(std::vector *source) { + // Inicializa variables int framesPerRow = 0; int frameWidth = 0; int frameHeight = 0; int maxTiles = 0; - static int number = 0; - number++; - std::cout << "Reading stream #" << number << std::endl; - - // Indicador de éxito en la carga + // Indicador de éxito en el proceso bool success = true; - std::string line; - while (std::getline(*stream, line)) + // Recorre todo el vector + int index = 0; + while (index < source->size()) { - std::cout << "***: "<at(index); - // Procesa el fichero linea a linea - while (std::getline(*stream, line)) - { // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { @@ -345,7 +340,9 @@ bool AnimatedSprite::loadFromStream(std::stringstream *stream) do { - std::getline(*stream, line); + // Aumenta el indice para leer la siguiente linea + index++; + line = source->at(index); // Encuentra la posición del caracter '=' int pos = line.find("="); @@ -440,13 +437,14 @@ bool AnimatedSprite::loadFromStream(std::stringstream *stream) } } } + + // Una vez procesada la linea, aumenta el indice para pasar a la siguiente + index++; } // Pone un valor por defecto setPos({0, 0, frameWidth, frameHeight}); - std::cout << "Closing stream #" << number << std::endl; - return success; } diff --git a/source/animatedsprite.h b/source/animatedsprite.h index 2b49ec3..d8b6f33 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -31,7 +31,7 @@ private: public: // Constructor - AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::stringstream *stream = nullptr); + AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector *buffer = nullptr); // Destructor ~AnimatedSprite(); @@ -70,8 +70,8 @@ public: // Carga la animación desde un fichero bool loadFromFile(std::string filePath); - // Carga la animación desde un stream - bool loadFromStream(std::stringstream *stream); + // Carga la animación desde un vector + bool loadFromVector(std::vector *source); // Establece la animacion actual void setCurrentAnimation(std::string name = "default"); diff --git a/source/balloon.cpp b/source/balloon.cpp index 13fe97a..0c3d582 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -2,9 +2,9 @@ #include "balloon.h" // Constructor -Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::stringstream *stream, SDL_Renderer *renderer) +Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *buffer, SDL_Renderer *renderer) { - mSprite = new AnimatedSprite(texture, renderer, "", stream); + mSprite = new AnimatedSprite(texture, renderer, "", buffer); disable(); mEnabled = true; diff --git a/source/balloon.h b/source/balloon.h index 3cbda11..e19f6d7 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -141,7 +141,7 @@ private: public: // Constructor - Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::stringstream *stream, SDL_Renderer *renderer); + Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *buffer, SDL_Renderer *renderer); // Destructor ~Balloon(); diff --git a/source/game.cpp b/source/game.cpp index 287d40e..bd04039 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -48,10 +48,10 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr mTextureGameText = new LTexture(mRenderer, mAsset->get("game_text.png")); mTextureItems = new LTexture(mRenderer, mAsset->get("items.png")); - balloon1Animation = new std::stringstream; - balloon2Animation = new std::stringstream; - balloon3Animation = new std::stringstream; - balloon4Animation = new std::stringstream; + balloon1Animation = new std::vector; + balloon2Animation = new std::vector; + balloon3Animation = new std::vector; + balloon4Animation = new std::vector; loadAnimations(mAsset->get("balloon1.ani"), balloon1Animation); loadAnimations(mAsset->get("balloon2.ani"), balloon2Animation); @@ -1871,7 +1871,6 @@ void Game::popBalloon(Balloon *balloon) // En cualquier otro caso, crea dos globos de un tipo inferior default: - // Balloon *b1 = new Balloon(0, balloon->getPosY(), balloon->getKind() - 1, BALLOON_VELX_NEGATIVE, mEnemySpeed, 0, mRenderer); const int index = createBalloon(0, balloon->getPosY(), balloon->getKind() - 1, BALLOON_VELX_NEGATIVE, mEnemySpeed, 0); balloons.at(index)->allignTo(balloon->getPosX() + (balloon->getWidth() / 2)); if (balloons.at(index)->getClass() == BALLOON_CLASS) @@ -2035,8 +2034,6 @@ Uint8 Game::countBalloons() // Obtiene la textura correspondiente en funcion del tipo LTexture *Game::balloonTexture(int kind) { - std::cout << " ********** kind: " << kind << std::endl; - if (kind == 1 || kind == 5) { return balloon1Texture; @@ -2061,7 +2058,7 @@ LTexture *Game::balloonTexture(int kind) } // Obtiene la animacion correspondiente en funcion del tipo -std::stringstream *Game::balloonStreamAnimation(int kind) +std::vector *Game::balloonStreamAnimation(int kind) { if (kind == 1 || kind == 5) { @@ -2186,9 +2183,9 @@ void Game::checkPlayerItemCollision(Player *player) // Comprueba y procesa la colisión entre las balas y los globos void Game::checkBulletBalloonCollision() { - for (auto balloon : balloons) + for (auto bullet : bullets) { - for (auto bullet : bullets) + for (auto balloon : balloons) { if (balloon->isEnabled() && (!balloon->isInvulnerable()) && bullet->isActive()) { @@ -2227,6 +2224,7 @@ void Game::checkBulletBalloonCollision() mCoffeeMachineEnabled = true; } } + break; } } @@ -3623,20 +3621,18 @@ void Game::checkEventHandler() } // Carga las animaciones -void Game::loadAnimations(std::string filePath, std::stringstream *buffer) +void Game::loadAnimations(std::string filePath, std::vector *buffer) { std::ifstream file(filePath); + std::string line; if (file) { std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl; - *buffer << file.rdbuf(); + while (std::getline(file, line)) + { + buffer->push_back(line); + } file.close(); } - - // std::string line; - // while (std::getline(*buffer, line)) - //{ - // std::cout << line << std::endl; - // } } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 23035ab..d7f7460 100644 --- a/source/game.h +++ b/source/game.h @@ -18,7 +18,7 @@ #include "text.h" #include "utils.h" #include "writer.h" -#include "iostream" +#include #ifndef GAME_H #define GAME_H @@ -151,10 +151,10 @@ private: LTexture *mTextureGameText; // Textura para los sprites con textos LTexture *mTextureItems; // Textura para los items - std::stringstream *balloon1Animation; // Información para la animación de los globos - std::stringstream *balloon2Animation; // Información para la animación de los globos - std::stringstream *balloon3Animation; // Información para la animación de los globos - std::stringstream *balloon4Animation; // Información para la animación de los globos + std::vector *balloon1Animation; // Información para la animación de los globos + std::vector *balloon2Animation; // Información para la animación de los globos + std::vector *balloon3Animation; // Información para la animación de los globos + std::vector *balloon4Animation; // Información para la animación de los globos Text *mText; // Fuente para los textos del juego Text *mTextBig; // Fuente de texto grande @@ -363,7 +363,7 @@ private: LTexture *balloonTexture(int kind); // Obtiene la animacion correspondiente en funcion del tipo - std::stringstream *balloonStreamAnimation(int kind); + std::vector *balloonStreamAnimation(int kind); // Vacia el vector de globos void freeBalloons(); @@ -510,7 +510,7 @@ private: bool allPlayersAreDead(); // Carga las animaciones - void loadAnimations(std::string filePath, std::stringstream *buffer); + void loadAnimations(std::string filePath, std::vector *buffer); public: // Constructor