From 4500845dcd392b8172754171c507d7a9ebd10386 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 30 Sep 2025 12:58:32 +0200 Subject: [PATCH] muguda la logica de demo de utils.cpp a demo.cpp --- CMakeLists.txt | 1 + source/demo.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++ source/demo.h | 54 ++++++++++++++++++++++++++++++++ source/resource.h | 2 +- source/sections/game.h | 3 +- source/utils.cpp | 62 ------------------------------------- source/utils.h | 44 -------------------------- 7 files changed, 128 insertions(+), 108 deletions(-) create mode 100644 source/demo.cpp create mode 100644 source/demo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b000469..9fe751b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,7 @@ set(APP_SOURCES # --- Otros --- source/color.cpp + source/demo.cpp source/define_buttons.cpp source/difficulty.cpp source/input_types.cpp diff --git a/source/demo.cpp b/source/demo.cpp new file mode 100644 index 0000000..0f26a71 --- /dev/null +++ b/source/demo.cpp @@ -0,0 +1,70 @@ +#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 \ No newline at end of file diff --git a/source/demo.h b/source/demo.h new file mode 100644 index 0000000..46012d0 --- /dev/null +++ b/source/demo.h @@ -0,0 +1,54 @@ +#pragma once + +#include // Para Uint8 +#include // Para string +#include // Para vector + +// --- Constantes --- +constexpr int TOTAL_DEMO_DATA = 2000; + +// --- Estructuras --- +struct DemoKeys { + Uint8 left; + Uint8 right; + Uint8 no_input; + Uint8 fire; + Uint8 fire_left; + Uint8 fire_right; + + explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0) + : left(l), + right(r), + no_input(ni), + fire(f), + fire_left(fl), + fire_right(fr) {} +}; + +// --- Tipos --- +using DemoData = std::vector; + +struct Demo { + bool enabled = false; // Indica si está activo el modo demo + bool recording = false; // Indica si está activado el modo para grabar la demo + float elapsed_s = 0.0F; // Segundos transcurridos de demo + int index = 0; // Contador para el modo demo + DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo + std::vector data; // Vector con diferentes sets de datos con los movimientos para la demo + + Demo() = default; + + Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector& d) + : enabled(e), + recording(r), + index(c), + keys(k), + data(d) {} +}; + +// --- Funciones --- +auto loadDemoDataFromFile(const std::string& file_path) -> DemoData; + +#ifdef RECORDING +bool saveDemoFile(const std::string& file_path, const DemoData& dd); +#endif \ No newline at end of file diff --git a/source/resource.h b/source/resource.h index 663f932..a5515ef 100644 --- a/source/resource.h +++ b/source/resource.h @@ -11,7 +11,7 @@ #include "animated_sprite.h" // Para AnimationsFileBuffer #include "text.h" // Para Text, TextFile #include "texture.h" // Para Texture -#include "utils.h" // Para DemoData +#include "demo.h" // Para DemoData struct JA_Music_t; struct JA_Sound_t; diff --git a/source/sections/game.h b/source/sections/game.h index 10d1c3f..cb0b993 100644 --- a/source/sections/game.h +++ b/source/sections/game.h @@ -15,7 +15,8 @@ #include "player.h" // Para Player #include "smart_sprite.h" // Para SmartSprite #include "stage.h" // Para StageManager -#include "utils.h" // Para Demo +#include "demo.h" // Para Demo +#include "utils.h" // Para otras utilidades class Background; class Balloon; diff --git a/source/utils.cpp b/source/utils.cpp index 60c2e0f..a04238a 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -320,68 +320,6 @@ void printWithDots(const std::string &text1, const std::string &text2, const std SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", formatted_text.c_str()); } -// 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 // Obtiene el nombre de un fichero a partir de una ruta completa auto getFileName(const std::string &path) -> std::string { diff --git a/source/utils.h b/source/utils.h index 73230d5..bd4669f 100644 --- a/source/utils.h +++ b/source/utils.h @@ -10,7 +10,6 @@ // --- Constantes --- constexpr int BLOCK = 8; -constexpr int TOTAL_DEMO_DATA = 2000; // --- Estructuras --- struct Overrides { @@ -32,43 +31,6 @@ struct Circle { r(radius) {} }; -struct DemoKeys { - Uint8 left; - Uint8 right; - Uint8 no_input; - Uint8 fire; - Uint8 fire_left; - Uint8 fire_right; - - explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0) - : left(l), - right(r), - no_input(ni), - fire(f), - fire_left(fl), - fire_right(fr) {} -}; - -// --- Tipos --- -using DemoData = std::vector; - -struct Demo { - bool enabled = false; // Indica si está activo el modo demo - bool recording = false; // Indica si está activado el modo para grabar la demo - float elapsed_s = 0.0F; // Segundos transcurridos de demo - int index = 0; // Contador para el modo demo - DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo - std::vector data; // Vector con diferentes sets de datos con los movimientos para la demo - - Demo() = default; - - Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector& d) - : enabled(e), - recording(r), - index(c), - keys(k), - data(d) {} -}; struct Zone { SDL_FRect rect; // Rectangulo que define la zona @@ -126,12 +88,6 @@ auto stringInVector(const std::vector& vec, const std::string& str) void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime una línea con puntos auto truncateWithEllipsis(const std::string& input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos -// Demo -auto loadDemoDataFromFile(const std::string& file_path) -> DemoData; - -#ifdef RECORDING -bool saveDemoFile(const std::string& file_path, const DemoData& dd); -#endif // Ficheros y rutas auto getFileName(const std::string& path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta