Precàrrega dels fitxers amb dades per al mode demostració

This commit is contained in:
2024-10-20 21:23:04 +02:00
parent b263e0c4be
commit a3a583deb7
6 changed files with 153 additions and 115 deletions

View File

@@ -202,4 +202,105 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
std::cout << text2;
std::cout << text3 << std::endl;
}
}
// Carga el fichero de datos para la demo
DemoData loadDemoDataFromFile(const std::string &file_path)
{
DemoData dd;
// Indicador de éxito en la carga
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
if (!file)
{
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
throw std::runtime_error("Fichero no encontrado: " + file_path);
}
/*{ // El fichero no existe
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
// Creamos el fichero para escritura
file = SDL_RWFromFile(file_path.c_str(), "w+b");
// Si ha creado el fichero
if (file)
{
std::cout << "New file (" << file_name.c_str() << ") created!" << std::endl;
// Inicializas los datos y los guarda en el fichero
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
{
DemoKeys dk = DemoKeys();
dd.push_back(dk);
SDL_RWwrite(file, &dk, sizeof(DemoKeys), 1);
}
// Cerramos el fichero
SDL_RWclose(file);
}
else
{ // Si no puede crear el fichero
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
}
}*/
// El fichero existe
else
{
// Mensaje de proceder a la carga de los datos
// std::cout << "Reading file: " << file_name.c_str() << std::endl;
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
{
DemoKeys dk = DemoKeys();
SDL_RWread(file, &dk, sizeof(DemoKeys), 1);
dd.push_back(dk);
}
// Cierra el fichero
SDL_RWclose(file);
// Mensaje de datos cargados a la carga de los datos
printWithDots("DemoData : ", file_name, "[ LOADED ]");
}
return dd;
}
#ifdef RECORDING
// Guarda el fichero de datos para la demo
bool saveDemoFile(const std::string &file_path, const DemoData &dd)
{
auto success = true;
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
if (file)
{
// Guarda los datos
for (const auto &data : dd)
{
if (SDL_RWwrite(file, &data, sizeof(DemoKeys), 1) != 1)
{
std::cerr << "Error al escribir el fichero " << file_name << std::endl;
success = false;
break;
}
}
if (success)
{
std::cout << "Writing file " << file_name.c_str() << std::endl;
}
// Cierra el fichero
SDL_RWclose(file);
}
else
{
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
success = false;
}
return success;
}
#endif // RECORDING