Passant std::cout a SDL_Log
This commit is contained in:
364
source/param.cpp
364
source/param.cpp
@@ -1,18 +1,88 @@
|
||||
#include "param.h"
|
||||
#include <fstream> // Para char_traits, basic_ostream, basic_ifstream, basi...
|
||||
#include <iostream> // Para cout
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include "utils.h" // Para Param, ParamGame, Zone, ParamBalloon
|
||||
#include <SDL3/SDL_log.h> // Para SDL_LogCategory, SDL_LogError, SDL_LogInfo
|
||||
#include <fstream> // Para basic_istream, basic_ifstream, ifstream
|
||||
#include <sstream> // Para basic_istringstream
|
||||
#include <stdexcept> // Para runtime_error
|
||||
#include <string> // Para operator==, stoi, char_traits, string, ope...
|
||||
#include "utils.h" // Para Zone, Color, NotifyPosition, getFileName
|
||||
|
||||
Param param;
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setParams(const std::string &var, const std::string &value);
|
||||
|
||||
// Calcula variables a partir de otras variables
|
||||
void precalculateZones();
|
||||
void precalculateZones()
|
||||
{
|
||||
// playArea
|
||||
param.game.play_area.center_x = param.game.play_area.rect.w / 2;
|
||||
param.game.play_area.first_quarter_x = param.game.play_area.rect.w / 4;
|
||||
param.game.play_area.third_quarter_x = param.game.play_area.rect.w / 4 * 3;
|
||||
param.game.play_area.center_y = param.game.play_area.rect.h / 2;
|
||||
param.game.play_area.first_quarter_y = param.game.play_area.rect.h / 4;
|
||||
param.game.play_area.third_quarter_y = param.game.play_area.rect.h / 4 * 3;
|
||||
|
||||
// gameArea
|
||||
param.game.game_area.rect = {0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)};
|
||||
param.game.game_area.center_x = param.game.game_area.rect.w / 2;
|
||||
param.game.game_area.first_quarter_x = param.game.game_area.rect.w / 4;
|
||||
param.game.game_area.third_quarter_x = param.game.game_area.rect.w / 4 * 3;
|
||||
param.game.game_area.center_y = param.game.game_area.rect.h / 2;
|
||||
param.game.game_area.first_quarter_y = param.game.game_area.rect.h / 4;
|
||||
param.game.game_area.third_quarter_y = param.game.game_area.rect.h / 4 * 3;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setParams(const std::string &var, const std::string &value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
auto success = true;
|
||||
|
||||
// GAME
|
||||
if (var == "game.width")
|
||||
{
|
||||
param.game.width = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.height")
|
||||
{
|
||||
param.game.height = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.item_size")
|
||||
{
|
||||
param.game.item_size = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.coffee_machine_w")
|
||||
{
|
||||
param.game.coffee_machine_w = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.coffee_machine_h")
|
||||
{
|
||||
param.game.coffee_machine_h = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.play_area.rect.x")
|
||||
{
|
||||
param.game.play_area.rect.x = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.play_area.rect.y")
|
||||
{
|
||||
param.game.play_area.rect.y = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.play_area.rect.w")
|
||||
{
|
||||
param.game.play_area.rect.w = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.play_area.rect.h")
|
||||
{
|
||||
param.game.play_area.rect.h = std::stoi(value);
|
||||
}
|
||||
else if (var == "game.enter_name_seconds")
|
||||
{
|
||||
param.game.enter_name_seconds = std::stoi(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Establece valores por defecto a las variables
|
||||
void initParam()
|
||||
@@ -65,6 +135,7 @@ void initParam()
|
||||
param.notification.color.b = 48;
|
||||
}
|
||||
|
||||
// Carga los parámetros desde un archivo
|
||||
void loadParamsFromFile(const std::string &file_path)
|
||||
{
|
||||
// Inicializa los parámetros con valores por defecto
|
||||
@@ -74,11 +145,11 @@ void loadParamsFromFile(const std::string &file_path)
|
||||
std::ifstream file(file_path);
|
||||
if (!file.is_open())
|
||||
{
|
||||
std::cerr << "Error: No se pudo abrir el archivo " << file_path << std::endl;
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo abrir el archivo %s", file_path.c_str());
|
||||
throw std::runtime_error("No se pudo abrir el archivo: " + file_path);
|
||||
}
|
||||
|
||||
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Leyendo archivo: %s", getFileName(file_path).c_str());
|
||||
|
||||
std::string line, param1, param2;
|
||||
while (std::getline(file, line))
|
||||
@@ -94,7 +165,10 @@ void loadParamsFromFile(const std::string &file_path)
|
||||
std::istringstream iss(line);
|
||||
if (iss >> param1 >> param2)
|
||||
{
|
||||
setParams(param1, param2);
|
||||
if (!setParams(param1, param2))
|
||||
{
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Parámetro desconocido: %s", param1.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,267 +178,3 @@ void loadParamsFromFile(const std::string &file_path)
|
||||
// Realiza cálculos adicionales después de cargar los parámetros
|
||||
precalculateZones();
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setParams(const std::string &var, const std::string &value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
auto success = true;
|
||||
|
||||
// GAME
|
||||
if (var == "game.width")
|
||||
{
|
||||
param.game.width = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.height")
|
||||
{
|
||||
param.game.height = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.item_size")
|
||||
{
|
||||
param.game.item_size = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.coffee_machine_w")
|
||||
{
|
||||
param.game.coffee_machine_w = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.coffee_machine_h")
|
||||
{
|
||||
param.game.coffee_machine_h = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.play_area.rect.x")
|
||||
{
|
||||
param.game.play_area.rect.x = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.play_area.rect.y")
|
||||
{
|
||||
param.game.play_area.rect.y = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.play_area.rect.w")
|
||||
{
|
||||
param.game.play_area.rect.w = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.play_area.rect.h")
|
||||
{
|
||||
param.game.play_area.rect.h = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "game.enter_name_seconds")
|
||||
{
|
||||
param.game.enter_name_seconds = std::stoi(value);
|
||||
}
|
||||
|
||||
// FADE
|
||||
else if (var == "fade.num_squares_width")
|
||||
{
|
||||
param.fade.num_squares_width = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "fade.num_squares_height")
|
||||
{
|
||||
param.fade.num_squares_height = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "fade.random_squares_delay")
|
||||
{
|
||||
param.fade.random_squares_delay = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "fade.random_squares_mult")
|
||||
{
|
||||
param.fade.random_squares_mult = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "fade.post_duration")
|
||||
{
|
||||
param.fade.post_duration = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "fade.venetian_size")
|
||||
{
|
||||
param.fade.venetian_size = std::stoi(value);
|
||||
}
|
||||
|
||||
// SCOREBOARD
|
||||
else if (var == "scoreboard.x")
|
||||
{
|
||||
param.scoreboard.x = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "scoreboard.y")
|
||||
{
|
||||
param.scoreboard.y = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "scoreboard.w")
|
||||
{
|
||||
param.scoreboard.w = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "scoreboard.h")
|
||||
{
|
||||
param.scoreboard.h = std::stoi(value);
|
||||
}
|
||||
|
||||
// TITLE
|
||||
else if (var == "title.press_start_position")
|
||||
{
|
||||
param.title.press_start_position = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "title.title_duration")
|
||||
{
|
||||
param.title.title_duration = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "title.arcade_edition_position")
|
||||
{
|
||||
param.title.arcade_edition_position = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "title.title_c_c_position")
|
||||
{
|
||||
param.title.title_c_c_position = std::stoi(value);
|
||||
}
|
||||
|
||||
// BACKGROUND
|
||||
else if (var == "background.attenuate_color.r")
|
||||
{
|
||||
param.background.attenuate_color.r = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "background.attenuate_color.g")
|
||||
{
|
||||
param.background.attenuate_color.g = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "background.attenuate_color.b")
|
||||
{
|
||||
param.background.attenuate_color.b = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "background.attenuate_alpha")
|
||||
{
|
||||
param.background.attenuate_alpha = std::stoi(value);
|
||||
}
|
||||
|
||||
// BALLOON
|
||||
else if (var == "balloon_1.vel")
|
||||
{
|
||||
param.balloon.at(0).vel = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_1.grav")
|
||||
{
|
||||
param.balloon.at(0).grav = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_2.vel")
|
||||
{
|
||||
param.balloon.at(1).vel = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_2.grav")
|
||||
{
|
||||
param.balloon.at(1).grav = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_3.vel")
|
||||
{
|
||||
param.balloon.at(2).vel = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_3.grav")
|
||||
{
|
||||
param.balloon.at(2).grav = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_4.vel")
|
||||
{
|
||||
param.balloon.at(3).vel = std::stof(value);
|
||||
}
|
||||
|
||||
else if (var == "balloon_4.grav")
|
||||
{
|
||||
param.balloon.at(3).grav = std::stof(value);
|
||||
}
|
||||
|
||||
// NOTIFICACIONES
|
||||
else if (var == "notification.pos_h")
|
||||
{
|
||||
if (value == "LEFT")
|
||||
{
|
||||
param.notification.pos_h = NotifyPosition::LEFT;
|
||||
}
|
||||
else if (value == "MIDDLE")
|
||||
{
|
||||
param.notification.pos_h = NotifyPosition::MIDDLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
param.notification.pos_h = NotifyPosition::RIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "notification.pos_v")
|
||||
{
|
||||
param.notification.pos_v = value == "TOP" ? NotifyPosition::TOP : NotifyPosition::BOTTOM;
|
||||
}
|
||||
|
||||
else if (var == "notification.sound")
|
||||
{
|
||||
param.notification.sound = stringToBool(value);
|
||||
}
|
||||
|
||||
else if (var == "notification.color.r")
|
||||
{
|
||||
param.notification.color.r = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "notification.color.g")
|
||||
{
|
||||
param.notification.color.g = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "notification.color.b")
|
||||
{
|
||||
param.notification.color.b = std::stoi(value);
|
||||
}
|
||||
|
||||
// RESTO
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Calcula variables a partir de otras variables
|
||||
void precalculateZones()
|
||||
{
|
||||
// playArea
|
||||
param.game.play_area.center_x = param.game.play_area.rect.w / 2;
|
||||
param.game.play_area.first_quarter_x = param.game.play_area.rect.w / 4;
|
||||
param.game.play_area.third_quarter_x = param.game.play_area.rect.w / 4 * 3;
|
||||
param.game.play_area.center_y = param.game.play_area.rect.h / 2;
|
||||
param.game.play_area.first_quarter_y = param.game.play_area.rect.h / 4;
|
||||
param.game.play_area.third_quarter_y = param.game.play_area.rect.h / 4 * 3;
|
||||
|
||||
// gameArea
|
||||
param.game.game_area.rect = {0, 0, static_cast<float>(param.game.width), static_cast<float>(param.game.height)};
|
||||
param.game.game_area.center_x = param.game.game_area.rect.w / 2;
|
||||
param.game.game_area.first_quarter_x = param.game.game_area.rect.w / 4;
|
||||
param.game.game_area.third_quarter_x = param.game.game_area.rect.w / 4 * 3;
|
||||
param.game.game_area.center_y = param.game.game_area.rect.h / 2;
|
||||
param.game.game_area.first_quarter_y = param.game.game_area.rect.h / 4;
|
||||
param.game.game_area.third_quarter_y = param.game.game_area.rect.h / 4 * 3;
|
||||
}
|
||||
Reference in New Issue
Block a user