Redistribuits els .cpp en carpetes
Actualitzat cmake Modificats els include de SDL2 a SDL3
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
#include "options.h"
|
||||
#include <SDL2/SDL_video.h> // Para SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||
#include <algorithm> // Para find_if
|
||||
#include <cctype> // Para isspace
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
|
||||
#include <functional> // Para function
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <sstream> // Para basic_istringstream
|
||||
#include <string> // Para char_traits, string, operator<<, hash
|
||||
#include <unordered_map> // Para unordered_map, operator==, _Node_const_i...
|
||||
#include <utility> // Para pair
|
||||
|
||||
#include <SDL3/SDL_video.h> // Para SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||
|
||||
#include <algorithm> // Para find_if
|
||||
#include <cctype> // Para isspace
|
||||
#include <fstream> // Para basic_ostream, operator<<, basic_ofstream
|
||||
#include <functional> // Para function
|
||||
#include <iostream> // Para cout, cerr
|
||||
#include <sstream> // Para basic_istringstream
|
||||
#include <string> // Para char_traits, string, operator<<, hash
|
||||
#include <unordered_map> // Para unordered_map, operator==, _Node_const_i...
|
||||
#include <utility> // Para pair
|
||||
|
||||
// Variables
|
||||
Options options;
|
||||
|
||||
bool setOptions(const std::string &var, const std::string &value);
|
||||
bool setOptions(const std::string& var, const std::string& value);
|
||||
|
||||
// Crea e inicializa las opciones del programa
|
||||
void initOptions()
|
||||
{
|
||||
void initOptions() {
|
||||
options = Options();
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -30,8 +31,7 @@ void initOptions()
|
||||
}
|
||||
|
||||
// Carga las opciones desde un fichero
|
||||
bool loadOptionsFromFile(const std::string &file_path)
|
||||
{
|
||||
bool loadOptionsFromFile(const std::string& file_path) {
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
@@ -43,28 +43,22 @@ bool loadOptionsFromFile(const std::string &file_path)
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// Si el fichero se puede abrir
|
||||
if (file.good())
|
||||
{
|
||||
if (file.good()) {
|
||||
// Procesa el fichero línea a línea
|
||||
if (options.console)
|
||||
{
|
||||
if (options.console) {
|
||||
std::cout << "Reading file config.txt\n";
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
while (std::getline(file, line)) {
|
||||
// Elimina espacios en blanco iniciales y finales
|
||||
line = std::string(std::find_if(line.begin(), line.end(), [](int ch)
|
||||
{ return !std::isspace(ch); }),
|
||||
line.end());
|
||||
line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch)
|
||||
{ return !std::isspace(ch); })
|
||||
line = std::string(std::find_if(line.begin(), line.end(), [](int ch) { return !std::isspace(ch); }),
|
||||
line.end());
|
||||
line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return !std::isspace(ch); })
|
||||
.base(),
|
||||
line.end());
|
||||
line.end());
|
||||
|
||||
// Ignora líneas vacías o comentarios
|
||||
if (line.empty() || line[0] == '#')
|
||||
{
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -72,12 +66,9 @@ bool loadOptionsFromFile(const std::string &file_path)
|
||||
std::istringstream iss(line);
|
||||
std::string key, value;
|
||||
|
||||
if (iss >> key >> value)
|
||||
{
|
||||
if (!setOptions(key, value))
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
if (iss >> key >> value) {
|
||||
if (!setOptions(key, value)) {
|
||||
if (options.console) {
|
||||
std::cout << "Warning: file config.txt\n";
|
||||
std::cout << "unknown parameter " << key << std::endl;
|
||||
}
|
||||
@@ -87,25 +78,20 @@ bool loadOptionsFromFile(const std::string &file_path)
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (options.console)
|
||||
{
|
||||
if (options.console) {
|
||||
std::cout << "Closing file config.txt\n\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Crea el fichero con los valores por defecto
|
||||
saveOptionsToFile(file_path);
|
||||
}
|
||||
|
||||
// Si la versión de fichero no coincide, crea un fichero nuevo con los valores por defecto
|
||||
if (configVersion != options.version)
|
||||
{
|
||||
if (configVersion != options.version) {
|
||||
initOptions();
|
||||
saveOptionsToFile(file_path);
|
||||
if (options.console)
|
||||
{
|
||||
if (options.console) {
|
||||
std::cout << "Wrong config file: initializing options.\n\n";
|
||||
}
|
||||
}
|
||||
@@ -114,23 +100,20 @@ bool loadOptionsFromFile(const std::string &file_path)
|
||||
}
|
||||
|
||||
// Guarda las opciones en un fichero
|
||||
bool saveOptionsToFile(const std::string &file_path)
|
||||
{
|
||||
bool saveOptionsToFile(const std::string& file_path) {
|
||||
// Crea y abre el fichero de texto
|
||||
std::ofstream file(file_path);
|
||||
bool success = file.is_open(); // Verifica si el archivo se abrió correctamente
|
||||
bool success = file.is_open(); // Verifica si el archivo se abrió correctamente
|
||||
|
||||
if (!success) // Si no se pudo abrir el archivo, muestra un mensaje de error y devuelve false
|
||||
if (!success) // Si no se pudo abrir el archivo, muestra un mensaje de error y devuelve false
|
||||
{
|
||||
if (options.console)
|
||||
{
|
||||
if (options.console) {
|
||||
std::cerr << "Error: Unable to open file " << file_path << " for writing." << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (options.console)
|
||||
{
|
||||
if (options.console) {
|
||||
std::cout << file_path << " open for writing" << std::endl;
|
||||
}
|
||||
|
||||
@@ -174,101 +157,68 @@ bool saveOptionsToFile(const std::string &file_path)
|
||||
return success;
|
||||
}
|
||||
|
||||
bool setOptions(const std::string &var, const std::string &value)
|
||||
{
|
||||
static const std::unordered_map<std::string, std::function<void(const std::string &)>> optionHandlers = {
|
||||
{"version", [](const std::string &v)
|
||||
{ options.version = v; }},
|
||||
{"keys", [](const std::string &v)
|
||||
{
|
||||
bool setOptions(const std::string& var, const std::string& value) {
|
||||
static const std::unordered_map<std::string, std::function<void(const std::string&)>> optionHandlers = {
|
||||
{"version", [](const std::string& v) { options.version = v; }},
|
||||
{"keys", [](const std::string& v) {
|
||||
int val = safeStoi(v, static_cast<int>(DEFAULT_CONTROL_SCHEME));
|
||||
if (val == static_cast<int>(ControlScheme::CURSOR) || val == static_cast<int>(ControlScheme::OPQA) || val == static_cast<int>(ControlScheme::WASD))
|
||||
{
|
||||
if (val == static_cast<int>(ControlScheme::CURSOR) || val == static_cast<int>(ControlScheme::OPQA) || val == static_cast<int>(ControlScheme::WASD)) {
|
||||
options.keys = static_cast<ControlScheme>(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.keys = DEFAULT_CONTROL_SCHEME;
|
||||
}
|
||||
}},
|
||||
{"window.zoom", [](const std::string &v)
|
||||
{
|
||||
{"window.zoom", [](const std::string& v) {
|
||||
int val = safeStoi(v, DEFAULT_WINDOW_ZOOM);
|
||||
if (val > 0)
|
||||
{
|
||||
if (val > 0) {
|
||||
options.window.zoom = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.window.zoom = DEFAULT_WINDOW_ZOOM;
|
||||
}
|
||||
}},
|
||||
{"video.mode", [](const std::string &v)
|
||||
{
|
||||
{"video.mode", [](const std::string& v) {
|
||||
int val = safeStoi(v, 0);
|
||||
if (val == 0 || val == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
if (val == 0 || val == SDL_WINDOW_FULLSCREEN_DESKTOP) {
|
||||
options.video.mode = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.video.mode = 0;
|
||||
}
|
||||
}},
|
||||
{"video.filter", [](const std::string &v)
|
||||
{
|
||||
{"video.filter", [](const std::string& v) {
|
||||
int val = safeStoi(v, static_cast<int>(DEFAULT_VIDEO_FILTER));
|
||||
if (val == static_cast<int>(ScreenFilter::NEAREST) || val == static_cast<int>(ScreenFilter::LINEAR))
|
||||
{
|
||||
if (val == static_cast<int>(ScreenFilter::NEAREST) || val == static_cast<int>(ScreenFilter::LINEAR)) {
|
||||
options.video.filter = static_cast<ScreenFilter>(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.video.filter = DEFAULT_VIDEO_FILTER;
|
||||
}
|
||||
}},
|
||||
{"video.shaders", [](const std::string &v)
|
||||
{ options.video.shaders = stringToBool(v); }},
|
||||
{"video.vertical_sync", [](const std::string &v)
|
||||
{ options.video.vertical_sync = stringToBool(v); }},
|
||||
{"video.integer_scale", [](const std::string &v)
|
||||
{ options.video.integer_scale = stringToBool(v); }},
|
||||
{"video.keep_aspect", [](const std::string &v)
|
||||
{ options.video.keep_aspect = stringToBool(v); }},
|
||||
{"video.border.enabled", [](const std::string &v)
|
||||
{ options.video.border.enabled = stringToBool(v); }},
|
||||
{"video.border.width", [](const std::string &v)
|
||||
{
|
||||
{"video.shaders", [](const std::string& v) { options.video.shaders = stringToBool(v); }},
|
||||
{"video.vertical_sync", [](const std::string& v) { options.video.vertical_sync = stringToBool(v); }},
|
||||
{"video.integer_scale", [](const std::string& v) { options.video.integer_scale = stringToBool(v); }},
|
||||
{"video.keep_aspect", [](const std::string& v) { options.video.keep_aspect = stringToBool(v); }},
|
||||
{"video.border.enabled", [](const std::string& v) { options.video.border.enabled = stringToBool(v); }},
|
||||
{"video.border.width", [](const std::string& v) {
|
||||
int val = safeStoi(v, DEFAULT_BORDER_WIDTH);
|
||||
if (val > 0)
|
||||
{
|
||||
if (val > 0) {
|
||||
options.video.border.width = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.video.border.width = DEFAULT_BORDER_WIDTH;
|
||||
}
|
||||
}},
|
||||
{"video.border.height", [](const std::string &v)
|
||||
{
|
||||
{"video.border.height", [](const std::string& v) {
|
||||
int val = safeStoi(v, DEFAULT_BORDER_HEIGHT);
|
||||
if (val > 0)
|
||||
{
|
||||
if (val > 0) {
|
||||
options.video.border.height = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
options.video.border.height = DEFAULT_BORDER_HEIGHT;
|
||||
}
|
||||
}},
|
||||
{"video.palette", [](const std::string &v)
|
||||
{
|
||||
{"video.palette", [](const std::string& v) {
|
||||
options.video.palette = v;
|
||||
}}};
|
||||
|
||||
auto it = optionHandlers.find(var);
|
||||
if (it != optionHandlers.end())
|
||||
{
|
||||
if (it != optionHandlers.end()) {
|
||||
it->second(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user