Singletonejada la classe Input

This commit is contained in:
2024-09-28 14:19:00 +02:00
parent fa82758ce1
commit 2767696a3f
19 changed files with 87 additions and 44 deletions

View File

@@ -4,10 +4,10 @@
#include "section.h"
// Constructor
DefineButtons::DefineButtons(Input *input, Text *text)
DefineButtons::DefineButtons(Text *text)
{
// Copia punteros a los objetos
this->input = input;
input = Input::get();
this->text = text;
// Inicializa variables

View File

@@ -43,7 +43,7 @@ private:
public:
// Constructor
DefineButtons(Input *input, Text *text);
DefineButtons(Text *text);
// Destructor
~DefineButtons();

View File

@@ -70,10 +70,11 @@ Director::Director(int argc, char *argv[])
// Crea los objetos
lang::loadFromFile(getLangFile((lang::lang_e)options.game.language));
input = new Input(asset->get("gamecontrollerdb.txt"));
Input::init(asset->get("gamecontrollerdb.txt"));
input = Input::get();
initInput();
Screen::init(window, renderer, input);
Screen::init(window, renderer);
screen = Screen::get();
// Carga los sonidos del juego
@@ -87,8 +88,8 @@ Director::~Director()
{
saveOptionsFile(asset->get("config.txt"));
delete input;
Asset::destroy();
Input::destroy();
Screen::destroy();
deleteSounds();
@@ -598,7 +599,7 @@ void Director::deleteMusics()
// Ejecuta la sección con el logo
void Director::runLogo()
{
logo = new Logo(input);
logo = new Logo();
logo->run();
delete logo;
}
@@ -606,7 +607,7 @@ void Director::runLogo()
// Ejecuta la sección con la secuencia de introducción
void Director::runIntro()
{
intro = new Intro(input, getMusic(musics, "intro.ogg"));
intro = new Intro(getMusic(musics, "intro.ogg"));
intro->run();
delete intro;
}
@@ -614,7 +615,7 @@ void Director::runIntro()
// Ejecuta la sección con el titulo del juego
void Director::runTitle()
{
title = new Title(input, getMusic(musics, "title.ogg"));
title = new Title(getMusic(musics, "title.ogg"));
title->run();
delete title;
}
@@ -624,7 +625,7 @@ void Director::runGame()
{
const int playerID = section::options == section::OPTIONS_GAME_PLAY_1P ? 1 : 2;
const int currentStage = 0;
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, input, getMusic(musics, "playing.ogg"));
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, getMusic(musics, "playing.ogg"));
game->run();
delete game;
}
@@ -632,7 +633,7 @@ void Director::runGame()
// Ejecuta la sección donde se muestran las instrucciones
void Director::runInstructions()
{
instructions = new Instructions(input, getMusic(musics, "title.ogg"));
instructions = new Instructions(getMusic(musics, "title.ogg"));
instructions->run();
delete instructions;
}
@@ -640,7 +641,7 @@ void Director::runInstructions()
// Ejecuta la sección donde se muestra la tabla de puntuaciones
void Director::runHiScoreTable()
{
hiScoreTable = new HiScoreTable(input, getMusic(musics, "title.ogg"));
hiScoreTable = new HiScoreTable(getMusic(musics, "title.ogg"));
hiScoreTable->run();
delete hiScoreTable;
}
@@ -650,7 +651,7 @@ void Director::runDemoGame()
{
const int playerID = (rand() % 2) + 1;
const int currentStage = 0;
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, input, nullptr);
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, nullptr);
demoGame->run();
delete demoGame;
}
@@ -693,7 +694,7 @@ int Director::run()
case section::NAME_INSTRUCTIONS:
runInstructions();
break;
default:
break;
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,27 @@
#include "input.h"
#include <iostream>
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Input *Input::input = nullptr;
// [SINGLETON] Crearemos el objeto input con esta función estática
void Input::init(std::string file)
{
Input::input = new Input(file);
}
// [SINGLETON] Destruiremos el objeto input con esta función estática
void Input::destroy()
{
delete Input::input;
}
// [SINGLETON] Con este método obtenemos el objeto input y podemos trabajar con él
Input *Input::get()
{
return Input::input;
}
// Constructor
Input::Input(std::string file)
{
@@ -47,6 +68,12 @@ Input::Input(std::string file)
buttonInputs.push_back(input_start);
}
// Destructor
Input::~Input()
{
}
// Actualiza el estado del objeto
void Input::update()
{

View File

@@ -63,6 +63,9 @@ enum i_disable_e
class Input
{
private:
// [SINGLETON] Objeto screen privado para Don Melitón
static Input *input;
struct keyBindings_t
{
Uint8 scancode; // Scancode asociado
@@ -93,10 +96,22 @@ private:
// Comprueba el eje del mando
bool checkAxisInput(inputs_e input, int index = 0);
public:
// Constructor
Input(std::string file);
// Destructor
~Input();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init(std::string file);
// [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 Input *get();
// Actualiza el estado del objeto
void update();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -13,9 +13,9 @@
Screen *Screen::screen = nullptr;
// [SINGLETON] Crearemos el objeto screen con esta función estática
void Screen::init(SDL_Window *window, SDL_Renderer *renderer, Input *input)
void Screen::init(SDL_Window *window, SDL_Renderer *renderer)
{
Screen::screen = new Screen(window, renderer, input);
Screen::screen = new Screen(window, renderer);
}
// [SINGLETON] Destruiremos el objeto screen con esta función estática
@@ -31,12 +31,12 @@ Screen *Screen::get()
}
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input)
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
{
// Copia punteros
this->window = window;
this->renderer = renderer;
this->input = input;
input = Input::get();
asset = Asset::get();
// Inicializa variables

View File

@@ -81,14 +81,14 @@ private:
// [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, Input *input);
Screen(SDL_Window *window, SDL_Renderer *renderer);
// Destructor
~Screen();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init(SDL_Window *window, SDL_Renderer *renderer, Input *input);
static void init(SDL_Window *window, SDL_Renderer *renderer);
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void destroy();

View File

@@ -3,11 +3,11 @@
#include "options.h"
// Constructor
Title::Title(Input *input, JA_Music_t *music)
Title::Title(JA_Music_t *music)
{
// Copia las direcciones de los punteros y objetos
this->input = input;
this->music = music;
input = Input::get();
asset = Asset::get();
screen = Screen::get();
SDL_Renderer *renderer = screen->getRenderer();
@@ -29,7 +29,7 @@ Title::Title(Input *input, JA_Music_t *music)
gameLogo = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition);
gameLogo->enable();
defineButtons = new DefineButtons(input, text2);
defineButtons = new DefineButtons(text2);
// Inicializa los valores
init();

View File

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