La tabla de puntuació ja mostra amb altre color la puntuació que s'acaba d'afegir
fix: la tabla de punts no guardava a disc el estat de 1CC de cada entrada
This commit is contained in:
@@ -13,7 +13,7 @@ void ManageHiScoreTable::clear()
|
||||
|
||||
// Añade 10 entradas predefinidas
|
||||
table_.push_back(HiScoreEntry("BRY", 1000000));
|
||||
table_.push_back(HiScoreEntry("USUFON", 500000, true));
|
||||
table_.push_back(HiScoreEntry("USUFON", 500000));
|
||||
table_.push_back(HiScoreEntry("GLUCAS", 100000));
|
||||
table_.push_back(HiScoreEntry("PDLGAT", 50000));
|
||||
table_.push_back(HiScoreEntry("PARRAB", 10000));
|
||||
@@ -27,7 +27,7 @@ void ManageHiScoreTable::clear()
|
||||
}
|
||||
|
||||
// Añade un elemento a la tabla
|
||||
void ManageHiScoreTable::add(HiScoreEntry entry)
|
||||
int ManageHiScoreTable::add(const HiScoreEntry &entry)
|
||||
{
|
||||
// Añade la entrada a la tabla
|
||||
table_.push_back(entry);
|
||||
@@ -35,8 +35,32 @@ void ManageHiScoreTable::add(HiScoreEntry entry)
|
||||
// Ordena la tabla
|
||||
sort();
|
||||
|
||||
// Encontrar la posición del nuevo elemento
|
||||
auto it = std::find_if(table_.begin(), table_.end(), [&](const HiScoreEntry &e)
|
||||
{ return e.name == entry.name &&
|
||||
e.score == entry.score &&
|
||||
e.one_credit_complete == entry.one_credit_complete; });
|
||||
|
||||
int position = -1;
|
||||
if (it != table_.end())
|
||||
{
|
||||
position = std::distance(table_.begin(), it);
|
||||
}
|
||||
|
||||
// Deja solo las 10 primeras entradas
|
||||
table_.resize(10);
|
||||
if (table_.size() > 10)
|
||||
{
|
||||
table_.resize(10);
|
||||
|
||||
// Si el nuevo elemento quedó fuera del top 10
|
||||
if (position >= 10)
|
||||
{
|
||||
position = -1; // No entró en el top 10
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve la posición
|
||||
return position;
|
||||
}
|
||||
|
||||
// Ordena la tabla
|
||||
@@ -50,52 +74,55 @@ void ManageHiScoreTable::sort()
|
||||
std::sort(table_.begin(), table_.end(), scoreDescendingComparator);
|
||||
}
|
||||
|
||||
// Carga la tabla con los datos de un fichero
|
||||
// Carga la tabla desde un fichero
|
||||
bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
|
||||
{
|
||||
clear();
|
||||
auto success = true;
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "rb");
|
||||
|
||||
if (file)
|
||||
{
|
||||
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
|
||||
table_.clear(); // Limpia la tabla actual
|
||||
|
||||
for (auto &entry : table_)
|
||||
// Lee el número de entradas en la tabla
|
||||
int tableSize = 0;
|
||||
SDL_RWread(file, &tableSize, sizeof(int), 1);
|
||||
|
||||
// Lee los datos de cada entrada
|
||||
for (int i = 0; i < tableSize; ++i)
|
||||
{
|
||||
HiScoreEntry entry;
|
||||
|
||||
// Lee la puntuación
|
||||
SDL_RWread(file, &entry.score, sizeof(int), 1);
|
||||
|
||||
// Lee el tamaño del nombre y luego el nombre
|
||||
int nameSize = 0;
|
||||
|
||||
if (SDL_RWread(file, &entry.score, sizeof(int), 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
SDL_RWread(file, &nameSize, sizeof(int), 1);
|
||||
|
||||
std::vector<char> nameBuffer(nameSize + 1);
|
||||
if (SDL_RWread(file, nameBuffer.data(), sizeof(char) * nameSize, 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
nameBuffer[nameSize] = '\0';
|
||||
SDL_RWread(file, nameBuffer.data(), nameSize, 1);
|
||||
nameBuffer[nameSize] = '\0'; // Asegurar el fin de la cadena
|
||||
entry.name = std::string(nameBuffer.data());
|
||||
|
||||
// Lee el valor de one_credit_complete
|
||||
int occValue = 0;
|
||||
SDL_RWread(file, &occValue, sizeof(int), 1);
|
||||
entry.one_credit_complete = (occValue != 0);
|
||||
|
||||
// Añade la entrada a la tabla
|
||||
table_.push_back(entry);
|
||||
}
|
||||
|
||||
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
if (!success)
|
||||
else
|
||||
{
|
||||
clear();
|
||||
std::cout << "Error: Unable to load " << getFileName(file_path) << " file! " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -107,21 +134,35 @@ bool ManageHiScoreTable::saveToFile(const std::string &file_path)
|
||||
|
||||
if (file)
|
||||
{
|
||||
// Guarda los datos
|
||||
for (int i = 0; i < (int)table_.size(); ++i)
|
||||
// Guarda el número de entradas en la tabla
|
||||
int tableSize = static_cast<int>(table_.size());
|
||||
SDL_RWwrite(file, &tableSize, sizeof(int), 1);
|
||||
|
||||
// Guarda los datos de cada entrada
|
||||
for (int i = 0; i < tableSize; ++i)
|
||||
{
|
||||
SDL_RWwrite(file, &table_.at(i).score, sizeof(int), 1);
|
||||
const int nameSize = (int)table_.at(i).name.size();
|
||||
const HiScoreEntry& entry = table_.at(i);
|
||||
|
||||
// Guarda la puntuación
|
||||
SDL_RWwrite(file, &entry.score, sizeof(int), 1);
|
||||
|
||||
// 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, table_.at(i).name.c_str(), nameSize, 1);
|
||||
SDL_RWwrite(file, entry.name.c_str(), nameSize, 1);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
std::cout << "Writing file: " << getFileName(file_path).c_str() << std::endl;
|
||||
std::cout << "Writing file: " << getFileName(file_path) << std::endl;
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: Unable to save " << getFileName(file_path).c_str() << " file! " << SDL_GetError() << std::endl;
|
||||
std::cout << "Error: Unable to save " << getFileName(file_path) << " file! " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
Reference in New Issue
Block a user