diff --git a/source/common/animatedsprite.cpp b/source/common/animatedsprite.cpp index 2889511..11fc79b 100644 --- a/source/common/animatedsprite.cpp +++ b/source/common/animatedsprite.cpp @@ -7,10 +7,17 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st setTexture(texture); setRenderer(renderer); + animatedSprite_t as; // Carga las animaciones if (file != "") { - loadFromFile(file); + as = loadFromFile(file); + + // Copia los datos de las animaciones + for (auto animation : as.animations) + { + this->animation.push_back(animation); + } } else if (buffer) @@ -22,6 +29,23 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st currentAnimation = 0; } +// Constructor +AnimatedSprite::AnimatedSprite(animatedSprite_t as) +{ + // Copia los punteros + setTexture(as.texture); + setRenderer(as.renderer); + + // Inicializa variables + currentAnimation = 0; + + // Copia los datos de las animaciones + for (auto animation : as.animations) + { + this->animation.push_back(animation); + } +} + // Destructor AnimatedSprite::~AnimatedSprite() { @@ -172,16 +196,15 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF) } // Carga la animación desde un fichero -bool AnimatedSprite::loadFromFile(std::string filePath) +animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath) { + // Inicializa variables + animatedSprite_t as; int framesPerRow = 0; int frameWidth = 0; int frameHeight = 0; int maxTiles = 0; - // Indicador de éxito en la carga - bool success = true; - const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1); std::ifstream file(filePath); std::string line; @@ -245,13 +268,13 @@ bool AnimatedSprite::loadFromFile(std::string filePath) else { printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); - success = false; } } } while (line != "[/animation]"); // Añade la animación al vector de animaciones - animation.push_back(buffer); + as.animations.push_back(buffer); + // animation.push_back(buffer); } // En caso contrario se parsea el fichero para buscar las variables y los valores @@ -281,7 +304,6 @@ bool AnimatedSprite::loadFromFile(std::string filePath) else { printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); - success = false; } // Normaliza valores @@ -307,13 +329,16 @@ bool AnimatedSprite::loadFromFile(std::string filePath) else { printf("Warning: Unable to open %s file\n", filename.c_str()); - success = false; } // Pone un valor por defecto setRect({0, 0, frameWidth, frameHeight}); - return success; + // Añade los punteros + as.texture = texture; + as.renderer = renderer; + + return as; } // Carga la animación desde un vector diff --git a/source/common/animatedsprite.h b/source/common/animatedsprite.h index c9bab9d..3f775ad 100644 --- a/source/common/animatedsprite.h +++ b/source/common/animatedsprite.h @@ -11,21 +11,27 @@ #ifndef ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H -// Clase AnimatedSprite +struct t_animation +{ + std::string name; // Nombre de la animacion + std::vector frames; // Cada uno de los frames que componen la animación + int speed; // Velocidad de la animación + int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva + bool completed; // Indica si ha finalizado la animación + int currentFrame; // Frame actual + int counter; // Contador para las animaciones +}; + +struct animatedSprite_t +{ + std::vector animations; // Vector con las diferentes animaciones + Texture *texture; // Textura con los graficos para el sprite + SDL_Renderer *renderer; // Renderizador para el sprite +}; + class AnimatedSprite : public MovingSprite { private: - struct t_animation - { - std::string name; // Nombre de la animacion - std::vector frames; // Cada uno de los frames que componen la animación - int speed; // Velocidad de la animación - int loop; // Indica a que frame vuelve la animación al terminar. -1 para que no vuelva - bool completed; // Indica si ha finalizado la animación - int currentFrame; // Frame actual - int counter; // Contador para las animaciones - }; - // Variables std::vector animation; // Vector con las diferentes animaciones int currentAnimation; // Animacion activa @@ -33,6 +39,7 @@ private: public: // Constructor AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector *buffer = nullptr); + AnimatedSprite(animatedSprite_t as); // Destructor ~AnimatedSprite(); @@ -72,7 +79,7 @@ public: int getIndex(std::string name); // Carga la animación desde un fichero - bool loadFromFile(std::string filePath); + animatedSprite_t loadFromFile(std::string filePath); // Carga la animación desde un vector bool loadFromVector(std::vector *source); diff --git a/source/game.cpp b/source/game.cpp index 6e50cc2..fc9456c 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -24,6 +24,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *opti spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; // **** + // Crea la cache de recursos + cacheResources(); + // Crea los objetos scoreboard = new ScoreBoard(renderer, asset, options, &board); itemTracker = new ItemTracker(); @@ -58,6 +61,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *opti Game::~Game() { + // Libera los recursos + freeCache(); + // Libera la memoria de los objetos delete scoreboard; delete itemTracker; @@ -453,4 +459,87 @@ void Game::renderBlackScreen() screen->clean(); screen->setBorderColor(stringToColor(options->palette, "black")); } +} + +// Carga todos los recursos necesarios +void Game::cacheResources() +{ + // Carga las texturas + std::vector textureList; + + // Jugador + textureList.push_back("player.png"); + + // Tilesets + textureList.push_back("standard.png"); + textureList.push_back("standard_zxarne.png"); + + // Enemigos + textureList.push_back("paco.png"); + textureList.push_back("chip.png"); + textureList.push_back("wave.png"); + textureList.push_back("wave_v.png"); + textureList.push_back("sigmasua.png"); + textureList.push_back("diskette.png"); + textureList.push_back("bird.png"); + textureList.push_back("bin.png"); + textureList.push_back("qvoid.png"); + textureList.push_back("batman.png"); + textureList.push_back("tuno.png"); + textureList.push_back("matatunos.png"); + textureList.push_back("abad.png"); + textureList.push_back("jailbattle_human.png"); + textureList.push_back("jailbattle_alien.png"); + textureList.push_back("jailer.png"); + textureList.push_back("jailer2.png"); + textureList.push_back("jailer3.png"); + textureList.push_back("printer.png"); + textureList.push_back("code.png"); + textureList.push_back("demon.png"); + textureList.push_back("dimallas.png"); + textureList.push_back("dimallas_v.png"); + textureList.push_back("heavy.png"); + textureList.push_back("spider.png"); + textureList.push_back("macaronni_ted.png"); + textureList.push_back("mummy.png"); + textureList.push_back("sam.png"); + textureList.push_back("amstrad_character_set.png"); + textureList.push_back("breakout.png"); + textureList.push_back("lamp.png"); + textureList.push_back("bry.png"); + textureList.push_back("tv.png"); + textureList.push_back("tv_panel.png"); + textureList.push_back("arounders_door.png"); + textureList.push_back("arounders_machine.png"); + textureList.push_back("arounder_walk.png"); + textureList.push_back("arounder_stop.png"); + textureList.push_back("arounder_fly.png"); + textureList.push_back("bat.png"); + + // Items + textureList.push_back("items.png"); + + // Texto + textureList.push_back("smb2.png"); + textureList.push_back("debug.png"); + + for (auto list : textureList) + { + texture_t t; + t.name = list; + t.texture = new Texture(renderer, asset->get(t.name)); + textures.push_back(t); + } +} + +// Libera los recursos +void Game::freeCache() +{ + // Libera las texturas + for (auto texture : textures) + { + texture.texture->unload(); + delete texture.texture; + } + textures.clear(); } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 07902ed..21f3b6c 100644 --- a/source/game.h +++ b/source/game.h @@ -24,6 +24,12 @@ class Game { private: + struct texture_t + { + std::string name; // Nombre de la textura + Texture *texture; // La textura + }; + // Objetos y punteros SDL_Renderer *renderer; // El renderizador de la ventana SDL_Event *eventHandler; // Manejador de eventos @@ -40,6 +46,8 @@ private: options_t *options; // Puntero a las opciones del juego Test *test; + std::vector textures; + // Variables JA_Music music; // Musica que suena durante el juego Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa @@ -107,6 +115,12 @@ private: // Dibuja la pantalla negra void renderBlackScreen(); + // Carga todos los recursos necesarios + void cacheResources(); + + // Libera los recursos + void freeCache(); + public: // Constructor Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, options_t *options, Input *input, Debug *debug);