Singletonejada la classe Input
This commit is contained in:
@@ -4,10 +4,10 @@
|
|||||||
#include "section.h"
|
#include "section.h"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
DefineButtons::DefineButtons(Input *input, Text *text)
|
DefineButtons::DefineButtons(Text *text)
|
||||||
{
|
{
|
||||||
// Copia punteros a los objetos
|
// Copia punteros a los objetos
|
||||||
this->input = input;
|
input = Input::get();
|
||||||
this->text = text;
|
this->text = text;
|
||||||
|
|
||||||
// Inicializa variables
|
// Inicializa variables
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
DefineButtons(Input *input, Text *text);
|
DefineButtons(Text *text);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~DefineButtons();
|
~DefineButtons();
|
||||||
|
|||||||
@@ -70,10 +70,11 @@ Director::Director(int argc, char *argv[])
|
|||||||
// Crea los objetos
|
// Crea los objetos
|
||||||
lang::loadFromFile(getLangFile((lang::lang_e)options.game.language));
|
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();
|
initInput();
|
||||||
|
|
||||||
Screen::init(window, renderer, input);
|
Screen::init(window, renderer);
|
||||||
screen = Screen::get();
|
screen = Screen::get();
|
||||||
|
|
||||||
// Carga los sonidos del juego
|
// Carga los sonidos del juego
|
||||||
@@ -87,8 +88,8 @@ Director::~Director()
|
|||||||
{
|
{
|
||||||
saveOptionsFile(asset->get("config.txt"));
|
saveOptionsFile(asset->get("config.txt"));
|
||||||
|
|
||||||
delete input;
|
|
||||||
Asset::destroy();
|
Asset::destroy();
|
||||||
|
Input::destroy();
|
||||||
Screen::destroy();
|
Screen::destroy();
|
||||||
|
|
||||||
deleteSounds();
|
deleteSounds();
|
||||||
@@ -598,7 +599,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(input);
|
logo = new Logo();
|
||||||
logo->run();
|
logo->run();
|
||||||
delete logo;
|
delete logo;
|
||||||
}
|
}
|
||||||
@@ -606,7 +607,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(input, getMusic(musics, "intro.ogg"));
|
intro = new Intro(getMusic(musics, "intro.ogg"));
|
||||||
intro->run();
|
intro->run();
|
||||||
delete intro;
|
delete intro;
|
||||||
}
|
}
|
||||||
@@ -614,7 +615,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(input, getMusic(musics, "title.ogg"));
|
title = new Title(getMusic(musics, "title.ogg"));
|
||||||
title->run();
|
title->run();
|
||||||
delete title;
|
delete title;
|
||||||
}
|
}
|
||||||
@@ -624,7 +625,7 @@ void Director::runGame()
|
|||||||
{
|
{
|
||||||
const int playerID = section::options == section::OPTIONS_GAME_PLAY_1P ? 1 : 2;
|
const int playerID = section::options == section::OPTIONS_GAME_PLAY_1P ? 1 : 2;
|
||||||
const int currentStage = 0;
|
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();
|
game->run();
|
||||||
delete game;
|
delete game;
|
||||||
}
|
}
|
||||||
@@ -632,7 +633,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(input, getMusic(musics, "title.ogg"));
|
instructions = new Instructions(getMusic(musics, "title.ogg"));
|
||||||
instructions->run();
|
instructions->run();
|
||||||
delete instructions;
|
delete instructions;
|
||||||
}
|
}
|
||||||
@@ -640,7 +641,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(input, getMusic(musics, "title.ogg"));
|
hiScoreTable = new HiScoreTable(getMusic(musics, "title.ogg"));
|
||||||
hiScoreTable->run();
|
hiScoreTable->run();
|
||||||
delete hiScoreTable;
|
delete hiScoreTable;
|
||||||
}
|
}
|
||||||
@@ -650,7 +651,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, input, nullptr);
|
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, nullptr);
|
||||||
demoGame->run();
|
demoGame->run();
|
||||||
delete demoGame;
|
delete demoGame;
|
||||||
}
|
}
|
||||||
@@ -693,7 +694,7 @@ int Director::run()
|
|||||||
case section::NAME_INSTRUCTIONS:
|
case section::NAME_INSTRUCTIONS:
|
||||||
runInstructions();
|
runInstructions();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,13 @@
|
|||||||
#define GAME_OVER_COUNTER 350
|
#define GAME_OVER_COUNTER 350
|
||||||
|
|
||||||
// Constructor
|
// 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
|
// Copia los punteros
|
||||||
screen = Screen::get();
|
|
||||||
asset = Asset::get();
|
|
||||||
this->input = input;
|
|
||||||
this->music = music;
|
this->music = music;
|
||||||
|
asset = Asset::get();
|
||||||
|
input = Input::get();
|
||||||
|
screen = Screen::get();
|
||||||
renderer = screen->getRenderer();
|
renderer = screen->getRenderer();
|
||||||
|
|
||||||
// Pasa variables
|
// Pasa variables
|
||||||
|
|||||||
@@ -447,7 +447,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// 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
|
// Destructor
|
||||||
~Game();
|
~Game();
|
||||||
|
|||||||
@@ -4,13 +4,13 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
HiScoreTable::HiScoreTable(Input *input, JA_Music_t *music)
|
HiScoreTable::HiScoreTable(JA_Music_t *music)
|
||||||
{
|
{
|
||||||
// Copia punteros
|
// Copia punteros
|
||||||
screen = Screen::get();
|
|
||||||
asset = Asset::get();
|
|
||||||
this->input = input;
|
|
||||||
this->music = music;
|
this->music = music;
|
||||||
|
asset = Asset::get();
|
||||||
|
input = Input::get();
|
||||||
|
screen = Screen::get();
|
||||||
renderer = screen->getRenderer();
|
renderer = screen->getRenderer();
|
||||||
|
|
||||||
// Objetos
|
// Objetos
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
HiScoreTable(Input *input, JA_Music_t *music);
|
HiScoreTable(JA_Music_t *music);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~HiScoreTable();
|
~HiScoreTable();
|
||||||
|
|||||||
@@ -1,6 +1,27 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <iostream>
|
#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
|
// Constructor
|
||||||
Input::Input(std::string file)
|
Input::Input(std::string file)
|
||||||
{
|
{
|
||||||
@@ -47,6 +68,12 @@ Input::Input(std::string file)
|
|||||||
buttonInputs.push_back(input_start);
|
buttonInputs.push_back(input_start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Input::~Input()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Actualiza el estado del objeto
|
// Actualiza el estado del objeto
|
||||||
void Input::update()
|
void Input::update()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -63,6 +63,9 @@ enum i_disable_e
|
|||||||
class Input
|
class Input
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
// [SINGLETON] Objeto screen privado para Don Melitón
|
||||||
|
static Input *input;
|
||||||
|
|
||||||
struct keyBindings_t
|
struct keyBindings_t
|
||||||
{
|
{
|
||||||
Uint8 scancode; // Scancode asociado
|
Uint8 scancode; // Scancode asociado
|
||||||
@@ -93,10 +96,22 @@ private:
|
|||||||
// Comprueba el eje del mando
|
// Comprueba el eje del mando
|
||||||
bool checkAxisInput(inputs_e input, int index = 0);
|
bool checkAxisInput(inputs_e input, int index = 0);
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Input(std::string file);
|
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
|
// Actualiza el estado del objeto
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Instructions::Instructions(Input *input, JA_Music_t *music)
|
Instructions::Instructions(JA_Music_t *music)
|
||||||
{
|
{
|
||||||
// Copia los punteros
|
// Copia los punteros
|
||||||
this->input = input;
|
|
||||||
this->music = music;
|
this->music = music;
|
||||||
|
input = Input::get();
|
||||||
screen = Screen::get();
|
screen = Screen::get();
|
||||||
asset = Asset::get();
|
asset = Asset::get();
|
||||||
renderer = screen->getRenderer();
|
renderer = screen->getRenderer();
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Instructions(Input *input, JA_Music_t *music);
|
Instructions(JA_Music_t *music);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Instructions();
|
~Instructions();
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Intro::Intro(Input *input, JA_Music_t *music)
|
Intro::Intro(JA_Music_t *music)
|
||||||
{
|
{
|
||||||
// Copia los punteros
|
// Copia los punteros
|
||||||
this->input = input;
|
|
||||||
this->music = music;
|
this->music = music;
|
||||||
|
input = Input::get();
|
||||||
asset = Asset::get();
|
asset = Asset::get();
|
||||||
screen = Screen::get();
|
screen = Screen::get();
|
||||||
SDL_Renderer *renderer = screen->getRenderer();
|
SDL_Renderer *renderer = screen->getRenderer();
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Intro(Input *input, JA_Music_t *music);
|
Intro(JA_Music_t *music);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Intro();
|
~Intro();
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Logo::Logo(Input *input)
|
Logo::Logo()
|
||||||
{
|
{
|
||||||
// Copia la dirección de los objetos
|
// Copia la dirección de los objetos
|
||||||
this->input = input;
|
input = Input::get();
|
||||||
screen = Screen::get();
|
screen = Screen::get();
|
||||||
asset = Asset::get();
|
asset = Asset::get();
|
||||||
SDL_Renderer *renderer = screen->getRenderer();
|
SDL_Renderer *renderer = screen->getRenderer();
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Logo(Input *input);
|
Logo();
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Logo();
|
~Logo();
|
||||||
|
|||||||
@@ -13,9 +13,9 @@
|
|||||||
Screen *Screen::screen = nullptr;
|
Screen *Screen::screen = nullptr;
|
||||||
|
|
||||||
// [SINGLETON] Crearemos el objeto screen con esta función estática
|
// [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
|
// [SINGLETON] Destruiremos el objeto screen con esta función estática
|
||||||
@@ -31,12 +31,12 @@ Screen *Screen::get()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input)
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
// Copia punteros
|
// Copia punteros
|
||||||
this->window = window;
|
this->window = window;
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
this->input = input;
|
input = Input::get();
|
||||||
asset = Asset::get();
|
asset = Asset::get();
|
||||||
|
|
||||||
// Inicializa variables
|
// Inicializa variables
|
||||||
|
|||||||
@@ -81,14 +81,14 @@ private:
|
|||||||
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
|
// [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, Input *input);
|
Screen(SDL_Window *window, SDL_Renderer *renderer);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Screen();
|
~Screen();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// [SINGLETON] Crearemos el objeto screen con esta función estática
|
// [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
|
// [SINGLETON] Destruiremos el objeto screen con esta función estática
|
||||||
static void destroy();
|
static void destroy();
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Title::Title(Input *input, JA_Music_t *music)
|
Title::Title(JA_Music_t *music)
|
||||||
{
|
{
|
||||||
// Copia las direcciones de los punteros y objetos
|
// Copia las direcciones de los punteros y objetos
|
||||||
this->input = input;
|
|
||||||
this->music = music;
|
this->music = music;
|
||||||
|
input = Input::get();
|
||||||
asset = Asset::get();
|
asset = Asset::get();
|
||||||
screen = Screen::get();
|
screen = Screen::get();
|
||||||
SDL_Renderer *renderer = screen->getRenderer();
|
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 = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition);
|
||||||
gameLogo->enable();
|
gameLogo->enable();
|
||||||
|
|
||||||
defineButtons = new DefineButtons(input, text2);
|
defineButtons = new DefineButtons(text2);
|
||||||
|
|
||||||
// Inicializa los valores
|
// Inicializa los valores
|
||||||
init();
|
init();
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Title(Input *input, JA_Music_t *music);
|
Title(JA_Music_t *music);
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
~Title();
|
~Title();
|
||||||
|
|||||||
Reference in New Issue
Block a user