Demo: añadida la clase screen

This commit is contained in:
2023-05-07 12:09:52 +02:00
parent 8bcd3e6068
commit 3ba5f6cce0
7 changed files with 96 additions and 34 deletions

BIN
data/notify.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
data/notify.wav Normal file

Binary file not shown.

View File

@@ -13,6 +13,7 @@ Código fuente creado por JailDesigner
#include "units/asset.h" #include "units/asset.h"
#include "units/movingsprite.h" #include "units/movingsprite.h"
#include "units/texture.h" #include "units/texture.h"
#include "units/screen.h"
SDL_Event *event; SDL_Event *event;
SDL_Window *window; SDL_Window *window;
@@ -30,6 +31,7 @@ int main(int argc, char *argv[])
// Inicializa las opciones // Inicializa las opciones
struct options_t *options = new options_t; struct options_t *options = new options_t;
initOptions(options);
options->gameWidth = 640; options->gameWidth = 640;
options->gameHeight = 480; options->gameHeight = 480;
options->console = true; options->console = true;
@@ -41,6 +43,8 @@ int main(int argc, char *argv[])
asset->add("/data/smb2.txt", t_font); asset->add("/data/smb2.txt", t_font);
asset->add("/data/smb2.png", t_bitmap); asset->add("/data/smb2.png", t_bitmap);
asset->add("/data/z80.png", t_bitmap); asset->add("/data/z80.png", t_bitmap);
asset->add("/data/notify.png", t_bitmap);
asset->add("/data/notify.wav", t_sound);
asset->setVerbose(options->console); asset->setVerbose(options->console);
if (!asset->check()) if (!asset->check())
{ {
@@ -74,6 +78,10 @@ int main(int argc, char *argv[])
sound = JA_LoadSound(asset->get("sound.wav").c_str()); sound = JA_LoadSound(asset->get("sound.wav").c_str());
int volume = 128; int volume = 128;
// Inicializa el objeto screen
Screen *screen = new Screen(window, renderer, options);
screen->addNotifier(asset->get("notify.png"), asset->get("smb2.png"), asset->get("smb2.txt"), asset->get("notify.wav"));
// Inicializa el texto // Inicializa el texto
Text *text = new Text(asset->get("smb2.txt"), asset->get("smb2.png"), renderer); Text *text = new Text(asset->get("smb2.txt"), asset->get("smb2.png"), renderer);
@@ -160,7 +168,7 @@ int main(int argc, char *argv[])
sprite->update(); sprite->update();
// Actualiza el degradado // Actualiza el degradado
//if (counter % 4 == 0) // if (counter % 4 == 0)
{ {
gradBreathDirection == 0 ? gradCurrentColor-- : gradCurrentColor++; gradBreathDirection == 0 ? gradCurrentColor-- : gradCurrentColor++;
if (gradCurrentColor == gradColorMin) if (gradCurrentColor == gradColorMin)
@@ -175,8 +183,10 @@ int main(int argc, char *argv[])
} }
// Dibuja en pantalla // Dibuja en pantalla
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); //SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer); //SDL_RenderClear(renderer);
screen->start();
screen->clean();
// Dibuja un degradado de fondo // Dibuja un degradado de fondo
const int gradFirstLine = options->gameHeight / 3; const int gradFirstLine = options->gameHeight / 3;
const int gradLastLine = options->gameHeight; const int gradLastLine = options->gameHeight;
@@ -195,7 +205,8 @@ int main(int argc, char *argv[])
// Dibuja el sprite // Dibuja el sprite
sprite->render(); sprite->render();
// Vuelca el buffer en pantalla // Vuelca el buffer en pantalla
SDL_RenderPresent(renderer); //SDL_RenderPresent(renderer);
screen->blit();
} }
// Finaliza el sprite // Finaliza el sprite
@@ -205,6 +216,9 @@ int main(int argc, char *argv[])
// Finaliza el texto // Finaliza el texto
delete text; delete text;
// Finaliza el objeto screen
delete screen;
// Finaliza jail_audio // Finaliza jail_audio
JA_DeleteSound(sound); JA_DeleteSound(sound);
JA_DeleteMusic(music); JA_DeleteMusic(music);

View File

@@ -3,16 +3,12 @@
#include <iostream> #include <iostream>
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options) Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
{ {
// Inicializa variables // Inicializa variables
this->window = window; this->window = window;
this->renderer = renderer; this->renderer = renderer;
this->options = options; this->options = options;
this->asset = asset;
// Crea los objetos
notify = new Notify(renderer, asset->get("notify.png"), 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;
@@ -41,13 +37,17 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
setVideoMode(options->videoMode); setVideoMode(options->videoMode);
// Inicializa variables // Inicializa variables
notifyAdded = false;
notifyActive = false; notifyActive = false;
} }
// Destructor // Destructor
Screen::~Screen() Screen::~Screen()
{ {
delete notify; if (notify != nullptr)
{
delete notify;
}
SDL_DestroyTexture(gameCanvas); SDL_DestroyTexture(gameCanvas);
} }
@@ -97,7 +97,7 @@ void Screen::setVideoMode(int videoMode)
SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
// Esconde la ventana // Esconde la ventana
//SDL_HideWindow(window); // SDL_HideWindow(window);
if (options->borderEnabled) if (options->borderEnabled)
{ {
@@ -118,7 +118,7 @@ void Screen::setVideoMode(int videoMode)
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// Muestra la ventana // Muestra la ventana
//SDL_ShowWindow(window); // SDL_ShowWindow(window);
} }
// Si está activo el modo de pantalla completa añade el borde // Si está activo el modo de pantalla completa añade el borde
@@ -385,30 +385,46 @@ void Screen::renderFX()
renderSpectrumFade(); renderSpectrumFade();
} }
// Añade un notificador a la pantalla
void Screen::addNotifier(string iconsFile, string bitmapFontFile, string offsetFontFile, string soundFile)
{
notify = new Notify(renderer, iconsFile, bitmapFontFile, offsetFontFile, soundFile, options);
notifyAdded = true;
}
// Actualiza el notificador // Actualiza el notificador
void Screen::updateNotifier() void Screen::updateNotifier()
{ {
notify->update(); if (notifyAdded)
notifyActive = notify->active(); {
notify->update();
notifyActive = notify->active();
}
} }
// Muestra una notificación de texto por pantalla; // Muestra una notificación de texto por pantalla;
void Screen::showNotification(std::string text1, std::string text2, int icon) void Screen::showNotification(std::string text1, std::string text2, int icon)
{ {
notify->showText(text1, text2, icon); if (notifyAdded)
{
notify->showText(text1, text2, icon);
}
} }
// Dibuja las notificaciones // Dibuja las notificaciones
void Screen::renderNotifications() void Screen::renderNotifications()
{ {
if (!notifyActive) if (notifyAdded)
{ {
return; if (!notifyActive)
} {
return;
}
SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight); SDL_RenderSetLogicalSize(renderer, notificationLogicalWidth, notificationLogicalHeight);
notify->render(); notify->render();
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
}
} }
// Establece el tamaño de las notificaciones // Establece el tamaño de las notificaciones

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "asset.h"
#include "notify.h" #include "notify.h"
#include "utils.h" #include "utils.h"
#include <vector> #include <vector>
@@ -18,7 +17,6 @@ private:
// Objetos y punteros // Objetos y punteros
SDL_Window *window; // Ventana de la aplicación SDL_Window *window; // Ventana de la aplicación
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con el listado de recursos
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
options_t *options; // Variable con todas las opciones del programa options_t *options; // Variable con todas las opciones del programa
Notify *notify; // Dibuja notificaciones por pantalla Notify *notify; // Dibuja notificaciones por pantalla
@@ -32,6 +30,7 @@ private:
int borderHeight; // Anltura del borde int borderHeight; // Anltura del borde
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
bool notifyAdded; // Indica si se ha añadido un notificador
bool notifyActive; // Indica si hay notificaciones activas bool notifyActive; // Indica si hay notificaciones activas
int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla int notificationLogicalWidth; // Ancho lógico de las notificaciones en relación al tamaño de pantalla
int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla int notificationLogicalHeight; // Alto lógico de las notificaciones en relación al tamaño de pantalla
@@ -71,7 +70,7 @@ private:
public: public:
// Constructor // Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options_t *options); Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options);
// Destructor // Destructor
~Screen(); ~Screen();
@@ -134,6 +133,9 @@ public:
// Dibuja los efectos // Dibuja los efectos
void renderFX(); void renderFX();
// Añade un notificador a la pantalla
void addNotifier(string iconsFile, string bitmapFontFile, string offsetFontFile, string soundFile);
// Actualiza el notificador // Actualiza el notificador
void updateNotifier(); void updateNotifier();

View File

@@ -554,3 +554,30 @@ bool colorAreEqual(color_t color1, color_t color2)
return (r && g && b); return (r && g && b);
} }
// Inicializa la estructura de opciones
void initOptions(options_t *options)
{
options->configVersion = "";
options->videoMode = 0;
options->windowSize = 1;
options->filter = 0;
options->vSync = true;
options->gameWidth = 320;
options->gameHeight = 240;
options->integerScale = true;
options->keepAspect = true;
options->borderEnabled = false;
options->borderWidth = 0;
options->borderHeight = 0;
options->palette = p_zxspectrum;
options->console = false;
options->notifications.posV = pos_top;
options->notifications.posH = pos_left;
options->notifications.sound = true;
options->notifications.color = {48, 48, 48};
options->screen.windowWidth = options->gameWidth * options->windowSize;
options->screen.windowHeight = options->gameHeight * options->windowSize;
}

View File

@@ -102,20 +102,20 @@ struct cheat_t
// Estructura para el servicio online // Estructura para el servicio online
struct online_t struct online_t
{ {
bool enabled; // Indica si se quiere usar el modo online o no bool enabled; // Indica si se quiere usar el modo online o no
bool sessionEnabled; // Indica ya se ha hecho login bool sessionEnabled; // Indica ya se ha hecho login
string server; // Servidor para los servicios online string server; // Servidor para los servicios online
int port; // Puerto del servidor int port; // Puerto del servidor
string gameID; // Identificador del juego para los servicios online string gameID; // Identificador del juego para los servicios online
string jailerID; // Identificador del jugador para los servicios online string jailerID; // Identificador del jugador para los servicios online
int score; // Puntuación almacenada online int score; // Puntuación almacenada online
}; };
// Estructura para almacenar estadísticas // Estructura para almacenar estadísticas
struct op_stats_t struct op_stats_t
{ {
int rooms; // Cantidad de habitaciones visitadas int rooms; // Cantidad de habitaciones visitadas
int items; // Cantidad de items obtenidos int items; // Cantidad de items obtenidos
string worstNightmare; // Habitación con más muertes acumuladas string worstNightmare; // Habitación con más muertes acumuladas
}; };
@@ -129,7 +129,7 @@ struct op_screen_t
// 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
{ {
string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles 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
@@ -199,4 +199,7 @@ string boolToString(bool value);
// Compara dos colores // Compara dos colores
bool colorAreEqual(color_t color1, color_t color2); bool colorAreEqual(color_t color1, color_t color2);
// Inicializa la estructura de opciones
void initOptions(options_t *options);
#endif #endif