From 7d778a9cfa5ac6f29296b7a027b537ce6c52bae9 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 16 Aug 2024 11:57:58 +0200 Subject: [PATCH] ja guarda i carrega la tabla de records en el disc --- source/director.cpp | 27 +++----- source/game.cpp | 153 +++++++++++++++++++++----------------------- source/game.h | 5 +- 3 files changed, 83 insertions(+), 102 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index efd069a..b94a967 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -589,7 +589,7 @@ bool Director::loadConfigFile() // Procesa el fichero linea a linea if (options->console) { - std::cout << "Reading file " << filePath << std::endl; + std::cout << "Reading file: " << filePath << std::endl; } while (std::getline(file, line)) { @@ -612,10 +612,6 @@ bool Director::loadConfigFile() } // Cierra el fichero - if (options->console) - { - std::cout << "Closing file " << filePath << std::endl; - } file.close(); } @@ -650,24 +646,21 @@ bool Director::loadConfigFile() // Guarda el fichero de configuración bool Director::saveConfigFile() { - bool success = true; + const std::string filename = "config.txt"; + std::ofstream file(asset->get(filename)); - // Crea y abre el fichero de texto - std::ofstream file(asset->get("config.txt")); - - if (file.good()) + if (!file.good()) { if (options->console) { - std::cout << asset->get("config.txt") << " open for writing" << std::endl; + std::cout << filename << " can't be opened" << std::endl; } + return false; } - else + + if (options->console) { - if (options->console) - { - std::cout << asset->get("config.txt") << " can't be opened" << std::endl; - } + std::cout << "Writing file: " << filename << std::endl; } // Opciones de video @@ -774,7 +767,7 @@ bool Director::saveConfigFile() // Cierra el fichero file.close(); - return success; + return true; } // Carga los sonidos del juego diff --git a/source/game.cpp b/source/game.cpp index 2a53fb7..d688c96 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -42,8 +42,8 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l loadDemoFile(asset->get("demo2.bin"), &this->demo.dataFile[index2]); } - // Carga las puntuaciones desde el fichero y establece la máxima puntuación - setHiScore(); + // Carga el fichero con la tabla de puntuaciones + loadScoreFile(); n1000Sprite = new SmartSprite(gameTextTexture); n2500Sprite = new SmartSprite(gameTextTexture); @@ -63,7 +63,7 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l Game::~Game() { - // saveScoreFile(); + saveScoreFile(); #ifdef RECORDING saveDemoFile(); #endif @@ -630,81 +630,74 @@ void Game::unloadMedia() JA_DeleteSound(coffeeMachineSound); } -// Carga el fichero de puntos +// Carga el fichero con la tabla de puntuaciones bool Game::loadScoreFile() { - // Indicador de éxito en la carga bool success = true; const std::string p = asset->get("score.bin"); const std::string filename = p.substr(p.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b"); - - // El fichero no existe - if (file == nullptr) - { - if (options->console) - { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl; - } - - // Creamos el fichero para escritura - file = SDL_RWFromFile(p.c_str(), "w+b"); - if (file != nullptr) - { - if (options->console) - { - std::cout << "New file (" << filename.c_str() << ") created!" << std::endl; - } - - // Inicializamos los datos - for (int i = 0; i < TOTAL_SCORE_DATA; ++i) - { - scoreDataFile[i] = 0; - SDL_RWwrite(file, &scoreDataFile[i], sizeof(Uint32), 1); - } - - // Cerramos el fichero - SDL_RWclose(file); - } - else - { - if (options->console) - { - std::cout << "Error: Unable to create file " << filename.c_str() << std::endl; - } - success = false; - } - } - // El fichero existe - else + if (file) { // Cargamos los datos if (options->console) { - std::cout << "Reading file " << filename.c_str() << std::endl; + std::cout << "Reading file: " << filename.c_str() << std::endl; + } + for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i) + { + int nameSize = 0; + + if (SDL_RWread(file, &options->game.hiScoreTable[i].score, sizeof(int), 1) == 0) + { + success = false; + break; + } + + if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0) + { + success = false; + break; + } + + char *name = (char *)malloc(nameSize + 1); + if (SDL_RWread(file, name, sizeof(char) * nameSize, 1) == 0) + { + success = false; + free(name); + break; + } + else + { + name[nameSize] = 0; + options->game.hiScoreTable[i].name = name; + free(name); + } +#ifdef DEBUG + if (false) + { + std::cout << "score: " << options->game.hiScoreTable[i].score << std::endl; + std::cout << "nsize: " << nameSize << std::endl; + std::cout << "name : " << options->game.hiScoreTable[i].name << std::endl; + std::cout << " ---" << std::endl; + } +#endif } - for (int i = 0; i < TOTAL_SCORE_DATA; ++i) - SDL_RWread(file, &scoreDataFile[i], sizeof(Uint32), 1); - // Cierra el fichero SDL_RWclose(file); } - // Establece el valor de la máxima puntuación a partir del vector con los datos - if (scoreDataFile[0] == 0) + if (!success) { - hiScore.score = 10000; - } - // Comprueba el checksum para ver si se ha modificado el fichero - else if (scoreDataFile[0] % 43 == scoreDataFile[1]) - { - hiScore.score = scoreDataFile[0]; - } - else - { - hiScore.score = 10000; + ManageHiScoreTable *m = new ManageHiScoreTable(&options->game.hiScoreTable); + m->clear(); + delete m; } + // Establece el valor de la variable desde la tabla de records + hiScore.score = options->game.hiScoreTable[0].score; + hiScore.name = options->game.hiScoreTable[0].name; + return success; } @@ -767,7 +760,7 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]) // Mensaje de proceder a la carga de los datos if (options->console) { - std::cout << "Reading file " << filename.c_str() << std::endl; + std::cout << "Reading file: " << filename.c_str() << std::endl; } // Lee todos los datos del fichero y los deja en el destino @@ -788,28 +781,36 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]) // Guarda el fichero de puntos bool Game::saveScoreFile() { - // Almacena la máxima puntuación en el fichero junto con un checksum - scoreDataFile[0] = hiScore.score; - scoreDataFile[1] = hiScore.score % 43; - bool success = true; const std::string p = asset->get("score.bin"); const std::string filename = p.substr(p.find_last_of("\\/") + 1); SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b"); - if (file != nullptr) + if (file) { - // Guardamos los datos - for (int i = 0; i < TOTAL_SCORE_DATA; ++i) + // Guarda los datos + for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i) { - SDL_RWwrite(file, &scoreDataFile[i], sizeof(Uint32), 1); + SDL_RWwrite(file, &options->game.hiScoreTable[i].score, sizeof(int), 1); + const int nameSize = (int)options->game.hiScoreTable[i].name.size(); + SDL_RWwrite(file, &nameSize, sizeof(int), 1); + SDL_RWwrite(file, options->game.hiScoreTable[i].name.c_str(), nameSize, 1); +#ifdef DEBUG + if (false) + { + std::cout << "score: " << options->game.hiScoreTable[i].score << std::endl; + std::cout << "nsize: " << nameSize << std::endl; + std::cout << "name : " << options->game.hiScoreTable[i].name << std::endl; + std::cout << " ---" << std::endl; + } +#endif } if (options->console) { - std::cout << "Writing file " << filename.c_str() << std::endl; + std::cout << "Writing file: " << filename.c_str() << std::endl; } - // Cerramos el fichero + // Cierra el fichero SDL_RWclose(file); } else @@ -2828,16 +2829,6 @@ void Game::reloadTextures() background->reloadTextures(); } -// Carga las puntuaciones desde el fichero y establece la máxima puntuación -void Game::setHiScore() -{ - // Carga el fichero de puntos - // loadScoreFile(); - - hiScore.score = options->game.hiScoreTable[0].score; - hiScore.name = options->game.hiScoreTable[0].name; -} - // Actualiza el marcador void Game::updateScoreboard() { diff --git a/source/game.h b/source/game.h index bb0ac1f..6af9739 100644 --- a/source/game.h +++ b/source/game.h @@ -222,7 +222,7 @@ private: // Libera los recursos previamente cargados void unloadMedia(); - // Carga el fichero de puntos + // Carga el fichero con la tabla de puntuaciones bool loadScoreFile(); // Carga el fichero de datos para la demo @@ -427,9 +427,6 @@ private: // Recarga las texturas void reloadTextures(); - // Carga las puntuaciones desde el fichero y establece la máxima puntuación - void setHiScore(); - // Actualiza el marcador void updateScoreboard();