Ya se guardan los logros en un fichero

This commit is contained in:
2023-01-02 09:46:09 +01:00
parent faf2e69b63
commit 5e7be1c2fb
4 changed files with 107 additions and 7 deletions

View File

@@ -1,22 +1,30 @@
#include "cheevos.h" #include "cheevos.h"
#include <iostream>
// Constructor // 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 // Copia la dirección de los objetos
this->options = options; this->options = options;
this->screen = screen; this->screen = screen;
this->file = file;
// Inicializa los logros // Inicializa los logros
init(); init();
// inicializa variables // Inicializa variables
enabled = true; enabled = true;
// Carga el estado de los logros desde un fichero
loadFromFile();
} }
// Destructor // Destructor
Cheevos::~Cheevos() Cheevos::~Cheevos()
{ {
// Guarda el estado de los logros en un fichero
saveToFile();
cheevos.clear(); cheevos.clear();
} }
@@ -81,7 +89,6 @@ void Cheevos::init()
c.id = 11; c.id = 11;
c.caption = "I LIKE MY MULTICOLOURED FRIENDS"; c.caption = "I LIKE MY MULTICOLOURED FRIENDS";
c.description = "Complete the game without dying"; c.description = "Complete the game without dying";
c.icon = 20;
cheevos.push_back(c); cheevos.push_back(c);
c.id = 12; c.id = 12;
@@ -149,3 +156,87 @@ void Cheevos::enable(bool value)
{ {
enabled = 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;
}
}
}

View File

@@ -28,6 +28,7 @@ private:
// Variables // Variables
std::vector<cheevos_t> cheevos; // Listado de logros std::vector<cheevos_t> cheevos; // Listado de logros
bool enabled; // Indica si los logros se pueden obtener bool enabled; // Indica si los logros se pueden obtener
std::string file; // Fichero done leer/almacenar el estado de los logros
// Inicializa los logros // Inicializa los logros
void init(); void init();
@@ -35,9 +36,15 @@ private:
// Busca un logro por id y devuelve el indice // Busca un logro por id y devuelve el indice
int find(int id); 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: public:
// Constructor // Constructor
Cheevos(Screen *screen, options_t *options); Cheevos(Screen *screen, options_t *options, std::string file);
// Destructor // Destructor
~Cheevos(); ~Cheevos();

View File

@@ -1364,6 +1364,7 @@ bool Director::setFileList()
asset->add(systemFolder + "/config.txt", t_data, false, true); asset->add(systemFolder + "/config.txt", t_data, false, true);
asset->add(systemFolder + "/stats_buffer.csv", 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 + "/stats.csv", t_data, false, true);
asset->add(systemFolder + "/cheevos.bin", t_data, false, true);
// Notificaciones // Notificaciones
asset->add(prefix + "/data/notifications/notify.png", t_bitmap); asset->add(prefix + "/data/notifications/notify.png", t_bitmap);

View File

@@ -29,7 +29,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
#endif #endif
// Crea los objetos // 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); scoreboard = new ScoreBoard(renderer, resource, asset, options, &board);
itemTracker = new ItemTracker(); itemTracker = new ItemTracker();
roomTracker = new RoomTracker(); roomTracker = new RoomTracker();
@@ -578,7 +578,8 @@ void Game::setScoreBoardColor()
bool Game::checkEndGame() bool Game::checkEndGame()
{ {
const bool isOnTheRoom = room->getName() == "THE JAIL"; // Estar en la habitación que toca 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) const bool isOnTheDoor = player->getRect().x <= 128; // Y en la ubicación que toca (En la puerta)
if (haveTheItems) if (haveTheItems)