Añadidos parametros al programa

This commit is contained in:
2022-11-02 08:36:00 +01:00
parent 12255750f6
commit 8232055d22
13 changed files with 292 additions and 93 deletions

View File

@@ -4,7 +4,7 @@
#include <string>
// Constructor
Director::Director(std::string path)
Director::Director(int argc, char *argv[])
{
section.name = SECTION_PROG_LOGO;
section.subsection = SUBSECTION_LOGO_TO_INTRO;
@@ -12,7 +12,8 @@ Director::Director(std::string path)
section.name = SECTION_PROG_GAME;
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(path.substr(0, path.find_last_of("\\/")));
executablePath = argv[0];
asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/")));
// Si falta algún fichero no inicia el programa
if (!setFileList())
@@ -20,6 +21,13 @@ Director::Director(std::string path)
section.name = SECTION_PROG_QUIT;
}
// Crea e inicializa las opciones del programa
iniOptions();
// Comprueba los parametros del programa
checkProgramArguments(argc, argv);
asset->setVerbose(options->console);
// Inicializa variables desde el fichero de configuración
loadConfig();
@@ -59,11 +67,13 @@ Director::~Director()
SDL_Quit();
}
// Carga el fichero de configuración
bool Director::loadConfig()
// Crea e inicializa las opciones del programa
void Director::iniOptions()
{
// Crea el puntero a la estructura de opciones e inicializa valores
// Crea el puntero a la estructura de opciones
options = new options_t;
// Inicializa valores
options->fullScreenMode = 0;
options->windowSize = 3;
options->filter = FILTER_NEAREST;
@@ -74,6 +84,31 @@ bool Director::loadConfig()
options->borderSize = 0.1f;
options->palette = p_zxspectrum;
// Estos valores no se guardan en el fichero de configuraci´ón
options->console = false;
options->infiniteLives = false;
}
// Comprueba los parametros del programa
void Director::checkProgramArguments(int argc, char *argv[])
{
for (int i = 0; i < argc; ++i)
{
if (strcmp(argv[i], "--console") == 0)
{
options->console = true;
}
else if (strcmp(argv[i], "--infiniteLives") == 0)
{
options->infiniteLives = true;
}
}
}
// Carga el fichero de configuración
bool Director::loadConfig()
{
// Indicador de éxito en la carga
bool success = true;
@@ -85,7 +120,10 @@ bool Director::loadConfig()
if (file.good())
{
// Procesa el fichero linea a linea
std::cout << "Reading file config.txt\n";
if (options->console)
{
std::cout << "Reading file config.txt\n";
}
while (std::getline(file, line))
{
// Comprueba que la linea no sea un comentario
@@ -96,15 +134,21 @@ bool Director::loadConfig()
// Procesa las dos subcadenas
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
std::cout << "Warning: file config.txt\n";
std::cout << "unknown parameter " << line.substr(0, pos).c_str() << std::endl;
if (options->console)
{
std::cout << "Warning: file config.txt\n";
std::cout << "unknown parameter " << line.substr(0, pos).c_str() << std::endl;
}
success = false;
}
}
}
// Cierra el fichero
std::cout << "Closing file config.txt\n\n";
if (options->console)
{
std::cout << "Closing file config.txt\n\n";
}
file.close();
}
@@ -182,7 +226,10 @@ bool Director::saveConfig()
// Carga los recursos
void Director::loadResources(section_t section)
{
std::cout << "** LOAD RESOURCES" << std::endl;
if (options->console)
{
std::cout << "** LOAD RESOURCES" << std::endl;
}
if (section.name == SECTION_PROG_LOGO)
{
@@ -503,7 +550,10 @@ void Director::loadResources(section_t section)
resource->loadRooms(roomList);
}
std::cout << "** RESOURCES LOADED" << std::endl;
if (options->console)
{
std::cout << "** RESOURCES LOADED" << std::endl;
}
}
// Asigna variables a partir de dos cadenas
@@ -608,6 +658,10 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
// Inicia las variables necesarias para arrancar el programa
void Director::initInput()
{
// Establece si ha de mostrar mensajes
input->setVerbose(options->console);
// Asigna inputs a teclas
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
@@ -645,7 +699,10 @@ bool Director::initSDL()
// Inicializa SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -656,27 +713,40 @@ bool Director::initSDL()
// Establece el filtro de la textura a nearest
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
{
std::cout << "Warning: Nearest texture filtering not enabled!\n";
if (options->console)
{
std::cout << "Warning: Nearest texture filtering not enabled!\n";
}
}
// Crea la ventana
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth, options->screenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (window == nullptr)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
{
// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
if (options->vSync)
{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
}
else
{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
if (renderer == nullptr)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
if (options->console)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
success = false;
}
else
@@ -693,7 +763,10 @@ bool Director::initSDL()
}
}
std::cout << std::endl;
if (options->console)
{
std::cout << std::endl;
}
return success;
}
@@ -1010,7 +1083,10 @@ void Director::setSection(section_t section)
// Ejecuta la seccion de juego con el logo
void Director::runLogo()
{
std::cout << "\n* SECTION: LOGO" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: LOGO" << std::endl;
}
loadResources(section);
logo = new Logo(renderer, screen, resource, asset, options, section.subsection);
setSection(logo->run());
@@ -1021,7 +1097,10 @@ void Director::runLogo()
// Ejecuta la seccion de juego de la introducción
void Director::runIntro()
{
std::cout << "\n* SECTION: INTRO" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: INTRO" << std::endl;
}
loadResources(section);
intro = new Intro(renderer, screen, resource, asset, options);
setSection(intro->run());
@@ -1032,7 +1111,10 @@ void Director::runIntro()
// Ejecuta la seccion de juego con el titulo y los menus
void Director::runTitle()
{
std::cout << "\n* SECTION: TITLE" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: TITLE" << std::endl;
}
if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
{
JA_PlayMusic(music);
@@ -1047,7 +1129,10 @@ void Director::runTitle()
// Ejecuta la seccion de los creditos del juego
void Director::runCredits()
{
std::cout << "\n* SECTION: CREDITS" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: CREDITS" << std::endl;
}
loadResources(section);
credits = new Credits(renderer, screen, resource, asset, options);
setSection(credits->run());
@@ -1058,7 +1143,10 @@ void Director::runCredits()
// Ejecuta la seccion de la demo, donde se ven pantallas del juego
void Director::runDemo()
{
std::cout << "\n* SECTION: DEMO" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: DEMO" << std::endl;
}
loadResources(section);
demo = new Demo(renderer, screen, resource, asset, options, debug);
setSection(demo->run());
@@ -1069,7 +1157,10 @@ void Director::runDemo()
// Ejecuta la seccion de juego donde se juega
void Director::runGame()
{
std::cout << "\n* SECTION: GAME" << std::endl;
if (options->console)
{
std::cout << "\n* SECTION: GAME" << std::endl;
}
JA_StopMusic();
loadResources(section);
game = new Game(renderer, screen, resource, asset, options, input, debug);