Revisada la clase DIRECTOR

This commit is contained in:
2022-09-27 19:34:13 +02:00
parent 96ee1fdbe5
commit 10882780e5
3 changed files with 57 additions and 86 deletions

View File

@@ -1,6 +1,6 @@
#include "const.h" #include "const.h"
#include "utils.h"
#include "director.h" #include "director.h"
#include "utils.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
@@ -8,40 +8,37 @@
Director::Director(std::string path) Director::Director(std::string path)
{ {
// Inicializa variables
section.name = PROG_SECTION_INTRO;
section.subsection = 0;
// Crea el objeto que controla los ficheros de recursos // Crea el objeto que controla los ficheros de recursos
asset = new Asset(path.substr(0, path.find_last_of("\\/")) + "/../"); asset = new Asset(path.substr(0, path.find_last_of("\\/")) + "/../");
// Establece la lista de ficheros
Uint8 section = PROG_SECTION_LOGO;
if (!setFileList())
{// Si falta algún fichero no inicies el programa
section = PROG_SECTION_QUIT;
}
// Crea el objeto de idioma // Establece la lista de ficheros
lang = new Lang(asset); if (!setFileList())
{ // Si falta algún fichero no inicia el programa
section.name = PROG_SECTION_QUIT;
}
// Crea el puntero a la estructura y carga el fichero de configuración // Crea el puntero a la estructura y carga el fichero de configuración
options = new options_t; options = new options_t;
loadConfigFile(); loadConfigFile();
// Crea los objetos
input = new Input(asset->get("controllerdb.txt"));
// Inicializa SDL // Inicializa SDL
initSDL(); initSDL();
// Crea el objeto para dibujar en pantalla (Requiere initSDL)
screen = new Screen(window, renderer, options, GAME_WIDTH, GAME_HEIGHT);
// Inicializa JailAudio // Inicializa JailAudio
initJailAudio(); initJailAudio();
// Aplica las opciones // Crea los objetos
lang = new Lang(asset);
lang->setLang(options->language); lang->setLang(options->language);
// Inicializa el resto de variables input = new Input(asset->get("controllerdb.txt"));
init(section); initInput();
screen = new Screen(window, renderer, options, GAME_WIDTH, GAME_HEIGHT);
} }
Director::~Director() Director::~Director()
@@ -49,39 +46,21 @@ Director::~Director()
saveConfigFile(); saveConfigFile();
delete asset; delete asset;
asset = nullptr;
delete input; delete input;
input = nullptr;
delete screen; delete screen;
screen = nullptr;
delete lang; delete lang;
lang = nullptr;
delete options; delete options;
options = nullptr;
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
renderer = nullptr;
window = nullptr;
SDL_Quit(); SDL_Quit();
} }
// Inicia las variables necesarias para arrancar el programa // Inicializa el objeto input
void Director::init(Uint8 name) void Director::initInput()
{ {
// Sección // Teclado
section.name = name;
section.subsection = 0;
// Textos
lang->setLang(options->language);
// Controles
input->bindKey(INPUT_UP, SDL_SCANCODE_UP); input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT); input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
@@ -94,6 +73,7 @@ void Director::init(Uint8 name)
input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); // PAUSE input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); // PAUSE
input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); // ESCAPE input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); // ESCAPE
// Mando
input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP); input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN); input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT); input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
@@ -103,7 +83,7 @@ void Director::init(Uint8 name)
input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X); input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X);
input->bindGameControllerButton(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y); input->bindGameControllerButton(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y);
input->bindGameControllerButton(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B); input->bindGameControllerButton(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B);
input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE
input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE
} }
@@ -282,7 +262,7 @@ bool Director::loadConfigFile()
// Crea el fichero para escritura // Crea el fichero para escritura
file = SDL_RWFromFile(p.c_str(), "w+b"); file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != nullptr) if (file != nullptr)
{ { // Ha podido crear el fichero
printf("New file (%s) created!\n", filename.c_str()); printf("New file (%s) created!\n", filename.c_str());
// Escribe los datos // Escribe los datos
@@ -303,7 +283,7 @@ bool Director::loadConfigFile()
SDL_RWclose(file); SDL_RWclose(file);
} }
else else
{ { // No ha podido crear el fichero
printf("Error: Unable to create file %s\n", filename.c_str()); printf("Error: Unable to create file %s\n", filename.c_str());
success = false; success = false;
} }
@@ -327,14 +307,23 @@ bool Director::loadConfigFile()
SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1); SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1);
// Normaliza los valores // Normaliza los valores
if (!((options->fullScreenMode == 0) || const bool a = options->fullScreenMode == 0;
(options->fullScreenMode == SDL_WINDOW_FULLSCREEN) || const bool b = options->fullScreenMode == SDL_WINDOW_FULLSCREEN;
(options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP))) const bool c = options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
if (!(a || b || c))
{
options->fullScreenMode = 0; options->fullScreenMode = 0;
if ((options->windowSize < 1) || (options->windowSize > 4)) }
if (options->windowSize < 1 || options->windowSize > 4)
{
options->windowSize = 3; options->windowSize = 3;
if ((options->language < 0) || (options->language > MAX_LANGUAGES)) }
if (options->language < 0 || options->language > MAX_LANGUAGES)
{
options->language = en_UK; options->language = en_UK;
}
// Cierra el fichero // Cierra el fichero
SDL_RWclose(file); SDL_RWclose(file);
@@ -374,22 +363,12 @@ bool Director::saveConfigFile()
else else
{ {
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError()); printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
success = false;
} }
return success; return success;
} }
// Obtiene el valor de la variable
Uint8 Director::getSubsection()
{
return section.subsection;
}
// Obtiene el valor de la variable
Uint8 Director::getSection()
{
return section.name;
}
// Establece el valor de la variable // Establece el valor de la variable
void Director::setSection(section_t section) void Director::setSection(section_t section)
{ {
@@ -419,16 +398,8 @@ void Director::runTitle()
void Director::runGame() void Director::runGame()
{ {
if (section.subsection == GAME_SECTION_PLAY_1P) const int numPlayers = section.subsection == GAME_SECTION_PLAY_1P ? 1 : 2;
{ game = new Game(numPlayers, 0, renderer, screen, asset, lang, input, false, options);
game = new Game(1, 0, renderer, screen, asset, lang, input, false, options);
}
else if (section.subsection == GAME_SECTION_PLAY_2P)
{
game = new Game(2, 0, renderer, screen, asset, lang, input, false, options);
}
setSection(game->run()); setSection(game->run());
delete game; delete game;
} }
@@ -436,9 +407,9 @@ void Director::runGame()
void Director::run() void Director::run()
{ {
// Bucle principal // Bucle principal
while (!(getSection() == PROG_SECTION_QUIT)) while (!section.name == PROG_SECTION_QUIT)
{ {
switch (getSection()) switch (section.name)
{ {
case PROG_SECTION_LOGO: case PROG_SECTION_LOGO:
runLogo(); runLogo();

View File

@@ -4,7 +4,6 @@
#include "asset.h" #include "asset.h"
#include "balloon.h" #include "balloon.h"
#include "bullet.h" #include "bullet.h"
#include "coffeedrop.h"
#include "const.h" #include "const.h"
#include "fade.h" #include "fade.h"
#include "game.h" #include "game.h"
@@ -34,6 +33,7 @@
class Director class Director
{ {
private: private:
// Objetos
SDL_Window *window; // La ventana donde dibujamos SDL_Window *window; // La ventana donde dibujamos
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla Screen *screen; // Objeto encargado de dibujar en pantalla
@@ -44,6 +44,8 @@ private:
Input *input; // Objeto Input para gestionar las entradas Input *input; // Objeto Input para gestionar las entradas
Lang *lang; // Objeto para gestionar los textos en diferentes idiomas Lang *lang; // Objeto para gestionar los textos en diferentes idiomas
Asset *asset; // Objeto que gestiona todos los ficheros de recursos Asset *asset; // Objeto que gestiona todos los ficheros de recursos
// Variables
struct options_t *options; // Variable con todas las opciones del programa struct options_t *options; // Variable con todas las opciones del programa
section_t section; // Sección y subsección actual del programa; section_t section; // Sección y subsección actual del programa;
@@ -53,6 +55,9 @@ private:
// Arranca SDL y crea la ventana // Arranca SDL y crea la ventana
bool initSDL(); bool initSDL();
// Inicializa el objeto input
void initInput();
// Crea el indice de ficheros // Crea el indice de ficheros
bool setFileList(); bool setFileList();
@@ -62,12 +67,6 @@ private:
// Guarda el fichero de configuración // Guarda el fichero de configuración
bool saveConfigFile(); bool saveConfigFile();
// Obtiene el valor de la variable
Uint8 getSubsection();
// Obtiene el valor de la variable
Uint8 getSection();
// Establece el valor de la variable // Establece el valor de la variable
void setSection(section_t section); void setSection(section_t section);
@@ -90,9 +89,6 @@ public:
// Destructor // Destructor
~Director(); ~Director();
// Inicia las variables necesarias para arrancar el programa
void init(Uint8 name);
// Bucle principal // Bucle principal
void run(); void run();
}; };

View File

@@ -1,4 +1,5 @@
/* /*
Código fuente creado por JailDesigner (2020) Código fuente creado por JailDesigner (2020)
Empezado en Castalla el 15/07/2020. Empezado en Castalla el 15/07/2020.
@@ -32,6 +33,10 @@ Los objetos globo tienen varios contadores para alternar de un estado a otro.
En los vectores que contienen objetos, se considera activos los objetos que tienen En los vectores que contienen objetos, se considera activos los objetos que tienen
un tipo asociado diferente a NO_KIND un tipo asociado diferente a NO_KIND
*** TODO ESTO ya no es valido, pero lo dejo como referencia
Reescribiendo el código el 27/09/2022
*/ */
#include "director.h" #include "director.h"
@@ -42,14 +47,13 @@ int main(int argc, char *args[])
printf("Starting the game...\n\n"); printf("Starting the game...\n\n");
// Crea el objeto Director // Crea el objeto Director
Director *mDirector = new Director(args[0]); Director *director = new Director(args[0]);
// Bucle principal // Bucle principal
mDirector->run(); director->run();
// Destruye el objeto Director // Destruye el objeto Director
delete mDirector; delete director;
mDirector = nullptr;
printf("\nShutting down the game...\n"); printf("\nShutting down the game...\n");