forked from jaildesigner-jailgames/jaildoctors_dilemma
Afegint smart pointers
Actualitzat Resources Actualitzades les classes Sprite i derivades Afegida nova tipografia Actualitzat Asset Actualitzat Text
This commit is contained in:
167
source/asset.cpp
167
source/asset.cpp
@@ -1,86 +1,77 @@
|
||||
#include "asset.h"
|
||||
#include <SDL2/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWops
|
||||
#include <SDL2/SDL_stdinc.h> // Para SDL_max
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <iostream> // Para basic_ostream, operator<<, cout, endl
|
||||
#include <algorithm> // Para find_if, max
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream, endl
|
||||
#include <iostream> // Para cout
|
||||
#include <string> // Para allocator, char_traits, string, operator+, oper...
|
||||
#include "utils.h" // Para getFileName, printWithDots
|
||||
|
||||
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
||||
Asset *Asset::asset_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
// [SINGLETON] Crearemos el objeto asset con esta función estática
|
||||
void Asset::init(const std::string &executable_path)
|
||||
{
|
||||
Asset::asset_ = new Asset(executable_path);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
// [SINGLETON] Destruiremos el objeto asset con esta función estática
|
||||
void Asset::destroy()
|
||||
{
|
||||
delete Asset::asset_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él
|
||||
Asset *Asset::get()
|
||||
{
|
||||
return Asset::asset_;
|
||||
}
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void Asset::add(std::string file, enum assetType type, bool required, bool absolute)
|
||||
void Asset::add(const std::string &file, AssetType type, bool required, bool absolute)
|
||||
{
|
||||
item_t temp;
|
||||
temp.file = absolute ? file : executable_path_ + file;
|
||||
temp.type = type;
|
||||
temp.required = required;
|
||||
fileList.push_back(temp);
|
||||
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||
longest_name_ = SDL_max(longest_name_, filename.size());
|
||||
file_list_.emplace_back(absolute ? file : executable_path_ + file, type, required);
|
||||
longest_name_ = std::max(longest_name_, static_cast<int>(file_list_.back().file.size()));
|
||||
}
|
||||
|
||||
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||
std::string Asset::get(std::string text)
|
||||
// Devuelve la ruta completa a un fichero a partir de una cadena
|
||||
std::string Asset::get(const std::string &text) const
|
||||
{
|
||||
for (auto f : fileList)
|
||||
{
|
||||
const size_t lastIndex = f.file.find_last_of("/") + 1;
|
||||
const std::string file = f.file.substr(lastIndex, std::string::npos);
|
||||
auto it = std::find_if(file_list_.begin(), file_list_.end(),
|
||||
[&text](const auto &f)
|
||||
{
|
||||
return getFileName(f.file) == text;
|
||||
});
|
||||
|
||||
if (file == text)
|
||||
{
|
||||
return f.file;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose_)
|
||||
if (it != file_list_.end())
|
||||
{
|
||||
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
|
||||
return it->file;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Warning: file " << text << " not found" << std::endl;
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Comprueba que existen todos los elementos
|
||||
bool Asset::check()
|
||||
bool Asset::check() const
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "\n** Checking files" << std::endl;
|
||||
std::cout << "\n** CHECKING FILES" << std::endl;
|
||||
|
||||
std::cout << "Executable path is: " << executable_path_ << std::endl;
|
||||
std::cout << "Sample filepath: " << fileList.back().file << std::endl;
|
||||
}
|
||||
// std::cout << "Executable path is: " << executable_path_ << std::endl;
|
||||
// std::cout << "Sample filepath: " << file_list_.back().file << std::endl;
|
||||
|
||||
// Comprueba la lista de ficheros clasificandolos por tipo
|
||||
for (int type = 0; type < t_maxAssetType; ++type)
|
||||
for (int type = 0; type < static_cast<int>(AssetType::MAX_ASSET_TYPE); ++type)
|
||||
{
|
||||
// Comprueba si hay ficheros de ese tipo
|
||||
bool any = false;
|
||||
|
||||
for (auto f : fileList)
|
||||
for (const auto &f : file_list_)
|
||||
{
|
||||
if ((f.required) && (f.type == type))
|
||||
if (f.required && f.type == static_cast<AssetType>(type))
|
||||
{
|
||||
any = true;
|
||||
}
|
||||
@@ -89,107 +80,77 @@ bool Asset::check()
|
||||
// Si hay ficheros de ese tipo, comprueba si existen
|
||||
if (any)
|
||||
{
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
|
||||
}
|
||||
std::cout << "\n>> " << getTypeName(static_cast<AssetType>(type)).c_str() << " FILES" << std::endl;
|
||||
|
||||
for (auto f : fileList)
|
||||
for (const auto &f : file_list_)
|
||||
{
|
||||
if ((f.required) && (f.type == type))
|
||||
if (f.required && f.type == static_cast<AssetType>(type))
|
||||
{
|
||||
success &= checkFile(f.file);
|
||||
}
|
||||
}
|
||||
if (success)
|
||||
std::cout << " All files are OK." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Resultado
|
||||
if (verbose_)
|
||||
{
|
||||
if (success)
|
||||
{
|
||||
std::cout << "\n** All files OK.\n"
|
||||
<< std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "\n** A file is missing. Exiting.\n"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << (success ? "\n** CHECKING FILES COMPLETED.\n" : "\n** CHECKING FILES FAILED.\n") << std::endl;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool Asset::checkFile(std::string path)
|
||||
bool Asset::checkFile(const std::string &path) const
|
||||
{
|
||||
bool success = false;
|
||||
std::string result = "ERROR";
|
||||
std::ifstream file(path);
|
||||
bool success = file.good();
|
||||
file.close();
|
||||
|
||||
// Comprueba si existe el fichero
|
||||
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb");
|
||||
|
||||
if (file != nullptr)
|
||||
{
|
||||
result = "OK";
|
||||
success = true;
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
if (verbose_)
|
||||
{
|
||||
std::cout.setf(std::ios::left, std::ios::adjustfield);
|
||||
std::cout << "Checking file: ";
|
||||
std::cout.width(longest_name_ + 2);
|
||||
std::cout.fill('.');
|
||||
std::cout << filename + " ";
|
||||
std::cout << " [" + result + "]" << std::endl;
|
||||
}
|
||||
if (!success)
|
||||
printWithDots("Checking file : ", getFileName(path), "[ ERROR ]");
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Devuelve el nombre del tipo de recurso
|
||||
std::string Asset::getTypeName(int type)
|
||||
std::string Asset::getTypeName(AssetType type) const
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case t_bitmap:
|
||||
case AssetType::BITMAP:
|
||||
return "BITMAP";
|
||||
break;
|
||||
|
||||
case t_music:
|
||||
case AssetType::MUSIC:
|
||||
return "MUSIC";
|
||||
break;
|
||||
|
||||
case t_sound:
|
||||
case AssetType::SOUND:
|
||||
return "SOUND";
|
||||
break;
|
||||
|
||||
case t_font:
|
||||
case AssetType::FONT:
|
||||
return "FONT";
|
||||
break;
|
||||
|
||||
case t_lang:
|
||||
case AssetType::LANG:
|
||||
return "LANG";
|
||||
break;
|
||||
|
||||
case t_data:
|
||||
case AssetType::DATA:
|
||||
return "DATA";
|
||||
break;
|
||||
|
||||
case t_room:
|
||||
return "ROOM";
|
||||
case AssetType::ANIMATION:
|
||||
return "ANIMATION";
|
||||
break;
|
||||
|
||||
case t_enemy:
|
||||
return "ENEMY";
|
||||
case AssetType::PALETTE:
|
||||
return "PALETTE";
|
||||
break;
|
||||
|
||||
case t_item:
|
||||
case AssetType::ITEM:
|
||||
return "ITEM";
|
||||
break;
|
||||
|
||||
@@ -199,8 +160,18 @@ std::string Asset::getTypeName(int type)
|
||||
}
|
||||
}
|
||||
|
||||
// Establece si ha de mostrar texto por pantalla
|
||||
void Asset::setVerbose(bool value)
|
||||
// Devuelve la lista de recursos de un tipo
|
||||
std::vector<std::string> Asset::getListByType(AssetType type) const
|
||||
{
|
||||
verbose_ = value;
|
||||
std::vector<std::string> list;
|
||||
|
||||
for (auto f : file_list_)
|
||||
{
|
||||
if (f.type == type)
|
||||
{
|
||||
list.push_back(f.file);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
Reference in New Issue
Block a user