Optimizada la carga de las animaciones de los globos. Cacheada

This commit is contained in:
2022-10-04 12:13:47 +02:00
parent ddb70c8c85
commit c3fd348a61
6 changed files with 45 additions and 51 deletions

View File

@@ -1,7 +1,7 @@
#include "animatedsprite.h" #include "animatedsprite.h"
// Constructor // 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<std::string> *buffer)
{ {
// Copia los punteros // Copia los punteros
setTexture(texture); setTexture(texture);
@@ -13,9 +13,9 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::s
loadFromFile(file); loadFromFile(file);
} }
else if (stream) else if (buffer)
{ {
loadFromStream(stream); loadFromVector(buffer);
} }
// Inicializa variables // Inicializa variables
@@ -310,31 +310,26 @@ bool AnimatedSprite::loadFromFile(std::string filePath)
return success; return success;
} }
// Carga la animación desde un stream // Carga la animación desde un vector
bool AnimatedSprite::loadFromStream(std::stringstream *stream) bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
{ {
// Inicializa variables
int framesPerRow = 0; int framesPerRow = 0;
int frameWidth = 0; int frameWidth = 0;
int frameHeight = 0; int frameHeight = 0;
int maxTiles = 0; int maxTiles = 0;
static int number = 0; // Indicador de éxito en el proceso
number++;
std::cout << "Reading stream #" << number << std::endl;
// Indicador de éxito en la carga
bool success = true; bool success = true;
std::string line; std::string line;
while (std::getline(*stream, line)) // Recorre todo el vector
int index = 0;
while (index < source->size())
{ {
std::cout << "***: "<<line << std::endl; // Lee desde el vector
} line = source->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 // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]") if (line == "[animation]")
{ {
@@ -345,7 +340,9 @@ bool AnimatedSprite::loadFromStream(std::stringstream *stream)
do do
{ {
std::getline(*stream, line); // Aumenta el indice para leer la siguiente linea
index++;
line = source->at(index);
// Encuentra la posición del caracter '=' // Encuentra la posición del caracter '='
int pos = line.find("="); 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 // Pone un valor por defecto
setPos({0, 0, frameWidth, frameHeight}); setPos({0, 0, frameWidth, frameHeight});
std::cout << "Closing stream #" << number << std::endl;
return success; return success;
} }

View File

@@ -31,7 +31,7 @@ private:
public: public:
// Constructor // 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<std::string> *buffer = nullptr);
// Destructor // Destructor
~AnimatedSprite(); ~AnimatedSprite();
@@ -70,8 +70,8 @@ public:
// Carga la animación desde un fichero // Carga la animación desde un fichero
bool loadFromFile(std::string filePath); bool loadFromFile(std::string filePath);
// Carga la animación desde un stream // Carga la animación desde un vector
bool loadFromStream(std::stringstream *stream); bool loadFromVector(std::vector<std::string> *source);
// Establece la animacion actual // Establece la animacion actual
void setCurrentAnimation(std::string name = "default"); void setCurrentAnimation(std::string name = "default");

View File

@@ -2,9 +2,9 @@
#include "balloon.h" #include "balloon.h"
// Constructor // 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<std::string> *buffer, SDL_Renderer *renderer)
{ {
mSprite = new AnimatedSprite(texture, renderer, "", stream); mSprite = new AnimatedSprite(texture, renderer, "", buffer);
disable(); disable();
mEnabled = true; mEnabled = true;

View File

@@ -141,7 +141,7 @@ private:
public: public:
// Constructor // 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<std::string> *buffer, SDL_Renderer *renderer);
// Destructor // Destructor
~Balloon(); ~Balloon();

View File

@@ -48,10 +48,10 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr
mTextureGameText = new LTexture(mRenderer, mAsset->get("game_text.png")); mTextureGameText = new LTexture(mRenderer, mAsset->get("game_text.png"));
mTextureItems = new LTexture(mRenderer, mAsset->get("items.png")); mTextureItems = new LTexture(mRenderer, mAsset->get("items.png"));
balloon1Animation = new std::stringstream; balloon1Animation = new std::vector<std::string>;
balloon2Animation = new std::stringstream; balloon2Animation = new std::vector<std::string>;
balloon3Animation = new std::stringstream; balloon3Animation = new std::vector<std::string>;
balloon4Animation = new std::stringstream; balloon4Animation = new std::vector<std::string>;
loadAnimations(mAsset->get("balloon1.ani"), balloon1Animation); loadAnimations(mAsset->get("balloon1.ani"), balloon1Animation);
loadAnimations(mAsset->get("balloon2.ani"), balloon2Animation); 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 // En cualquier otro caso, crea dos globos de un tipo inferior
default: 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); const int index = createBalloon(0, balloon->getPosY(), balloon->getKind() - 1, BALLOON_VELX_NEGATIVE, mEnemySpeed, 0);
balloons.at(index)->allignTo(balloon->getPosX() + (balloon->getWidth() / 2)); balloons.at(index)->allignTo(balloon->getPosX() + (balloon->getWidth() / 2));
if (balloons.at(index)->getClass() == BALLOON_CLASS) if (balloons.at(index)->getClass() == BALLOON_CLASS)
@@ -2035,8 +2034,6 @@ Uint8 Game::countBalloons()
// Obtiene la textura correspondiente en funcion del tipo // Obtiene la textura correspondiente en funcion del tipo
LTexture *Game::balloonTexture(int kind) LTexture *Game::balloonTexture(int kind)
{ {
std::cout << " ********** kind: " << kind << std::endl;
if (kind == 1 || kind == 5) if (kind == 1 || kind == 5)
{ {
return balloon1Texture; return balloon1Texture;
@@ -2061,7 +2058,7 @@ LTexture *Game::balloonTexture(int kind)
} }
// Obtiene la animacion correspondiente en funcion del tipo // Obtiene la animacion correspondiente en funcion del tipo
std::stringstream *Game::balloonStreamAnimation(int kind) std::vector<std::string> *Game::balloonStreamAnimation(int kind)
{ {
if (kind == 1 || kind == 5) 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 // Comprueba y procesa la colisión entre las balas y los globos
void Game::checkBulletBalloonCollision() 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()) if (balloon->isEnabled() && (!balloon->isInvulnerable()) && bullet->isActive())
{ {
@@ -2227,6 +2224,7 @@ void Game::checkBulletBalloonCollision()
mCoffeeMachineEnabled = true; mCoffeeMachineEnabled = true;
} }
} }
break; break;
} }
} }
@@ -3623,20 +3621,18 @@ void Game::checkEventHandler()
} }
// Carga las animaciones // Carga las animaciones
void Game::loadAnimations(std::string filePath, std::stringstream *buffer) void Game::loadAnimations(std::string filePath, std::vector<std::string> *buffer)
{ {
std::ifstream file(filePath); std::ifstream file(filePath);
std::string line;
if (file) if (file)
{ {
std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl; 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(); file.close();
} }
// std::string line;
// while (std::getline(*buffer, line))
//{
// std::cout << line << std::endl;
// }
} }

View File

@@ -18,7 +18,7 @@
#include "text.h" #include "text.h"
#include "utils.h" #include "utils.h"
#include "writer.h" #include "writer.h"
#include "iostream" #include <iostream>
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
@@ -151,10 +151,10 @@ private:
LTexture *mTextureGameText; // Textura para los sprites con textos LTexture *mTextureGameText; // Textura para los sprites con textos
LTexture *mTextureItems; // Textura para los items LTexture *mTextureItems; // Textura para los items
std::stringstream *balloon1Animation; // Información para la animación de los globos std::vector<std::string> *balloon1Animation; // Información para la animación de los globos
std::stringstream *balloon2Animation; // Información para la animación de los globos std::vector<std::string> *balloon2Animation; // Información para la animación de los globos
std::stringstream *balloon3Animation; // Información para la animación de los globos std::vector<std::string> *balloon3Animation; // Información para la animación de los globos
std::stringstream *balloon4Animation; // Información para la animación de los globos std::vector<std::string> *balloon4Animation; // Información para la animación de los globos
Text *mText; // Fuente para los textos del juego Text *mText; // Fuente para los textos del juego
Text *mTextBig; // Fuente de texto grande Text *mTextBig; // Fuente de texto grande
@@ -363,7 +363,7 @@ private:
LTexture *balloonTexture(int kind); LTexture *balloonTexture(int kind);
// Obtiene la animacion correspondiente en funcion del tipo // Obtiene la animacion correspondiente en funcion del tipo
std::stringstream *balloonStreamAnimation(int kind); std::vector<std::string> *balloonStreamAnimation(int kind);
// Vacia el vector de globos // Vacia el vector de globos
void freeBalloons(); void freeBalloons();
@@ -510,7 +510,7 @@ private:
bool allPlayersAreDead(); bool allPlayersAreDead();
// Carga las animaciones // Carga las animaciones
void loadAnimations(std::string filePath, std::stringstream *buffer); void loadAnimations(std::string filePath, std::vector<std::string> *buffer);
public: public:
// Constructor // Constructor