From 92f7f540c00883c323bdc324ae814db1375714a8 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 28 Sep 2024 11:11:11 +0200 Subject: [PATCH] Classe Screen melitonada --- source/director.cpp | 19 ++++++++++--------- source/game.cpp | 4 ++-- source/game.h | 2 +- source/game_logo.cpp | 6 +++--- source/game_logo.h | 2 +- source/hiscore_table.cpp | 4 ++-- source/hiscore_table.h | 2 +- source/instructions.cpp | 6 +++--- source/instructions.h | 2 +- source/intro.cpp | 4 ++-- source/intro.h | 2 +- source/logo.cpp | 4 ++-- source/logo.h | 2 +- source/screen.cpp | 21 +++++++++++++++++++++ source/screen.h | 25 ++++++++++++++++++++----- source/tiledbg.cpp | 10 ++++++---- source/tiledbg.h | 19 ++++++++----------- source/title.cpp | 8 ++++---- source/title.h | 2 +- 19 files changed, 90 insertions(+), 54 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index f2ba9dd..b5267c2 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -72,7 +72,8 @@ Director::Director(int argc, char *argv[]) input = new Input(asset->get("gamecontrollerdb.txt")); initInput(); - screen = new Screen(window, renderer, asset, input); + Screen::Init(window, renderer, asset, input); + screen = Screen::get(); // Carga los sonidos del juego loadSounds(); @@ -87,7 +88,7 @@ Director::~Director() delete asset; delete input; - delete screen; + Screen::Destroy(); deleteSounds(); deleteMusics(); @@ -599,7 +600,7 @@ void Director::deleteMusics() // Ejecuta la sección con el logo void Director::runLogo() { - logo = new Logo(screen, asset, input); + logo = new Logo(asset, input); logo->run(); delete logo; } @@ -607,7 +608,7 @@ void Director::runLogo() // Ejecuta la sección con la secuencia de introducción void Director::runIntro() { - intro = new Intro(screen, asset, input, getMusic(musics, "intro.ogg")); + intro = new Intro(asset, input, getMusic(musics, "intro.ogg")); intro->run(); delete intro; } @@ -615,7 +616,7 @@ void Director::runIntro() // Ejecuta la sección con el titulo del juego void Director::runTitle() { - title = new Title(screen, asset, input, getMusic(musics, "title.ogg")); + title = new Title(asset, input, getMusic(musics, "title.ogg")); title->run(); delete title; } @@ -625,7 +626,7 @@ void Director::runGame() { const int playerID = section::options; 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(); delete game; } @@ -633,7 +634,7 @@ void Director::runGame() // Ejecuta la sección donde se muestran las instrucciones void Director::runInstructions() { - instructions = new Instructions(screen, asset, input, getMusic(musics, "title.ogg")); + instructions = new Instructions(asset, input, getMusic(musics, "title.ogg")); instructions->run(); delete instructions; } @@ -641,7 +642,7 @@ void Director::runInstructions() // Ejecuta la sección donde se muestra la tabla de puntuaciones void Director::runHiScoreTable() { - hiScoreTable = new HiScoreTable(screen, asset, input, getMusic(musics, "title.ogg")); + hiScoreTable = new HiScoreTable(asset, input, getMusic(musics, "title.ogg")); hiScoreTable->run(); delete hiScoreTable; } @@ -651,7 +652,7 @@ void Director::runDemoGame() { const int playerID = (rand() % 2) + 1; 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(); delete demoGame; } diff --git a/source/game.cpp b/source/game.cpp index d6fad5d..8d7a49b 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -5,10 +5,10 @@ #define GAME_OVER_COUNTER 350 // 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 - this->screen = screen; + screen = Screen::get(); this->asset = asset; this->input = input; this->music = music; diff --git a/source/game.h b/source/game.h index aaaae20..eb9e8b6 100644 --- a/source/game.h +++ b/source/game.h @@ -447,7 +447,7 @@ private: public: // 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 ~Game(); diff --git a/source/game_logo.cpp b/source/game_logo.cpp index 4a095db..10487a5 100644 --- a/source/game_logo.cpp +++ b/source/game_logo.cpp @@ -2,11 +2,11 @@ #include "param.h" // 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 - this->renderer = renderer; - this->screen = screen; + screen = Screen::get(); + renderer = screen->getRenderer(); this->asset = asset; this->x = x; this->y = y; diff --git a/source/game_logo.h b/source/game_logo.h index 6274f31..e444fb8 100644 --- a/source/game_logo.h +++ b/source/game_logo.h @@ -60,7 +60,7 @@ private: public: // Constructor - GameLogo(SDL_Renderer *renderer, Screen *screen, Asset *asset, int x, int y); + GameLogo(Asset *asset, int x, int y); // Destructor ~GameLogo(); diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index 08218df..8b3684a 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -4,10 +4,10 @@ #include // 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 - this->screen = screen; + screen = Screen::get(); this->asset = asset; this->input = input; this->music = music; diff --git a/source/hiscore_table.h b/source/hiscore_table.h index 1167698..2d17005 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -79,7 +79,7 @@ private: public: // Constructor - HiScoreTable(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); + HiScoreTable(Asset *asset, Input *input, JA_Music_t *music); // Destructor ~HiScoreTable(); diff --git a/source/instructions.cpp b/source/instructions.cpp index 341ec5f..9a545ce 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -4,10 +4,10 @@ #include // 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 - this->screen = screen; + screen = Screen::get(); this->asset = asset; this->input = input; this->music = music; @@ -16,7 +16,7 @@ Instructions::Instructions(Screen *screen, Asset *asset, Input *input, JA_Music_ // Crea objetos eventHandler = new SDL_Event(); 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); // Crea un backbuffer para el renderizador diff --git a/source/instructions.h b/source/instructions.h index 4702f70..f30c37c 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -86,7 +86,7 @@ private: public: // Constructor - Instructions(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); + Instructions(Asset *asset, Input *input, JA_Music_t *music); // Destructor ~Instructions(); diff --git a/source/intro.cpp b/source/intro.cpp index 8722028..ba853f0 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -3,10 +3,10 @@ #include "options.h" // 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 - this->screen = screen; + screen = Screen::get(); this->asset = asset; this->input = input; this->music = music; diff --git a/source/intro.h b/source/intro.h index 51f6b48..ffd381c 100644 --- a/source/intro.h +++ b/source/intro.h @@ -60,7 +60,7 @@ private: public: // Constructor - Intro(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); + Intro(Asset *asset, Input *input, JA_Music_t *music); // Destructor ~Intro(); diff --git a/source/logo.cpp b/source/logo.cpp index 32e0007..788d1b4 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -4,10 +4,10 @@ #include // Constructor -Logo::Logo(Screen *screen, Asset *asset, Input *input) +Logo::Logo(Asset *asset, Input *input) { // Copia la dirección de los objetos - this->screen = screen; + screen = Screen::get(); this->asset = asset; this->input = input; SDL_Renderer *renderer = screen->getRenderer(); diff --git a/source/logo.h b/source/logo.h index 6222292..966214b 100644 --- a/source/logo.h +++ b/source/logo.h @@ -71,7 +71,7 @@ private: public: // Constructor - Logo(Screen *screen, Asset *asset, Input *input); + Logo(Asset *asset, Input *input); // Destructor ~Logo(); diff --git a/source/screen.cpp b/source/screen.cpp index 1c08701..b9de282 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -9,6 +9,27 @@ #endif #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 Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input) { diff --git a/source/screen.h b/source/screen.h index db250fb..092a363 100644 --- a/source/screen.h +++ b/source/screen.h @@ -1,11 +1,11 @@ #pragma once #include -#include "asset.h" -#include "utils.h" -#include "notify.h" -#include "input.h" #include +#include "asset.h" +#include "input.h" +#include "notify.h" +#include "utils.h" #define SCREEN_FILTER_NEAREST 0 #define SCREEN_FILTER_LINEAL 1 @@ -16,6 +16,9 @@ class Screen { private: + // [SINGLETON] Objeto screen privado para Don Melitón + static Screen *screen; + // Objetos y punteros SDL_Window *window; // Ventana de la aplicación SDL_Renderer *renderer; // El renderizador de la ventana @@ -74,13 +77,25 @@ private: // Muestra información por pantalla void displayInfo(); -public: + + // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera + // Constructor Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input); // Destructor ~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 void update(); diff --git a/source/tiledbg.cpp b/source/tiledbg.cpp index 0c6f73f..c979571 100644 --- a/source/tiledbg.cpp +++ b/source/tiledbg.cpp @@ -1,11 +1,13 @@ +#include "screen.h" +#include "sprite.h" #include "tiledbg.h" // 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 - this->renderer = renderer; - this->asset = asset; + renderer = Screen::get()->getRenderer(); + this->texturePath = texturePath; this->pos = pos; this->mode = mode; @@ -54,7 +56,7 @@ void Tiledbg::init() void Tiledbg::fillTexture() { // 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); // Prepara para dibujar sobre la textura diff --git a/source/tiledbg.h b/source/tiledbg.h index 3f854cc..decb225 100644 --- a/source/tiledbg.h +++ b/source/tiledbg.h @@ -1,9 +1,6 @@ #pragma once #include -#include "asset.h" -#include "screen.h" -#include "sprite.h" // Modos de funcionamiento para el tileado de fondo #define TILED_MODE_CIRCLE 0 @@ -23,17 +20,17 @@ class Tiledbg private: // Objetos y punteros 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_Texture *canvas; // Textura donde dibujar el fondo formado por tiles // Variables - SDL_Rect pos; // Posición y tamaña del mosaico - int counter; // Contador - int mode; // Tipo de movimiento del mosaico - float sin[360]; // Vector con los valores del seno precalculados - int tileWidth; // Ancho del tile - int tileHeight; // Alto del tile + std::string texturePath; // Fichero para usar en la textura + SDL_Rect pos; // Posición y tamaña del mosaico + int counter; // Contador + int mode; // Tipo de movimiento del mosaico + float sin[360]; // Vector con los valores del seno precalculados + int tileWidth; // Ancho del tile + int tileHeight; // Alto del tile // Inicializa las variables void init(); @@ -43,7 +40,7 @@ private: public: // Constructor - Tiledbg(SDL_Renderer *renderer, Asset *asset, SDL_Rect pos, int mode); + Tiledbg(std::string texturePath, SDL_Rect pos, int mode); // Destructor ~Tiledbg(); diff --git a/source/title.cpp b/source/title.cpp index 255952f..896774a 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -3,13 +3,13 @@ #include "options.h" // 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 - this->screen = screen; this->input = input; this->asset = asset; this->music = music; + screen = Screen::get(); SDL_Renderer *renderer = screen->getRenderer(); // 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")); 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(); defineButtons = new DefineButtons(input, text2); diff --git a/source/title.h b/source/title.h index dc593f7..a2fc574 100644 --- a/source/title.h +++ b/source/title.h @@ -101,7 +101,7 @@ private: public: // Constructor - Title(Screen *screen, Asset *asset, Input *input, JA_Music_t *music); + Title(Asset *asset, Input *input, JA_Music_t *music); // Destructor ~Title();