Arreglades les herencies de Sprite

Abans de llevar mil coses que sobren i replantejar-se estes 4 classes
This commit is contained in:
2024-10-13 10:01:07 +02:00
parent 33ea8d90ca
commit b060f21696
17 changed files with 1204 additions and 284 deletions

View File

@@ -5,35 +5,35 @@
#include <iostream> // for basic_ostream, operator<<, cout, endl
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Asset *Asset::asset = nullptr;
Asset *Asset::asset_ = nullptr;
// [SINGLETON] Crearemos el objeto asset con esta función estática
void Asset::init(std::string executable_path)
void Asset::init(const std::string &executable_path)
{
Asset::asset = new Asset(executable_path);
Asset::asset_ = new Asset(executable_path);
}
// [SINGLETON] Destruiremos el objeto asset con esta función estática
void Asset::destroy()
{
delete Asset::asset;
delete Asset::asset_;
}
// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él
Asset *Asset::get()
{
return Asset::asset;
return Asset::asset_;
}
// Constructor
Asset::Asset(std::string executable_path)
Asset::Asset(const std::string &executable_path)
{
executable_path_ = executable_path.substr(0, executable_path.find_last_of("\\/"));
longest_name_ = 0;
}
// Añade un elemento a la lista
void Asset::add(std::string file, AssetType type, bool required, bool absolute)
void Asset::add(const std::string &file, AssetType type, bool required, bool absolute)
{
AssetItem ai;
ai.file = absolute ? file : executable_path_ + file;
@@ -46,9 +46,9 @@ void Asset::add(std::string file, AssetType type, bool required, bool absolute)
}
// Devuelve el fichero de un elemento de la lista a partir de una cadena
std::string Asset::get(std::string text) const
std::string Asset::get(const std::string &text) const
{
for (auto f : file_list_)
for (const auto &f : file_list_)
{
const size_t last_index = f.file.find_last_of("/") + 1;
const std::string file = f.file.substr(last_index, std::string::npos);
@@ -83,7 +83,7 @@ bool Asset::check() const
// Comprueba si hay ficheros de ese tipo
bool any = false;
for (auto f : file_list_)
for (const auto &f : file_list_)
{
if (f.required && f.type == static_cast<AssetType>(type))
{
@@ -98,7 +98,7 @@ bool Asset::check() const
std::cout << "\n>> " << getTypeName(static_cast<AssetType>(type)).c_str() << " FILES" << std::endl;
#endif
for (auto f : file_list_)
for (const auto &f : file_list_)
{
if (f.required && f.type == static_cast<AssetType>(type))
{
@@ -117,29 +117,27 @@ bool Asset::check() const
}
// Comprueba que existe un fichero
bool Asset::checkFile(std::string path) const
bool Asset::checkFile(const std::string &path) const
{
bool success = false;
std::string result = "ERROR";
auto success = false;
// Comprueba si existe el fichero
const std::string file_name = path.substr(path.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb");
auto file = SDL_RWFromFile(path.c_str(), "rb");
if (file != nullptr)
if (file)
{
result = "OK";
success = true;
SDL_RWclose(file);
}
#ifdef VERBOSE
const std::string file_name = path.substr(path.find_last_of("\\/") + 1);
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << "Checking file: ";
std::cout.width(longest_name_ + 2);
std::cout.fill('.');
std::cout << file_name + " ";
std::cout << " [" + result + "]" << std::endl;
std::cout << file_name;
std::cout << (success ? " [OK]" : " [ERROR]") << std::endl;
#endif
return success;