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 <iostream>
// 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;
}
}
}

View File

@@ -28,6 +28,7 @@ private:
// Variables
std::vector<cheevos_t> 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();

View File

@@ -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);

View File

@@ -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)