Compare commits

...

4 Commits

8 changed files with 486 additions and 102 deletions

285
source/common/notify.cpp Normal file
View File

@@ -0,0 +1,285 @@
#include "notify.h"
#include <string>
#include <stdio.h>
#include <iostream>
// Constructor
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;
this->options = options;
bgColor = options->notification.color;
waitTime = 300;
// Crea objetos
iconTexture = new Texture(renderer, iconFile);
textTexture = new Texture(renderer, bitmapFile);
text = new Text(textFile, textTexture, renderer);
sound = JA_LoadSound(soundFile.c_str());
}
// Destructor
Notify::~Notify()
{
// Libera la memoria de los objetos
delete textTexture;
delete iconTexture;
delete text;
JA_DeleteSound(sound);
for (auto notification : notifications)
{
delete notification.sprite;
delete notification.texture;
}
}
// Dibuja las notificaciones por pantalla
void Notify::render()
{
for (int i = (int)notifications.size() - 1; i >= 0; --i)
{
notifications[i].sprite->render();
}
}
// Actualiza el estado de las notificaiones
void Notify::update()
{
for (int i = 0; i < (int)notifications.size(); ++i)
{
// Si la notificación anterior está "saliendo", no hagas nada
if (i > 0)
{
if (notifications[i - 1].state == ns_rising)
{
break;
}
}
notifications[i].counter++;
// Hace sonar la notificación en el primer frame
if (notifications[i].counter == 1)
{
if (options->notification.sound)
{
if (notifications[i].state == ns_rising)
{ // Reproduce el sonido de la notificación
JA_PlaySound(sound);
}
}
}
// Comprueba los estados
if (notifications[i].state == ns_rising)
{
const float step = ((float)notifications[i].counter / notifications[i].travelDist);
const int alpha = 255 * step;
if (options->notification.posV == pos_top)
{
notifications[i].rect.y++;
}
else
{
notifications[i].rect.y--;
}
notifications[i].texture->setAlpha(alpha);
if (notifications[i].rect.y == notifications[i].y)
{
notifications[i].state = ns_stay;
notifications[i].texture->setAlpha(255);
notifications[i].counter = 0;
}
}
else if (notifications[i].state == ns_stay)
{
if (notifications[i].counter == waitTime)
{
notifications[i].state = ns_vanishing;
notifications[i].counter = 0;
}
}
else if (notifications[i].state == ns_vanishing)
{
const float step = (notifications[i].counter / (float)notifications[i].travelDist);
const int alpha = 255 * (1 - step);
if (options->notification.posV == pos_top)
{
notifications[i].rect.y--;
}
else
{
notifications[i].rect.y++;
}
notifications[i].texture->setAlpha(alpha);
if (notifications[i].rect.y == notifications[i].y - notifications[i].travelDist)
{
notifications[i].state = ns_finished;
}
}
notifications[i].sprite->setRect(notifications[i].rect);
}
clearFinishedNotifications();
}
// Elimina las notificaciones finalizadas
void Notify::clearFinishedNotifications()
{
for (int i = (int)notifications.size() - 1; i >= 0; --i)
{
if (notifications[i].state == ns_finished)
{
delete notifications[i].sprite;
delete notifications[i].texture;
notifications.erase(notifications.begin() + i);
}
}
}
// Muestra una notificación de texto por pantalla;
void Notify::showText(std::string text1, std::string text2, int icon)
{
// Inicializa variables
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;
if (options->notification.posH == pos_left)
{
despH = padding;
}
else if (options->notification.posH == pos_middle)
{
despH = ((options->video.gameWidth / 2) - (width / 2));
}
else
{
despH = options->video.gameWidth - width - padding;
}
// Posición vertical
int despV = 0;
if (options->notification.posV == pos_top)
{
despV = padding;
}
else
{
despV = options->video.gameHeight - height - padding;
}
const int travelDist = height + padding;
// Offset
int offset = 0;
if (options->notification.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
notification_t n;
// Inicializa variables
n.y = offset;
n.travelDist = travelDist;
n.counter = 0;
n.state = ns_rising;
n.text1 = text1;
n.text2 = text2;
if (options->notification.posV == pos_top)
{
n.rect = {despH, offset - travelDist, width, height};
}
else
{
n.rect = {despH, offset + travelDist, width, height};
}
// Crea la textura
n.texture = new Texture(renderer);
n.texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
n.texture->setBlendMode(SDL_BLENDMODE_BLEND);
// 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);
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 de la notificación
n.sprite = new Sprite(n.rect, n.texture, renderer);
// Deja la notificación invisible
n.texture->setAlpha(0);
// Añade la notificación a la lista
notifications.push_back(n);
}
// Indica si hay notificaciones activas
bool Notify::active()
{
if ((int)notifications.size() > 0)
{
return true;
}
return false;
}

87
source/common/notify.h Normal file
View File

@@ -0,0 +1,87 @@
#pragma once
#include <SDL2/SDL.h>
#include "jail_audio.h"
#include "sprite.h"
#include "text.h"
#include "texture.h"
#include "utils.h"
#include <vector>
#ifndef NOTIFY_H
#define NOTIFY_H
class Notify
{
private:
enum notification_state_e
{
ns_rising,
ns_stay,
ns_vanishing,
ns_finished
};
enum notification_position_e
{
upperLeft,
upperCenter,
upperRight,
middleLeft,
middleRight,
bottomLeft,
bottomCenter,
bottomRight
};
struct notification_t
{
std::string text1;
std::string text2;
int counter;
notification_state_e state;
notification_position_e position;
Texture *texture;
Sprite *sprite;
SDL_Rect rect;
int y;
int travelDist;
};
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
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
// Variables
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
// Elimina las notificaciones finalizadas
void clearFinishedNotifications();
public:
// Dibuja las notificaciones por pantalla
void render();
// Actualiza el estado de las notificaiones
void update();
// Constructor
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 text1 = "", std::string text2 = "", int icon = -1);
// Indica si hay notificaciones activas
bool active();
};
#endif

View File

@@ -25,10 +25,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *
borderHeight = options->video.border.height * 2;
dest = {0, 0, 0, 0};
borderColor = {0, 0, 0};
fadeEffect.enabled = false;
fadeEffect.counter = 0;
fadeEffect.lenght = 0;
fadeEffect.color = {0xFF, 0xFF, 0xFF};
flashEffect.enabled = false;
flashEffect.counter = 0;
flashEffect.lenght = 0;
@@ -41,8 +37,6 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *
shakeEffect.origin = 0;
attenuateEffect = false;
iniFade();
// Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00};
@@ -84,6 +78,9 @@ void Screen::blit()
// Atenua la pantalla
doAttenuate();
// Pinta las notificaciones
notify->render();
#ifdef NO_SHADERS
// Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(renderer, nullptr);
@@ -299,78 +296,11 @@ void Screen::switchBorder()
setVideoMode(VIDEO_MODE_WINDOW);
}
// Activa el fade
void Screen::setFade()
{
fadeEffect.enabled = true;
}
// Comprueba si ha terminado el fade
bool Screen::fadeEnded()
{
if (fadeEffect.enabled || fadeEffect.counter > 0)
{
return false;
}
return true;
}
// Inicializa las variables para el fade
void Screen::iniFade()
{
fadeEffect.enabled = false;
fadeEffect.counter = 0;
fadeEffect.lenght = 200;
}
// Actualiza el fade
void Screen::updateFade()
{
if (!fadeEffect.enabled)
{
return;
}
fadeEffect.counter++;
if (fadeEffect.counter > fadeEffect.lenght)
{
iniFade();
}
}
// Dibuja el fade
void Screen::renderFade()
{
if (!fadeEffect.enabled)
{
return;
}
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
color_t color = {0, 0, 0};
const float step = (float)fadeEffect.counter / (float)fadeEffect.lenght;
const int alpha = 0 + (255 - 0) * step;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
SDL_RenderFillRect(renderer, &rect);
}
// Actualiza los efectos
void Screen::updateFX()
{
updateFade();
}
// Dibuja los efectos
void Screen::renderFX()
{
renderFade();
}
// Actualiza la lógica de la clase
void Screen::update()
{
updateShake();
notify->update();
}
// Comprueba las entradas
@@ -483,4 +413,10 @@ void Screen::switchShaders()
void Screen::attenuate(bool value)
{
attenuateEffect = value;
}
// Muestra una notificación de texto por pantalla;
void Screen::showNotification(std::string text1, std::string text2, int icon)
{
notify->showText(text1, text2, icon);
}

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h>
#include "asset.h"
#include "utils.h"
#include "notify.h"
#include "input.h"
#include "../const.h"
#include <vector>
@@ -21,6 +22,7 @@ private:
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con el listado de recursos
Input *input; // Objeto para leer las entradas de teclado o mando
Notify *notify; // Pinta notificaciones en pantalla
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
options_t *options; // Variable con todas las opciones del programa
@@ -44,7 +46,6 @@ private:
};
// Variables - Efectos
effect_t fadeEffect; // Variable para gestionar el efecto de fade
effect_t flashEffect; // Variable para gestionar el efecto de flash
struct shake_t
@@ -57,21 +58,6 @@ private:
int origin; // Valor inicial de la pantalla para dejarla igual tras el desplazamiento
} shakeEffect;
// Inicializa las variables para el fade
void iniFade();
// Actualiza el fade
void updateFade();
// Dibuja el fade
void renderFade();
// Actualiza los efectos
void updateFX();
// Dibuja los efectos
void renderFX();
// Actualiza la logica para agitar la pantalla
void updateShake();
@@ -134,12 +120,6 @@ public:
// Cambia entre borde visible y no visible
void switchBorder();
// Activa el fade
void setFade();
// Comprueba si ha terminado el fade
bool fadeEnded();
// Agita la pantalla
void shake();
@@ -151,4 +131,7 @@ public:
// Atenua la pantalla
void attenuate(bool value);
// Muestra una notificación de texto por pantalla;
void showNotification(std::string text1 = "", std::string text2 = "", int icon = -1);
};

View File

@@ -87,6 +87,15 @@ void Sprite::setPos(SDL_Point p)
this->y = p.y;
}
// Establece la posición del objeto
void Sprite::setPos(SDL_Rect r)
{
this->x = r.x;
this->y = r.y;
this->w = r.w;
this->h = r.h;
}
// Establece el valor de la variable
void Sprite::setPosX(int x)
{

View File

@@ -42,6 +42,7 @@ public:
// Establece la posición del objeto
void setPos(SDL_Point p);
void setPos(SDL_Rect r);
// Establece el valor de la variable
void setPosX(int x);

View File

@@ -161,6 +161,15 @@ struct op_controller_t
std::vector<SDL_GameControllerButton> buttons; // Listado de botones asignados a cada input
};
// 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 con todas las opciones de configuración del programa
struct options_t
{
@@ -168,6 +177,7 @@ struct options_t
op_game_t game; // Opciones para el propio juego
op_video_t video; // Opciones relativas a la clase screen
op_audio_t audio; // Opciones para el audio
op_notification_t notification; // Opciones para las notificaciones
std::vector<op_controller_t> controller; // Opciones con las asignaciones del mando para cada jugador
};

View File

@@ -338,10 +338,10 @@ bool Director::setFileList()
asset->add(prefix + "/data/gfx/explosion3.ani", t_animation);
asset->add(prefix + "/data/gfx/explosion4.png", t_bitmap);
asset->add(prefix + "/data/gfx/explosion4.ani", t_animation);
asset->add(prefix + "/data/gfx/powerball.png", t_bitmap);
asset->add(prefix + "/data/gfx/powerball.ani", t_animation);
asset->add(prefix + "/data/gfx/bullet.png", t_bitmap);
asset->add(prefix + "/data/gfx/game_buildings.png", t_bitmap);
@@ -382,17 +382,17 @@ bool Director::setFileList()
asset->add(prefix + "/data/gfx/player1_pal1.gif", t_palette);
asset->add(prefix + "/data/gfx/player1_pal2.gif", t_palette);
asset->add(prefix + "/data/gfx/player1_pal3.gif", t_palette);
asset->add(prefix + "/data/gfx/player2.gif", t_bitmap);
asset->add(prefix + "/data/gfx/player2_pal1.gif", t_palette);
asset->add(prefix + "/data/gfx/player2_pal2.gif", t_palette);
asset->add(prefix + "/data/gfx/player2_pal3.gif", t_palette);
asset->add(prefix + "/data/gfx/player.ani", t_animation);
asset->add(prefix + "/data/gfx/player1_power.gif", t_bitmap);
asset->add(prefix + "/data/gfx/player2_power.gif", t_bitmap);
asset->add(prefix + "/data/gfx/player_power.ani", t_animation);
// Fuentes de texto
@@ -458,6 +458,12 @@ void Director::initOptions()
options->video.border.enabled = false;
options->video.shaders = true;
// Opciones de las notificaciones
options->notification.posV = pos_top;
options->notification.posH = pos_left;
options->notification.sound = true;
options->notification.color = {48, 48, 48};
// Opciones de audio
options->audio.music.enabled = true;
options->audio.music.volume = 128;
@@ -692,6 +698,39 @@ bool Director::saveConfigFile()
file << "video.border.width=" + std::to_string(options->video.border.width) + "\n";
file << "video.border.height=" + std::to_string(options->video.border.height) + "\n";
// Opciones de notificaciones
file << "\n\n## NOTIFICATION\n";
file << "## notification.posV [pos_top | pos_bottom]\n";
file << "## notification.posH [pos_left | pos_middle | pos_right]\n";
file << "\n";
if (options->notification.posV == pos_top)
{
file << "notification.posV=pos_top\n";
}
else
{
file << "notification.posV=pos_bottom\n";
}
if (options->notification.posH == pos_left)
{
file << "notification.posH=pos_left\n";
}
else if (options->notification.posH == pos_middle)
{
file << "notification.posH=pos_middle\n";
}
else
{
file << "notification.posH=pos_right\n";
}
file << "notification.sound=" + boolToString(options->notification.sound) + "\n";
// Opciones de audio
file << "\n\n## AUDIO\n";
file << "## volume [0 .. 128]\n";
@@ -965,6 +1004,40 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
options->video.border.height = std::stoi(value);
}
// Opciones de notificaciones
else if (var == "notification.posH")
{
if (value == "pos_left")
{
options->notification.posH = pos_left;
}
else if (value == "pos_middle")
{
options->notification.posH = pos_middle;
}
else
{
options->notification.posH = pos_right;
}
}
else if (var == "notification.posV")
{
if (value == "pos_top")
{
options->notification.posV = pos_top;
}
else
{
options->notification.posV = pos_bottom;
}
}
else if (var == "notification.sound")
{
options->notification.sound = stringToBool(value);
}
// Opciones de audio
else if (var == "audio.music.enabled")
{