singletoning

This commit is contained in:
2025-02-21 18:03:09 +01:00
parent debcc3409e
commit 5f68c6256f
8 changed files with 601 additions and 563 deletions

View File

@@ -4,25 +4,38 @@
#include <stddef.h> // Para size_t
#include <iostream> // Para basic_ostream, operator<<, cout, endl
// Constructor
Asset::Asset(std::string executablePath)
// [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
void Asset::init(const std::string &executable_path)
{
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
longestName = 0;
verbose = true;
Asset::asset_ = new Asset(executable_path);
}
// [SINGLETON] Destruiremos el objeto 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
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)
{
item_t temp;
temp.file = absolute ? file : executablePath + file;
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);
longestName = SDL_max(longestName, filename.size());
longest_name_ = SDL_max(longest_name_, filename.size());
}
// Devuelve el fichero de un elemento de la lista a partir de una cadena
@@ -39,7 +52,7 @@ std::string Asset::get(std::string text)
}
}
if (verbose)
if (verbose_)
{
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
}
@@ -51,11 +64,11 @@ bool Asset::check()
{
bool success = true;
if (verbose)
if (verbose_)
{
std::cout << "\n** Checking files" << std::endl;
std::cout << "Executable path is: " << executablePath << std::endl;
std::cout << "Executable path is: " << executable_path_ << std::endl;
std::cout << "Sample filepath: " << fileList.back().file << std::endl;
}
@@ -76,7 +89,7 @@ bool Asset::check()
// Si hay ficheros de ese tipo, comprueba si existen
if (any)
{
if (verbose)
if (verbose_)
{
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
}
@@ -92,7 +105,7 @@ bool Asset::check()
}
// Resultado
if (verbose)
if (verbose_)
{
if (success)
{
@@ -126,11 +139,11 @@ bool Asset::checkFile(std::string path)
SDL_RWclose(file);
}
if (verbose)
if (verbose_)
{
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << "Checking file: ";
std::cout.width(longestName + 2);
std::cout.width(longest_name_ + 2);
std::cout.fill('.');
std::cout << filename + " ";
std::cout << " [" + result + "]" << std::endl;
@@ -189,5 +202,5 @@ std::string Asset::getTypeName(int type)
// Establece si ha de mostrar texto por pantalla
void Asset::setVerbose(bool value)
{
verbose = value;
verbose_ = value;
}