Los datos se guardan en la carpeta de sistema

This commit is contained in:
2022-12-07 11:23:19 +01:00
parent c87e1e68a9
commit bbe82d329b
7 changed files with 262 additions and 64 deletions

View File

@@ -5,25 +5,38 @@
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#ifndef _WIN32
#include <pwd.h>
#endif
// Constructor
Director::Director(std::string path)
Director::Director(int argc, char *argv[])
{
// Inicializa variables
section.name = PROG_SECTION_LOGO;
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(path);
// Establece la lista de ficheros
if (!setFileList())
{ // Si falta algún fichero no inicia el programa
section.name = PROG_SECTION_QUIT;
}
// Inicializa las opciones del programa
initOptions();
// Comprueba los parametros del programa
checkProgramArguments(argc, argv);
// Crea la carpeta del sistema donde guardar datos
createSystemFolder();
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(executablePath);
asset->setVerbose(options->console);
// Si falta algún fichero no inicia el programa
if (!setFileList())
{
exit(EXIT_FAILURE);
}
// Carga el fichero de configuración
loadConfigFile();
@@ -107,8 +120,11 @@ bool Director::initSDL()
// Inicializa SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
// if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
{
if (options->console)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -118,9 +134,12 @@ bool Director::initSDL()
// Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
{
if (options->console)
{
std::cout << "Warning: Nearest texture filtering not enabled!\n";
}
}
// Crea la ventana
int incW = 0;
@@ -132,8 +151,11 @@ bool Director::initSDL()
}
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (options->gameWidth + incW) * options->windowSize, (options->gameHeight + incH) * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr)
{
if (options->console)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -149,8 +171,11 @@ bool Director::initSDL()
}
if (renderer == nullptr)
{
if (options->console)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -167,7 +192,10 @@ bool Director::initSDL()
}
}
if (options->console)
{
std::cout << std::endl;
}
return success;
}
@@ -181,10 +209,9 @@ bool Director::setFileList()
#endif
// Ficheros de configuración
asset->add(prefix + "/data/config/score.bin", t_data, false);
asset->add(systemFolder + "/config.txt", t_data, false, true);
asset->add(systemFolder + "/score.bin", t_data, false, true);
asset->add(prefix + "/data/config/demo.bin", t_data);
asset->add(prefix + "/data/config/config.txt", t_data, false);
asset->add(prefix + "/data/config/jailer_id.txt", t_data, false);
asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data);
// Musicas
@@ -321,13 +348,11 @@ void Director::initOptions()
inp.deviceType = INPUT_USE_GAMECONTROLLER;
options->input.push_back(inp);
// Video
options->gameWidth = GAMECANVAS_WIDTH;
options->gameHeight = GAMECANVAS_HEIGHT;
options->videoMode = 0;
options->windowSize = 3;
options->language = ba_BA;
options->difficulty = DIFFICULTY_NORMAL;
options->playerSelected = 0;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->integerScale = true;
@@ -336,6 +361,12 @@ void Director::initOptions()
options->borderHeight = 0;
options->borderEnabled = false;
// Varios
options->playerSelected = 0;
options->difficulty = DIFFICULTY_NORMAL;
options->language = ba_BA;
options->console = false;
// Online
options->online.enabled = false;
options->online.server = "";
@@ -345,6 +376,77 @@ void Director::initOptions()
options->online.score = 0;
}
// Comprueba los parametros del programa
void Director::checkProgramArguments(int argc, char *argv[])
{
// Establece la ruta del programa
executablePath = argv[0];
// Comprueba el resto de parametros
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--console") == 0)
{
options->console = true;
}
}
}
// Crea la carpeta del sistema donde guardar datos
void Director::createSystemFolder()
{
#ifdef DEBUG
const std::string folderName = "coffee_crisis_debug";
#else
const std::string folderName = "coffee_crisis";
#endif
#ifdef _WIN32
systemFolder = std::string(getenv("APPDATA")) + "/" + folderName;
#elif __APPLE__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
systemFolder = std::string(homedir) + "/Library/Application Support/" + folderName;
#elif __linux__
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
systemFolder = std::string(homedir) + "/." + folderName;
#endif
struct stat st = {0};
if (stat(systemFolder.c_str(), &st) == -1)
{
errno = 0;
#ifdef _WIN32
int ret = mkdir(systemFolder.c_str());
#else
int ret = mkdir(systemFolder.c_str(), S_IRWXU);
#endif
if (ret == -1)
{
switch (errno)
{
case EACCES:
printf("the parent directory does not allow write");
exit(EXIT_FAILURE);
case EEXIST:
printf("pathname already exists");
exit(EXIT_FAILURE);
case ENAMETOOLONG:
printf("pathname is too long");
exit(EXIT_FAILURE);
default:
perror("mkdir");
exit(EXIT_FAILURE);
}
}
}
}
// Carga el fichero de configuración
bool Director::loadConfigFile()
{
@@ -352,14 +454,18 @@ bool Director::loadConfigFile()
bool success = true;
// Variables para manejar el fichero
const std::string filePath = "config.txt";
std::string line;
std::ifstream file(asset->get("config.txt"));
std::ifstream file(asset->get(filePath));
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero linea a linea
std::cout << "Reading file config.txt\n";
if (options->console)
{
std::cout << "Reading file " << filePath << std::endl;
}
while (std::getline(file, line))
{
// Comprueba que la linea no sea un comentario
@@ -370,15 +476,21 @@ bool Director::loadConfigFile()
// Procesa las dos subcadenas
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
std::cout << "Warning: file config.txt\n";
std::cout << "unknown parameter " << line.substr(0, pos).c_str() << std::endl;
if (options->console)
{
std::cout << "Warning: file " << filePath << std::endl;
std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl;
}
success = false;
}
}
}
// Cierra el fichero
std::cout << "Closing file config.txt\n\n";
if (options->console)
{
std::cout << "Closing file " << filePath << std::endl;
}
file.close();
}
@@ -544,8 +656,11 @@ void Director::initOnline()
if (options->online.jailerID == "")
{ // Jailer ID no definido
screen->showNotification("No ha especificado ningun Jailer ID");
if (options->console)
{
std::cout << "No ha especificado ningun Jailer ID" << std::endl;
}
}
else
{ // Jailer ID iniciado
@@ -556,12 +671,18 @@ void Director::initOnline()
if (jscore::initOnlineScore(options->online.gameID))
{
screen->showNotification(options->online.jailerID + " ha iniciado sesion");
if (options->console)
{
std::cout << options->online.jailerID << " ha iniciado sesion" << std::endl;
}
}
else
{
screen->showNotification("Fallo al conectar a " + options->online.server);
if (options->console)
{
std::cout << "Fallo al conectar a " << options->online.server << std::endl;
}
options->online.enabled = false;
@@ -573,8 +694,11 @@ void Director::initOnline()
if (points == 0)
{ // Fallo de conexión o no hay registros
screen->showNotification("No se ha podido obtener la puntuacion online");
if (options->console)
{
std::cout << "No se ha podido obtener la puntuacion online" << std::endl;
}
}
else
{
options->online.score = points;

View File

@@ -47,6 +47,8 @@ private:
// Variables
struct options_t *options; // Variable con todas las opciones del programa
section_t section; // Sección y subsección actual del programa;
std::string executablePath; // Path del ejecutable
std::string systemFolder; // Carpeta del sistema donde guardar datos
// Inicializa jail_audio
void initJailAudio();
@@ -75,6 +77,12 @@ private:
// Guarda el fichero de configuración
bool saveConfigFile();
// Comprueba los parametros del programa
void checkProgramArguments(int argc, char *argv[]);
// Crea la carpeta del sistema donde guardar datos
void createSystemFolder();
// Establece el valor de la variable
void setSection(section_t section);
@@ -92,7 +100,7 @@ private:
public:
// Constructor
Director(std::string path);
Director(int argc, char *argv[]);
// Destructor
~Director();

View File

@@ -385,8 +385,11 @@ void Game::init()
// Carga los recursos necesarios para la sección 'Game'
void Game::loadMedia()
{
if (options->console)
{
std::cout << std::endl
<< "** LOADING RESOURCES FOR GAME SECTION" << std::endl;
}
// Carga ficheros
loadScoreFile();
@@ -573,8 +576,11 @@ void Game::loadMedia()
// Musicas
gameMusic = JA_LoadMusic(asset->get("playing.ogg").c_str());
if (options->console)
{
std::cout << "** RESOURCES FOR GAME SECTION LOADED" << std::endl
<< std::endl;
}
}
// Carga el fichero de puntos
@@ -588,14 +594,20 @@ bool Game::loadScoreFile()
// 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)
@@ -608,8 +620,11 @@ bool Game::loadScoreFile()
SDL_RWclose(file);
}
else
{
if (options->console)
{
std::cout << "Error: Unable to create file " << filename.c_str() << std::endl;
}
success = false;
}
}
@@ -617,7 +632,10 @@ bool Game::loadScoreFile()
else
{
// Cargamos los datos
if (options->console)
{
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);
@@ -654,14 +672,20 @@ bool Game::loadDemoFile()
// 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_DEMO_DATA; ++i)
@@ -680,8 +704,11 @@ bool Game::loadDemoFile()
SDL_RWclose(file);
}
else
{
if (options->console)
{
std::cout << "Error: Unable to create file " << filename.c_str() << std::endl;
}
success = false;
}
}
@@ -689,7 +716,10 @@ bool Game::loadDemoFile()
else
{
// Cargamos los datos
if (options->console)
{
std::cout << "Reading file " << filename.c_str() << std::endl;
}
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
SDL_RWread(file, &demo.dataFile[i], sizeof(demoKeys_t), 1);
@@ -715,15 +745,21 @@ bool Game::saveScoreFile()
SDL_RWwrite(file, &scoreDataFile[i], sizeof(Uint32), 1);
}
if (options->console)
{
std::cout << "Writing file " << filename.c_str() << std::endl;
}
// Cerramos 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;
}
@@ -771,16 +807,22 @@ bool Game::saveDemoFile()
SDL_RWwrite(file, &demo.dataFile[i], sizeof(demoKeys_t), 1);
}
if (options->console)
{
std::cout << "Writing file " << filename.c_str() << std::endl;
}
// Cerramos 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;
}
@@ -3850,8 +3892,11 @@ void Game::loadAnimations(std::string filePath, std::vector<std::string> *buffer
std::string line;
if (file)
{
if (options->console)
{
std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl;
}
while (std::getline(file, line))
{
buffer->push_back(line);

View File

@@ -21,9 +21,12 @@ HiScoreTable::HiScoreTable(SDL_Renderer *renderer, Screen *screen, Asset *asset,
// Crea un backbuffer para el renderizador
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (backbuffer == nullptr)
{
if (options->console)
{
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
// Inicializa variables
section.name = SELF;

View File

@@ -42,12 +42,12 @@ Reescribiendo el código el 27/09/2022
#include "director.h"
#include <stdio.h>
int main(int argc, char *args[])
int main(int argc, char *argv[])
{
std::cout << "Starting the game...\n\n";
std::cout << "Starting the game..." << std::endl;
// Crea el objeto Director
Director *director = new Director(args[0]);
Director *director = new Director(argc, argv);
// Bucle principal
director->run();

View File

@@ -1025,8 +1025,11 @@ bool Title::updatePlayerInputs(int numPlayer)
}
else
{ // Si hay mas de un dispositivo, se recorre el vector
if (options->console)
{
std::cout << "numplayer:" << numPlayer << std::endl;
std::cout << "deviceindex:" << deviceIndex[numPlayer] << std::endl;
}
// Incrementa el indice
if (deviceIndex[numPlayer] < numDevices - 1)
@@ -1037,7 +1040,10 @@ bool Title::updatePlayerInputs(int numPlayer)
{
deviceIndex[numPlayer] = 0;
}
if (options->console)
{
std::cout << "deviceindex:" << deviceIndex[numPlayer] << std::endl;
}
// Si coincide con el del otro jugador, se lo intercambian
if (deviceIndex[0] == deviceIndex[1])
@@ -1064,9 +1070,12 @@ void Title::createTiledBackground()
// Crea la textura para el mosaico de fondo
background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH * 2, GAMECANVAS_HEIGHT * 2);
if (background == nullptr)
{
if (options->console)
{
std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
}
// Crea los objetos para pintar en la textura de fondo
Texture *bgTileTexture = new Texture(renderer, asset->get("title_bg_tile.png"));
@@ -1101,7 +1110,10 @@ void Title::createTiledBackground()
// Comprueba cuantos mandos hay conectados para gestionar el menu de opciones
void Title::checkInputDevices()
{
if (options->console)
{
std::cout << "Filling devices for options menu..." << std::endl;
}
input->discoverGameController();
const int numControllers = input->getNumControllers();
availableInputDevices.clear();
@@ -1115,16 +1127,22 @@ void Title::checkInputDevices()
temp.name = input->getControllerName(i);
temp.deviceType = INPUT_USE_GAMECONTROLLER;
availableInputDevices.push_back(temp);
if (options->console)
{
std::cout << "Device " << (int)availableInputDevices.size() << " - " << temp.name.c_str() << std::endl;
}
}
// Añade el teclado al final
temp.id = -1;
temp.name = "KEYBOARD";
temp.deviceType = INPUT_USE_KEYBOARD;
availableInputDevices.push_back(temp);
if (options->console)
{
std::cout << "Device " << (int)availableInputDevices.size() << " - " << temp.name.c_str() << std::endl;
std::cout << std::endl;
}
}
// Recarga las texturas