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