migrant a SDL3

This commit is contained in:
2025-03-27 08:14:37 +01:00
parent a9c869baf6
commit d2286905dc
83 changed files with 570 additions and 541 deletions
+29 -29
View File
@@ -1,6 +1,6 @@
#include "manage_hiscore_table.h"
#include <SDL3/SDL_error.h> // Para SDL_GetError
#include <SDL3/SDL_rwops.h> // Para SDL_RWread, SDL_RWwrite, SDL_RWFromFile
#include <SDL3/SDL.h> // Para SDL_RWread, SDL_WriteIO, SDL_RWFromFile
#include <algorithm> // Para find_if, sort
#include <iostream> // Para basic_ostream, operator<<, cout, endl
#include <iterator> // Para distance
@@ -70,9 +70,9 @@ void ManageHiScoreTable::sort()
struct
{
bool operator()(const HiScoreEntry &a, const HiScoreEntry &b) const { return a.score > b.score; }
} scoreDescendingComparator;
} score_descending_comparator;
std::sort(table_.begin(), table_.end(), scoreDescendingComparator);
std::sort(table_.begin(), table_.end(), score_descending_comparator);
}
// Carga la tabla desde un fichero
@@ -80,44 +80,44 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
{
clear();
auto success = true;
auto file = SDL_RWFromFile(file_path.c_str(), "rb");
auto file = SDL_IOFromFile(file_path.c_str(), "rb");
if (file)
{
table_.clear(); // Limpia la tabla actual
// Lee el número de entradas en la tabla
int tableSize = 0;
SDL_RWread(file, &tableSize, sizeof(int), 1);
int table_size = 0;
SDL_ReadIO(file, &table_size, sizeof(int));
// Lee los datos de cada entrada
for (int i = 0; i < tableSize; ++i)
for (int i = 0; i < table_size; ++i)
{
HiScoreEntry entry;
// Lee la puntuación
SDL_RWread(file, &entry.score, sizeof(int), 1);
SDL_ReadIO(file, &entry.score, sizeof(int));
// Lee el tamaño del nombre y luego el nombre
int nameSize = 0;
SDL_RWread(file, &nameSize, sizeof(int), 1);
int name_size = 0;
SDL_ReadIO(file, &name_size, sizeof(int));
std::vector<char> nameBuffer(nameSize + 1);
SDL_RWread(file, nameBuffer.data(), nameSize, 1);
nameBuffer[nameSize] = '\0'; // Asegurar el fin de la cadena
entry.name = std::string(nameBuffer.data());
std::vector<char> name_buffer(name_size + 1);
SDL_ReadIO(file, name_buffer.data(), name_size);
name_buffer[name_size] = '\0'; // Asegurar el fin de la cadena
entry.name = std::string(name_buffer.data());
// Lee el valor de one_credit_complete
int occValue = 0;
SDL_RWread(file, &occValue, sizeof(int), 1);
entry.one_credit_complete = (occValue != 0);
int occ_value = 0;
SDL_ReadIO(file, &occ_value, sizeof(int));
entry.one_credit_complete = (occ_value != 0);
// Añade la entrada a la tabla
table_.push_back(entry);
}
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
SDL_RWclose(file);
SDL_CloseIO(file);
}
else
{
@@ -131,34 +131,34 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
bool ManageHiScoreTable::saveToFile(const std::string &file_path)
{
auto success = true;
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
if (file)
{
// Guarda el número de entradas en la tabla
int tableSize = static_cast<int>(table_.size());
SDL_RWwrite(file, &tableSize, sizeof(int), 1);
int table_size = static_cast<int>(table_.size());
SDL_WriteIO(file, &table_size, sizeof(int));
// Guarda los datos de cada entrada
for (int i = 0; i < tableSize; ++i)
for (int i = 0; i < table_size; ++i)
{
const HiScoreEntry &entry = table_.at(i);
// Guarda la puntuación
SDL_RWwrite(file, &entry.score, sizeof(int), 1);
SDL_WriteIO(file, &entry.score, sizeof(int));
// Guarda el tamaño del nombre y luego el nombre
int nameSize = static_cast<int>(entry.name.size());
SDL_RWwrite(file, &nameSize, sizeof(int), 1);
SDL_RWwrite(file, entry.name.c_str(), nameSize, 1);
int name_size = static_cast<int>(entry.name.size());
SDL_WriteIO(file, &name_size, sizeof(int));
SDL_WriteIO(file, entry.name.c_str(), name_size);
// Guarda el valor de one_credit_complete como un entero (0 o 1)
int occValue = entry.one_credit_complete ? 1 : 0;
SDL_RWwrite(file, &occValue, sizeof(int), 1);
int occ_value = entry.one_credit_complete ? 1 : 0;
SDL_WriteIO(file, &occ_value, sizeof(int));
}
std::cout << "Writing file: " << getFileName(file_path) << std::endl;
SDL_RWclose(file);
SDL_CloseIO(file);
}
else
{