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 <iostream>
// Constructor
Asset::Asset(std::string executablePath)
{
this->executablePath = executablePath.substr(0, executablePath.find_last_of("\\/"));
longestName = 0;
}
// Destructor
Asset::~Asset()
{
verbose = true;
}
// 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;
temp.file = executablePath + file;
temp.file = absolute ? file : executablePath + file;
temp.type = type;
temp.required = required;
fileList.push_back(temp);
@@ -30,13 +27,19 @@ std::string Asset::get(std::string text)
{
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;
}
}
printf("Warning: file %s not found\n", text.c_str());
if (verbose)
{
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
}
return "";
}
@@ -45,7 +48,13 @@ bool Asset::check()
{
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
for (int type = 0; type < t_maxAssetType; ++type)
@@ -64,7 +73,10 @@ bool Asset::check()
// Si hay ficheros de ese tipo, comprueba si existen
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)
{
@@ -77,13 +89,18 @@ bool Asset::check()
}
// Resultado
if (success)
if (verbose)
{
printf("\n** All files OK.\n\n");
}
else
{
printf("\n** A file is missing. Exiting.\n\n");
if (success)
{
std::cout << "\n** All files OK.\n"
<< std::endl;
}
else
{
std::cout << "\n** A file is missing. Exiting.\n"
<< std::endl;
}
}
return success;
@@ -97,7 +114,7 @@ bool Asset::checkFile(std::string path)
// Comprueba si existe el fichero
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)
{
@@ -106,8 +123,15 @@ bool Asset::checkFile(std::string path)
SDL_RWclose(file);
}
const std::string s = "Checking file %-" + std::to_string(longestName) + "s [" + result + "]\n";
printf(s.c_str(), filename.c_str());
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;
}
return success;
}
@@ -157,4 +181,10 @@ std::string Asset::getTypeName(int type)
return "ERROR";
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
enum assetType type; // Indica el tipo de recurso
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
};
int longestName; // Contiene la longitud del nombre de fichero mas largo
std::vector<item_t> fileList;
std::string executablePath;
// Variables
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::string executablePath; // Ruta al ejecutable
bool verbose; // Indica si ha de mostrar información por pantalla
// Comprueba que existe un fichero
bool checkFile(std::string path);
bool checkFile(std::string executablePath);
// Devuelve el nombre del tipo de recurso
std::string getTypeName(int type);
public:
// Constructor
Asset(std::string executablePath);
// Destructor
~Asset();
Asset(std::string path);
// 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
std::string get(std::string text);
// Comprueba que existen todos los elementos
bool check();
// Establece si ha de mostrar texto por pantalla
void setVerbose(bool value);
};
#endif

View File

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