Actualizado asset.cpp

This commit is contained in:
2022-12-07 08:08:04 +01:00
parent 90785d2506
commit 3cca4373d3
4 changed files with 64 additions and 32 deletions

BIN
data/sound/notify.wav Normal file

Binary file not shown.

View File

@@ -1,22 +1,19 @@
#include "asset.h" #include "asset.h"
#include <iostream>
// Constructor // Constructor
Asset::Asset(std::string executablePath) Asset::Asset(std::string executablePath)
{ {
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/")); this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
longestName = 0; longestName = 0;
} verbose = true;
// Destructor
Asset::~Asset()
{
} }
// Añade un elemento a la lista // Añade un elemento a la lista
void Asset::add(std::string file, enum assetType type, bool required) void Asset::add(std::string file, enum assetType type, bool required, bool absolute)
{ {
item_t temp; item_t temp;
temp.file = executablePath + file; temp.file = absolute ? file : executablePath + file;
temp.type = type; temp.type = type;
temp.required = required; temp.required = required;
fileList.push_back(temp); fileList.push_back(temp);
@@ -30,13 +27,19 @@ std::string Asset::get(std::string text)
{ {
for (auto f : fileList) for (auto f : fileList)
{ {
if (f.file.find(text) != std::string::npos) const size_t lastIndex = f.file.find_last_of("/") + 1;
const std::string file = f.file.substr(lastIndex, std::string::npos);
if (file == text)
{ {
return f.file; return f.file;
} }
} }
printf("Warning: file %s not found\n", text.c_str()); if (verbose)
{
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
}
return ""; return "";
} }
@@ -45,7 +48,13 @@ bool Asset::check()
{ {
bool success = true; bool success = true;
printf("\n** Checking files.\n"); if (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;
}
// Comprueba la lista de ficheros clasificandolos por tipo // Comprueba la lista de ficheros clasificandolos por tipo
for (int type = 0; type < t_maxAssetType; ++type) for (int type = 0; type < t_maxAssetType; ++type)
@@ -64,7 +73,10 @@ bool Asset::check()
// Si hay ficheros de ese tipo, comprueba si existen // Si hay ficheros de ese tipo, comprueba si existen
if (any) if (any)
{ {
printf("\n>> %s FILES\n", getTypeName(type).c_str()); if (verbose)
{
std::cout << "\n>> " << getTypeName(type).c_str() << " FILES" << std::endl;
}
for (auto f : fileList) for (auto f : fileList)
{ {
@@ -77,13 +89,18 @@ bool Asset::check()
} }
// Resultado // Resultado
if (verbose)
{
if (success) if (success)
{ {
printf("\n** All files OK.\n\n"); std::cout << "\n** All files OK.\n"
<< std::endl;
} }
else else
{ {
printf("\n** A file is missing. Exiting.\n\n"); std::cout << "\n** A file is missing. Exiting.\n"
<< std::endl;
}
} }
return success; return success;
@@ -97,7 +114,7 @@ bool Asset::checkFile(std::string path)
// Comprueba si existe el fichero // Comprueba si existe el fichero
const std::string filename = path.substr(path.find_last_of("\\/") + 1); const std::string filename = path.substr(path.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b"); SDL_RWops *file = SDL_RWFromFile(path.c_str(), "rb");
if (file != nullptr) if (file != nullptr)
{ {
@@ -106,8 +123,15 @@ bool Asset::checkFile(std::string path)
SDL_RWclose(file); SDL_RWclose(file);
} }
const std::string s = "Checking file %-" + std::to_string(longestName) + "s [" + result + "]\n"; if (verbose)
printf(s.c_str(), filename.c_str()); {
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;
}
return success; return success;
} }
@@ -158,3 +182,9 @@ std::string Asset::getTypeName(int type)
break; break;
} }
} }
// Establece si ha de mostrar texto por pantalla
void Asset::setVerbose(bool value)
{
verbose = value;
}

View File

@@ -31,34 +31,36 @@ private:
std::string file; // Ruta del fichero desde la raiz del directorio std::string file; // Ruta del fichero desde la raiz del directorio
enum assetType type; // Indica el tipo de recurso enum assetType type; // Indica el tipo de recurso
bool required; // Indica si es un fichero que debe de existir bool required; // Indica si es un fichero que debe de existir
//bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta
}; };
// Variables
int longestName; // Contiene la longitud del nombre de fichero mas largo int longestName; // Contiene la longitud del nombre de fichero mas largo
std::vector<item_t> fileList; // Listado con todas las rutas a los ficheros
std::vector<item_t> fileList; std::string executablePath; // Ruta al ejecutable
std::string executablePath; bool verbose; // Indica si ha de mostrar información por pantalla
// Comprueba que existe un fichero // Comprueba que existe un fichero
bool checkFile(std::string path); bool checkFile(std::string executablePath);
// Devuelve el nombre del tipo de recurso // Devuelve el nombre del tipo de recurso
std::string getTypeName(int type); std::string getTypeName(int type);
public: public:
// Constructor // Constructor
Asset(std::string executablePath); Asset(std::string path);
// Destructor
~Asset();
// Añade un elemento a la lista // Añade un elemento a la lista
void add(std::string file, enum assetType type, bool required = true); void add(std::string file, enum assetType type, bool required = true, bool absolute = false);
// Devuelve un elemento de la lista a partir de una cadena // Devuelve un elemento de la lista a partir de una cadena
std::string get(std::string text); std::string get(std::string text);
// Comprueba que existen todos los elementos // Comprueba que existen todos los elementos
bool check(); bool check();
// Establece si ha de mostrar texto por pantalla
void setVerbose(bool value);
}; };
#endif #endif

View File

@@ -37,7 +37,7 @@ Director::Director(std::string path)
lang = new Lang(asset); lang = new Lang(asset);
lang->setLang(options->language); lang->setLang(options->language);
input = new Input(asset->get("controllerdb.txt")); input = new Input(asset->get("gamecontrollerdb.txt"));
initInput(); initInput();
screen = new Screen(window, renderer, asset, options); screen = new Screen(window, renderer, asset, options);
@@ -183,7 +183,6 @@ bool Director::setFileList()
// Ficheros de configuración // Ficheros de configuración
asset->add(prefix + "/data/config/score.bin", t_data, false); asset->add(prefix + "/data/config/score.bin", t_data, false);
asset->add(prefix + "/data/config/demo.bin", t_data); asset->add(prefix + "/data/config/demo.bin", t_data);
asset->add(prefix + "/data/config/config.bin", t_data, false);
asset->add(prefix + "/data/config/config.txt", t_data, false); asset->add(prefix + "/data/config/config.txt", t_data, false);
asset->add(prefix + "/data/config/jailer_id.txt", t_data, false); asset->add(prefix + "/data/config/jailer_id.txt", t_data, false);
asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data); asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data);
@@ -211,6 +210,7 @@ bool Director::setFileList()
asset->add(prefix + "/data/sound/title.wav", t_sound); asset->add(prefix + "/data/sound/title.wav", t_sound);
asset->add(prefix + "/data/sound/clock.wav", t_sound); asset->add(prefix + "/data/sound/clock.wav", t_sound);
asset->add(prefix + "/data/sound/powerball.wav", t_sound); asset->add(prefix + "/data/sound/powerball.wav", t_sound);
asset->add(prefix + "/data/sound/notify.wav", t_sound);
// Texturas // Texturas
asset->add(prefix + "/data/gfx/balloon1.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon1.png", t_bitmap);