Actualizadas las notificaciones a la última versión

This commit is contained in:
2023-02-12 21:28:49 +01:00
parent 3da900b102
commit 8df2638905
5 changed files with 171 additions and 28 deletions

View File

@@ -4,7 +4,7 @@
#include <iostream>
// Constructor
Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
Notify::Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options)
{
// Inicializa variables
this->renderer = renderer;
@@ -13,8 +13,9 @@ Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textF
waitTime = 300;
// Crea objetos
texture = new Texture(renderer, bitmapFile);
text = new Text(textFile, texture, renderer);
iconTexture = new Texture(renderer, iconFile);
textTexture = new Texture(renderer, bitmapFile);
text = new Text(textFile, textTexture, renderer);
sound = JA_LoadSound(soundFile.c_str());
}
@@ -22,7 +23,8 @@ Notify::Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textF
Notify::~Notify()
{
// Libera la memoria de los objetos
delete texture;
delete textTexture;
delete iconTexture;
delete text;
JA_DeleteSound(sound);
@@ -124,12 +126,15 @@ void Notify::clearFinishedNotifications()
}
// Muestra una notificación de texto por pantalla;
void Notify::showText(std::string text)
void Notify::showText(std::string text1, std::string text2, int icon)
{
// Inicializa variables
const int width = this->text->lenght(text) + (this->text->getCharacterSize() * 2);
const int height = this->text->getCharacterSize() * 2;
const int padding = (this->text->getCharacterSize() / 2);
const int iconSize = 16;
const int padding = text->getCharacterSize();
const int iconSpace = icon >= 0 ? iconSize + padding : 0;
const std::string txt = text1.length() > text2.length() ? text1 : text2;
const int width = text->lenght(txt) + (padding * 2) + iconSpace;
const int height = (text->getCharacterSize() * 2) + (padding * 2);
// Posición horizontal
int despH = 0;
@@ -178,7 +183,8 @@ void Notify::showText(std::string text)
n.travelDist = travelDist;
n.counter = 0;
n.state = ns_rising;
n.text = text;
n.text1 = text1;
n.text2 = text2;
if (options->notifications.posV == pos_top)
{
n.rect = {despH, offset - travelDist, width, height};
@@ -191,14 +197,52 @@ void Notify::showText(std::string text)
// Crea la textura
n.texture = new Texture(renderer);
n.texture->createBlank(renderer, width, height, SDL_TEXTUREACCESS_TARGET);
n.texture->setAsRenderTarget(renderer);
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(renderer);
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
this->text->writeDX(TXT_CENTER | TXT_STROKE, width / 2, padding, text, 1, {255, 255, 255}, 1, {0, 0, 0});
// Prepara para dibujar en la textura
n.texture->setAsRenderTarget(renderer);
// Dibuja el fondo de la notificación
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_Rect rect;
rect = {4, 0, width - (4 * 2), height};
SDL_RenderFillRect(renderer, &rect);
rect = {4 / 2, 1, width - 4, height - 2};
SDL_RenderFillRect(renderer, &rect);
rect = {1, 4 / 2, width - 2, height - 4};
SDL_RenderFillRect(renderer, &rect);
rect = {0, 4, width, height - (4 * 2)};
SDL_RenderFillRect(renderer, &rect);
// Dibuja el icono de la notificación
if (icon >= 0)
{
Sprite *sp = new Sprite({0, 0, iconSize, iconSize}, iconTexture, renderer);
sp->setPos({padding, padding, iconSize, iconSize});
sp->setSpriteClip({iconSize * (icon % 10), iconSize * (icon / 10), iconSize, iconSize});
sp->render();
delete sp;
}
// Escribe el texto de la notificación
color_t color = {255, 255, 255};
if (text2 != "")
{ // Dos lineas de texto
text->writeColored(padding + iconSpace, padding, text1, color);
text->writeColored(padding + iconSpace, padding + text->getCharacterSize() + 1, text2, color);
}
else
{ // Una linea de texto
text->writeColored(padding + iconSpace, (height / 2) - (text->getCharacterSize() / 2), text1, color);
}
// Deja de dibujar en la textura
SDL_SetRenderTarget(renderer, nullptr);
// Crea el sprite
// Crea el sprite de la notificación
n.sprite = new Sprite(n.rect, n.texture, renderer);
// Añade la notificación a la lista

View File

@@ -36,7 +36,8 @@ private:
struct notification_t
{
std::string text;
std::string text1;
std::string text2;
int counter;
notification_state_e state;
notification_position_e position;
@@ -49,7 +50,8 @@ private:
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
Texture *texture; // Textura para la fuente de las notificaciones
Texture *textTexture; // Textura para la fuente de las notificaciones
Texture *iconTexture; // Textura para los iconos de las notificaciones
Text *text; // Objeto para dibujar texto
options_t *options; // Variable con todas las opciones del programa
@@ -57,7 +59,7 @@ private:
color_t bgColor; // Color de fondo de las notificaciones
int waitTime; // Tiempo que se ve la notificación
std::vector<notification_t> notifications; // La lista de notificaciones activas
JA_Sound_t* sound; // Sonido a reproducir cuando suena la notificación
JA_Sound_t *sound; // Sonido a reproducir cuando suena la notificación
// Elimina las notificaciones finalizadas
void clearFinishedNotifications();
@@ -70,13 +72,13 @@ public:
void update();
// Constructor
Notify(SDL_Renderer *renderer, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options);
Notify(SDL_Renderer *renderer, std::string iconFile, std::string bitmapFile, std::string textFile, std::string soundFile, options_t *options);
// Destructor
~Notify();
// Muestra una notificación de texto por pantalla;
void showText(std::string text);
void showText(std::string text1 = "", std::string text2 = "", int icon = -1);
// Indica si hay notificaciones activas
bool active();

View File

@@ -12,7 +12,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
this->asset = asset;
// Crea los objetos
notify = new Notify(renderer, asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
notify = new Notify(renderer, asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"), options);
gameCanvasWidth = options->gameWidth;
gameCanvasHeight = options->gameHeight;
@@ -48,6 +48,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
Screen::~Screen()
{
delete notify;
SDL_DestroyTexture(gameCanvas);
}
// Limpia la pantalla
@@ -95,6 +96,9 @@ void Screen::setVideoMode(int videoMode)
// Muestra el puntero
SDL_ShowCursor(SDL_ENABLE);
// Esconde la ventana
//SDL_HideWindow(window);
if (options->borderEnabled)
{
windowWidth = gameCanvasWidth + borderWidth;
@@ -112,6 +116,9 @@ void Screen::setVideoMode(int videoMode)
// Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Muestra la ventana
//SDL_ShowWindow(window);
}
// Si está activo el modo de pantalla completa añade el borde
@@ -386,9 +393,9 @@ void Screen::updateNotifier()
}
// Muestra una notificación de texto por pantalla;
void Screen::showNotification(std::string text)
void Screen::showNotification(std::string text1, std::string text2, int icon)
{
notify->showText(text);
notify->showText(text1, text2, icon);
}
// Dibuja las notificaciones

View File

@@ -4,6 +4,7 @@
#include "asset.h"
#include "notify.h"
#include "utils.h"
#include "../const.h"
#include <vector>
#ifndef SCREEN_H
@@ -138,7 +139,7 @@ public:
void updateNotifier();
// Muestra una notificación de texto por pantalla;
void showNotification(std::string text);
void showNotification(std::string text1 = "", std::string text2 = "", int icon = -1);
};
#endif

View File

@@ -358,9 +358,10 @@ bool Director::setFileList()
// Inicializa las opciones del programa
void Director::initOptions()
{
// Crea el puntero a la estructura de opciones
options = new options_t;
// Pone unos valores por defecto
// Pone unos valores por defecto para las opciones de control
options->input.clear();
input_t inp;
@@ -374,7 +375,7 @@ void Director::initOptions()
inp.deviceType = INPUT_USE_GAMECONTROLLER;
options->input.push_back(inp);
// Video
// Opciones de video
options->gameWidth = GAMECANVAS_WIDTH;
options->gameHeight = GAMECANVAS_HEIGHT;
options->videoMode = 0;
@@ -387,19 +388,29 @@ void Director::initOptions()
options->borderHeight = 0;
options->borderEnabled = false;
// Varios
// Opciones varios
options->playerSelected = 0;
options->difficulty = DIFFICULTY_NORMAL;
options->language = ba_BA;
options->console = false;
// Online
// Opciones online
options->online.enabled = false;
options->online.server = "jaildoctor.duckdns.org";
options->online.port = 9911;
#ifdef DEBUG
options->online.gameID = "coffee_crisis_test";
#else
options->online.gameID = "coffee_crisis";
#endif
options->online.jailerID = "";
options->online.score = 0;
// Opciones de las notificaciones
options->notifications.posV = pos_top;
options->notifications.posH = pos_left;
options->notifications.sound = true;
options->notifications.color = {48, 48, 48};
}
// Comprueba los parametros del programa
@@ -550,7 +561,22 @@ bool Director::saveConfigFile()
// Crea y abre el fichero de texto
std::ofstream file(asset->get("config.txt"));
// Escribe en el fichero
if (file.good())
{
if (options->console)
{
std::cout << asset->get("config.txt") << " open for writing" << std::endl;
}
}
else
{
if (options->console)
{
std::cout << asset->get("config.txt") << " can't be opened" << std::endl;
}
}
// Opciones g´raficas
file << "## VISUAL OPTIONS\n";
if (options->videoMode == 0)
{
@@ -585,18 +611,48 @@ bool Director::saveConfigFile()
file << "borderWidth=" + std::to_string(options->borderWidth) + "\n";
file << "borderHeight=" + std::to_string(options->borderHeight) + "\n";
// Otras opciones del programa
file << "\n## OTHER OPTIONS\n";
file << "language=" + std::to_string(options->language) + "\n";
file << "difficulty=" + std::to_string(options->difficulty) + "\n";
file << "input0=" + std::to_string(options->input[0].deviceType) + "\n";
file << "input1=" + std::to_string(options->input[1].deviceType) + "\n";
// Opciones sobre la conexión online
file << "\n## ONLINE OPTIONS\n";
file << "enabled=" + boolToString(options->online.enabled) + "\n";
file << "server=" + options->online.server + "\n";
file << "port=" + std::to_string(options->online.port) + "\n";
file << "jailerID=" + options->online.jailerID + "\n";
// Opciones de las notificaciones
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
file.close();
@@ -873,6 +929,39 @@ bool Director::setOptions(options_t *options, std::string var, std::string 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) == "#")
{
}