Files
jaildoctors_dilemma/source/common/resource.cpp

229 lines
4.8 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)
{
res_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)
{
// Extrae el nombre del fichero sin la extension para crear el nombre del fichero de la textura
const size_t lastIndex = l.find_last_of(".");
const std:: string pngFile = l.substr(0, lastIndex)+".png";
res_animation_t as;
as.name = l;
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(l)));
animations.push_back(as);
}
}
// Carga los offsets desde una lista
void Resource::loadOffsets(std::vector<std::string> list)
{
for (auto l : list)
{
res_textOffset_t to;
to.name = l;
to.textFile = new textFile_t(LoadTextFile(asset->get(l)));
offsets.push_back(to);
}
}
// Carga los mapas de tiles desde una lista
void Resource::loadTileMaps(std::vector<std::string> list)
{
for (auto l : list)
{
res_tileMap_t tm;
tm.name = l;
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(l)));
tileMaps.push_back(tm);
}
}
// Carga las habitaciones desde una lista
void Resource::loadRooms(std::vector<std::string> list)
{
for (auto l : list)
{
res_room_t r;
r.name = l;
r.room = new room_t(loadRoomFile(asset->get(l)));
r.room->tileMap = getTileMap(r.room->tileMapFile);
for (auto &e:r.room->enemies)
{
e.animation = getAnimation(e.animationString);
}
for (auto &i:r.room->items)
{
i.texture = getTexture(i.tileSetFile);
}
r.room->textureA = getTexture("standard.png");
r.room->textureB = getTexture("standard_zxarne.png");
rooms.push_back(r);
}
}
// 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 los mapas de tiles
void Resource::freeTileMaps()
{
for (auto t : tileMaps)
{
delete t.tileMap;
}
tileMaps.clear();
}
// Libera las habitaciones
void Resource::freeRooms()
{
for (auto r : rooms)
{
delete r.room;
}
rooms.clear();
}
// Libera todos los recursos
void Resource::free()
{
freeTextures();
freeAnimations();
freeOffsets();
freeTileMaps();
freeRooms();
}
// Obtiene una textura
Texture *Resource::getTexture(std::string name)
{
for (auto texture : textures)
{
if (texture.name.find(name) != std::string::npos)
{
return texture.texture;
}
}
std::cout << "NOT FOUND ON CACHE: " << 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)
{
return animation.animation;
}
}
std::cout << "NOT FOUND ON CACHE: " << 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)
{
return offset.textFile;
}
}
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
// Obtiene un mapa de tiles
std::vector<int> *Resource::getTileMap(std::string name)
{
for (auto tileMap : tileMaps)
{
if (tileMap.name.find(name) != std::string::npos)
{
return tileMap.tileMap;
}
}
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
// Obtiene una habitacion
room_t *Resource::getRoom(std::string name)
{
for (auto room : rooms)
{
if (room.name.find(name) != std::string::npos)
{
return room.room;
}
}
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}