forked from jaildesigner-jailgames/jaildoctors_dilemma
102 lines
2.1 KiB
C++
102 lines
2.1 KiB
C++
#include "resource.h"
|
|
#include <iostream>
|
|
|
|
// Constructor
|
|
Resource::Resource(SDL_Renderer *renderer, Asset *asset, options_t *options)
|
|
{
|
|
this->renderer = renderer;
|
|
this->asset = asset;
|
|
this->options = options;
|
|
}
|
|
|
|
// Carga las texturas de una lista
|
|
void Resource::loadTextures(std::vector<std::string> list)
|
|
{
|
|
for (auto l : list)
|
|
{
|
|
texture_t t;
|
|
t.name = l;
|
|
t.texture = new Texture(renderer, asset->get(t.name));
|
|
textures.push_back(t);
|
|
}
|
|
}
|
|
|
|
// Carga las animaciones desde una lista
|
|
void Resource::loadAnimations(std::vector<std::string> 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")));
|
|
animations.push_back(as);
|
|
}
|
|
}
|
|
|
|
// Recarga las texturas
|
|
void Resource::reLoadTextures()
|
|
{
|
|
for (auto texture : textures)
|
|
{
|
|
texture.texture->reLoad();
|
|
}
|
|
}
|
|
|
|
// Libera las texturas
|
|
void Resource::freeTextures()
|
|
{
|
|
for (auto texture : textures)
|
|
{
|
|
delete texture.texture;
|
|
}
|
|
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)
|
|
{
|
|
for (auto texture : textures)
|
|
{
|
|
if (texture.name.find(name) != std::string::npos)
|
|
{
|
|
std::cout << "CACHE: " << name << std::endl;
|
|
return texture.texture;
|
|
}
|
|
}
|
|
|
|
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;
|
|
return nullptr;
|
|
} |