ja guarda i carrega la tabla de records en el disc
This commit is contained in:
@@ -589,7 +589,7 @@ bool Director::loadConfigFile()
|
|||||||
// Procesa el fichero linea a linea
|
// Procesa el fichero linea a linea
|
||||||
if (options->console)
|
if (options->console)
|
||||||
{
|
{
|
||||||
std::cout << "Reading file " << filePath << std::endl;
|
std::cout << "Reading file: " << filePath << std::endl;
|
||||||
}
|
}
|
||||||
while (std::getline(file, line))
|
while (std::getline(file, line))
|
||||||
{
|
{
|
||||||
@@ -612,10 +612,6 @@ bool Director::loadConfigFile()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cierra el fichero
|
// Cierra el fichero
|
||||||
if (options->console)
|
|
||||||
{
|
|
||||||
std::cout << "Closing file " << filePath << std::endl;
|
|
||||||
}
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -650,24 +646,21 @@ bool Director::loadConfigFile()
|
|||||||
// Guarda el fichero de configuración
|
// Guarda el fichero de configuración
|
||||||
bool Director::saveConfigFile()
|
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
|
if (!file.good())
|
||||||
std::ofstream file(asset->get("config.txt"));
|
|
||||||
|
|
||||||
if (file.good())
|
|
||||||
{
|
{
|
||||||
if (options->console)
|
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)
|
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
|
// Opciones de video
|
||||||
@@ -774,7 +767,7 @@ bool Director::saveConfigFile()
|
|||||||
// Cierra el fichero
|
// Cierra el fichero
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
return success;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga los sonidos del juego
|
// Carga los sonidos del juego
|
||||||
|
|||||||
145
source/game.cpp
145
source/game.cpp
@@ -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]);
|
loadDemoFile(asset->get("demo2.bin"), &this->demo.dataFile[index2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga las puntuaciones desde el fichero y establece la máxima puntuación
|
// Carga el fichero con la tabla de puntuaciones
|
||||||
setHiScore();
|
loadScoreFile();
|
||||||
|
|
||||||
n1000Sprite = new SmartSprite(gameTextTexture);
|
n1000Sprite = new SmartSprite(gameTextTexture);
|
||||||
n2500Sprite = 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()
|
Game::~Game()
|
||||||
{
|
{
|
||||||
// saveScoreFile();
|
saveScoreFile();
|
||||||
#ifdef RECORDING
|
#ifdef RECORDING
|
||||||
saveDemoFile();
|
saveDemoFile();
|
||||||
#endif
|
#endif
|
||||||
@@ -630,80 +630,73 @@ void Game::unloadMedia()
|
|||||||
JA_DeleteSound(coffeeMachineSound);
|
JA_DeleteSound(coffeeMachineSound);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga el fichero de puntos
|
// Carga el fichero con la tabla de puntuaciones
|
||||||
bool Game::loadScoreFile()
|
bool Game::loadScoreFile()
|
||||||
{
|
{
|
||||||
// Indicador de éxito en la carga
|
|
||||||
bool success = true;
|
bool success = true;
|
||||||
const std::string p = asset->get("score.bin");
|
const std::string p = asset->get("score.bin");
|
||||||
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||||
|
if (file)
|
||||||
// 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
|
|
||||||
{
|
{
|
||||||
// Cargamos los datos
|
// Cargamos los datos
|
||||||
if (options->console)
|
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)
|
for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i)
|
||||||
SDL_RWread(file, &scoreDataFile[i], sizeof(Uint32), 1);
|
{
|
||||||
|
int nameSize = 0;
|
||||||
|
|
||||||
// Cierra el fichero
|
if (SDL_RWread(file, &options->game.hiScoreTable[i].score, sizeof(int), 1) == 0)
|
||||||
SDL_RWclose(file);
|
{
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece el valor de la máxima puntuación a partir del vector con los datos
|
if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0)
|
||||||
if (scoreDataFile[0] == 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
|
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;
|
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
|
// Mensaje de proceder a la carga de los datos
|
||||||
if (options->console)
|
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
|
// 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
|
// Guarda el fichero de puntos
|
||||||
bool Game::saveScoreFile()
|
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;
|
bool success = true;
|
||||||
const std::string p = asset->get("score.bin");
|
const std::string p = asset->get("score.bin");
|
||||||
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
||||||
if (file != nullptr)
|
if (file)
|
||||||
{
|
{
|
||||||
// Guardamos los datos
|
// Guarda los datos
|
||||||
for (int i = 0; i < TOTAL_SCORE_DATA; ++i)
|
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)
|
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);
|
SDL_RWclose(file);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2828,16 +2829,6 @@ void Game::reloadTextures()
|
|||||||
background->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
|
// Actualiza el marcador
|
||||||
void Game::updateScoreboard()
|
void Game::updateScoreboard()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ private:
|
|||||||
// Libera los recursos previamente cargados
|
// Libera los recursos previamente cargados
|
||||||
void unloadMedia();
|
void unloadMedia();
|
||||||
|
|
||||||
// Carga el fichero de puntos
|
// Carga el fichero con la tabla de puntuaciones
|
||||||
bool loadScoreFile();
|
bool loadScoreFile();
|
||||||
|
|
||||||
// Carga el fichero de datos para la demo
|
// Carga el fichero de datos para la demo
|
||||||
@@ -427,9 +427,6 @@ private:
|
|||||||
// Recarga las texturas
|
// Recarga las texturas
|
||||||
void reloadTextures();
|
void reloadTextures();
|
||||||
|
|
||||||
// Carga las puntuaciones desde el fichero y establece la máxima puntuación
|
|
||||||
void setHiScore();
|
|
||||||
|
|
||||||
// Actualiza el marcador
|
// Actualiza el marcador
|
||||||
void updateScoreboard();
|
void updateScoreboard();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user