Afegides comprobacions de valors per al fitxer de configuració
This commit is contained in:
@@ -53,8 +53,13 @@ bool loadOptionsFromFile(const std::string &file_path)
|
|||||||
while (std::getline(file, line))
|
while (std::getline(file, line))
|
||||||
{
|
{
|
||||||
// Elimina espacios en blanco iniciales y finales
|
// 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 = std::string(std::find_if(line.begin(), line.end(), [](int ch)
|
||||||
line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return !std::isspace(ch); }).base(), line.end());
|
{ 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
|
// Ignora líneas vacías o comentarios
|
||||||
if (line.empty() || line[0] == '#')
|
if (line.empty() || line[0] == '#')
|
||||||
@@ -107,7 +112,6 @@ bool loadOptionsFromFile(const std::string &file_path)
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Guarda las opciones en un fichero
|
// Guarda las opciones en un fichero
|
||||||
bool saveOptionsToFile(const std::string &file_path)
|
bool saveOptionsToFile(const std::string &file_path)
|
||||||
{
|
{
|
||||||
@@ -169,36 +173,105 @@ bool saveOptionsToFile(const std::string &file_path)
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece las opciones
|
|
||||||
bool setOptions(const std::string &var, const std::string &value)
|
bool setOptions(const std::string &var, const std::string &value)
|
||||||
{
|
{
|
||||||
static const std::unordered_map<std::string, std::function<void(std::string)>> optionHandlers = {
|
static const std::unordered_map<std::string, std::function<void(const std::string &)>> optionHandlers = {
|
||||||
{"version", [](std::string v)
|
{"version", [](const std::string &v)
|
||||||
{ options.version = v; }},
|
{ options.version = v; }},
|
||||||
{"keys", [](std::string v)
|
{"keys", [](const std::string &v)
|
||||||
{ options.keys = static_cast<ControlScheme>(safeStoi(v, static_cast<int>(ControlScheme::CURSOR))); }},
|
{
|
||||||
{"window.zoom", [](std::string v)
|
int val = safeStoi(v, static_cast<int>(DEFAULT_CONTROL_SCHEME));
|
||||||
{ options.window.zoom = safeStoi(v, 1); }},
|
if (val == static_cast<int>(ControlScheme::CURSOR) || val == static_cast<int>(ControlScheme::OPQA) || val == static_cast<int>(ControlScheme::WASD))
|
||||||
{"video.mode", [](std::string v)
|
{
|
||||||
{ options.video.mode = safeStoi(v, 0); }},
|
options.keys = static_cast<ControlScheme>(val);
|
||||||
{"video.filter", [](std::string v)
|
}
|
||||||
{ options.video.filter = static_cast<ScreenFilter>(safeStoi(v, static_cast<int>(DEFAULT_VIDEO_FILTER))); }},
|
else
|
||||||
{"video.shaders", [](std::string v)
|
{
|
||||||
|
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); }},
|
{ options.video.shaders = stringToBool(v); }},
|
||||||
{"video.vertical_sync", [](std::string v)
|
{"video.vertical_sync", [](const std::string &v)
|
||||||
{ options.video.vertical_sync = stringToBool(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); }},
|
{ 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); }},
|
{ 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); }},
|
{ options.video.border.enabled = stringToBool(v); }},
|
||||||
{"video.border.width", [](std::string v)
|
{"video.border.width", [](const std::string &v)
|
||||||
{ options.video.border.width = safeStoi(v, 32); }},
|
{
|
||||||
{"video.border.height", [](std::string v)
|
int val = safeStoi(v, DEFAULT_BORDER_WIDTH);
|
||||||
{ options.video.border.height = safeStoi(v, 24); }},
|
if (val > 0)
|
||||||
{"video.palette", [](std::string v)
|
{
|
||||||
{ options.video.palette = static_cast<Palette>(safeStoi(v, static_cast<int>(DEFAULT_PALETTE))); }}};
|
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);
|
auto it = optionHandlers.find(var);
|
||||||
if (it != optionHandlers.end())
|
if (it != optionHandlers.end())
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
|||||||
: window_(window),
|
: window_(window),
|
||||||
renderer_(renderer)
|
renderer_(renderer)
|
||||||
{
|
{
|
||||||
|
// Ajusta los tamaños
|
||||||
adjustGameCanvasRect();
|
adjustGameCanvasRect();
|
||||||
adjustWindowSize();
|
adjustWindowSize();
|
||||||
|
|
||||||
@@ -343,6 +344,8 @@ void Screen::adjustWindowSize()
|
|||||||
window_width_ = options.game.width + (options.video.border.enabled ? options.video.border.width * 2 : 0);
|
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);
|
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
|
// Establece el nuevo tamaño
|
||||||
if (options.video.mode == 0)
|
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;
|
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_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_
|
// Ajusta game_canvas_rect_
|
||||||
@@ -389,7 +389,12 @@ int Screen::getMaxZoom()
|
|||||||
SDL_GetCurrentDisplayMode(0, &DM);
|
SDL_GetCurrentDisplayMode(0, &DM);
|
||||||
|
|
||||||
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
|
// 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
|
// Renderiza un frame negro
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ enum class ScreenFilter : Uint32
|
|||||||
class Screen
|
class Screen
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
// Constantes
|
||||||
|
static constexpr int WINDOWS_DECORATIONS_ = 35;
|
||||||
|
|
||||||
// [SINGLETON] Objeto privado
|
// [SINGLETON] Objeto privado
|
||||||
static Screen *screen_;
|
static Screen *screen_;
|
||||||
|
|
||||||
@@ -51,9 +54,6 @@ private:
|
|||||||
// Ajusta el tamaño lógico del renderizador
|
// Ajusta el tamaño lógico del renderizador
|
||||||
void adjustRenderLogicalSize();
|
void adjustRenderLogicalSize();
|
||||||
|
|
||||||
// Obtiene el tamaño máximo de zoom posible para la ventana
|
|
||||||
int getMaxZoom();
|
|
||||||
|
|
||||||
// Renderiza un frame negro
|
// Renderiza un frame negro
|
||||||
void renderBlackFrame();
|
void renderBlackFrame();
|
||||||
|
|
||||||
@@ -126,6 +126,9 @@ public:
|
|||||||
// Oculta la ventana
|
// Oculta la ventana
|
||||||
void hide();
|
void hide();
|
||||||
|
|
||||||
|
// Obtiene el tamaño máximo de zoom posible para la ventana
|
||||||
|
int getMaxZoom();
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
SDL_Renderer *getRenderer() { return renderer_; }
|
SDL_Renderer *getRenderer() { return renderer_; }
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user