Classe Screen melitonada

This commit is contained in:
2024-09-28 11:11:11 +02:00
parent f2cc0dc352
commit 92f7f540c0
19 changed files with 90 additions and 54 deletions

View File

@@ -72,7 +72,8 @@ Director::Director(int argc, char *argv[])
input = new Input(asset->get("gamecontrollerdb.txt")); input = new Input(asset->get("gamecontrollerdb.txt"));
initInput(); initInput();
screen = new Screen(window, renderer, asset, input); Screen::Init(window, renderer, asset, input);
screen = Screen::get();
// Carga los sonidos del juego // Carga los sonidos del juego
loadSounds(); loadSounds();
@@ -87,7 +88,7 @@ Director::~Director()
delete asset; delete asset;
delete input; delete input;
delete screen; Screen::Destroy();
deleteSounds(); deleteSounds();
deleteMusics(); deleteMusics();
@@ -599,7 +600,7 @@ void Director::deleteMusics()
// Ejecuta la sección con el logo // Ejecuta la sección con el logo
void Director::runLogo() void Director::runLogo()
{ {
logo = new Logo(screen, asset, input); logo = new Logo(asset, input);
logo->run(); logo->run();
delete logo; delete logo;
} }
@@ -607,7 +608,7 @@ void Director::runLogo()
// Ejecuta la sección con la secuencia de introducción // Ejecuta la sección con la secuencia de introducción
void Director::runIntro() void Director::runIntro()
{ {
intro = new Intro(screen, asset, input, getMusic(musics, "intro.ogg")); intro = new Intro(asset, input, getMusic(musics, "intro.ogg"));
intro->run(); intro->run();
delete intro; delete intro;
} }
@@ -615,7 +616,7 @@ void Director::runIntro()
// Ejecuta la sección con el titulo del juego // Ejecuta la sección con el titulo del juego
void Director::runTitle() void Director::runTitle()
{ {
title = new Title(screen, asset, input, getMusic(musics, "title.ogg")); title = new Title(asset, input, getMusic(musics, "title.ogg"));
title->run(); title->run();
delete title; delete title;
} }
@@ -625,7 +626,7 @@ void Director::runGame()
{ {
const int playerID = section::options; const int playerID = section::options;
const int currentStage = 0; const int currentStage = 0;
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, screen, asset, input, getMusic(musics, "playing.ogg")); game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, asset, input, getMusic(musics, "playing.ogg"));
game->run(); game->run();
delete game; delete game;
} }
@@ -633,7 +634,7 @@ void Director::runGame()
// Ejecuta la sección donde se muestran las instrucciones // Ejecuta la sección donde se muestran las instrucciones
void Director::runInstructions() void Director::runInstructions()
{ {
instructions = new Instructions(screen, asset, input, getMusic(musics, "title.ogg")); instructions = new Instructions(asset, input, getMusic(musics, "title.ogg"));
instructions->run(); instructions->run();
delete instructions; delete instructions;
} }
@@ -641,7 +642,7 @@ void Director::runInstructions()
// Ejecuta la sección donde se muestra la tabla de puntuaciones // Ejecuta la sección donde se muestra la tabla de puntuaciones
void Director::runHiScoreTable() void Director::runHiScoreTable()
{ {
hiScoreTable = new HiScoreTable(screen, asset, input, getMusic(musics, "title.ogg")); hiScoreTable = new HiScoreTable(asset, input, getMusic(musics, "title.ogg"));
hiScoreTable->run(); hiScoreTable->run();
delete hiScoreTable; delete hiScoreTable;
} }
@@ -651,7 +652,7 @@ void Director::runDemoGame()
{ {
const int playerID = (rand() % 2) + 1; const int playerID = (rand() % 2) + 1;
const int currentStage = 0; const int currentStage = 0;
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, screen, asset, input, nullptr); demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, asset, input, nullptr);
demoGame->run(); demoGame->run();
delete demoGame; delete demoGame;
} }

View File

@@ -5,10 +5,10 @@
#define GAME_OVER_COUNTER 350 #define GAME_OVER_COUNTER 350
// Constructor // Constructor
Game::Game(int playerID, int currentStage, bool demo, Screen *screen, Asset *asset, Input *input, JA_Music_t *music) Game::Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music)
{ {
// Copia los punteros // Copia los punteros
this->screen = screen; screen = Screen::get();
this->asset = asset; this->asset = asset;
this->input = input; this->input = input;
this->music = music; this->music = music;

View File

@@ -447,7 +447,7 @@ private:
public: public:
// Constructor // Constructor
Game(int playerID, int currentStage, bool demo, Screen *screen, Asset *asset, Input *input, JA_Music_t *music); Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music);
// Destructor // Destructor
~Game(); ~Game();

View File

@@ -2,11 +2,11 @@
#include "param.h" #include "param.h"
// Constructor // Constructor
GameLogo::GameLogo(SDL_Renderer *renderer, Screen *screen, Asset *asset, int x, int y) GameLogo::GameLogo(Asset *asset, int x, int y)
{ {
// Copia los punteros // Copia los punteros
this->renderer = renderer; screen = Screen::get();
this->screen = screen; renderer = screen->getRenderer();
this->asset = asset; this->asset = asset;
this->x = x; this->x = x;
this->y = y; this->y = y;

View File

@@ -60,7 +60,7 @@ private:
public: public:
// Constructor // Constructor
GameLogo(SDL_Renderer *renderer, Screen *screen, Asset *asset, int x, int y); GameLogo(Asset *asset, int x, int y);
// Destructor // Destructor
~GameLogo(); ~GameLogo();

View File

@@ -4,10 +4,10 @@
#include <iostream> #include <iostream>
// Constructor // Constructor
HiScoreTable::HiScoreTable(Screen *screen, Asset *asset, Input *input, JA_Music_t *music) HiScoreTable::HiScoreTable(Asset *asset, Input *input, JA_Music_t *music)
{ {
// Copia punteros // Copia punteros
this->screen = screen; screen = Screen::get();
this->asset = asset; this->asset = asset;
this->input = input; this->input = input;
this->music = music; this->music = music;

View File

@@ -79,7 +79,7 @@ private:
public: public:
// Constructor // Constructor
HiScoreTable(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); HiScoreTable(Asset *asset, Input *input, JA_Music_t *music);
// Destructor // Destructor
~HiScoreTable(); ~HiScoreTable();

View File

@@ -4,10 +4,10 @@
#include <iostream> #include <iostream>
// Constructor // Constructor
Instructions::Instructions(Screen *screen, Asset *asset, Input *input, JA_Music_t *music) Instructions::Instructions(Asset *asset, Input *input, JA_Music_t *music)
{ {
// Copia los punteros // Copia los punteros
this->screen = screen; screen = Screen::get();
this->asset = asset; this->asset = asset;
this->input = input; this->input = input;
this->music = music; this->music = music;
@@ -16,7 +16,7 @@ Instructions::Instructions(Screen *screen, Asset *asset, Input *input, JA_Music_
// Crea objetos // Crea objetos
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
text = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); text = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer);
tiledbg = new Tiledbg(renderer, asset, {0, 0, param.game.width, param.game.height}, TILED_MODE_STATIC); tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_STATIC);
fade = new Fade(renderer); fade = new Fade(renderer);
// Crea un backbuffer para el renderizador // Crea un backbuffer para el renderizador

View File

@@ -86,7 +86,7 @@ private:
public: public:
// Constructor // Constructor
Instructions(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); Instructions(Asset *asset, Input *input, JA_Music_t *music);
// Destructor // Destructor
~Instructions(); ~Instructions();

View File

@@ -3,10 +3,10 @@
#include "options.h" #include "options.h"
// Constructor // Constructor
Intro::Intro(Screen *screen, Asset *asset, Input *input, JA_Music_t *music) Intro::Intro(Asset *asset, Input *input, JA_Music_t *music)
{ {
// Copia los punteros // Copia los punteros
this->screen = screen; screen = Screen::get();
this->asset = asset; this->asset = asset;
this->input = input; this->input = input;
this->music = music; this->music = music;

View File

@@ -60,7 +60,7 @@ private:
public: public:
// Constructor // Constructor
Intro(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); Intro(Asset *asset, Input *input, JA_Music_t *music);
// Destructor // Destructor
~Intro(); ~Intro();

View File

@@ -4,10 +4,10 @@
#include <iostream> #include <iostream>
// Constructor // Constructor
Logo::Logo(Screen *screen, Asset *asset, Input *input) Logo::Logo(Asset *asset, Input *input)
{ {
// Copia la dirección de los objetos // Copia la dirección de los objetos
this->screen = screen; screen = Screen::get();
this->asset = asset; this->asset = asset;
this->input = input; this->input = input;
SDL_Renderer *renderer = screen->getRenderer(); SDL_Renderer *renderer = screen->getRenderer();

View File

@@ -71,7 +71,7 @@ private:
public: public:
// Constructor // Constructor
Logo(Screen *screen, Asset *asset, Input *input); Logo(Asset *asset, Input *input);
// Destructor // Destructor
~Logo(); ~Logo();

View File

@@ -9,6 +9,27 @@
#endif #endif
#include "dbgtxt.h" #include "dbgtxt.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Screen *Screen::screen = nullptr;
// [SINGLETON] Crearemos el objeto screen con esta función estática
void Screen::Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input)
{
Screen::screen = new Screen(window, renderer, asset, input);
}
// [SINGLETON] Destruiremos el objeto screen con esta función estática
void Screen::Destroy()
{
delete Screen::screen;
}
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
Screen *Screen::get()
{
return Screen::screen;
}
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input) Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input)
{ {

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "asset.h"
#include "utils.h"
#include "notify.h"
#include "input.h"
#include <vector> #include <vector>
#include "asset.h"
#include "input.h"
#include "notify.h"
#include "utils.h"
#define SCREEN_FILTER_NEAREST 0 #define SCREEN_FILTER_NEAREST 0
#define SCREEN_FILTER_LINEAL 1 #define SCREEN_FILTER_LINEAL 1
@@ -16,6 +16,9 @@
class Screen class Screen
{ {
private: private:
// [SINGLETON] Objeto screen privado para Don Melitón
static Screen *screen;
// 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
@@ -74,13 +77,25 @@ private:
// Muestra información por pantalla // Muestra información por pantalla
void displayInfo(); void displayInfo();
public:
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
// Constructor // Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input); Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input);
// Destructor // Destructor
~Screen(); ~Screen();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input);
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void Destroy();
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static Screen *get();
// Actualiza la lógica de la clase // Actualiza la lógica de la clase
void update(); void update();

View File

@@ -1,11 +1,13 @@
#include "screen.h"
#include "sprite.h"
#include "tiledbg.h" #include "tiledbg.h"
// Constructor // Constructor
Tiledbg::Tiledbg(SDL_Renderer *renderer, Asset *asset, SDL_Rect pos, int mode) Tiledbg::Tiledbg(std::string texturePath, SDL_Rect pos, int mode)
{ {
// Copia los punteros // Copia los punteros
this->renderer = renderer; renderer = Screen::get()->getRenderer();
this->asset = asset; this->texturePath = texturePath;
this->pos = pos; this->pos = pos;
this->mode = mode; this->mode = mode;
@@ -54,7 +56,7 @@ void Tiledbg::init()
void Tiledbg::fillTexture() void Tiledbg::fillTexture()
{ {
// Crea los objetos para pintar en la textura de fondo // Crea los objetos para pintar en la textura de fondo
Texture *bgTileTexture = new Texture(renderer, asset->get("title_bg_tile.png")); Texture *bgTileTexture = new Texture(renderer, texturePath);
Sprite *tile = new Sprite({0, 0, tileWidth, tileHeight}, bgTileTexture); Sprite *tile = new Sprite({0, 0, tileWidth, tileHeight}, bgTileTexture);
// Prepara para dibujar sobre la textura // Prepara para dibujar sobre la textura

View File

@@ -1,9 +1,6 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "asset.h"
#include "screen.h"
#include "sprite.h"
// Modos de funcionamiento para el tileado de fondo // Modos de funcionamiento para el tileado de fondo
#define TILED_MODE_CIRCLE 0 #define TILED_MODE_CIRCLE 0
@@ -23,17 +20,17 @@ class Tiledbg
private: private:
// Objetos y punteros // Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
SDL_Rect window; // Ventana visible para la textura de fondo del titulo SDL_Rect window; // Ventana visible para la textura de fondo del titulo
SDL_Texture *canvas; // Textura donde dibujar el fondo formado por tiles SDL_Texture *canvas; // Textura donde dibujar el fondo formado por tiles
// Variables // Variables
SDL_Rect pos; // Posición y tamaña del mosaico std::string texturePath; // Fichero para usar en la textura
int counter; // Contador SDL_Rect pos; // Posición y tamaña del mosaico
int mode; // Tipo de movimiento del mosaico int counter; // Contador
float sin[360]; // Vector con los valores del seno precalculados int mode; // Tipo de movimiento del mosaico
int tileWidth; // Ancho del tile float sin[360]; // Vector con los valores del seno precalculados
int tileHeight; // Alto del tile int tileWidth; // Ancho del tile
int tileHeight; // Alto del tile
// Inicializa las variables // Inicializa las variables
void init(); void init();
@@ -43,7 +40,7 @@ private:
public: public:
// Constructor // Constructor
Tiledbg(SDL_Renderer *renderer, Asset *asset, SDL_Rect pos, int mode); Tiledbg(std::string texturePath, SDL_Rect pos, int mode);
// Destructor // Destructor
~Tiledbg(); ~Tiledbg();

View File

@@ -3,13 +3,13 @@
#include "options.h" #include "options.h"
// Constructor // Constructor
Title::Title(Screen *screen, Asset *asset, Input *input, JA_Music_t *music) Title::Title(Asset *asset, Input *input, JA_Music_t *music)
{ {
// Copia las direcciones de los punteros y objetos // Copia las direcciones de los punteros y objetos
this->screen = screen;
this->input = input; this->input = input;
this->asset = asset; this->asset = asset;
this->music = music; this->music = music;
screen = Screen::get();
SDL_Renderer *renderer = screen->getRenderer(); SDL_Renderer *renderer = screen->getRenderer();
// Reserva memoria y crea los objetos // Reserva memoria y crea los objetos
@@ -24,9 +24,9 @@ Title::Title(Screen *screen, Asset *asset, Input *input, JA_Music_t *music)
miniLogoTexture = new Texture(renderer, asset->get("logo_jailgames_mini.png")); miniLogoTexture = new Texture(renderer, asset->get("logo_jailgames_mini.png"));
miniLogoSprite = new Sprite(param.game.gameArea.centerX - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture); miniLogoSprite = new Sprite(param.game.gameArea.centerX - miniLogoTexture->getWidth() / 2, 0, miniLogoTexture->getWidth(), miniLogoTexture->getHeight(), miniLogoTexture);
tiledbg = new Tiledbg(renderer, asset, {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM); tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM);
gameLogo = new GameLogo(renderer, screen, asset, param.game.gameArea.centerX, param.title.titleCCPosition); gameLogo = new GameLogo(asset, param.game.gameArea.centerX, param.title.titleCCPosition);
gameLogo->enable(); gameLogo->enable();
defineButtons = new DefineButtons(input, text2); defineButtons = new DefineButtons(input, text2);

View File

@@ -101,7 +101,7 @@ private:
public: public:
// Constructor // Constructor
Title(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); Title(Asset *asset, Input *input, JA_Music_t *music);
// Destructor // Destructor
~Title(); ~Title();