Afegides comprobacions de valors per al fitxer de configuració

This commit is contained in:
2025-02-28 11:34:51 +01:00
parent 366fe404ca
commit 3992fc08bf
3 changed files with 115 additions and 34 deletions

View File

@@ -53,8 +53,13 @@ bool loadOptionsFromFile(const std::string &file_path)
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); }).base(), line.end());
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());
// Ignora líneas vacías o comentarios
if (line.empty() || line[0] == '#')
@@ -107,7 +112,6 @@ bool loadOptionsFromFile(const std::string &file_path)
return success;
}
// Guarda las opciones en un fichero
bool saveOptionsToFile(const std::string &file_path)
{
@@ -169,36 +173,105 @@ bool saveOptionsToFile(const std::string &file_path)
return success;
}
// Establece las opciones
bool setOptions(const std::string &var, const std::string &value)
{
static const std::unordered_map<std::string, std::function<void(std::string)>> optionHandlers = {
{"version", [](std::string v)
static const std::unordered_map<std::string, std::function<void(const std::string &)>> optionHandlers = {
{"version", [](const std::string &v)
{ options.version = v; }},
{"keys", [](std::string v)
{ options.keys = static_cast<ControlScheme>(safeStoi(v, static_cast<int>(ControlScheme::CURSOR))); }},
{"window.zoom", [](std::string v)
{ options.window.zoom = safeStoi(v, 1); }},
{"video.mode", [](std::string v)
{ options.video.mode = safeStoi(v, 0); }},
{"video.filter", [](std::string v)
{ options.video.filter = static_cast<ScreenFilter>(safeStoi(v, static_cast<int>(DEFAULT_VIDEO_FILTER))); }},
{"video.shaders", [](std::string 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))
{
options.keys = static_cast<ControlScheme>(val);
}
else
{
options.keys = DEFAULT_CONTROL_SCHEME;
}
}},
{"window.zoom", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_WINDOW_ZOOM);
if (val > 0)
{
options.window.zoom = val;
}
else
{
options.window.zoom = DEFAULT_WINDOW_ZOOM;
}
}},
{"video.mode", [](const std::string &v)
{
int val = safeStoi(v, 0);
if (val == 0 || val == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
options.video.mode = val;
}
else
{
options.video.mode = 0;
}
}},
{"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))
{
options.video.filter = static_cast<ScreenFilter>(val);
}
else
{
options.video.filter = DEFAULT_VIDEO_FILTER;
}
}},
{"video.shaders", [](const std::string &v)
{ options.video.shaders = stringToBool(v); }},
{"video.vertical_sync", [](std::string v)
{"video.vertical_sync", [](const std::string &v)
{ options.video.vertical_sync = stringToBool(v); }},
{"video.integer_scale", [](std::string v)
{"video.integer_scale", [](const std::string &v)
{ options.video.integer_scale = stringToBool(v); }},
{"video.keep_aspect", [](std::string v)
{"video.keep_aspect", [](const std::string &v)
{ options.video.keep_aspect = stringToBool(v); }},
{"video.border.enabled", [](std::string v)
{"video.border.enabled", [](const std::string &v)
{ options.video.border.enabled = stringToBool(v); }},
{"video.border.width", [](std::string v)
{ options.video.border.width = safeStoi(v, 32); }},
{"video.border.height", [](std::string v)
{ options.video.border.height = safeStoi(v, 24); }},
{"video.palette", [](std::string v)
{ options.video.palette = static_cast<Palette>(safeStoi(v, static_cast<int>(DEFAULT_PALETTE))); }}};
{"video.border.width", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_BORDER_WIDTH);
if (val > 0)
{
options.video.border.width = val;
}
else
{
options.video.border.width = DEFAULT_BORDER_WIDTH;
}
}},
{"video.border.height", [](const std::string &v)
{
int val = safeStoi(v, DEFAULT_BORDER_HEIGHT);
if (val > 0)
{
options.video.border.height = val;
}
else
{
options.video.border.height = DEFAULT_BORDER_HEIGHT;
}
}},
{"video.palette", [](const std::string &v)
{
int val = safeStoi(v, static_cast<int>(DEFAULT_PALETTE));
if (val == static_cast<int>(Palette::ZXSPECTRUM) || val == static_cast<int>(Palette::ZXARNE))
{
options.video.palette = static_cast<Palette>(val);
}
else
{
options.video.palette = DEFAULT_PALETTE;
}
}}};
auto it = optionHandlers.find(var);
if (it != optionHandlers.end())

View File

@@ -40,6 +40,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
: window_(window),
renderer_(renderer)
{
// Ajusta los tamaños
adjustGameCanvasRect();
adjustWindowSize();
@@ -343,6 +344,8 @@ void Screen::adjustWindowSize()
window_width_ = options.game.width + (options.video.border.enabled ? options.video.border.width * 2 : 0);
window_height_ = options.game.height + (options.video.border.enabled ? options.video.border.height * 2 : 0);
options.window.max_zoom = getMaxZoom();
// Establece el nuevo tamaño
if (options.video.mode == 0)
{
@@ -356,11 +359,8 @@ void Screen::adjustWindowSize()
int new_pos_y = old_pos_y + (old_height - (window_height_ * options.window.zoom)) / 2;
SDL_SetWindowSize(window_, window_width_ * options.window.zoom, window_height_ * options.window.zoom);
SDL_SetWindowPosition(window_, std::max(new_pos_x, 30), std::max(new_pos_y, 10));
SDL_SetWindowPosition(window_, std::max(new_pos_x, WINDOWS_DECORATIONS_), std::max(new_pos_y, 0));
}
options.window.max_zoom = getMaxZoom();
renderBlackFrame();
}
// Ajusta game_canvas_rect_
@@ -389,7 +389,12 @@ int Screen::getMaxZoom()
SDL_GetCurrentDisplayMode(0, &DM);
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
return std::min(DM.w / window_width_, DM.h / window_height_);
const int max_zoom = std::min(DM.w / window_width_, (DM.h - WINDOWS_DECORATIONS_) / window_height_);
// Normaliza los valores de zoom
options.window.zoom = std::min(options.window.zoom, max_zoom);
return max_zoom;
}
// Renderiza un frame negro

View File

@@ -18,6 +18,9 @@ enum class ScreenFilter : Uint32
class Screen
{
private:
// Constantes
static constexpr int WINDOWS_DECORATIONS_ = 35;
// [SINGLETON] Objeto privado
static Screen *screen_;
@@ -51,9 +54,6 @@ private:
// Ajusta el tamaño lógico del renderizador
void adjustRenderLogicalSize();
// Obtiene el tamaño máximo de zoom posible para la ventana
int getMaxZoom();
// Renderiza un frame negro
void renderBlackFrame();
@@ -126,6 +126,9 @@ public:
// Oculta la ventana
void hide();
// Obtiene el tamaño máximo de zoom posible para la ventana
int getMaxZoom();
// Getters
SDL_Renderer *getRenderer() { return renderer_; }
};