Ya guarda el fichero de configuración
This commit is contained in:
291
source/prog.cpp
291
source/prog.cpp
@@ -1,25 +1,12 @@
|
||||
#include "prog.h"
|
||||
|
||||
// Constructor
|
||||
Prog::Prog(std::string executablePath)
|
||||
Prog::Prog(std::string path)
|
||||
{
|
||||
// Establece las opciones por defecto
|
||||
options = new options_t;
|
||||
options->fullScreenMode = 0;
|
||||
options->windowSize = 2;
|
||||
options->filter = FILTER_NEAREST;
|
||||
options->vSync = true;
|
||||
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
|
||||
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
|
||||
options->integerScale = true;
|
||||
options->keepAspect = true;
|
||||
// Crea el objeto que controla los ficheros de recursos
|
||||
asset = new Asset(path.substr(0, path.find_last_of("\\/")));
|
||||
|
||||
// Inicia las librerias
|
||||
initSDL();
|
||||
initJailAudio();
|
||||
|
||||
// Crea los objetos
|
||||
asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/")));
|
||||
// Si falta algún fichero no inicia el programa
|
||||
if (!setFileList())
|
||||
{
|
||||
section.name = SECTION_PROG_QUIT;
|
||||
@@ -27,37 +14,31 @@ Prog::Prog(std::string executablePath)
|
||||
else
|
||||
{
|
||||
section.name = SECTION_PROG_GAME;
|
||||
section.subsection = 0;
|
||||
}
|
||||
|
||||
// Inicializa variables desde el fichero de configuración
|
||||
loadConfig();
|
||||
|
||||
// Inicializa SDL
|
||||
initSDL();
|
||||
|
||||
// Inicializa JailAudio
|
||||
initJailAudio();
|
||||
|
||||
// Crea los objetos
|
||||
input = new Input(asset->get("gamecontrollerdb.txt"));
|
||||
initInput();
|
||||
screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
screen->setBorderColor(borderColor);
|
||||
|
||||
// Controles
|
||||
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
|
||||
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
|
||||
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
|
||||
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
|
||||
input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
|
||||
input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
|
||||
input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
|
||||
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
|
||||
input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_R);
|
||||
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
|
||||
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);
|
||||
|
||||
input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||||
input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||||
input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||||
input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||||
input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
|
||||
input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE);
|
||||
screen->setVideoMode(options->fullScreenMode);
|
||||
}
|
||||
|
||||
Prog::~Prog()
|
||||
{
|
||||
// Guarda las opciones de configuración
|
||||
saveConfig();
|
||||
|
||||
delete options;
|
||||
delete asset;
|
||||
delete input;
|
||||
@@ -68,6 +49,207 @@ Prog::~Prog()
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Carga el fichero de configuración
|
||||
bool Prog::loadConfig()
|
||||
{
|
||||
// Crea el puntero a la estructura de opciones e inicializa valores
|
||||
options = new options_t;
|
||||
options->fullScreenMode = 0;
|
||||
options->windowSize = 3;
|
||||
options->filter = FILTER_NEAREST;
|
||||
options->vSync = true;
|
||||
options->integerScale = true;
|
||||
options->keepAspect = true;
|
||||
options->borderEnabled = false;
|
||||
options->borderSize = 0.1f;
|
||||
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
// Variables para manejar el fichero
|
||||
std::string line;
|
||||
std::ifstream file(asset->get("config.txt"));
|
||||
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
// Procesa el fichero linea a linea
|
||||
printf("Reading file config.txt\n");
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Comprueba que la linea no sea un comentario
|
||||
if (line.substr(0, 1) != "#")
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", "config.txt", line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
printf("Closing file config.txt\n\n");
|
||||
file.close();
|
||||
}
|
||||
|
||||
// El fichero no existe
|
||||
else
|
||||
{ // Crea el fichero con los valores por defecto
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
// Aplica opciones
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
const int incWidth = GAMECANVAS_WIDTH * options->borderSize;
|
||||
const int incHeight = GAMECANVAS_HEIGHT * options->borderSize;
|
||||
options->screenWidth = (GAMECANVAS_WIDTH + incWidth) * options->windowSize;
|
||||
options->screenHeight = (GAMECANVAS_HEIGHT + incHeight) * options->windowSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
|
||||
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Guarda el fichero de configuración
|
||||
bool Prog::saveConfig()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
// Crea y abre el fichero de texto
|
||||
std::ofstream file(asset->get("config.txt"));
|
||||
|
||||
// Escribe en el fichero
|
||||
if (options->fullScreenMode == 0)
|
||||
{
|
||||
file << "fullScreenMode=0\n";
|
||||
}
|
||||
|
||||
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n";
|
||||
}
|
||||
|
||||
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
|
||||
}
|
||||
|
||||
file << "windowSize=" + std::to_string(options->windowSize) + "\n";
|
||||
|
||||
if (options->filter == FILTER_NEAREST)
|
||||
{
|
||||
file << "filter=FILTER_NEAREST\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
file << "filter=FILTER_LINEAL\n";
|
||||
}
|
||||
|
||||
file << "vSync=" + boolToString(options->vSync) + "\n";
|
||||
file << "integerScale=" + boolToString(options->integerScale) + "\n";
|
||||
file << "keepAspect=" + boolToString(options->keepAspect) + "\n";
|
||||
file << "borderEnabled=" + boolToString(options->borderEnabled) + "\n";
|
||||
file << "borderSize=" + std::to_string(options->borderSize) + "\n";
|
||||
|
||||
// Cierra el fichero
|
||||
file.close();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Prog::setOptions(options_t *options, std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "fullScreenMode")
|
||||
{
|
||||
if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
else if (value == "SDL_WINDOW_FULLSCREEN")
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "windowSize")
|
||||
{
|
||||
options->windowSize = std::stoi(value);
|
||||
if ((options->windowSize < 1) || (options->windowSize > 4))
|
||||
{
|
||||
options->windowSize = 3;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "filter")
|
||||
{
|
||||
if (value == "FILTER_LINEAL")
|
||||
{
|
||||
options->filter = FILTER_LINEAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->filter = FILTER_NEAREST;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "vSync")
|
||||
{
|
||||
options->vSync = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "integerScale")
|
||||
{
|
||||
options->integerScale = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "keepAspect")
|
||||
{
|
||||
options->keepAspect = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderEnabled")
|
||||
{
|
||||
options->borderEnabled = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "borderSize")
|
||||
{
|
||||
options->borderSize = std::stof(value);
|
||||
if (options->borderSize < 0.0f || options->borderSize > 0.5f)
|
||||
{
|
||||
options->borderSize = 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Inicializa JailAudio
|
||||
void Prog::initJailAudio()
|
||||
{
|
||||
@@ -135,6 +317,33 @@ bool Prog::initSDL()
|
||||
return success;
|
||||
}
|
||||
|
||||
// Inicia las variables necesarias para arrancar el programa
|
||||
void Prog::initInput()
|
||||
{
|
||||
// Inicializa los controles
|
||||
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
|
||||
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
|
||||
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
|
||||
input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
|
||||
input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
|
||||
input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
|
||||
input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
|
||||
input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
|
||||
input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_R);
|
||||
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
|
||||
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);
|
||||
|
||||
input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||||
input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||||
input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||||
input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||||
input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
|
||||
input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE);
|
||||
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE);
|
||||
}
|
||||
|
||||
// Crea el indice de ficheros de recursos
|
||||
bool Prog::setFileList()
|
||||
{
|
||||
@@ -165,7 +374,7 @@ bool Prog::setFileList()
|
||||
asset->add("/data/map/surface.png", bitmap);
|
||||
|
||||
// Ficheros de configuración
|
||||
asset->add("/data/config.bin", data, false);
|
||||
asset->add("/data/config/config.txt", data, false);
|
||||
asset->add("/data/input/gamecontrollerdb.txt", data);
|
||||
|
||||
// Ficheros del jugador
|
||||
|
||||
@@ -32,12 +32,24 @@ private:
|
||||
section_t section; // Sección y subsección actual del programa;
|
||||
struct options_t *options; // Contiene las opciones del programa
|
||||
|
||||
// Carga el fichero de configuración
|
||||
bool loadConfig();
|
||||
|
||||
// Guarda el fichero de configuración
|
||||
bool saveConfig();
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setOptions(options_t *options, std::string var, std::string value);
|
||||
|
||||
// Inicializa jail_audio
|
||||
void initJailAudio();
|
||||
|
||||
// Arranca SDL y crea la ventana
|
||||
bool initSDL();
|
||||
|
||||
// Inicializa el objeto Input
|
||||
void initInput();
|
||||
|
||||
// Crea el indice de ficheros
|
||||
bool setFileList();
|
||||
|
||||
@@ -64,7 +76,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Prog(std::string executablePath);
|
||||
Prog(std::string path);
|
||||
|
||||
// Destructor
|
||||
~Prog();
|
||||
|
||||
Reference in New Issue
Block a user