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"));
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;
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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;

View File

@@ -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();

View File

@@ -4,10 +4,10 @@
#include <iostream>
// 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;

View File

@@ -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();

View File

@@ -4,10 +4,10 @@
#include <iostream>
// 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

View File

@@ -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();

View File

@@ -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;

View File

@@ -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();

View File

@@ -4,10 +4,10 @@
#include <iostream>
// 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();

View File

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

View File

@@ -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)
{

View File

@@ -1,11 +1,11 @@
#pragma once
#include <SDL2/SDL.h>
#include "asset.h"
#include "utils.h"
#include "notify.h"
#include "input.h"
#include <vector>
#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();

View File

@@ -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

View File

@@ -1,9 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
#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();

View File

@@ -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);

View File

@@ -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();