#include "resource.h" #include // 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 list) { for (auto l : list) { if (options->console) { std::cout << "\nLOAD TEXTURE: " << l << std::endl; std::cout << "png: " << asset->get(l) << std::endl; } res_texture_t t; t.name = l; t.texture = new Texture(renderer, asset->get(l), options->console); textures.push_back(t); } } // Carga las animaciones desde una lista void Resource::loadAnimations(std::vector 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"; if (options->console) { std::cout << "\nLOAD ANIMATION: " << l << std::endl; std::cout << "png: " << asset->get(pngFile) << std::endl; std::cout << "ani: " << asset->get(l) << std::endl; } res_animation_t as; as.name = l; as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(pngFile), asset->get(l), options->console)); animations.push_back(as); } } // Carga los offsets desde una lista void Resource::loadOffsets(std::vector list) { for (auto l : list) { res_textOffset_t to; to.name = l; to.textFile = new textFile_t(LoadTextFile(asset->get(l), options->console)); offsets.push_back(to); } } // Carga los mapas de tiles desde una lista void Resource::loadTileMaps(std::vector list) { for (auto l : list) { res_tileMap_t tm; tm.name = l; tm.tileMap = new std::vector(loadRoomTileFile(asset->get(l), options->console)); tileMaps.push_back(tm); } } // Carga las habitaciones desde una lista void Resource::loadRooms(std::vector list) { for (auto l : list) { res_room_t r; r.name = l; r.room = new room_t(loadRoomFile(asset->get(l), options->console)); 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(); } } // Recarga las habitaciones void Resource::reLoadRooms() { for (auto r : rooms) { delete r.room; r.room = new room_t(loadRoomFile(asset->get(r.name))); 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"); } } // 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) if (texture.name == name) { // std::cout << "\nTEXTURE REQUESTED: " << name << std::endl; // std::cout << "served: " << texture.name << std::endl; return texture.texture; } } if (options->console) { 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) if (animation.name == name) { // std::cout << "\nANIMATION REQUESTED: " << name << std::endl; // std::cout << "served: " << animation.name << std::endl; return animation.animation; } } if (options->console) { 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) if (offset.name == name) { return offset.textFile; } } if (options->console) { std::cout << "NOT FOUND ON CACHE: " << name << std::endl; } return nullptr; } // Obtiene un mapa de tiles std::vector *Resource::getTileMap(std::string name) { for (auto tileMap : tileMaps) { // if (tileMap.name.find(name) != std::string::npos) if (tileMap.name == name) { return tileMap.tileMap; } } if (options->console) { 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) if (room.name == name) { return room.room; } } if (options->console) { std::cout << "NOT FOUND ON CACHE: " << name << std::endl; } return nullptr; } // Obtiene todas las habitaciones std::vector *Resource::getAllRooms() { return &rooms; }