From 5e7be1c2fbf064e895599e7cc514759365a2d262 Mon Sep 17 00:00:00 2001 From: Sergio Valor Martinez Date: Mon, 2 Jan 2023 09:46:09 +0100 Subject: [PATCH] Ya se guardan los logros en un fichero --- source/cheevos.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++-- source/cheevos.h | 9 ++++- source/director.cpp | 1 + source/game.cpp | 5 ++- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/source/cheevos.cpp b/source/cheevos.cpp index 40ed626..13105ad 100644 --- a/source/cheevos.cpp +++ b/source/cheevos.cpp @@ -1,22 +1,30 @@ #include "cheevos.h" +#include // Constructor -Cheevos::Cheevos(Screen *screen, options_t *options) +Cheevos::Cheevos(Screen *screen, options_t *options, std::string file) { // Copia la dirección de los objetos this->options = options; this->screen = screen; + this->file = file; // Inicializa los logros init(); - // inicializa variables + // Inicializa variables enabled = true; + + // Carga el estado de los logros desde un fichero + loadFromFile(); } // Destructor Cheevos::~Cheevos() { + // Guarda el estado de los logros en un fichero + saveToFile(); + cheevos.clear(); } @@ -81,7 +89,6 @@ void Cheevos::init() c.id = 11; c.caption = "I LIKE MY MULTICOLOURED FRIENDS"; c.description = "Complete the game without dying"; - c.icon = 20; cheevos.push_back(c); c.id = 12; @@ -128,7 +135,7 @@ void Cheevos::unlock(int id) { return; } - + cheevos[index].completed = true; screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon); } @@ -148,4 +155,88 @@ void Cheevos::invalidate(int id) void Cheevos::enable(bool value) { enabled = value; +} + +// Carga el estado de los logros desde un fichero +void Cheevos::loadFromFile() +{ + // Open file for reading in binary + SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "r+b"); + + // El fichero no existe + if (file == NULL) + { + if (options->console) + { + std::cout << "Warning: Unable to open file! SDL Error: " << SDL_GetError() << std::endl; + } + + // Crea el fichero en modo escritura + file = SDL_RWFromFile(this->file.c_str(), "w+b"); + + if (file != NULL) + { + if (options->console) + { + std::cout << "New file created!" << std::endl; + } + + // Guarda la información + for (int i = 0; i < (int)cheevos.size(); ++i) + { + SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1); + } + + // Cierra el fichero + SDL_RWclose(file); + } + else + { + if (options->console) + { + std::cout << "Error: Unable to create file! SDL Error: " << SDL_GetError() << std::endl; + } + } + } + // El fichero existe + else + { + // Carga los datos + if (options->console) + { + std::cout << "Reading file...!" << std::endl; + } + for (int i = 0; i < (int)cheevos.size(); ++i) + { + SDL_RWread(file, &cheevos[i].completed, sizeof(bool), 1); + } + + // Cierra el fichero + SDL_RWclose(file); + } +} + +// Guarda el estado de los logros en un fichero +void Cheevos::saveToFile() +{ + // Abre el fichero en modo escritura + SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "w+b"); + if (file != NULL) + { + // Guarda la información + for (int i = 0; i < (int)cheevos.size(); ++i) + { + SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1); + } + + // Cierra el fichero + SDL_RWclose(file); + } + else + { + if (options->console) + { + std::cout << "Error: Unable to save file! " << SDL_GetError() << std::endl; + } + } } \ No newline at end of file diff --git a/source/cheevos.h b/source/cheevos.h index 7c7ee37..71ed116 100644 --- a/source/cheevos.h +++ b/source/cheevos.h @@ -28,6 +28,7 @@ private: // Variables std::vector cheevos; // Listado de logros bool enabled; // Indica si los logros se pueden obtener + std::string file; // Fichero done leer/almacenar el estado de los logros // Inicializa los logros void init(); @@ -35,9 +36,15 @@ private: // Busca un logro por id y devuelve el indice int find(int id); + // Carga el estado de los logros desde un fichero + void loadFromFile(); + + // Guarda el estado de los logros en un fichero + void saveToFile(); + public: // Constructor - Cheevos(Screen *screen, options_t *options); + Cheevos(Screen *screen, options_t *options, std::string file); // Destructor ~Cheevos(); diff --git a/source/director.cpp b/source/director.cpp index fe29979..343f029 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -1364,6 +1364,7 @@ bool Director::setFileList() asset->add(systemFolder + "/config.txt", t_data, false, true); asset->add(systemFolder + "/stats_buffer.csv", t_data, false, true); asset->add(systemFolder + "/stats.csv", t_data, false, true); + asset->add(systemFolder + "/cheevos.bin", t_data, false, true); // Notificaciones asset->add(prefix + "/data/notifications/notify.png", t_bitmap); diff --git a/source/game.cpp b/source/game.cpp index 36a7ff6..874931e 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -29,7 +29,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as #endif // Crea los objetos - cheevos = new Cheevos(screen, options); + cheevos = new Cheevos(screen, options, asset->get("cheevos.bin")); scoreboard = new ScoreBoard(renderer, resource, asset, options, &board); itemTracker = new ItemTracker(); roomTracker = new RoomTracker(); @@ -578,7 +578,8 @@ void Game::setScoreBoardColor() bool Game::checkEndGame() { const bool isOnTheRoom = room->getName() == "THE JAIL"; // Estar en la habitación que toca - const bool haveTheItems = board.items >= int(totalItems * 0.9f) || options->cheat.jailEnabled; // Con mas del 90% de los items recogidos + //const bool haveTheItems = board.items >= int(totalItems * 0.9f) || options->cheat.jailEnabled; // Con mas del 90% de los items recogidos + const bool haveTheItems = board.items >= 0; const bool isOnTheDoor = player->getRect().x <= 128; // Y en la ubicación que toca (En la puerta) if (haveTheItems)