ja guarda i carrega la tabla de records en el disc

This commit is contained in:
2024-08-16 11:57:58 +02:00
parent 9012e3d79d
commit 7d778a9cfa
3 changed files with 83 additions and 102 deletions

View File

@@ -589,7 +589,7 @@ bool Director::loadConfigFile()
// Procesa el fichero linea a linea
if (options->console)
{
std::cout << "Reading file " << filePath << std::endl;
std::cout << "Reading file: " << filePath << std::endl;
}
while (std::getline(file, line))
{
@@ -612,10 +612,6 @@ bool Director::loadConfigFile()
}
// Cierra el fichero
if (options->console)
{
std::cout << "Closing file " << filePath << std::endl;
}
file.close();
}
@@ -650,24 +646,21 @@ bool Director::loadConfigFile()
// Guarda el fichero de configuración
bool Director::saveConfigFile()
{
bool success = true;
const std::string filename = "config.txt";
std::ofstream file(asset->get(filename));
// Crea y abre el fichero de texto
std::ofstream file(asset->get("config.txt"));
if (file.good())
if (!file.good())
{
if (options->console)
{
std::cout << asset->get("config.txt") << " open for writing" << std::endl;
std::cout << filename << " can't be opened" << std::endl;
}
return false;
}
else
{
if (options->console)
{
std::cout << asset->get("config.txt") << " can't be opened" << std::endl;
}
std::cout << "Writing file: " << filename << std::endl;
}
// Opciones de video
@@ -774,7 +767,7 @@ bool Director::saveConfigFile()
// Cierra el fichero
file.close();
return success;
return true;
}
// Carga los sonidos del juego

View File

@@ -42,8 +42,8 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l
loadDemoFile(asset->get("demo2.bin"), &this->demo.dataFile[index2]);
}
// Carga las puntuaciones desde el fichero y establece la máxima puntuación
setHiScore();
// Carga el fichero con la tabla de puntuaciones
loadScoreFile();
n1000Sprite = new SmartSprite(gameTextTexture);
n2500Sprite = new SmartSprite(gameTextTexture);
@@ -63,7 +63,7 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l
Game::~Game()
{
// saveScoreFile();
saveScoreFile();
#ifdef RECORDING
saveDemoFile();
#endif
@@ -630,80 +630,73 @@ void Game::unloadMedia()
JA_DeleteSound(coffeeMachineSound);
}
// Carga el fichero de puntos
// Carga el fichero con la tabla de puntuaciones
bool Game::loadScoreFile()
{
// Indicador de éxito en la carga
bool success = true;
const std::string p = asset->get("score.bin");
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
// El fichero no existe
if (file == nullptr)
{
if (options->console)
{
std::cout << "Warning: Unable to open " << filename.c_str() << " file" << std::endl;
}
// Creamos el fichero para escritura
file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != nullptr)
{
if (options->console)
{
std::cout << "New file (" << filename.c_str() << ") created!" << std::endl;
}
// Inicializamos los datos
for (int i = 0; i < TOTAL_SCORE_DATA; ++i)
{
scoreDataFile[i] = 0;
SDL_RWwrite(file, &scoreDataFile[i], sizeof(Uint32), 1);
}
// Cerramos el fichero
SDL_RWclose(file);
}
else
{
if (options->console)
{
std::cout << "Error: Unable to create file " << filename.c_str() << std::endl;
}
success = false;
}
}
// El fichero existe
else
if (file)
{
// Cargamos los datos
if (options->console)
{
std::cout << "Reading file " << filename.c_str() << std::endl;
std::cout << "Reading file: " << filename.c_str() << std::endl;
}
for (int i = 0; i < TOTAL_SCORE_DATA; ++i)
SDL_RWread(file, &scoreDataFile[i], sizeof(Uint32), 1);
for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i)
{
int nameSize = 0;
// Cierra el fichero
SDL_RWclose(file);
if (SDL_RWread(file, &options->game.hiScoreTable[i].score, sizeof(int), 1) == 0)
{
success = false;
break;
}
// Establece el valor de la máxima puntuación a partir del vector con los datos
if (scoreDataFile[0] == 0)
if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0)
{
hiScore.score = 10000;
success = false;
break;
}
// Comprueba el checksum para ver si se ha modificado el fichero
else if (scoreDataFile[0] % 43 == scoreDataFile[1])
char *name = (char *)malloc(nameSize + 1);
if (SDL_RWread(file, name, sizeof(char) * nameSize, 1) == 0)
{
hiScore.score = scoreDataFile[0];
success = false;
free(name);
break;
}
else
{
hiScore.score = 10000;
name[nameSize] = 0;
options->game.hiScoreTable[i].name = name;
free(name);
}
#ifdef DEBUG
if (false)
{
std::cout << "score: " << options->game.hiScoreTable[i].score << std::endl;
std::cout << "nsize: " << nameSize << std::endl;
std::cout << "name : " << options->game.hiScoreTable[i].name << std::endl;
std::cout << " ---" << std::endl;
}
#endif
}
SDL_RWclose(file);
}
if (!success)
{
ManageHiScoreTable *m = new ManageHiScoreTable(&options->game.hiScoreTable);
m->clear();
delete m;
}
// Establece el valor de la variable desde la tabla de records
hiScore.score = options->game.hiScoreTable[0].score;
hiScore.name = options->game.hiScoreTable[0].name;
return success;
}
@@ -767,7 +760,7 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
// Mensaje de proceder a la carga de los datos
if (options->console)
{
std::cout << "Reading file " << filename.c_str() << std::endl;
std::cout << "Reading file: " << filename.c_str() << std::endl;
}
// Lee todos los datos del fichero y los deja en el destino
@@ -788,28 +781,36 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
// Guarda el fichero de puntos
bool Game::saveScoreFile()
{
// Almacena la máxima puntuación en el fichero junto con un checksum
scoreDataFile[0] = hiScore.score;
scoreDataFile[1] = hiScore.score % 43;
bool success = true;
const std::string p = asset->get("score.bin");
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != nullptr)
if (file)
{
// Guardamos los datos
for (int i = 0; i < TOTAL_SCORE_DATA; ++i)
// Guarda los datos
for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i)
{
SDL_RWwrite(file, &scoreDataFile[i], sizeof(Uint32), 1);
SDL_RWwrite(file, &options->game.hiScoreTable[i].score, sizeof(int), 1);
const int nameSize = (int)options->game.hiScoreTable[i].name.size();
SDL_RWwrite(file, &nameSize, sizeof(int), 1);
SDL_RWwrite(file, options->game.hiScoreTable[i].name.c_str(), nameSize, 1);
#ifdef DEBUG
if (false)
{
std::cout << "score: " << options->game.hiScoreTable[i].score << std::endl;
std::cout << "nsize: " << nameSize << std::endl;
std::cout << "name : " << options->game.hiScoreTable[i].name << std::endl;
std::cout << " ---" << std::endl;
}
#endif
}
if (options->console)
{
std::cout << "Writing file " << filename.c_str() << std::endl;
std::cout << "Writing file: " << filename.c_str() << std::endl;
}
// Cerramos el fichero
// Cierra el fichero
SDL_RWclose(file);
}
else
@@ -2828,16 +2829,6 @@ void Game::reloadTextures()
background->reloadTextures();
}
// Carga las puntuaciones desde el fichero y establece la máxima puntuación
void Game::setHiScore()
{
// Carga el fichero de puntos
// loadScoreFile();
hiScore.score = options->game.hiScoreTable[0].score;
hiScore.name = options->game.hiScoreTable[0].name;
}
// Actualiza el marcador
void Game::updateScoreboard()
{

View File

@@ -222,7 +222,7 @@ private:
// Libera los recursos previamente cargados
void unloadMedia();
// Carga el fichero de puntos
// Carga el fichero con la tabla de puntuaciones
bool loadScoreFile();
// Carga el fichero de datos para la demo
@@ -427,9 +427,6 @@ private:
// Recarga las texturas
void reloadTextures();
// Carga las puntuaciones desde el fichero y establece la máxima puntuación
void setHiScore();
// Actualiza el marcador
void updateScoreboard();