From e1fa1d210268e8706fa5182d8c0a2e5042dd89fc Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 11 Oct 2024 09:03:57 +0200 Subject: [PATCH] Canviades certes opcions a parametres i afegides noves opcions --- data/config/param_320x240.txt | 10 +++- data/config/param_320x256.txt | 24 ++++++--- source/director.cpp | 10 ++-- source/notify.cpp | 18 +++---- source/options.cpp | 99 +++++++---------------------------- source/options.h | 4 +- source/param.cpp | 42 +++++++++++++++ source/utils.h | 21 ++++---- 8 files changed, 113 insertions(+), 115 deletions(-) diff --git a/data/config/param_320x240.txt b/data/config/param_320x240.txt index c2356cc..0df82af 100644 --- a/data/config/param_320x240.txt +++ b/data/config/param_320x240.txt @@ -41,4 +41,12 @@ balloon_2.grav 0.10f balloon_3.vel 4.70f balloon_3.grav 0.10f balloon_4.vel 5.45f -balloon_4.grav 0.10f \ No newline at end of file +balloon_4.grav 0.10f + +## NOTIFICATION +notification.pos_v TOP +notification.pos_h LEFT +notification.sound false +notification.color.r 48 +notification.color.g 48 +notification.color.b 48 \ No newline at end of file diff --git a/data/config/param_320x256.txt b/data/config/param_320x256.txt index 1fd79e5..2eeb011 100644 --- a/data/config/param_320x256.txt +++ b/data/config/param_320x256.txt @@ -1,11 +1,11 @@ ## GAME -game.item_size 20 -game.width 320 -game.height 256 -game.play_area.rect.x 0 -game.play_area.rect.y 0 -game.play_area.rect.w 320 -game.play_area.rect.h 216 +game.item_size 20 +game.width 320 +game.height 256 +game.play_area.rect.x 0 +game.play_area.rect.y 0 +game.play_area.rect.w 320 +game.play_area.rect.h 216 ## FADE fade.num_squares_width 160 @@ -41,4 +41,12 @@ balloon_2.grav 0.10f balloon_3.vel 4.70f balloon_3.grav 0.10f balloon_4.vel 5.45f -balloon_4.grav 0.10f \ No newline at end of file +balloon_4.grav 0.10f + +## NOTIFICATION +notification.pos_v TOP +notification.pos_h LEFT +notification.sound false +notification.color.r 48 +notification.color.g 48 +notification.color.b 48 \ No newline at end of file diff --git a/source/director.cpp b/source/director.cpp index 1bd3874..dce94e3 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -243,7 +243,7 @@ void Director::initJailAudio() bool Director::initSDL() { // Indicador de éxito - bool success = true; + auto success = true; // Inicializa SDL if (SDL_Init(SDL_INIT_EVERYTHING) < 0) @@ -291,7 +291,7 @@ bool Director::initSDL() #endif // NO_SHADERS // Crea la ventana window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options.video.window.size, param.game.height * options.video.window.size, SDL_WINDOW_HIDDEN); - if (window == nullptr) + if (!window) { #ifdef VERBOSE std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl; @@ -312,7 +312,7 @@ bool Director::initSDL() #endif renderer = SDL_CreateRenderer(window, -1, flags); - if (renderer == nullptr) + if (!renderer) { #ifdef VERBOSE std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl; @@ -326,7 +326,7 @@ bool Director::initSDL() // Establece el tamaño del buffer de renderizado SDL_RenderSetLogicalSize(renderer, param.game.width, param.game.height); - SDL_RenderSetIntegerScale(renderer, SDL_TRUE); + SDL_RenderSetIntegerScale(renderer, static_cast(options.video.integer_scale)); // Establece el modo de mezcla SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); @@ -520,7 +520,7 @@ void Director::createSystemFolder(std::string folder) #elif __linux__ struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; - systemFolder = std::string(homedir) + "/." + folder; + systemFolder = std::string(homedir) + "/.config/" + folder; #endif struct stat st = {0}; diff --git a/source/notify.cpp b/source/notify.cpp index d89f139..ecbbdb7 100644 --- a/source/notify.cpp +++ b/source/notify.cpp @@ -14,7 +14,7 @@ Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapF : renderer(renderer) { // Inicializa variables - bgColor = options.notification.color; + bgColor = param.notification.color; waitTime = 150; stack = false; hasIcons = iconFile == "" ? false : true; @@ -65,7 +65,7 @@ void Notify::update() // Hace sonar la notificación en el primer frame if (notifications[i].counter == 1) { - if (options.notification.sound) + if (param.notification.sound) { if (notifications[i].status == NotificationStatus::RISING) { // Reproduce el sonido de la notificación @@ -80,7 +80,7 @@ void Notify::update() const float step = ((float)notifications[i].counter / notifications[i].travelDist); const int alpha = 255 * step; - if (options.notification.pos_v == NotifyPosition::TOP) + if (param.notification.pos_v == NotifyPosition::TOP) { notifications[i].rect.y++; } @@ -112,7 +112,7 @@ void Notify::update() const float step = (notifications[i].counter / (float)notifications[i].travelDist); const int alpha = 255 * (1 - step); - if (options.notification.pos_v == NotifyPosition::TOP) + if (param.notification.pos_v == NotifyPosition::TOP) { notifications[i].rect.y--; } @@ -182,11 +182,11 @@ void Notify::showText(std::string text1, std::string text2, int icon) // Posición horizontal auto despH = 0; - if (options.notification.pos_h == NotifyPosition::LEFT) + if (param.notification.pos_h == NotifyPosition::LEFT) { despH = paddingOut; } - else if (options.notification.pos_h == NotifyPosition::MIDDLE) + else if (param.notification.pos_h == NotifyPosition::MIDDLE) { despH = ((param.game.width / 2) - (width / 2)); } @@ -196,12 +196,12 @@ void Notify::showText(std::string text1, std::string text2, int icon) } // Posición vertical - const int despV = (options.notification.pos_v == NotifyPosition::TOP) ? paddingOut : (param.game.height - height - paddingOut); + const int despV = (param.notification.pos_v == NotifyPosition::TOP) ? paddingOut : (param.game.height - height - paddingOut); // Offset const auto travelDist = height + paddingOut; auto offset = 0; - if (options.notification.pos_v == NotifyPosition::TOP) + if (param.notification.pos_v == NotifyPosition::TOP) { offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV; } @@ -221,7 +221,7 @@ void Notify::showText(std::string text1, std::string text2, int icon) n.text1 = text1; n.text2 = text2; n.shape = shape; - auto yPos = offset + (options.notification.pos_v == NotifyPosition::TOP ? -travelDist : travelDist); + auto yPos = offset + (param.notification.pos_v == NotifyPosition::TOP ? -travelDist : travelDist); n.rect = {despH, yPos, width, height}; // Crea la textura diff --git a/source/options.cpp b/source/options.cpp index 6b5bd30..23739ab 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -28,14 +28,9 @@ void initOptions() #endif options.video.filter = ScreenFilter::NEAREST; options.video.v_sync = true; + options.video.integer_scale = true; options.video.shaders = true; - // Opciones de las notificaciones - options.notification.pos_v = NotifyPosition::TOP; - options.notification.pos_h = NotifyPosition::LEFT; - options.notification.sound = false; - options.notification.color = (Color){48, 48, 48}; - // Opciones de audio options.audio.music.enabled = true; options.audio.music.volume = 128; @@ -51,8 +46,8 @@ void initOptions() options.controller.clear(); OptionsController c; - constexpr int numPlayers = 2; - for (int index = 0; index < numPlayers; ++index) + constexpr int num_players = 2; + for (int index = 0; index < num_players; ++index) { c.index = index; c.player_id = index + 1; @@ -91,7 +86,7 @@ bool loadOptionsFile(std::string file_path) bool success = true; // Variables para manejar el fichero - const std::string fileName = file_path.substr(file_path.find_last_of("\\/") + 1); + const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1); std::ifstream file(file_path); // Si el fichero se puede abrir @@ -99,7 +94,7 @@ bool loadOptionsFile(std::string file_path) { // Procesa el fichero linea a linea #ifdef VERBOSE - std::cout << "Reading file: " << fileName << std::endl; + std::cout << "Reading file: " << file_name << std::endl; #endif std::string line; while (std::getline(file, line)) @@ -113,7 +108,7 @@ bool loadOptionsFile(std::string file_path) if (!setOptions(line.substr(0, pos), line.substr(pos + 1, line.length()))) { #ifdef VERBOSE - std::cout << "Warning: file " << fileName << std::endl; + std::cout << "Warning: file " << file_name << std::endl; std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl; #endif success = false; @@ -155,19 +150,19 @@ bool loadOptionsFile(std::string file_path) // Guarda el fichero de configuración bool saveOptionsFile(std::string file_path) { - const std::string fileName = file_path.substr(file_path.find_last_of("\\/") + 1); + const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1); std::ofstream file(file_path); if (!file.good()) { #ifdef VERBOSE - std::cout << fileName << " can't be opened" << std::endl; + std::cout << file_name << " can't be opened" << std::endl; #endif return false; } #ifdef VERBOSE - std::cout << "Writing file: " << fileName << std::endl; + std::cout << "Writing file: " << file_name << std::endl; #endif // Opciones de video @@ -189,41 +184,9 @@ bool saveOptionsFile(std::string file_path) const auto valueFilter = std::to_string(static_cast(options.video.filter)); file << "video.filter=" << valueFilter << "\n"; - file << "video.shaders=" + boolToString(options.video.shaders) + "\n"; file << "video.v_sync=" + boolToString(options.video.v_sync) + "\n"; - - // Opciones de notificaciones - file << "\n\n## NOTIFICATION\n"; - file << "## notification.pos_v [TOP | BOTTOM]\n"; - file << "## notification.pos_h [LEFT | MIDDLE | RIGHT]\n"; - file << "\n"; - - if (options.notification.pos_v == NotifyPosition::TOP) - { - file << "notification.pos_v=TOP\n"; - } - - else - { - file << "notification.pos_v=BOTTOM\n"; - } - - if (options.notification.pos_h == NotifyPosition::LEFT) - { - file << "notification.pos_h=LEFT\n"; - } - - else if (options.notification.pos_h == NotifyPosition::MIDDLE) - { - file << "notification.posH=MIDDLE\n"; - } - - else - { - file << "notification.pos_h=RIGHT\n"; - } - - file << "notification.sound=" + boolToString(options.notification.sound) + "\n"; + file << "video.integer_scale=" + boolToString(options.video.integer_scale) + "\n"; + file << "video.shaders=" + boolToString(options.video.shaders) + "\n"; // Opciones de audio file << "\n\n## AUDIO\n"; @@ -252,8 +215,8 @@ bool saveOptionsFile(std::string file_path) file << "\n\n## CONTROLLERS\n"; file << "\n"; - const int numPlayers = 2; - for (int index = 0; index < numPlayers; ++index) + const int num_players = 2; + for (int index = 0; index < num_players; ++index) { const std::string joyIndex = std::to_string(index + 1); file << "controller" + joyIndex + ".name=" + options.controller[index].name + "\n"; @@ -264,7 +227,7 @@ bool saveOptionsFile(std::string file_path) file << "controller" + joyIndex + ".button.start=" + std::to_string((int)options.controller[index].buttons[3]) + "\n"; file << "controller" + joyIndex + ".button.service=" + std::to_string((int)options.controller[index].buttons[4]) + "\n"; - if (index < numPlayers - 1) + if (index < num_players - 1) { file << "\n"; } @@ -285,7 +248,6 @@ bool setOptions(std::string var, std::string value) // Opciones de video if (var == "video.mode") { - // options.video.mode = value == std::to_string(static_cast(ScreenVideoMode::WINDOW)) ? ScreenVideoMode::WINDOW : ScreenVideoMode::FULLSCREEN; options.video.mode = static_cast(std::stoi(value)); } @@ -300,7 +262,6 @@ bool setOptions(std::string var, std::string value) else if (var == "video.filter") { - // options.video.filter = value == std::to_string(static_cast(ScreenFilter::NEAREST)) ? ScreenFilter::NEAREST : ScreenFilter::LINEAL; options.video.filter = static_cast(std::stoi(value)); } @@ -309,38 +270,16 @@ bool setOptions(std::string var, std::string value) options.video.shaders = stringToBool(value); } + else if (var == "video.integer_scale") + { + options.video.integer_scale = stringToBool(value); + } + else if (var == "video.v_sync") { options.video.v_sync = stringToBool(value); } - // Opciones de notificaciones - else if (var == "notification.pos_h") - { - if (value == "LEFT") - { - options.notification.pos_h = NotifyPosition::LEFT; - } - else if (value == "MIDDLE") - { - options.notification.pos_h = NotifyPosition::MIDDLE; - } - else - { - options.notification.pos_h = NotifyPosition::RIGHT; - } - } - - else if (var == "notification.pos_v") - { - options.notification.pos_v = value == "TOP" ? NotifyPosition::TOP : NotifyPosition::BOTTOM; - } - - else if (var == "notification.sound") - { - options.notification.sound = stringToBool(value); - } - // Opciones de audio else if (var == "audio.music.enabled") { diff --git a/source/options.h b/source/options.h index 4b31c05..2987999 100644 --- a/source/options.h +++ b/source/options.h @@ -7,7 +7,7 @@ struct Options; extern Options options; // Carga el fichero de configuración -bool loadOptionsFile(std::string filePath); +bool loadOptionsFile(std::string file_path); // Guarda el fichero de configuración -bool saveOptionsFile(std::string filePath); \ No newline at end of file +bool saveOptionsFile(std::string file_path); \ No newline at end of file diff --git a/source/param.cpp b/source/param.cpp index 0689518..ffa0480 100644 --- a/source/param.cpp +++ b/source/param.cpp @@ -328,6 +328,48 @@ bool setParams(std::string var, std::string value) param.balloon_4.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 { diff --git a/source/utils.h b/source/utils.h index d590a93..5bf584a 100644 --- a/source/utils.h +++ b/source/utils.h @@ -84,6 +84,7 @@ struct OptionsVideo ScreenVideoMode mode; // Contiene el valor del modo de pantalla completa ScreenFilter filter; // Filtro usado para el escalado de la imagen bool v_sync; // Indica si se quiere usar vsync o no + bool integer_scale; // Indica si se va a usar el escalado entero bool shaders; // Indica si se van a usar shaders para los filtros de video }; @@ -129,22 +130,12 @@ struct OptionsController std::vector buttons; // Listado de botones asignados a cada input }; -// Estructura para las opciones de las notificaciones -struct OptionsNotification -{ - NotifyPosition pos_h; // Ubicación de las notificaciones en pantalla - NotifyPosition pos_v; // Ubicación de las notificaciones en pantalla - bool sound; // Indica si las notificaciones suenan - Color color; // Color de las notificaciones -}; - // Estructura con todas las opciones de configuración del programa struct Options { OptionsGame game; // Opciones para el propio juego OptionsVideo video; // Opciones relativas a la clase screen OptionsAudio audio; // Opciones para el audio - OptionsNotification notification; // Opciones para las notificaciones std::vector controller; // Opciones con las asignaciones del mando para cada jugador }; @@ -204,6 +195,15 @@ struct ParamBalloon float vel; // Velocidad inicial que tienen al rebotar contra el suelo }; +// Estructura para las opciones de las notificaciones +struct ParamNotification +{ + NotifyPosition pos_h; // Ubicación de las notificaciones en pantalla + NotifyPosition pos_v; // Ubicación de las notificaciones en pantalla + bool sound; // Indica si las notificaciones suenan + Color color; // Color de las notificaciones +}; + // Estructura para almacenar todos los parámetros del juego struct Param { @@ -213,6 +213,7 @@ struct Param ParamTitle title; // Parametros con ajustes para la sección Title ParamBackground background; // Parametros que afectan a la clase Background ParamBalloon balloon_1, balloon_2, balloon_3, balloon_4; // Parametros de velocidad y gravedad de cada tipo de globo + ParamNotification notification; // Opciones para las notificaciones }; // Estructura para almacenar ficheros de sonido y su nombre