forked from jaildesigner-jailgames/jaildoctors_dilemma
Redistribuits els .cpp en carpetes
Actualitzat cmake Modificats els include de SDL2 a SDL3
This commit is contained in:
@@ -1,50 +1,47 @@
|
||||
#include "cheevos.h"
|
||||
#include <SDL2/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL2/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWwrite
|
||||
#include <stddef.h> // Para NULL
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include "notifier.h" // Para Notifier
|
||||
#include "options.h" // Para Options, options
|
||||
|
||||
#include <SDL3/SDL_error.h> // Para SDL_GetError
|
||||
#include <SDL3/SDL_rwops.h> // Para SDL_RWFromFile, SDL_RWclose, SDL_RWwrite
|
||||
#include <stddef.h> // Para NULL
|
||||
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
|
||||
#include <iostream> // Para cout, cerr
|
||||
|
||||
#include "notifier.h" // Para Notifier
|
||||
#include "options.h" // Para Options, options
|
||||
|
||||
// [SINGLETON]
|
||||
Cheevos *Cheevos::cheevos_ = nullptr;
|
||||
Cheevos* Cheevos::cheevos_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Cheevos::init(const std::string &file)
|
||||
{
|
||||
void Cheevos::init(const std::string& file) {
|
||||
Cheevos::cheevos_ = new Cheevos(file);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Cheevos::destroy()
|
||||
{
|
||||
void Cheevos::destroy() {
|
||||
delete Cheevos::cheevos_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Cheevos *Cheevos::get()
|
||||
{
|
||||
Cheevos* Cheevos::get() {
|
||||
return Cheevos::cheevos_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Cheevos::Cheevos(const std::string &file)
|
||||
: file_(file)
|
||||
{
|
||||
Cheevos::Cheevos(const std::string& file)
|
||||
: file_(file) {
|
||||
init();
|
||||
loadFromFile();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Cheevos::~Cheevos()
|
||||
{
|
||||
Cheevos::~Cheevos() {
|
||||
saveToFile();
|
||||
}
|
||||
|
||||
// Inicializa los logros
|
||||
void Cheevos::init()
|
||||
{
|
||||
void Cheevos::init() {
|
||||
cheevos_list_.clear();
|
||||
cheevos_list_.emplace_back(1, "SHINY THINGS", "Get 25% of the items", 2);
|
||||
cheevos_list_.emplace_back(2, "HALF THE WORK", "Get 50% of the items", 2);
|
||||
@@ -61,12 +58,9 @@ void Cheevos::init()
|
||||
}
|
||||
|
||||
// Busca un logro por id y devuelve el indice
|
||||
int Cheevos::find(int id)
|
||||
{
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i)
|
||||
{
|
||||
if (cheevos_list_[i].id == id)
|
||||
{
|
||||
int Cheevos::find(int id) {
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i) {
|
||||
if (cheevos_list_[i].id == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -75,13 +69,11 @@ int Cheevos::find(int id)
|
||||
}
|
||||
|
||||
// Desbloquea un logro
|
||||
void Cheevos::unlock(int id)
|
||||
{
|
||||
void Cheevos::unlock(int id) {
|
||||
const int INDEX = find(id);
|
||||
|
||||
// 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_list_.at(INDEX).obtainable || cheevos_list_.at(INDEX).completed || !enabled_)
|
||||
{
|
||||
if (INDEX == -1 || !cheevos_list_.at(INDEX).obtainable || cheevos_list_.at(INDEX).completed || !enabled_) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -96,103 +88,80 @@ void Cheevos::unlock(int id)
|
||||
}
|
||||
|
||||
// Invalida un logro
|
||||
void Cheevos::setUnobtainable(int id)
|
||||
{
|
||||
void Cheevos::setUnobtainable(int id) {
|
||||
const int index = find(id);
|
||||
|
||||
// Si el índice es válido, se invalida el logro
|
||||
if (index != -1)
|
||||
{
|
||||
if (index != -1) {
|
||||
cheevos_list_.at(index).obtainable = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga el estado de los logros desde un fichero
|
||||
void Cheevos::loadFromFile()
|
||||
{
|
||||
void Cheevos::loadFromFile() {
|
||||
std::ifstream file(file_, std::ios::binary);
|
||||
|
||||
// El fichero no existe
|
||||
if (!file)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
if (!file) {
|
||||
if (options.console) {
|
||||
std::cout << "Warning: Unable to open " << file_ << "! Creating new file..." << std::endl;
|
||||
}
|
||||
|
||||
// Crea el fichero en modo escritura (binario)
|
||||
std::ofstream newFile(file_, std::ios::binary);
|
||||
|
||||
if (newFile)
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
if (newFile) {
|
||||
if (options.console) {
|
||||
std::cout << "New " << file_ << " created!" << std::endl;
|
||||
}
|
||||
|
||||
// Guarda la información
|
||||
for (const auto &cheevo : cheevos_list_)
|
||||
{
|
||||
newFile.write(reinterpret_cast<const char *>(&cheevo.completed), sizeof(bool));
|
||||
for (const auto& cheevo : cheevos_list_) {
|
||||
newFile.write(reinterpret_cast<const char*>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
} else {
|
||||
if (options.console) {
|
||||
std::cerr << "Error: Unable to create " << file_ << "!" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
else {
|
||||
if (options.console) {
|
||||
std::cout << "Reading " << file_ << std::endl;
|
||||
}
|
||||
|
||||
// Carga los datos
|
||||
for (auto &cheevo : cheevos_list_)
|
||||
{
|
||||
file.read(reinterpret_cast<char *>(&cheevo.completed), sizeof(bool));
|
||||
for (auto& cheevo : cheevos_list_) {
|
||||
file.read(reinterpret_cast<char*>(&cheevo.completed), sizeof(bool));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Guarda el estado de los logros en un fichero
|
||||
void Cheevos::saveToFile()
|
||||
{
|
||||
void Cheevos::saveToFile() {
|
||||
// Abre el fichero en modo escritura (binario)
|
||||
SDL_RWops *file = SDL_RWFromFile(this->file_.c_str(), "w+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
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_list_.size(); ++i)
|
||||
{
|
||||
for (int i = 0; i < (int)cheevos_list_.size(); ++i) {
|
||||
SDL_RWwrite(file, &cheevos_list_[i].completed, sizeof(bool), 1);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
} else {
|
||||
if (options.console) {
|
||||
std::cout << "Error: Unable to save file! " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve el número total de logros desbloqueados
|
||||
int Cheevos::getTotalUnlockedAchievements()
|
||||
{
|
||||
int Cheevos::getTotalUnlockedAchievements() {
|
||||
int count = 0;
|
||||
for (const auto &cheevo : cheevos_list_)
|
||||
{
|
||||
if (cheevo.completed)
|
||||
{
|
||||
for (const auto& cheevo : cheevos_list_) {
|
||||
if (cheevo.completed) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -200,10 +169,8 @@ int Cheevos::getTotalUnlockedAchievements()
|
||||
}
|
||||
|
||||
// Elimina el estado "no obtenible"
|
||||
void Cheevos::clearUnobtainableState()
|
||||
{
|
||||
for (auto &cheevo : cheevos_list_)
|
||||
{
|
||||
void Cheevos::clearUnobtainableState() {
|
||||
for (auto& cheevo : cheevos_list_) {
|
||||
cheevo.obtainable = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user