Singletonejant

Borrat menu.cpp que no estava gastantse...mmm.. desde mai
This commit is contained in:
2025-02-22 18:27:23 +01:00
parent e361d295c1
commit fc01676df2
38 changed files with 440 additions and 1751 deletions

View File

@@ -3,113 +3,49 @@
#include <SDL2/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWwrite
#include <stddef.h> // Para NULL
#include <iostream> // Para basic_ostream, operator<<, cout, endl
#include "screen.h" // Para Screen
#include "notifier.h" // Para Screen
#include "utils.h" // Para options_t
#include "options.h"
#include <fstream> // Para fstream
// Constructor
Cheevos::Cheevos(Screen *screen, std::string file)
Cheevos::Cheevos(const std::string &file)
: file_(file)
{
// Copia la dirección de los objetos
this->screen = screen;
this->file = file;
// Inicializa los logros
init();
// Inicializa variables
enabled = true;
// Carga el estado de los logros
load();
loadFromFile();
}
// Destructor
Cheevos::~Cheevos()
{
// Guarda el estado de los logros
save();
cheevos.clear();
saveToFile();
}
// Inicializa los logros
void Cheevos::init()
{
cheevos.clear();
cheevos_t c;
c.completed = false;
c.valid = true;
c.icon = 2;
c.id = 1;
c.caption = "SHINY THINGS";
c.description = "Get 25\% of the items";
cheevos.push_back(c);
c.id = 2;
c.caption = "HALF THE WORK";
c.description = "Get 50\% of the items";
cheevos.push_back(c);
c.id = 3;
c.caption = "GETTING THERE";
c.description = "Get 75\% of the items";
cheevos.push_back(c);
c.id = 4;
c.caption = "THE COLLECTOR";
c.description = "Get 100\% of the items";
cheevos.push_back(c);
c.id = 5;
c.caption = "WANDERING AROUND";
c.description = "Visit 20 rooms";
cheevos.push_back(c);
c.id = 6;
c.caption = "I GOT LOST";
c.description = "Visit 40 rooms";
cheevos.push_back(c);
c.id = 7;
c.caption = "I LIKE TO EXPLORE";
c.description = "Visit all rooms";
cheevos.push_back(c);
c.id = 8;
c.caption = "FINISH THE GAME";
c.description = "Complete the game";
cheevos.push_back(c);
c.id = 9;
c.caption = "I WAS SUCKED BY A HOLE";
c.description = "Complete the game without entering the jail";
cheevos.push_back(c);
c.id = 10;
c.caption = "MY LITTLE PROJECTS";
c.description = "Complete the game with all items";
cheevos.push_back(c);
c.id = 11;
c.caption = "I LIKE MY MULTICOLOURED FRIENDS";
c.description = "Complete the game without dying";
cheevos.push_back(c);
c.id = 12;
c.caption = "SHIT PROJECTS DONE FAST";
c.description = "Complete the game in under 30 minutes";
cheevos.push_back(c);
cheevos_.clear();
cheevos_.emplace_back(1, "SHINY THINGS", "Get 25% of the items", 2);
cheevos_.emplace_back(2, "HALF THE WORK", "Get 50% of the items", 2);
cheevos_.emplace_back(3, "GETTING THERE", "Get 75% of the items", 2);
cheevos_.emplace_back(4, "THE COLLECTOR", "Get 100% of the items", 2);
cheevos_.emplace_back(5, "WANDERING AROUND", "Visit 20 rooms", 2);
cheevos_.emplace_back(6, "I GOT LOST", "Visit 40 rooms", 2);
cheevos_.emplace_back(7, "I LIKE TO EXPLORE", "Visit all rooms", 2);
cheevos_.emplace_back(8, "FINISH THE GAME", "Complete the game", 2);
cheevos_.emplace_back(9, "I WAS SUCKED BY A HOLE", "Complete the game without entering the jail", 2);
cheevos_.emplace_back(10, "MY LITTLE PROJECTS", "Complete the game with all items", 2);
cheevos_.emplace_back(11, "I LIKE MY MULTICOLOURED FRIENDS", "Complete the game without dying", 2);
cheevos_.emplace_back(12, "SHIT PROJECTS DONE FAST", "Complete the game in under 30 minutes", 2);
}
// Busca un logro por id y devuelve el indice
int Cheevos::find(int id)
{
for (int i = 0; i < (int)cheevos.size(); ++i)
for (int i = 0; i < (int)cheevos_.size(); ++i)
{
if (cheevos[i].id == id)
if (cheevos_[i].id == id)
{
return i;
}
@@ -123,80 +59,49 @@ void Cheevos::unlock(int id)
{
const int index = find(id);
if (index == -1)
// Si el índice es inválido, el logro no es válido, ya está completado o el sistema de logros no está habilitado, no hacemos nada
if (index == -1 || !cheevos_.at(index).valid || cheevos_.at(index).completed || !enabled_)
{
return;
}
if (!cheevos[index].valid)
{
return;
}
if (cheevos[index].completed)
{
return;
}
if (!enabled)
{
return;
}
cheevos[index].completed = true;
screen->showNotification("ACHIEVEMENT UNLOCKED!", cheevos[index].caption, cheevos[index].icon);
save();
// Marcar el logro como completado
cheevos_.at(index).completed = true;
// Mostrar notificación en la pantalla
Notifier::get()->show("ACHIEVEMENT UNLOCKED!", cheevos_.at(index).caption, cheevos_.at(index).icon);
// Guardar el estado de los logros
saveToFile();
}
// Invalida un logro
void Cheevos::invalidate(int id)
{
const int index = find(id);
if (index == -1)
// Si el índice es válido, se invalida el logro
if (index != -1)
{
return;
cheevos_.at(index).valid = false;
}
cheevos[index].valid = false;
}
// Habilita o deshabilita los logros
void Cheevos::enable(bool value)
{
enabled = value;
}
// Carga el estado de los logros
void Cheevos::load()
{
// Carga el estado de los logros desde un fichero
loadFromFile();
}
// Guarda el estado de los logros
void Cheevos::save()
{
// Guarda el estado de los logros en un fichero
saveToFile();
}
// Carga el estado de los logros desde un fichero
void Cheevos::loadFromFile()
{
// Abre el fichero en modo lectura (binario)
SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "r+b");
std::ifstream file(this->file_, std::ios::binary);
// El fichero no existe
if (file == NULL)
if (!file)
{
if (options.console)
{
std::cout << "Warning: Unable to open file! SDL Error: " << SDL_GetError() << std::endl;
std::cout << "Warning: Unable to open file! Creating new file..." << std::endl;
}
// Crea el fichero en modo escritura (binario)
file = SDL_RWFromFile(this->file.c_str(), "w+b");
std::ofstream newFile(this->file_, std::ios::binary);
if (file != NULL)
if (newFile)
{
if (options.console)
{
@@ -204,37 +109,32 @@ void Cheevos::loadFromFile()
}
// Guarda la información
for (int i = 0; i < (int)cheevos.size(); ++i)
for (const auto &cheevo : cheevos_)
{
SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1);
newFile.write(reinterpret_cast<const char *>(&cheevo.completed), sizeof(bool));
}
// Cierra el fichero
SDL_RWclose(file);
}
else
{
if (options.console)
{
std::cout << "Error: Unable to create file! SDL Error: " << SDL_GetError() << std::endl;
std::cerr << "Error: Unable to create file!" << 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);
// Carga los datos
for (auto &cheevo : cheevos_)
{
file.read(reinterpret_cast<char *>(&cheevo.completed), sizeof(bool));
}
}
}
@@ -242,13 +142,13 @@ void Cheevos::loadFromFile()
void Cheevos::saveToFile()
{
// Abre el fichero en modo escritura (binario)
SDL_RWops *file = SDL_RWFromFile(this->file.c_str(), "w+b");
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)
for (int i = 0; i < (int)cheevos_.size(); ++i)
{
SDL_RWwrite(file, &cheevos[i].completed, sizeof(bool), 1);
SDL_RWwrite(file, &cheevos_[i].completed, sizeof(bool), 1);
}
// Cierra el fichero
@@ -263,32 +163,16 @@ void Cheevos::saveToFile()
}
}
// Lista los logros
std::vector<cheevos_t> Cheevos::list()
{
return cheevos;
}
// Devuelve el número total de logros desbloqueados
int Cheevos::unlocked()
{
int count = 0;
for (auto cheevo : cheevos)
for (auto cheevo : cheevos_)
{
if (cheevo.completed)
{
count++;
}
}
return count;
}
// Devuelve el número total de logros
int Cheevos::count()
{
return cheevos.size();
}
// Vuelve a cargar los logros desde el origen
void Cheevos::reload()
{
load();
}