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