forked from jaildesigner-jailgames/jaildoctors_dilemma
141 lines
2.9 KiB
C++
141 lines
2.9 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(l));
|
|
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);
|
|
}
|
|
}
|
|
|
|
// Carga los offsets desde una lista
|
|
void Resource::loadOffsets(std::vector<std::string> list)
|
|
{
|
|
for (auto l : list)
|
|
{
|
|
textOffset_t to;
|
|
to.name = l;
|
|
to.textFile = new textFile_t(LoadTextFile(asset->get(l)));
|
|
offsets.push_back(to);
|
|
}
|
|
}
|
|
|
|
// 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 los offsets
|
|
void Resource::freeOffsets()
|
|
{
|
|
for (auto o : offsets)
|
|
{
|
|
delete o.textFile;
|
|
}
|
|
offsets.clear();
|
|
}
|
|
|
|
// Libera todos los recursos
|
|
void Resource::free()
|
|
{
|
|
freeTextures();
|
|
freeAnimations();
|
|
freeOffsets();
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Obtiene un offset
|
|
textFile_t *Resource::getOffset(std::string name)
|
|
{
|
|
for (auto offset : offsets)
|
|
{
|
|
if (offset.name.find(name) != std::string::npos)
|
|
{
|
|
std::cout << "CACHE: " << name << std::endl;
|
|
return offset.textFile;
|
|
}
|
|
}
|
|
|
|
std::cout << "NOT FOUND: " << name << std::endl;
|
|
return nullptr;
|
|
} |