From 84238032e0890e509d2346a3b7494c01aa5b5b4f Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 21 Oct 2024 17:46:05 +0200 Subject: [PATCH] Afegits uns overrides pa agafar parametres per linea de comandos --- source/director.cpp | 38 +++++++++++++++++++++++++++------ source/director.h | 1 - source/manage_hiscore_table.cpp | 22 ++++++++----------- source/screen.cpp | 5 ++++- source/utils.cpp | 3 +++ source/utils.h | 14 ++++++++++++ 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index c73e83f..dea9797 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -84,13 +84,22 @@ Director::Director(int argc, const char *argv[]) #ifdef ANBERNIC const std::string paramFilePath = asset->get("param_320x240.txt"); #else - const std::string paramFilePath = param_file_argument_ == "--320x240" ? Asset::get()->get("param_320x240.txt") : Asset::get()->get("param_320x256.txt"); + const std::string paramFilePath = overrides.param_file == "--320x240" ? Asset::get()->get("param_320x240.txt") : Asset::get()->get("param_320x256.txt"); #endif loadParams(paramFilePath); // Carga el fichero de puntuaciones - auto manager = std::make_unique(&options.game.hi_score_table); - manager->loadFromFile(Asset::get()->get("score.bin")); + { + auto manager = std::make_unique(&options.game.hi_score_table); + if (overrides.clear_hi_score_table) + { + manager->clear(); + } + else + { + manager->loadFromFile(Asset::get()->get("score.bin")); + } + } // Inicializa SDL initSDL(); @@ -513,18 +522,33 @@ void Director::loadParams(const std::string &file_path) // Comprueba los parametros del programa void Director::checkProgramArguments(int argc, const char *argv[]) { + const std::vector argument_list = {"--h", "--320x240", "--clear_score"}; + // Establece la ruta del programa executable_path_ = argv[0]; - // Valores por defecto - param_file_argument_.clear(); - // Comprueba el resto de parĂ¡metros for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "--h") == 0) + { + for (const auto &argument : argument_list) + { + std::cout << argument << std::endl; + } + // std::exit(EXIT_SUCCESS); + section::name = section::Name::QUIT; + section::options = section::Options::QUIT_FROM_EVENT; + } + if (strcmp(argv[i], "--320x240") == 0) { - param_file_argument_ = argv[i]; + overrides.param_file = argv[i]; + } + + if (strcmp(argv[i], "--clear_score") == 0) + { + overrides.clear_hi_score_table = true; } } } diff --git a/source/director.h b/source/director.h index 1993504..061944c 100644 --- a/source/director.h +++ b/source/director.h @@ -25,7 +25,6 @@ private: // Variables std::string executable_path_; // Path del ejecutable std::string system_folder_; // Carpeta del sistema donde guardar datos - std::string param_file_argument_; // Argumento para gestionar el fichero con los parametros del programa // Inicializa jail_audio void initJailAudio(); diff --git a/source/manage_hiscore_table.cpp b/source/manage_hiscore_table.cpp index 693a49a..a6efc2f 100644 --- a/source/manage_hiscore_table.cpp +++ b/source/manage_hiscore_table.cpp @@ -60,19 +60,19 @@ void ManageHiScoreTable::sort() bool ManageHiScoreTable::loadFromFile(const std::string &file_path) { clear(); - auto success = true; auto file = SDL_RWFromFile(file_path.c_str(), "r+b"); if (file) { const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1); - std::cout << "Reading file: " << file_name.c_str() << std::endl; - for (int i = 0; i < (int)table_->size(); ++i) + std::cout << "Reading file: " << file_name << std::endl; + + for (auto &entry : *table_) { int nameSize = 0; - if (SDL_RWread(file, &table_->at(i).score, sizeof(int), 1) == 0) + if (SDL_RWread(file, &entry.score, sizeof(int), 1) == 0) { success = false; break; @@ -84,19 +84,15 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path) break; } - char *name = static_cast(malloc(nameSize + 1)); - if (SDL_RWread(file, name, sizeof(char) * nameSize, 1) == 0) + std::vector nameBuffer(nameSize + 1); + if (SDL_RWread(file, nameBuffer.data(), sizeof(char) * nameSize, 1) == 0) { success = false; - free(name); break; } - else - { - name[nameSize] = 0; - table_->at(i).name = name; - free(name); - } + + nameBuffer[nameSize] = '\0'; + entry.name = std::string(nameBuffer.data()); } SDL_RWclose(file); diff --git a/source/screen.cpp b/source/screen.cpp index d1e5dc2..23453cb 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -480,7 +480,10 @@ void Screen::displayInfo() // Resolution dbg_print(0, 0, info_resolution_.c_str(), 255, 255, 0); - dbg_print(0, 8, std::to_string(globalInputs::service_pressed_counter[0]).c_str(), 255, 255, 0); + // Contador de service_pressed_counter + const int counter = globalInputs::service_pressed_counter[0]; + if (counter > 0) + dbg_print(0, 8, std::to_string(counter).c_str(), 255, 0, 255); } } diff --git a/source/utils.cpp b/source/utils.cpp index 8448f51..8770cf4 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -8,6 +8,9 @@ struct JA_Music_t; // lines 7-7 struct JA_Sound_t; // lines 8-8 +// Variables +Overrides overrides = Overrides(); + // Colores const Color bg_color = {0x27, 0x27, 0x36}; const Color no_color = {0xFF, 0xFF, 0xFF}; diff --git a/source/utils.h b/source/utils.h index 11ca31f..d0de72a 100644 --- a/source/utils.h +++ b/source/utils.h @@ -29,6 +29,20 @@ enum class GameDifficulty HARD = 2, }; +// Variables para que los argumentos del programa tengan mas peso que los definidos en otros lugares +struct Overrides +{ + std::string param_file; // Fichero de parametros a utilizar + bool clear_hi_score_table; // Reinicia la tabla de records + bool set_v_sync; // Establece el vsync + + // Constructor por defecto + Overrides() + : param_file(""), clear_hi_score_table(false), set_v_sync(false) {} +}; + +extern Overrides overrides; + // Estructura para definir un circulo struct Circle {