Guardado de las opciones de las notificaciones

This commit is contained in:
2022-11-24 12:55:34 +01:00
parent ba19dcb904
commit 3817a01712
5 changed files with 193 additions and 35 deletions

View File

@@ -4,17 +4,18 @@
#include <iostream> #include <iostream>
// Constructor // Constructor
Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile) Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
{ {
// Inicializa variables // Inicializa variables
this->renderer = renderer; this->renderer = renderer;
bgColor = {64, 64, 64}; this->options = options;
bgColor = options->notifications.color;
waitTime = 300; waitTime = 300;
// Crea objetos // Crea objetos
texture = new Texture(renderer, bitmapFile); texture = new Texture(renderer, bitmapFile);
text = new Text(textFile, texture, renderer); text = new Text(textFile, texture, renderer);
sound = JA_LoadSound(soundFile.c_str()); sound = JA_LoadSound(soundFile.c_str());
} }
// Destructor // Destructor
@@ -23,7 +24,7 @@ Notify::~Notify()
// Libera la memoria de los objetos // Libera la memoria de los objetos
delete texture; delete texture;
delete text; delete text;
JA_DeleteSound(sound); JA_DeleteSound(sound);
for (auto notification : notifications) for (auto notification : notifications)
{ {
@@ -54,7 +55,14 @@ void Notify::update()
const float step = ((float)notifications.at(i).counter / notifications.at(i).travelDist); const float step = ((float)notifications.at(i).counter / notifications.at(i).travelDist);
const int alpha = 255 * step; const int alpha = 255 * step;
notifications.at(i).rect.y++; if (options->notifications.posV == pos_top)
{
notifications.at(i).rect.y++;
}
else
{
notifications.at(i).rect.y--;
}
notifications.at(i).texture->setAlpha(alpha); notifications.at(i).texture->setAlpha(alpha);
if (notifications.at(i).rect.y == notifications.at(i).y) if (notifications.at(i).rect.y == notifications.at(i).y)
@@ -79,7 +87,14 @@ void Notify::update()
const float step = (notifications.at(i).counter / (float)notifications.at(i).travelDist); const float step = (notifications.at(i).counter / (float)notifications.at(i).travelDist);
const int alpha = 255 * (1 - step); const int alpha = 255 * (1 - step);
notifications.at(i).rect.y--; if (options->notifications.posV == pos_top)
{
notifications.at(i).rect.y--;
}
else
{
notifications.at(i).rect.y++;
}
notifications.at(i).texture->setAlpha(alpha); notifications.at(i).texture->setAlpha(alpha);
if (notifications.at(i).rect.y == notifications.at(i).y - notifications.at(i).travelDist) if (notifications.at(i).rect.y == notifications.at(i).y - notifications.at(i).travelDist)
@@ -111,24 +126,67 @@ void Notify::clearFinishedNotifications()
// Muestra una notificación de texto por pantalla; // Muestra una notificación de texto por pantalla;
void Notify::showText(std::string text) void Notify::showText(std::string text)
{ {
// Crea constantes // Inicializa variables
const int width = this->text->lenght(text) + (this->text->getCharacterSize() * 2); const int width = this->text->lenght(text) + (this->text->getCharacterSize() * 2);
const int height = this->text->getCharacterSize() * 2; const int height = this->text->getCharacterSize() * 2;
const int despH = this->text->getCharacterSize() / 2; const int padding = (this->text->getCharacterSize() / 2);
const int despV = despH;
const int travelDist = height + despV; // Posición horizontal
const int offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV; int despH = 0;
if (options->notifications.posH == pos_left)
{
despH = padding;
}
else if (options->notifications.posH == pos_middle)
{
despH = ((options->screen.windowWidth * options->windowSize) / 2 - (width / 2));
}
else
{
despH = (options->screen.windowWidth * options->windowSize) - width - padding;
}
// Posición vertical
int despV = 0;
if (options->notifications.posV == pos_top)
{
despV = padding;
}
else
{
despV = (options->screen.windowHeight * options->windowSize) - height - padding;
}
const int travelDist = height + padding;
// Offset
int offset = 0;
if (options->notifications.posV == pos_top)
{
offset = (int)notifications.size() > 0 ? notifications.back().y + travelDist : despV;
}
else
{
offset = (int)notifications.size() > 0 ? notifications.back().y - travelDist : despV;
}
// Crea la notificacion // Crea la notificacion
notification_t n; notification_t n;
// inicializa variables // Inicializa variables
n.y = offset; n.y = offset;
n.travelDist = travelDist; n.travelDist = travelDist;
n.counter = 0; n.counter = 0;
n.state = ns_rising; n.state = ns_rising;
n.text = text; n.text = text;
n.rect = {despH, offset - travelDist, width, height}; if (options->notifications.posV == pos_top)
{
n.rect = {despH, offset - travelDist, width, height};
}
else
{
n.rect = {despH, offset + travelDist, width, height};
}
// Crea la textura // Crea la textura
n.texture = new Texture(renderer); n.texture = new Texture(renderer);
@@ -137,7 +195,7 @@ void Notify::showText(std::string text)
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255); SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
n.texture->setBlendMode(SDL_BLENDMODE_BLEND); n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, despV, text, 1, {255, 255, 255}, 1, {0, 0, 0}); this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, padding, text, 1, {255, 255, 255}, 1, {0, 0, 0});
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
// Crea el sprite // Crea el sprite
@@ -147,7 +205,10 @@ void Notify::showText(std::string text)
notifications.push_back(n); notifications.push_back(n);
// Reproduce el sonido de la notificación // Reproduce el sonido de la notificación
JA_PlaySound(sound); if (options->notifications.sound)
{
JA_PlaySound(sound);
}
} }
// Indica si hay notificaciones activas // Indica si hay notificaciones activas

View File

@@ -51,6 +51,7 @@ private:
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Texture *texture; // Textura para la fuente de las notificaciones Texture *texture; // Textura para la fuente de las notificaciones
Text *text; // Objeto para dibujar texto Text *text; // Objeto para dibujar texto
options_t *options; // Variable con todas las opciones del programa
// Variables // Variables
color_t bgColor; // Color de fondo de las notificaciones color_t bgColor; // Color de fondo de las notificaciones
@@ -69,7 +70,7 @@ public:
void update(); void update();
// Constructor // Constructor
Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile); Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options);
// Destructor // Destructor
~Notify(); ~Notify();

View File

@@ -12,7 +12,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
this->asset = asset; this->asset = asset;
// Crea los objetos // Crea los objetos
notify = new Notify(renderer, asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav")); notify = new Notify(renderer, asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
gameCanvasWidth = options->gameWidth; gameCanvasWidth = options->gameWidth;
gameCanvasHeight = options->gameHeight; gameCanvasHeight = options->gameHeight;
@@ -167,8 +167,10 @@ void Screen::setVideoMode(int videoMode)
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
} }
// Actualiza el valor de la variable // Actualiza las opciones
options->videoMode = videoMode; options->videoMode = videoMode;
options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight;
// Establece el tamaño de las notificaciones // Establece el tamaño de las notificaciones
setNotificationSize(); setNotificationSize();

View File

@@ -54,6 +54,25 @@ enum palette_e
p_zxarne p_zxarne
}; };
// Posiciones de las notificaciones
enum not_pos_e
{
pos_top,
pos_bottom,
pos_left,
pos_middle,
pos_right
};
// Estructura para las opciones de las notificaciones
struct op_notification_t
{
not_pos_e posH; // Ubicación de las notificaciones en pantalla
not_pos_e posV; // Ubicación de las notificaciones en pantalla
bool sound; // Indica si las notificaciones suenan
color_t color; // Color de las notificaciones
};
// Estructura para saber la seccion y subseccion del programa // Estructura para saber la seccion y subseccion del programa
struct section_t struct section_t
{ {
@@ -89,25 +108,34 @@ struct op_stats_t
std::string worstNightmare; // Habitación con más muertes acumuladas std::string worstNightmare; // Habitación con más muertes acumuladas
}; };
// Estructura con opciones de la pantalla
struct op_screen_t
{
int windowWidth; // Ancho de la ventana
int windowHeight; // Alto de la ventana
};
// Estructura con todas las opciones de configuración del programa // Estructura con todas las opciones de configuración del programa
struct options_t struct options_t
{ {
std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles std::string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles
Uint32 videoMode; // Contiene el valor del modo de pantalla completa Uint32 videoMode; // Contiene el valor del modo de pantalla completa
int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana
Uint32 filter; // Filtro usado para el escalado de la imagen Uint32 filter; // Filtro usado para el escalado de la imagen
bool vSync; // Indica si se quiere usar vsync o no bool vSync; // Indica si se quiere usar vsync o no
int gameWidth; // Ancho de la resolucion nativa del juego int gameWidth; // Ancho de la resolucion nativa del juego
int gameHeight; // Alto de la resolucion nativa del juego int gameHeight; // Alto de la resolucion nativa del juego
bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa
bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa
bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana
float borderSize; // Porcentaje de borde que se añade a lo ventana float borderSize; // Porcentaje de borde que se añade a lo ventana
palette_e palette; // Paleta de colores a usar en el juego palette_e palette; // Paleta de colores a usar en el juego
bool console; // Indica si ha de mostrar información por la consola de texto bool console; // Indica si ha de mostrar información por la consola de texto
cheat_t cheat; // Contiene trucos y ventajas para el juego cheat_t cheat; // Contiene trucos y ventajas para el juego
op_stats_t stats; // Datos con las estadisticas de juego op_stats_t stats; // Datos con las estadisticas de juego
online_t online; // Datos del servicio online online_t online; // Datos del servicio online
op_notification_t notifications; // Opciones relativas a las notificaciones;
op_screen_t screen; // Opciones relativas a la clase screen
}; };
// Calcula el cuadrado de la distancia entre dos puntos // Calcula el cuadrado de la distancia entre dos puntos

View File

@@ -59,7 +59,7 @@ Director::Director(int argc, char *argv[])
music = JA_LoadMusic(asset->get("title.ogg").c_str()); music = JA_LoadMusic(asset->get("title.ogg").c_str());
// Inicializa los servicios online // Inicializa los servicios online
//initOnline(); // initOnline();
} }
Director::~Director() Director::~Director()
@@ -147,6 +147,12 @@ void Director::initOptions()
options->online.gameID = "jaildoctors_dilemma"; options->online.gameID = "jaildoctors_dilemma";
#endif #endif
options->online.jailerID = ""; options->online.jailerID = "";
// Opciones de las notificaciones
options->notifications.posV = pos_top;
options->notifications.posH = pos_left;
options->notifications.sound = true;
options->notifications.color = {64, 64, 64};
} }
// Comprueba los parametros del programa // Comprueba los parametros del programa
@@ -346,6 +352,33 @@ bool Director::saveConfig()
file << "port=" + std::to_string(options->online.port) + "\n"; file << "port=" + std::to_string(options->online.port) + "\n";
file << "jailerID=" + options->online.jailerID + "\n"; file << "jailerID=" + options->online.jailerID + "\n";
file << "\n## NOTIFICATION OPTIONS\n";
file << "## notifications.posV = pos_top | pos_bottom\n";
if (options->notifications.posV == pos_top)
{
file << "notifications.posV=pos_top\n";
}
else
{
file << "notifications.posV=pos_bottom\n";
}
file << "## notifications.posH = pos_left | pos_middle | pos_right\n";
if (options->notifications.posH == pos_left)
{
file << "notifications.posH=pos_left\n";
}
else if (options->notifications.posH == pos_middle)
{
file << "notifications.posH=pos_middle\n";
}
else
{
file << "notifications.posH=pos_right\n";
}
file << "notifications.sound=" + boolToString(options->notifications.sound) + "\n";
// Cierra el fichero // Cierra el fichero
file.close(); file.close();
@@ -1081,6 +1114,39 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
options->online.jailerID = value; options->online.jailerID = value;
} }
else if (var == "notifications.posH")
{
if (value == "pos_left")
{
options->notifications.posH = pos_left;
}
else if (value == "pos_middle")
{
options->notifications.posH = pos_middle;
}
else
{
options->notifications.posH = pos_right;
}
}
else if (var == "notifications.posV")
{
if (value == "pos_top")
{
options->notifications.posV = pos_top;
}
else
{
options->notifications.posV = pos_bottom;
}
}
else if (var == "notifications.sound")
{
options->notifications.sound = stringToBool(value);
}
else if (var == "" || var.substr(0, 1) == "#") else if (var == "" || var.substr(0, 1) == "#")
{ {
} }