Trabajando en la cache de animaciones

This commit is contained in:
2022-10-27 18:13:22 +02:00
parent 6186d1fac1
commit 35b4d19188
6 changed files with 110 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
#include "animatedsprite.h" #include "animatedsprite.h"
// Carga la animación desde un fichero // Carga la animación desde un fichero
animatedSprite_t loadFromFile(Texture *texture, std::string filePath) animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath)
{ {
// Inicializa variables // Inicializa variables
animatedSprite_t as; animatedSprite_t as;
@@ -80,7 +80,6 @@ animatedSprite_t loadFromFile(Texture *texture, std::string filePath)
// Añade la animación al vector de animaciones // Añade la animación al vector de animaciones
as.animations.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 // En caso contrario se parsea el fichero para buscar las variables y los valores
@@ -173,19 +172,19 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
} }
// Constructor // Constructor
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t as) AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation)
{ {
// Copia los punteros // Copia los punteros
setTexture(as.texture); setTexture(animation->texture);
setRenderer(renderer); setRenderer(renderer);
// Inicializa variables // Inicializa variables
currentAnimation = 0; currentAnimation = 0;
// Copia los datos de las animaciones // Copia los datos de las animaciones
for (auto animation : as.animations) for (auto a : animation->animations)
{ {
this->animation.push_back(animation); this->animation.push_back(a);
} }
} }

View File

@@ -29,7 +29,7 @@ struct animatedSprite_t
}; };
// Carga la animación desde un fichero // Carga la animación desde un fichero
animatedSprite_t loadFromFile(Texture *texture, std::string filePath); animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath);
class AnimatedSprite : public MovingSprite class AnimatedSprite : public MovingSprite
{ {
@@ -41,7 +41,7 @@ private:
public: public:
// Constructor // Constructor
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr); AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t as); AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation);
// Destructor // Destructor
~AnimatedSprite(); ~AnimatedSprite();

View File

@@ -9,10 +9,10 @@ Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options)
this->options = options; this->options = options;
} }
// Carga todos los recursos necesarios // Carga las texturas de una lista
void Resource::loadTextures(std::vector<std::string> list) void Resource::loadTextures(std::vector<std::string> list)
{ {
std::cout << "** LOAD RESOURCES" << std::endl; std::cout << "** LOAD TEXTURES" << std::endl;
for (auto l : list) for (auto l : list)
{ {
texture_t t; texture_t t;
@@ -20,7 +20,21 @@ void Resource::loadTextures(std::vector<std::string> list)
t.texture = new Texture(renderer, asset->get(t.name)); t.texture = new Texture(renderer, asset->get(t.name));
textures.push_back(t); textures.push_back(t);
} }
std::cout << "** RESOURCES LOADED" << std::endl; std::cout << "** TEXTURES LOADED" << std::endl;
}
// Carga las animaciones desde una lista
void Resource::loadAnimations(std::vector<std::string> list)
{
std::cout << "** LOAD ANIMATIONS" << std::endl;
for (auto l : list)
{
animation_t as;
as.name = l+".ani";
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(l+".png"), asset->get(l+".ani")));
animations.push_back(as);
}
std::cout << "** ANIMATIONS LOADED" << std::endl;
} }
// Recarga las texturas // Recarga las texturas
@@ -54,6 +68,22 @@ Texture *Resource::getTexture(std::string name)
} }
} }
std::cout << "NOT FOUND: " << name << std::endl;
return nullptr;
}
// Obtiene una animación
animatedSprite_t *Resource::getAnimation(std::string name)
{
for (auto animation : animations)
{
if (animation.name.find(name) != std::string::npos)
{
std::cout << "CACHE: " << name << std::endl;
return animation.animation;
}
}
std::cout << "NOT FOUND: " << name << std::endl; std::cout << "NOT FOUND: " << name << std::endl;
return nullptr; return nullptr;
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "animatedsprite.h"
#include "asset.h" #include "asset.h"
#include "texture.h" #include "texture.h"
#include "utils.h" #include "utils.h"
@@ -16,6 +17,12 @@ struct texture_t
Texture *texture; // La textura Texture *texture; // La textura
}; };
struct animation_t
{
std::string name; // Nombre de la textura
animatedSprite_t *animation; // La animación
};
class Resource class Resource
{ {
private: private:
@@ -26,13 +33,17 @@ private:
// Variables // Variables
std::vector<texture_t> textures; std::vector<texture_t> textures;
std::vector<animation_t> animations;
public: public:
// Constructor // Constructor
Resource(SDL_Renderer *renderer, Asset *asset, options_t *options); Resource(SDL_Renderer *renderer, Asset *asset, options_t *options);
// Carga todos los recursos necesarios // Carga las texturas de una lista
void loadTextures(std::vector<std::string> textureList); void loadTextures(std::vector<std::string> list);
// Carga las animaciones desde una lista
void loadAnimations(std::vector<std::string> list);
// Recarga las texturas // Recarga las texturas
void reLoadTextures(); void reLoadTextures();
@@ -41,7 +52,10 @@ public:
void freeTextures(); void freeTextures();
// Obtiene una textura // Obtiene una textura
Texture* getTexture(std::string name); Texture *getTexture(std::string name);
// Obtiene una animación
animatedSprite_t *getAnimation(std::string name);
}; };
#endif #endif

View File

@@ -14,7 +14,7 @@ Credits::Credits(SDL_Renderer *renderer, Screen *screen, Resource *resource, Ass
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer); text = new Text("smb2.png", asset->get("smb2.txt"), resource, renderer);
texture = resource->getTexture("shine.png"); texture = resource->getTexture("shine.png");
sprite = new AnimatedSprite(texture, renderer, asset->get("shine.ani")); sprite = new AnimatedSprite(renderer, resource->getAnimation("shine.ani"));
// Inicializa variables // Inicializa variables
counter = 0; counter = 0;

View File

@@ -221,13 +221,14 @@ void Director::loadResources(section_t section)
resource->loadTextures(textureList); resource->loadTextures(textureList);
std::vector<std::string> animationList; std::vector<std::string> animationList;
animationList.push_back("shine.ani"); animationList.push_back("shine");
resource->loadAnimations(animationList); resource->loadAnimations(animationList);
} }
else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO) else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO)
{ {
// Texturas
std::vector<std::string> textureList; std::vector<std::string> textureList;
// Jugador // Jugador
@@ -287,6 +288,56 @@ void Director::loadResources(section_t section)
textureList.push_back("debug.png"); textureList.push_back("debug.png");
resource->loadTextures(textureList); resource->loadTextures(textureList);
// Animaciones
std::vector<std::string> animationList;
// Jugador
animationList.push_back("player");
// Enemigos
animationList.push_back("paco");
animationList.push_back("chip");
animationList.push_back("wave");
animationList.push_back("wave_v");
animationList.push_back("sigmasua");
animationList.push_back("diskette");
animationList.push_back("bird");
animationList.push_back("bin");
animationList.push_back("qvoid");
animationList.push_back("batman");
animationList.push_back("tuno");
animationList.push_back("matatunos");
animationList.push_back("abad");
animationList.push_back("jailbattle_human");
animationList.push_back("jailbattle_alien");
animationList.push_back("jailer");
animationList.push_back("jailer2");
animationList.push_back("jailer3");
animationList.push_back("printer");
animationList.push_back("code");
animationList.push_back("demon");
animationList.push_back("dimallas");
animationList.push_back("dimallas_v");
animationList.push_back("heavy");
animationList.push_back("spider");
animationList.push_back("macaronni_ted");
animationList.push_back("mummy");
animationList.push_back("sam");
animationList.push_back("amstrad_character_set");
animationList.push_back("breakout");
animationList.push_back("lamp");
animationList.push_back("bry");
animationList.push_back("tv");
animationList.push_back("tv_panel");
animationList.push_back("arounders_door");
animationList.push_back("arounders_machine");
animationList.push_back("arounder_walk");
animationList.push_back("arounder_stop");
animationList.push_back("arounder_fly");
animationList.push_back("bat");
resource->loadAnimations(animationList);
} }
} }