Estandaritzant noms segons convencions

This commit is contained in:
2024-10-10 20:27:31 +02:00
parent 9e5f41644e
commit d6c3c89872
67 changed files with 1457 additions and 1504 deletions

View File

@@ -1,16 +1,16 @@
#include "asset.h"
#include <SDL2/SDL_rwops.h> // for SDL_RWFromFile, SDL_RWclose, SDL_RWops
#include <SDL2/SDL_stdinc.h> // for SDL_max
#include <stddef.h> // for size_t
#include <iostream> // for basic_ostream, operator<<, cout, endl
#include <SDL2/SDL_rwops.h> // for SDL_RWFromFile, SDL_RWclose, SDL_RWops
#include <SDL2/SDL_stdinc.h> // for SDL_max
#include <stddef.h> // for size_t
#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;
// [SINGLETON] Crearemos el objeto asset con esta función estática
void Asset::init(std::string executablePath)
void Asset::init(std::string executable_path)
{
Asset::asset = new Asset(executablePath);
Asset::asset = new Asset(executable_path);
}
// [SINGLETON] Destruiremos el objeto asset con esta función estática
@@ -26,37 +26,32 @@ Asset *Asset::get()
}
// Constructor
Asset::Asset(std::string executablePath)
Asset::Asset(std::string executable_path)
{
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
longestName = 0;
#ifdef VERBOSE
verbose = true;
#else
verbose = false;
#endif
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, enum assetType type, bool required, bool absolute)
void Asset::add(std::string file, AssetType type, bool required, bool absolute)
{
item_t temp;
temp.file = absolute ? file : executablePath + file;
temp.type = type;
temp.required = required;
fileList.push_back(temp);
AssetItem ai;
ai.file = absolute ? file : executable_path_ + file;
ai.type = type;
ai.required = required;
file_list_.push_back(ai);
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
longestName = SDL_max(longestName, filename.size());
const std::string file_name = file.substr(file.find_last_of("\\/") + 1);
longest_name_ = SDL_max(longest_name_, file_name.size());
}
// Devuelve el fichero de un elemento de la lista a partir de una cadena
std::string Asset::get(std::string text)
std::string Asset::get(std::string text) const
{
for (auto f : fileList)
for (auto f : file_list_)
{
const size_t lastIndex = f.file.find_last_of("/") + 1;
const std::string file = f.file.substr(lastIndex, std::string::npos);
const size_t last_index = f.file.find_last_of("/") + 1;
const std::string file = f.file.substr(last_index, std::string::npos);
if (file == text)
{
@@ -64,35 +59,33 @@ std::string Asset::get(std::string text)
}
}
if (verbose)
{
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
}
#ifdef VERBOSE
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
#endif
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;
#ifdef VERBOSE
std::cout << "\n** Checking files" << std::endl;
std::cout << "Executable path is: " << executablePath << 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;
#endif
// 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 (auto f : file_list_)
{
if ((f.required) && (f.type == type))
if (f.required && f.type == static_cast<AssetType>(type))
{
any = true;
}
@@ -101,14 +94,13 @@ 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;
}
#ifdef VERBOSE
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
#endif
for (auto f : fileList)
for (auto f : file_list_)
{
if ((f.required) && (f.type == type))
if (f.required && f.type == static_cast<AssetType>(type))
{
success &= checkFile(f.file);
}
@@ -116,32 +108,22 @@ bool Asset::check()
}
}
// 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;
}
}
// Resultado
#ifdef VERBOSE
std::cout << (success ? "\n** All files OK.\n" : "\n** A file is missing. Exiting.\n") << std::endl;
#endif
return success;
}
// Comprueba que existe un fichero
bool Asset::checkFile(std::string path)
bool Asset::checkFile(std::string path) const
{
bool success = false;
std::string result = "ERROR";
// Comprueba si existe el fichero
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
const std::string file_name = path.substr(path.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb");
if (file != nullptr)
@@ -151,57 +133,56 @@ bool Asset::checkFile(std::string path)
SDL_RWclose(file);
}
if (verbose)
{
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << "Checking file: ";
std::cout.width(longestName + 2);
std::cout.fill('.');
std::cout << filename + " ";
std::cout << " [" + result + "]" << std::endl;
}
#ifdef 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 << file_name + " ";
std::cout << " [" + result + "]" << std::endl;
#endif
return success;
}
// Devuelve el nombre del tipo de recurso
std::string Asset::getTypeName(int type)
std::string Asset::getTypeName(int type) const
{
switch (type)
{
case t_bitmap:
case static_cast<int>(AssetType::BITMAP):
return "BITMAP";
break;
case t_music:
case static_cast<int>(AssetType::MUSIC):
return "MUSIC";
break;
case t_sound:
case static_cast<int>(AssetType::SOUND):
return "SOUND";
break;
case t_font:
case static_cast<int>(AssetType::FONT):
return "FONT";
break;
case t_lang:
case static_cast<int>(AssetType::LANG):
return "LANG";
break;
case t_data:
case static_cast<int>(AssetType::DATA):
return "DATA";
break;
case t_animation:
case static_cast<int>(AssetType::ANIMATION):
return "ANIMATION";
break;
case t_palette:
case static_cast<int>(AssetType::PALETTE):
return "PALETTE";
break;
case t_item:
case static_cast<int>(AssetType::ITEM):
return "ITEM";
break;
@@ -211,18 +192,12 @@ std::string Asset::getTypeName(int type)
}
}
// Establece si ha de mostrar texto por pantalla
void Asset::setVerbose(bool value)
{
verbose = value;
}
// Devuelve la lista de recursos de un tipo
std::vector<std::string> Asset::getListByType(assetType type)
std::vector<std::string> Asset::getListByType(AssetType type) const
{
std::vector<std::string> list;
for (auto f : fileList)
for (auto f : file_list_)
{
if (f.type == type)
{