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:
@@ -49,6 +49,11 @@ Director::Director(int argc, char *argv[])
|
|||||||
// Carga el fichero de configuración
|
// Carga el fichero de configuración
|
||||||
loadConfigFile();
|
loadConfigFile();
|
||||||
|
|
||||||
|
// Carga el fichero de puntuaciones
|
||||||
|
ManageHiScoreTable *manager = new ManageHiScoreTable(&options->game.hiScoreTable);
|
||||||
|
manager->loadFromFile(asset->get("score.bin"));
|
||||||
|
delete manager;
|
||||||
|
|
||||||
// Inicializa SDL
|
// Inicializa SDL
|
||||||
initSDL();
|
initSDL();
|
||||||
|
|
||||||
@@ -473,9 +478,6 @@ void Director::initOptions()
|
|||||||
// Opciones de juego
|
// Opciones de juego
|
||||||
options->game.difficulty = DIFFICULTY_NORMAL;
|
options->game.difficulty = DIFFICULTY_NORMAL;
|
||||||
options->game.language = ba_BA;
|
options->game.language = ba_BA;
|
||||||
ManageHiScoreTable *m = new ManageHiScoreTable(&options->game.hiScoreTable);
|
|
||||||
m->clear();
|
|
||||||
delete m;
|
|
||||||
|
|
||||||
// Opciones de control
|
// Opciones de control
|
||||||
options->controller.clear();
|
options->controller.clear();
|
||||||
|
|||||||
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]);
|
loadDemoFile(asset->get("demo2.bin"), &this->demo.dataFile[index2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga el fichero con la tabla de puntuaciones
|
|
||||||
loadScoreFile();
|
|
||||||
|
|
||||||
n1000Sprite = new SmartSprite(gameTextTexture);
|
n1000Sprite = new SmartSprite(gameTextTexture);
|
||||||
n2500Sprite = new SmartSprite(gameTextTexture);
|
n2500Sprite = new SmartSprite(gameTextTexture);
|
||||||
n5000Sprite = 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()
|
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
|
#ifdef RECORDING
|
||||||
saveDemoFile();
|
saveDemoFile();
|
||||||
#endif
|
#endif
|
||||||
@@ -163,6 +163,8 @@ void Game::init(int playerID)
|
|||||||
scoreboard->setMode(SCOREBOARD_CENTER_PANEL, SCOREBOARD_MODE_STAGE_INFO);
|
scoreboard->setMode(SCOREBOARD_CENTER_PANEL, SCOREBOARD_MODE_STAGE_INFO);
|
||||||
|
|
||||||
// Resto de variables
|
// Resto de variables
|
||||||
|
hiScore.score = options->game.hiScoreTable[0].score;
|
||||||
|
hiScore.name = options->game.hiScoreTable[0].name;
|
||||||
paused = false;
|
paused = false;
|
||||||
gameCompleted = false;
|
gameCompleted = false;
|
||||||
gameCompletedCounter = 0;
|
gameCompletedCounter = 0;
|
||||||
@@ -630,77 +632,6 @@ void Game::unloadMedia()
|
|||||||
JA_DeleteSound(coffeeMachineSound);
|
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
|
// Carga el fichero de datos para la demo
|
||||||
bool Game::loadDemoFile(std::string f, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA])
|
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;
|
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
|
#ifdef RECORDING
|
||||||
// Guarda el fichero de datos para la demo
|
// Guarda el fichero de datos para la demo
|
||||||
bool Game::saveDemoFile()
|
bool Game::saveDemoFile()
|
||||||
@@ -2858,10 +2744,10 @@ void Game::pause(bool value)
|
|||||||
// Añade una puntuación a la tabla de records
|
// Añade una puntuación a la tabla de records
|
||||||
void Game::addScoreToScoreBoard(std::string name, int score)
|
void Game::addScoreToScoreBoard(std::string name, int score)
|
||||||
{
|
{
|
||||||
hiScoreEntry_t entry = {name, score};
|
const hiScoreEntry_t entry = {name, score};
|
||||||
ManageHiScoreTable *m = new ManageHiScoreTable(&options->game.hiScoreTable);
|
ManageHiScoreTable *manager = new ManageHiScoreTable(&options->game.hiScoreTable);
|
||||||
m->add(entry);
|
manager->add(entry);
|
||||||
delete m;
|
delete manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba el estado de los jugadores
|
// Comprueba el estado de los jugadores
|
||||||
|
|||||||
@@ -222,19 +222,12 @@ private:
|
|||||||
// Libera los recursos previamente cargados
|
// Libera los recursos previamente cargados
|
||||||
void unloadMedia();
|
void unloadMedia();
|
||||||
|
|
||||||
// Carga el fichero con la tabla de puntuaciones
|
|
||||||
bool loadScoreFile();
|
|
||||||
|
|
||||||
// Carga el fichero de datos para la demo
|
// Carga el fichero de datos para la demo
|
||||||
bool loadDemoFile(std::string fileName, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]);
|
bool loadDemoFile(std::string fileName, demoKeys_t (*dataFile)[TOTAL_DEMO_DATA]);
|
||||||
|
|
||||||
// Guarda el fichero de puntos
|
|
||||||
bool saveScoreFile();
|
|
||||||
#ifdef RECORDING
|
#ifdef RECORDING
|
||||||
// Guarda el fichero de datos para la demo
|
// Guarda el fichero de datos para la demo
|
||||||
bool saveDemoFile();
|
bool saveDemoFile();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Crea una formación de enemigos
|
// Crea una formación de enemigos
|
||||||
void deployEnemyFormation();
|
void deployEnemyFormation();
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "manage_hiscore_table.h"
|
#include "manage_hiscore_table.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
ManageHiScoreTable::ManageHiScoreTable(std::vector<hiScoreEntry_t> *table)
|
ManageHiScoreTable::ManageHiScoreTable(std::vector<hiScoreEntry_t> *table)
|
||||||
@@ -56,3 +57,94 @@ void ManageHiScoreTable::sort()
|
|||||||
|
|
||||||
std::sort(table->begin(), table->end(), customLess);
|
std::sort(table->begin(), table->end(), customLess);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Carga la tabla con los datos de un fichero
|
||||||
|
bool ManageHiScoreTable::loadFromFile(std::string filepath)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
|
||||||
|
bool success = true;
|
||||||
|
const std::string p = filepath;
|
||||||
|
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||||
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||||
|
|
||||||
|
if (file)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "Reading file: " << filename.c_str() << std::endl;
|
||||||
|
#endif
|
||||||
|
for (int i = 0; i < (int)table->size(); ++i)
|
||||||
|
{
|
||||||
|
int nameSize = 0;
|
||||||
|
|
||||||
|
if (SDL_RWread(file, &table->at(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;
|
||||||
|
table->at(i).name = name;
|
||||||
|
free(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RWclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guarda la tabla en un fichero
|
||||||
|
bool ManageHiScoreTable::saveToFile(std::string filepath)
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
const std::string p = filepath;
|
||||||
|
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)table->size(); ++i)
|
||||||
|
{
|
||||||
|
SDL_RWwrite(file, &table->at(i).score, sizeof(int), 1);
|
||||||
|
const int nameSize = (int)table->at(i).name.size();
|
||||||
|
SDL_RWwrite(file, &nameSize, sizeof(int), 1);
|
||||||
|
SDL_RWwrite(file, table->at(i).name.c_str(), nameSize, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "Writing file: " << filename.c_str() << std::endl;
|
||||||
|
#endif
|
||||||
|
// Cierra el fichero
|
||||||
|
SDL_RWclose(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "Error: Unable to save " << filename.c_str() << " file! " << SDL_GetError() << std::endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
@@ -5,8 +5,10 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Esta clase sirve para añadir elementos hiScoreEntry_r a un vector (tabla), de manera
|
Esta clase sirve para añadir elementos hiScoreEntry_r a un vector (tabla), de manera
|
||||||
que la tabla siempre está ordenada. Además también tiene un método para dejar la tabla
|
que la tabla siempre está ordenada.
|
||||||
con sus valores iniciales
|
|
||||||
|
Además tiene un método para dejar la tabla con sus valores iniciales y métodos para
|
||||||
|
leer y escribir la tabla a un fichero
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Clase ManageHiScoreTable
|
// Clase ManageHiScoreTable
|
||||||
@@ -31,4 +33,10 @@ public:
|
|||||||
|
|
||||||
// Añade un elemento a la tabla
|
// Añade un elemento a la tabla
|
||||||
void add(hiScoreEntry_t entry);
|
void add(hiScoreEntry_t entry);
|
||||||
|
|
||||||
|
// Carga la tabla con los datos de un fichero
|
||||||
|
bool loadFromFile(std::string filepath);
|
||||||
|
|
||||||
|
// Guarda la tabla en un fichero
|
||||||
|
bool saveToFile(std::string filepath);
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user