#include "demo.h" #include // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO #include // Para runtime_error #include "resource_helper.h" // Para ResourceHelper #include "utils.h" // Para printWithDots, getFileName // Carga el fichero de datos para la demo auto loadDemoDataFromFile(const std::string &file_path) -> DemoData { DemoData dd; SDL_IOStream *file = nullptr; // Intentar cargar desde ResourceHelper primero auto resource_data = ResourceHelper::loadFile(file_path); if (!resource_data.empty()) { file = SDL_IOFromConstMem(resource_data.data(), resource_data.size()); } else { // Fallback a filesystem directo file = SDL_IOFromFile(file_path.c_str(), "r+b"); } if (file == nullptr) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str()); throw std::runtime_error("Fichero no encontrado: " + file_path); } printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]"); // 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_ReadIO(file, &dk, sizeof(DemoKeys)); dd.push_back(dk); } // Cierra el fichero SDL_CloseIO(file); 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; auto file = SDL_IOFromFile(file_path.c_str(), "w+b"); if (file) { // Guarda los datos for (const auto &data : dd) { if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str()); success = false; break; } } if (success) { SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str()); } // Cierra el fichero SDL_CloseIO(file); } else { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError()); success = false; } return success; } #endif // RECORDING