moguts els dos metodes per llegir i escriure la tabla de puntuacions a fitxer a la classe ManageHiScoreTable
el fitxer amb les puntuacions ara nomes es llig al carregar el programa i no cada volta que començem a jugar
This commit is contained in:
134
source/game.cpp
134
source/game.cpp
@@ -42,9 +42,6 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l
|
||||
loadDemoFile(asset->get("demo2.bin"), &this->demo.dataFile[index2]);
|
||||
}
|
||||
|
||||
// Carga el fichero con la tabla de puntuaciones
|
||||
loadScoreFile();
|
||||
|
||||
n1000Sprite = new SmartSprite(gameTextTexture);
|
||||
n2500Sprite = new SmartSprite(gameTextTexture);
|
||||
n5000Sprite = new SmartSprite(gameTextTexture);
|
||||
@@ -63,7 +60,10 @@ Game::Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *l
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
saveScoreFile();
|
||||
// Guarda las puntuaciones en un fichero
|
||||
ManageHiScoreTable *manager = new ManageHiScoreTable(&options->game.hiScoreTable);
|
||||
manager->saveToFile(asset->get("score.bin"));
|
||||
delete manager;
|
||||
#ifdef RECORDING
|
||||
saveDemoFile();
|
||||
#endif
|
||||
@@ -163,6 +163,8 @@ void Game::init(int playerID)
|
||||
scoreboard->setMode(SCOREBOARD_CENTER_PANEL, SCOREBOARD_MODE_STAGE_INFO);
|
||||
|
||||
// Resto de variables
|
||||
hiScore.score = options->game.hiScoreTable[0].score;
|
||||
hiScore.name = options->game.hiScoreTable[0].name;
|
||||
paused = false;
|
||||
gameCompleted = false;
|
||||
gameCompletedCounter = 0;
|
||||
@@ -630,77 +632,6 @@ void Game::unloadMedia()
|
||||
JA_DeleteSound(coffeeMachineSound);
|
||||
}
|
||||
|
||||
// Carga el fichero con la tabla de puntuaciones
|
||||
bool Game::loadScoreFile()
|
||||
{
|
||||
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");
|
||||
if (file)
|
||||
{
|
||||
// Cargamos los datos
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Reading file: " << filename.c_str() << std::endl;
|
||||
}
|
||||
for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i)
|
||||
{
|
||||
int nameSize = 0;
|
||||
|
||||
if (SDL_RWread(file, &options->game.hiScoreTable[i].score, sizeof(int), 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
char *name = (char *)malloc(nameSize + 1);
|
||||
if (SDL_RWread(file, name, sizeof(char) * nameSize, 1) == 0)
|
||||
{
|
||||
success = false;
|
||||
free(name);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
|
||||
{
|
||||
@@ -778,51 +709,6 @@ bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
|
||||
return success;
|
||||
}
|
||||
|
||||
// Guarda el fichero de puntos
|
||||
bool Game::saveScoreFile()
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Guarda los datos
|
||||
for (int i = 0; i < (int)options->game.hiScoreTable.size(); ++i)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (options->console)
|
||||
{
|
||||
std::cout << "Error: Unable to save " << filename.c_str() << " file! " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool Game::saveDemoFile()
|
||||
@@ -2858,10 +2744,10 @@ void Game::pause(bool value)
|
||||
// Añade una puntuación a la tabla de records
|
||||
void Game::addScoreToScoreBoard(std::string name, int score)
|
||||
{
|
||||
hiScoreEntry_t entry = {name, score};
|
||||
ManageHiScoreTable *m = new ManageHiScoreTable(&options->game.hiScoreTable);
|
||||
m->add(entry);
|
||||
delete m;
|
||||
const hiScoreEntry_t entry = {name, score};
|
||||
ManageHiScoreTable *manager = new ManageHiScoreTable(&options->game.hiScoreTable);
|
||||
manager->add(entry);
|
||||
delete manager;
|
||||
}
|
||||
|
||||
// Comprueba el estado de los jugadores
|
||||
|
||||
Reference in New Issue
Block a user