forked from jaildesigner-jailgames/jaildoctors_dilemma
Trabajando en el fichero de configuración
This commit is contained in:
9
data/config.txt
Normal file
9
data/config.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
### NO TOCAR SI NO SE SABE LO QUE SE ESTÁ HACIENDO
|
||||
fullScreenMode=0
|
||||
windowSize=1
|
||||
filter=FILTER_NEAREST
|
||||
vSync=true
|
||||
integerScale=true
|
||||
keepAspect=true
|
||||
borderEnabled=true
|
||||
borderSize=0.1f
|
||||
@@ -21,16 +21,8 @@ Director::Director(std::string path)
|
||||
section.subsection = 0;
|
||||
}
|
||||
|
||||
// Crea el puntero a la estructura y carga el fichero de configuración
|
||||
options = new options_t;
|
||||
options->fullScreenMode = 0;
|
||||
options->windowSize = 3;
|
||||
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;
|
||||
// Inicializa variables desde el fichero de configuración
|
||||
loadConfig();
|
||||
|
||||
// Inicializa SDL
|
||||
initSDL();
|
||||
@@ -59,6 +51,229 @@ Director::~Director()
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Carga el fichero de configuración
|
||||
bool Director::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->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
|
||||
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*const std::string p = asset->get("config.txt");
|
||||
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r");
|
||||
|
||||
// El fichero no existe
|
||||
if (file == NULL)
|
||||
{
|
||||
printf("Warning: Unable to open %s file\n", filename.c_str());
|
||||
|
||||
// Crea el fichero para escritura
|
||||
file = SDL_RWFromFile(p.c_str(), "w");
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("New file (%s) created!\n", filename.c_str());
|
||||
|
||||
// Escribe los datos
|
||||
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
SDL_RWwrite(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
|
||||
SDL_RWwrite(file, &options->borderSize, sizeof(options->borderSize), 1);
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error: Unable to create file %s\n", filename.c_str());
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
// El fichero existe
|
||||
else
|
||||
{
|
||||
// Carga los datos
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
SDL_RWread(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWread(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWread(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWread(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWread(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWread(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWread(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
SDL_RWread(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
|
||||
SDL_RWread(file, &options->borderSize, sizeof(options->borderSize), 1);
|
||||
|
||||
// Normaliza los valores
|
||||
const bool a = options->fullScreenMode == 0;
|
||||
const bool b = options->fullScreenMode == SDL_WINDOW_FULLSCREEN;
|
||||
const bool c = options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
if (!(a || b || c))
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
|
||||
if ((options->windowSize < 1) || (options->windowSize > 4))
|
||||
{
|
||||
options->windowSize = 3;
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}*/
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Guarda el fichero de configuración
|
||||
bool Director::saveConfig()
|
||||
{
|
||||
bool success = true;
|
||||
const std::string p = asset->get("config.txt");
|
||||
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
// Guarda los datos
|
||||
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
|
||||
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
|
||||
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
|
||||
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
|
||||
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
|
||||
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
|
||||
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
|
||||
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
|
||||
SDL_RWwrite(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
|
||||
SDL_RWwrite(file, &options->borderSize, sizeof(options->borderSize), 1);
|
||||
|
||||
printf("Writing file %s\n", filename.c_str());
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool Director::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 == "0")
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
else if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "windowSize")
|
||||
{
|
||||
options->windowSize = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "filter")
|
||||
{
|
||||
if (value == "FILTER_NEAREST")
|
||||
{
|
||||
options->filter = FILTER_NEAREST;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->filter = FILTER_LINEAL;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
else if (var == "")
|
||||
{
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Inicia las variables necesarias para arrancar el programa
|
||||
void Director::initInput()
|
||||
{
|
||||
@@ -160,6 +375,7 @@ bool Director::setFileList()
|
||||
asset->add("/media/font/debug.txt", font);
|
||||
|
||||
asset->add("/data/gamecontrollerdb.txt", data);
|
||||
asset->add("/data/config.txt", data, false);
|
||||
|
||||
asset->add("/data/room/01.room", room);
|
||||
asset->add("/data/room/02.room", room);
|
||||
|
||||
@@ -37,6 +37,15 @@ private:
|
||||
std::string executablePath; // Path del ejecutable
|
||||
section_t section; // Sección y subsección actual 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();
|
||||
|
||||
|
||||
@@ -65,14 +65,14 @@ Room::~Room()
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero
|
||||
bool Room::load(std::string _file_path)
|
||||
bool Room::load(std::string file_path)
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
std::string filename = _file_path.substr(_file_path.find_last_of("\\/") + 1);
|
||||
std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
||||
std::string line;
|
||||
std::ifstream file(_file_path);
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good())
|
||||
@@ -275,6 +275,7 @@ bool Room::setVars(std::string var, std::string value)
|
||||
}
|
||||
|
||||
return success;
|
||||
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura enemy_t
|
||||
|
||||
@@ -15,9 +15,8 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, i
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
borderColor = {0x00, 0x00, 0x00};
|
||||
borderEnabled = true;
|
||||
borderWidth = 0.1f;
|
||||
borderHeight = 0.1f;
|
||||
borderEnabled = false;
|
||||
borderSize = 0.1f;
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
|
||||
@@ -83,8 +82,8 @@ void Screen::setVideoMode(int fullScreenMode)
|
||||
{
|
||||
if (borderEnabled)
|
||||
{
|
||||
const int incWidth = gameCanvasWidth * borderWidth;
|
||||
const int incHeight = gameCanvasHeight * borderHeight;
|
||||
const int incWidth = gameCanvasWidth * borderSize;
|
||||
const int incHeight = gameCanvasHeight * borderSize;
|
||||
screenWidth = gameCanvasWidth + incWidth;
|
||||
screenHeight = gameCanvasHeight + incHeight;
|
||||
dest = {0 + (incWidth / 2), 0 + (incHeight / 2), gameCanvasWidth, gameCanvasHeight};
|
||||
@@ -189,10 +188,9 @@ void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderSize(float w, float h)
|
||||
void Screen::setBorderSize(float s)
|
||||
{
|
||||
borderWidth = w;
|
||||
borderHeight = h;
|
||||
borderSize = s;
|
||||
}
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
|
||||
@@ -33,8 +33,7 @@ private:
|
||||
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
|
||||
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
|
||||
float borderWidth; // Porcentaje de borde que se añade a lo ancho
|
||||
float borderHeight; // Porcentaje de borde que se añade a lo alto
|
||||
float borderSize; // Porcentaje de borde que se añade a lo ventana
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -68,7 +67,7 @@ public:
|
||||
void setBlendMode(SDL_BlendMode blendMode);
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void setBorderSize(float w, float h);
|
||||
void setBorderSize(float s);
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void setBorderEnabled(bool value);
|
||||
|
||||
@@ -530,3 +530,16 @@ color_t stringToColor(std::string str)
|
||||
|
||||
return {0x00, 0x00, 0x00};
|
||||
}
|
||||
|
||||
// Devuelve un color_t a partir de un string
|
||||
bool stringToBool(std::string str)
|
||||
{
|
||||
if (str == "true")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,8 @@ struct options_t
|
||||
int screenHeight; // Alto de la pantalla o ventana
|
||||
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
|
||||
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
|
||||
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
|
||||
float borderSize; // Porcentaje de borde que se añade a lo ventana
|
||||
};
|
||||
|
||||
// Calcula el cuadrado de la distancia entre dos puntos
|
||||
@@ -128,4 +130,7 @@ void normalizeLine(d_line_t &l);
|
||||
// Devuelve un color_t a partir de un string
|
||||
color_t stringToColor(std::string str);
|
||||
|
||||
// Devuelve un color_t a partir de un string
|
||||
bool stringToBool(std::string str);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user