diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index b8c4c12..35eb09a 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -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 diff --git a/source/define_buttons.h b/source/define_buttons.h index 8930868..d34f59e 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -43,7 +43,7 @@ private: public: // Constructor - DefineButtons(Input *input, Text *text); + DefineButtons(Text *text); // Destructor ~DefineButtons(); diff --git a/source/director.cpp b/source/director.cpp index 2dc048f..d6665d0 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -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; } diff --git a/source/game.cpp b/source/game.cpp index 8d2660d..66c42f1 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -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 diff --git a/source/game.h b/source/game.h index b8222e1..9f19e62 100644 --- a/source/game.h +++ b/source/game.h @@ -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(); diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index e6db35f..2f71027 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -4,13 +4,13 @@ #include // 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 diff --git a/source/hiscore_table.h b/source/hiscore_table.h index e411706..317da82 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -79,7 +79,7 @@ private: public: // Constructor - HiScoreTable(Input *input, JA_Music_t *music); + HiScoreTable(JA_Music_t *music); // Destructor ~HiScoreTable(); diff --git a/source/input.cpp b/source/input.cpp index 275b980..adce7a2 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -1,6 +1,27 @@ #include "input.h" #include +// [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() { diff --git a/source/input.h b/source/input.h index 2747edc..9f1ae97 100644 --- a/source/input.h +++ b/source/input.h @@ -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(); diff --git a/source/instructions.cpp b/source/instructions.cpp index 2fc26bc..dce88a0 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -4,11 +4,11 @@ #include // 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(); diff --git a/source/instructions.h b/source/instructions.h index 0a2961b..da55336 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -86,7 +86,7 @@ private: public: // Constructor - Instructions(Input *input, JA_Music_t *music); + Instructions(JA_Music_t *music); // Destructor ~Instructions(); diff --git a/source/intro.cpp b/source/intro.cpp index 3ce6fba..3a7aa5a 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -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(); diff --git a/source/intro.h b/source/intro.h index 29ca6be..e465dae 100644 --- a/source/intro.h +++ b/source/intro.h @@ -60,7 +60,7 @@ private: public: // Constructor - Intro(Input *input, JA_Music_t *music); + Intro(JA_Music_t *music); // Destructor ~Intro(); diff --git a/source/logo.cpp b/source/logo.cpp index b223d6e..2bd237d 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -4,10 +4,10 @@ #include // 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(); diff --git a/source/logo.h b/source/logo.h index 59bf6e2..36d9526 100644 --- a/source/logo.h +++ b/source/logo.h @@ -71,7 +71,7 @@ private: public: // Constructor - Logo(Input *input); + Logo(); // Destructor ~Logo(); diff --git a/source/screen.cpp b/source/screen.cpp index 6384270..87fc2f0 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -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 diff --git a/source/screen.h b/source/screen.h index a8aa8fe..d0cd5e8 100644 --- a/source/screen.h +++ b/source/screen.h @@ -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(); diff --git a/source/title.cpp b/source/title.cpp index 5f43a5d..4c40142 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -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(); diff --git a/source/title.h b/source/title.h index 8128246..ef84080 100644 --- a/source/title.h +++ b/source/title.h @@ -101,7 +101,7 @@ private: public: // Constructor - Title(Input *input, JA_Music_t *music); + Title(JA_Music_t *music); // Destructor ~Title();