Precàrrega dels fitxers amb dades per al mode demostració
This commit is contained in:
@@ -69,10 +69,10 @@ Game::Game(int player_id, int current_stage, bool demo)
|
||||
// Inicializa los vectores con los datos para la demo
|
||||
if (demo_.enabled)
|
||||
{ // Aleatoriza la asignación del fichero
|
||||
const std::string demo1 = rand() % 2 == 0 ? "demo1.bin" : "demo2.bin";
|
||||
const std::string demo2 = (demo1 == "demo1.bin") ? "demo2.bin" : "demo1.bin";
|
||||
demo_.data.emplace_back(loadDemoDataFromFile(asset_->get(demo1)));
|
||||
demo_.data.emplace_back(loadDemoDataFromFile(asset_->get(demo2)));
|
||||
const auto demo1 = rand() % 2;
|
||||
const auto demo2 = (demo1 == 0) ? 1 : 0;
|
||||
demo_.data.emplace_back(Resource::get()->getDemoData(demo1));
|
||||
demo_.data.emplace_back(Resource::get()->getDemoData(demo2));
|
||||
}
|
||||
|
||||
background_->setPos(param.game.play_area.rect);
|
||||
@@ -98,7 +98,7 @@ Game::~Game()
|
||||
manager->saveToFile(asset_->get("score.bin"));
|
||||
}
|
||||
#ifdef RECORDING
|
||||
saveDemoFile(Asset::get()->get("demo1.bin"));
|
||||
saveDemoFile(Asset::get()->get("demo1.bin"), demo_.data.at(0));
|
||||
#endif
|
||||
|
||||
// Elimina todos los objetos contenidos en vectores
|
||||
@@ -419,93 +419,6 @@ void Game::unloadMedia()
|
||||
item_animations_.clear();
|
||||
}
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
DemoData Game::loadDemoDataFromFile(const std::string &file_path)
|
||||
{
|
||||
DemoData dd;
|
||||
|
||||
// Indicador de éxito en la carga
|
||||
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
|
||||
if (!file)
|
||||
{ // El fichero no existe
|
||||
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
|
||||
|
||||
// Creamos el fichero para escritura
|
||||
file = SDL_RWFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
// Si ha creado el fichero
|
||||
if (file)
|
||||
{
|
||||
std::cout << "New file (" << file_name.c_str() << ") created!" << std::endl;
|
||||
|
||||
// Inicializas los datos y los guarda en el fichero
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
DemoKeys dk = DemoKeys();
|
||||
dd.push_back(dk);
|
||||
SDL_RWwrite(file, &dk, sizeof(DemoKeys), 1);
|
||||
}
|
||||
|
||||
// Cerramos el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{ // Si no puede crear el fichero
|
||||
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Mensaje de proceder a la carga de los datos
|
||||
std::cout << "Reading file: " << file_name.c_str() << std::endl;
|
||||
|
||||
// Lee todos los datos del fichero y los deja en el destino
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
DemoKeys dk = DemoKeys();
|
||||
SDL_RWread(file, &dk, sizeof(DemoKeys), 1);
|
||||
dd.push_back(dk);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
return dd;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool Game::saveDemoFile(const std::string &file_path)
|
||||
{
|
||||
auto success = true;
|
||||
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
|
||||
if (file)
|
||||
{
|
||||
// Guarda los datos
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
SDL_RWwrite(file, &demo_.data[0][i], sizeof(DemoKeys), 1);
|
||||
}
|
||||
|
||||
std::cout << "Writing file " << file_name.c_str() << std::endl;
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
#endif // RECORDING
|
||||
|
||||
// Crea una formación de enemigos
|
||||
void Game::deployBalloonFormation()
|
||||
{
|
||||
|
||||
@@ -32,7 +32,6 @@ constexpr bool GAME_MODE_DEMO_ON = true;
|
||||
|
||||
// Cantidad de elementos a escribir en los ficheros de datos
|
||||
constexpr int TOTAL_SCORE_DATA = 3;
|
||||
constexpr int TOTAL_DEMO_DATA = 2000;
|
||||
|
||||
/*
|
||||
Esta clase gestiona un estado del programa. Se encarga de toda la parte en la
|
||||
@@ -60,17 +59,6 @@ constexpr int TOTAL_DEMO_DATA = 2000;
|
||||
puntuación mínima.
|
||||
*/
|
||||
|
||||
using DemoData = std::vector<DemoKeys>;
|
||||
|
||||
struct Demo
|
||||
{
|
||||
bool enabled; // Indica si está activo el modo demo
|
||||
bool recording; // Indica si está activado el modo para grabar la demo
|
||||
int counter; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
};
|
||||
|
||||
// Clase Game
|
||||
class Game
|
||||
{
|
||||
@@ -204,12 +192,6 @@ private:
|
||||
// Libera los recursos previamente cargados
|
||||
void unloadMedia();
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
DemoData loadDemoDataFromFile(const std::string &file_path);
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool saveDemoFile(const std::string &file_path);
|
||||
#endif
|
||||
// Crea una formación de enemigos
|
||||
void deployBalloonFormation();
|
||||
|
||||
|
||||
@@ -33,9 +33,9 @@ Resource::Resource()
|
||||
loadTextures();
|
||||
loadTextFiles();
|
||||
loadAnimations();
|
||||
loadDemoData();
|
||||
addPalettes();
|
||||
std::cout << "\n** RESOURCES LOADED" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -118,6 +118,12 @@ Animations &Resource::getAnimation(const std::string &name)
|
||||
throw std::runtime_error("Animación no encontrada: " + name);
|
||||
}
|
||||
|
||||
// Obtiene el fichero con los datos para el modo demostración a partir de un çindice
|
||||
DemoData &Resource::getDemoData(int index)
|
||||
{
|
||||
return demos_.at(index);
|
||||
}
|
||||
|
||||
// Carga los sonidos
|
||||
void Resource::loadSounds()
|
||||
{
|
||||
@@ -223,6 +229,14 @@ void Resource::loadAnimations()
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los datos para el modo demostración
|
||||
void Resource::loadDemoData()
|
||||
{
|
||||
std::cout << "\n>> DEMO_FILES" << std::endl;
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo1.bin")));
|
||||
demos_.emplace_back(loadDemoDataFromFile(Asset::get()->get("demo2.bin")));
|
||||
}
|
||||
|
||||
// Añade paletas a las texturas
|
||||
void Resource::addPalettes()
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "jail_audio.h"
|
||||
#include "texture.h"
|
||||
#include "text.h"
|
||||
#include "utils.h"
|
||||
#include "animated_sprite.h"
|
||||
|
||||
// Estructura para almacenar ficheros de sonido y su nombre
|
||||
@@ -75,6 +76,7 @@ private:
|
||||
std::vector<ResourceTexture> textures_; // Vector con las musicas
|
||||
std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto
|
||||
std::vector<ResourceAnimation> animations_; // Vector con las animaciones
|
||||
std::vector<DemoData> demos_; // Vector con los ficheros de datos para el modo demostración
|
||||
|
||||
// Carga los sonidos
|
||||
void loadSounds();
|
||||
@@ -91,6 +93,9 @@ private:
|
||||
// Carga las animaciones
|
||||
void loadAnimations();
|
||||
|
||||
// Carga los datos para el modo demostración
|
||||
void loadDemoData();
|
||||
|
||||
// Añade paletas a las texturas
|
||||
void addPalettes();
|
||||
|
||||
@@ -126,4 +131,7 @@ public:
|
||||
|
||||
// Obtiene la animación a partir de un nombre
|
||||
Animations &getAnimation(const std::string &name);
|
||||
|
||||
// Obtiene el fichero con los datos para el modo demostración a partir de un çindice
|
||||
DemoData &getDemoData(int index);
|
||||
};
|
||||
101
source/utils.cpp
101
source/utils.cpp
@@ -203,3 +203,104 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
|
||||
|
||||
std::cout << text3 << std::endl;
|
||||
}
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
DemoData loadDemoDataFromFile(const std::string &file_path)
|
||||
{
|
||||
DemoData dd;
|
||||
|
||||
// Indicador de éxito en la carga
|
||||
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "Error: Fichero no encontrado " << file_path << std::endl;
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
/*{ // El fichero no existe
|
||||
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
|
||||
|
||||
// Creamos el fichero para escritura
|
||||
file = SDL_RWFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
// Si ha creado el fichero
|
||||
if (file)
|
||||
{
|
||||
std::cout << "New file (" << file_name.c_str() << ") created!" << std::endl;
|
||||
|
||||
// Inicializas los datos y los guarda en el fichero
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
DemoKeys dk = DemoKeys();
|
||||
dd.push_back(dk);
|
||||
SDL_RWwrite(file, &dk, sizeof(DemoKeys), 1);
|
||||
}
|
||||
|
||||
// Cerramos el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{ // Si no puede crear el fichero
|
||||
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
|
||||
}
|
||||
}*/
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Mensaje de proceder a la carga de los datos
|
||||
// std::cout << "Reading file: " << file_name.c_str() << std::endl;
|
||||
|
||||
// Lee todos los datos del fichero y los deja en el destino
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
|
||||
{
|
||||
DemoKeys dk = DemoKeys();
|
||||
SDL_RWread(file, &dk, sizeof(DemoKeys), 1);
|
||||
dd.push_back(dk);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
|
||||
// Mensaje de datos cargados a la carga de los datos
|
||||
printWithDots("DemoData : ", file_name, "[ LOADED ]");
|
||||
}
|
||||
|
||||
return dd;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool saveDemoFile(const std::string &file_path, const DemoData &dd)
|
||||
{
|
||||
auto success = true;
|
||||
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
if (file)
|
||||
{
|
||||
// Guarda los datos
|
||||
for (const auto &data : dd)
|
||||
{
|
||||
if (SDL_RWwrite(file, &data, sizeof(DemoKeys), 1) != 1)
|
||||
{
|
||||
std::cerr << "Error al escribir el fichero " << file_name << std::endl;
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (success)
|
||||
{
|
||||
std::cout << "Writing file " << file_name.c_str() << std::endl;
|
||||
}
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
#endif // RECORDING
|
||||
@@ -17,6 +17,10 @@ namespace lang
|
||||
struct JA_Music_t; // lines 12-12
|
||||
struct JA_Sound_t; // lines 13-13
|
||||
|
||||
// Constantes
|
||||
constexpr int BLOCK = 8;
|
||||
constexpr int TOTAL_DEMO_DATA = 2000;
|
||||
|
||||
// Dificultad del juego
|
||||
enum class GameDifficulty
|
||||
{
|
||||
@@ -25,9 +29,6 @@ enum class GameDifficulty
|
||||
HARD = 2,
|
||||
};
|
||||
|
||||
// Tamaño de bloque
|
||||
constexpr int BLOCK = 8;
|
||||
|
||||
// Estructura para definir un circulo
|
||||
struct Circle
|
||||
{
|
||||
@@ -72,6 +73,17 @@ struct DemoKeys
|
||||
: left(l), right(r), no_input(ni), fire(f), fire_left(fl), fire_right(fr) {}
|
||||
};
|
||||
|
||||
using DemoData = std::vector<DemoKeys>;
|
||||
|
||||
struct Demo
|
||||
{
|
||||
bool enabled; // Indica si está activo el modo demo
|
||||
bool recording; // Indica si está activado el modo para grabar la demo
|
||||
int counter; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
};
|
||||
|
||||
// Estructura para las opciones de la ventana
|
||||
struct OptionsWindow
|
||||
{
|
||||
@@ -269,6 +281,14 @@ bool stringInVector(const std::vector<std::string> &vec, const std::string &str)
|
||||
// Imprime por pantalla una linea de texto de tamaño fijo rellena con puntos
|
||||
void printWithDots(const std::string &text1, const std::string &text2, const std::string &text3);
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
DemoData loadDemoDataFromFile(const std::string &file_path);
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool saveDemoFile(const std::string &file_path, const DemoData &dd);
|
||||
#endif
|
||||
|
||||
// Colores
|
||||
extern const Color bg_color;
|
||||
extern const Color no_color;
|
||||
|
||||
Reference in New Issue
Block a user