Un altre punteret a pendre per cul: options

This commit is contained in:
2024-09-27 19:00:09 +02:00
parent 0de9188547
commit 20de9e4b72
26 changed files with 677 additions and 679 deletions

View File

@@ -1,6 +1,7 @@
#include "director.h"
#include "utils.h"
#include "const.h"
#include "director.h"
#include "options.h"
#include <iostream>
#include <fstream>
#include <string>
@@ -33,12 +34,8 @@ Director::Director(int argc, char *argv[])
createSystemFolder("jailgames");
createSystemFolder("jailgames/coffee_crisis_arcade_edition");
// Inicializa las opciones del programa
initOptions();
// Crea el objeto que controla los ficheros de recursos
asset = new Asset(executablePath);
asset->setVerbose(options->console);
// Si falta algún fichero no inicia el programa
if (!setFileList())
@@ -46,8 +43,10 @@ Director::Director(int argc, char *argv[])
exit(EXIT_FAILURE);
}
// Carga los parametros para configurar el juego
// Carga el fichero de configuración
loadOptionsFile(asset->get("config.txt"));
// Carga los parametros para configurar el juego
#ifdef ANBERNIC
const std::string paramFilePath = asset->get("param_320x240.txt");
#else
@@ -55,11 +54,8 @@ Director::Director(int argc, char *argv[])
#endif
loadParams(paramFilePath);
// Carga el fichero de configuración
loadConfigFile();
// Carga el fichero de puntuaciones
ManageHiScoreTable *manager = new ManageHiScoreTable(&options->game.hiScoreTable);
ManageHiScoreTable *manager = new ManageHiScoreTable(&options.game.hiScoreTable);
manager->loadFromFile(asset->get("score.bin"));
delete manager;
@@ -73,12 +69,12 @@ Director::Director(int argc, char *argv[])
dbg_init(renderer);
// 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"));
initInput();
screen = new Screen(window, renderer, asset, input, options);
screen = new Screen(window, renderer, asset, input);
// Carga los sonidos del juego
loadSounds();
@@ -89,12 +85,11 @@ Director::Director(int argc, char *argv[])
Director::~Director()
{
saveConfigFile();
saveOptionsFile(asset->get("config.txt"));
delete asset;
delete input;
delete screen;
delete options;
delete section;
deleteSounds();
@@ -113,7 +108,7 @@ void Director::initInput()
#ifdef VERBOSE
input->setVerbose(true);
#else
input->setVerbose(options->console);
input->setVerbose(options.console);
#endif
// Busca si hay mandos conectados
input->discoverGameControllers();
@@ -171,11 +166,11 @@ void Director::initInput()
// Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso
for (int i = 0; i < numGamePads; ++i)
for (int index = 0; index < (int)options->controller.size(); ++index)
if (input->getControllerName(i) == options->controller[index].name)
for (int j = 0; j < (int)options->controller[index].inputs.size(); ++j)
for (int index = 0; index < (int)options.controller.size(); ++index)
if (input->getControllerName(i) == options.controller[index].name)
for (int j = 0; j < (int)options.controller[index].inputs.size(); ++j)
{
input->bindGameControllerButton(i, options->controller[index].inputs[j], options->controller[index].buttons[j]);
input->bindGameControllerButton(i, options.controller[index].inputs[j], options.controller[index].buttons[j]);
}
// Asigna botones a inputs desde otros inputs
@@ -194,10 +189,10 @@ void Director::initInput()
// Guarda las asignaciones de botones en las opciones
for (int i = 0; i < numGamePads; ++i)
{
options->controller[i].name = input->getControllerName(i);
for (int j = 0; j < (int)options->controller[i].inputs.size(); ++j)
options.controller[i].name = input->getControllerName(i);
for (int j = 0; j < (int)options.controller[i].inputs.size(); ++j)
{
options->controller[i].buttons[j] = input->getControllerBinding(i, options->controller[i].inputs[j]);
options.controller[i].buttons[j] = input->getControllerBinding(i, options.controller[i].inputs[j]);
}
}
}
@@ -206,10 +201,10 @@ void Director::initInput()
void Director::initJailAudio()
{
JA_Init(48000, AUDIO_S16, 2);
JA_EnableMusic(options->audio.music.enabled);
JA_EnableSound(options->audio.sound.enabled);
JA_SetMusicVolume(options->audio.music.volume);
JA_SetSoundVolume(options->audio.sound.volume);
JA_EnableMusic(options.audio.music.enabled);
JA_EnableSound(options.audio.sound.enabled);
JA_SetMusicVolume(options.audio.music.volume);
JA_SetSoundVolume(options.audio.sound.volume);
}
// Arranca SDL y crea la ventana
@@ -221,7 +216,7 @@ bool Director::initSDL()
// Inicializa SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
if (options->console)
if (options.console)
{
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
}
@@ -233,7 +228,7 @@ bool Director::initSDL()
std::srand(static_cast<unsigned int>(SDL_GetTicks()));
// Muestra información de la pantalla
if (options->console)
if (options.console)
{
/*std::cout << "\nDisplay modes list:" << std::endl;
for (int i = 0; i < SDL_GetNumDisplayModes(0); ++i)
@@ -246,13 +241,13 @@ bool Director::initSDL()
SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
std::cout << "\nCurrent display mode: " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl;
std::cout << "Window resolution : " + std::to_string(param.game.width) + "x" + std::to_string(param.game.height) + " x" + std::to_string(options->video.window.size) << std::endl;
std::cout << "Window resolution : " + std::to_string(param.game.width) + "x" + std::to_string(param.game.height) + " x" + std::to_string(options.video.window.size) << std::endl;
}
// Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->video.filter).c_str()))
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options.video.filter).c_str()))
{
if (options->console)
if (options.console)
{
std::cout << "Warning: texture filtering not enabled!\n";
}
@@ -260,17 +255,17 @@ bool Director::initSDL()
#ifndef NO_SHADERS
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
{
if (options->console)
if (options.console)
{
std::cout << "Warning: opengl not enabled!\n";
}
}
#endif
// Crea la ventana
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options->video.window.size, param.game.height * options->video.window.size, SDL_WINDOW_HIDDEN);
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options.video.window.size, param.game.height * options.video.window.size, SDL_WINDOW_HIDDEN);
if (window == nullptr)
{
if (options->console)
if (options.console)
{
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
@@ -280,7 +275,7 @@ bool Director::initSDL()
{
// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
Uint32 flags = 0;
if (options->video.vSync)
if (options.video.vSync)
{
flags = flags | SDL_RENDERER_PRESENTVSYNC;
}
@@ -292,7 +287,7 @@ bool Director::initSDL()
if (renderer == nullptr)
{
if (options->console)
if (options.console)
{
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
@@ -312,7 +307,7 @@ bool Director::initSDL()
}
}
if (options->console)
if (options.console)
{
std::cout << std::endl;
}
@@ -469,89 +464,10 @@ void Director::loadParams(std::string filepath)
loadParamsFromFile(filepath);
// Modifica las opciones desde el fichero de parametros
options->video.window.width = options->video.window.size * param.game.width;
options->video.window.height = options->video.window.size * param.game.height;
options->video.gameWidth = param.game.width;
options->video.gameHeight = param.game.height;
}
// Inicializa las opciones del programa
void Director::initOptions()
{
// Crea el puntero a la estructura de opciones
options = new options_t;
// Pone unos valores por defecto para las opciones
// Opciones varias
#ifdef VERBOSE
options->console = true;
#else
options->console = false;
#endif
// Opciones de video
#ifdef ANBERNIC
options->video.mode = 0;
options->video.window.size = 3;
#else
options->video.mode = 0;
options->video.window.size = 2;
#endif
options->video.filter = SCREEN_FILTER_NEAREST;
options->video.vSync = true;
options->video.integerScale = true;
options->video.keepAspect = true;
options->video.shaders = true;
// Opciones de las notificaciones
options->notification.posV = pos_top;
options->notification.posH = pos_left;
options->notification.sound = false;
options->notification.color = {48, 48, 48};
// Opciones de audio
options->audio.music.enabled = true;
options->audio.music.volume = 128;
options->audio.sound.enabled = true;
options->audio.sound.volume = 64;
// Opciones de juego
options->game.difficulty = DIFFICULTY_NORMAL;
options->game.language = lang::ba_BA;
options->game.autofire = true;
// Opciones de control
options->controller.clear();
op_controller_t c;
const int numPlayers = 2;
for (int index = 0; index < numPlayers; ++index)
{
c.index = index;
c.playerId = index + 1;
c.deviceType = INPUT_USE_GAMECONTROLLER;
c.name = "NO NAME";
// Inputs que se guardan en las opciones y, por tanto, a disco
c.inputs.clear();
c.inputs.push_back(input_fire_left);
c.inputs.push_back(input_fire_center);
c.inputs.push_back(input_fire_right);
c.inputs.push_back(input_start);
c.inputs.push_back(input_service);
// Botones asociados a los inputs anteriores
c.buttons.clear();
c.buttons.push_back(SDL_CONTROLLER_BUTTON_X);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_Y);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_B);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_START);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_BACK);
options->controller.push_back(c);
}
options->controller[0].deviceType = INPUT_USE_ANY; // El primer jugador puede usar tanto el teclado como el primer mando
options.video.window.width = options.video.window.size * param.game.width;
options.video.window.height = options.video.window.size * param.game.height;
options.video.gameWidth = param.game.width;
options.video.gameHeight = param.game.height;
}
// Comprueba los parametros del programa
@@ -568,7 +484,7 @@ void Director::checkProgramArguments(int argc, char *argv[])
{
if (strcmp(argv[i], "--console") == 0)
{
options->console = true;
options.console = true;
}
if (strcmp(argv[i], "--320x240") == 0)
@@ -627,203 +543,6 @@ void Director::createSystemFolder(std::string folder)
}
}
// Carga el fichero de configuración
bool Director::loadConfigFile()
{
// Indicador de éxito en la carga
bool success = true;
// Variables para manejar el fichero
const std::string filePath = "config.txt";
std::string line;
std::ifstream file(asset->get(filePath));
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero linea a linea
if (options->console)
{
std::cout << "Reading file: " << filePath << std::endl;
}
while (std::getline(file, line))
{
// Comprueba que la linea no sea un comentario
if (line.substr(0, 1) != "#")
{
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
if (options->console)
{
std::cout << "Warning: file " << filePath << std::endl;
std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl;
}
success = false;
}
}
}
// Cierra el fichero
file.close();
}
// El fichero no existe
else
{ // Crea el fichero con los valores por defecto
saveConfigFile();
}
// Normaliza los valores
const bool a = options->video.mode == 0;
const bool b = options->video.mode == SDL_WINDOW_FULLSCREEN;
const bool c = options->video.mode == SDL_WINDOW_FULLSCREEN_DESKTOP;
if (!(a || b || c))
{
options->video.mode = SCREEN_VIDEO_MODE_WINDOW;
}
if (options->video.window.size < 1 || options->video.window.size > 4)
{
options->video.window.size = 3;
}
if (options->game.language < 0 || options->game.language > 2)
{
options->game.language = lang::en_UK;
}
return success;
}
// Guarda el fichero de configuración
bool Director::saveConfigFile()
{
const std::string filename = "config.txt";
std::ofstream file(asset->get(filename));
if (!file.good())
{
if (options->console)
{
std::cout << filename << " can't be opened" << std::endl;
}
return false;
}
if (options->console)
{
std::cout << "Writing file: " << filename << std::endl;
}
// Opciones de video
file << "## VIDEO\n";
file << "## video.mode [0: window, 1: full screen]\n";
file << "## video.filter [0: nearest, 1: lineal]\n";
file << "\n";
if (options->video.mode == SCREEN_VIDEO_MODE_WINDOW)
{
file << "video.mode=0\n";
}
else if (options->video.mode == SCREEN_VIDEO_MODE_FULLSCREEN)
{
file << "video.mode=1\n";
}
file << "video.window.size=" + std::to_string(options->video.window.size) + "\n";
options->video.filter == SCREEN_FILTER_NEAREST ? file << "video.filter=0\n" : file << "video.filter=1\n";
file << "video.shaders=" + boolToString(options->video.shaders) + "\n";
file << "video.vSync=" + boolToString(options->video.vSync) + "\n";
file << "video.integerScale=" + boolToString(options->video.integerScale) + "\n";
file << "video.keepAspect=" + boolToString(options->video.keepAspect) + "\n";
// Opciones de notificaciones
file << "\n\n## NOTIFICATION\n";
file << "## notification.posV [pos_top | pos_bottom]\n";
file << "## notification.posH [pos_left | pos_middle | pos_right]\n";
file << "\n";
if (options->notification.posV == pos_top)
{
file << "notification.posV=pos_top\n";
}
else
{
file << "notification.posV=pos_bottom\n";
}
if (options->notification.posH == pos_left)
{
file << "notification.posH=pos_left\n";
}
else if (options->notification.posH == pos_middle)
{
file << "notification.posH=pos_middle\n";
}
else
{
file << "notification.posH=pos_right\n";
}
file << "notification.sound=" + boolToString(options->notification.sound) + "\n";
// Opciones de audio
file << "\n\n## AUDIO\n";
file << "## volume [0 .. 128]\n";
file << "\n";
file << "audio.music.enabled=" + boolToString(options->audio.music.enabled) + "\n";
file << "audio.music.volume=" + std::to_string(options->audio.music.volume) + "\n";
file << "audio.sound.enabled=" + boolToString(options->audio.sound.enabled) + "\n";
file << "audio.sound.volume=" + std::to_string(options->audio.sound.volume) + "\n";
// Opciones del juego
file << "\n\n## GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\n";
file << "## game.difficulty [0: easy, 1: normal, 2: hard]\n";
file << "\n";
file << "game.language=" + std::to_string(options->game.language) + "\n";
file << "game.difficulty=" + std::to_string(options->game.difficulty) + "\n";
file << "game.autofire=" + boolToString(options->game.autofire) + "\n";
// Opciones de mandos
file << "\n\n## CONTROLLERS\n";
file << "\n";
const int numPlayers = 2;
for (int index = 0; index < numPlayers; ++index)
{
const std::string joyIndex = std::to_string(index + 1);
file << "controller" + joyIndex + ".name=" + options->controller[index].name + "\n";
file << "controller" + joyIndex + ".player=" + std::to_string(options->controller[index].playerId) + "\n";
file << "controller" + joyIndex + ".button.fire_left=" + std::to_string((int)options->controller[index].buttons[0]) + "\n";
file << "controller" + joyIndex + ".button.fire_center=" + std::to_string((int)options->controller[index].buttons[1]) + "\n";
file << "controller" + joyIndex + ".button.fire_right=" + std::to_string((int)options->controller[index].buttons[2]) + "\n";
file << "controller" + joyIndex + ".button.start=" + std::to_string((int)options->controller[index].buttons[3]) + "\n";
file << "controller" + joyIndex + ".button.service=" + std::to_string((int)options->controller[index].buttons[4]) + "\n";
if (index < numPlayers - 1)
{
file << "\n";
}
}
// Cierra el fichero
file.close();
return true;
}
// Carga los sonidos del juego
void Director::loadSounds()
{
@@ -883,7 +602,7 @@ void Director::deleteMusics()
// Ejecuta la sección con el logo
void Director::runLogo()
{
logo = new Logo(screen, asset, input, options, section);
logo = new Logo(screen, asset, input, section);
logo->run();
delete logo;
}
@@ -891,7 +610,7 @@ void Director::runLogo()
// Ejecuta la sección con la secuencia de introducción
void Director::runIntro()
{
intro = new Intro(screen, asset, input, options, section, getMusic(musics, "intro.ogg"));
intro = new Intro(screen, asset, input, section, getMusic(musics, "intro.ogg"));
intro->run();
delete intro;
}
@@ -899,7 +618,7 @@ void Director::runIntro()
// Ejecuta la sección con el titulo del juego
void Director::runTitle()
{
title = new Title(screen, asset, input, options, section, getMusic(musics, "title.ogg"));
title = new Title(screen, asset, input, section, getMusic(musics, "title.ogg"));
title->run();
delete title;
}
@@ -909,7 +628,7 @@ void Director::runGame()
{
const int playerID = section->options;
const int currentStage = 0;
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, screen, asset, input, options, section, getMusic(musics, "playing.ogg"));
game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, screen, asset, input, section, getMusic(musics, "playing.ogg"));
game->run();
delete game;
}
@@ -917,7 +636,7 @@ void Director::runGame()
// Ejecuta la sección donde se muestran las instrucciones
void Director::runInstructions()
{
instructions = new Instructions(screen, asset, input, options, section, getMusic(musics, "title.ogg"));
instructions = new Instructions(screen, asset, input, section, getMusic(musics, "title.ogg"));
instructions->run();
delete instructions;
}
@@ -925,7 +644,7 @@ void Director::runInstructions()
// Ejecuta la sección donde se muestra la tabla de puntuaciones
void Director::runHiScoreTable()
{
hiScoreTable = new HiScoreTable(screen, asset, input, options, section, getMusic(musics, "title.ogg"));
hiScoreTable = new HiScoreTable(screen, asset, input, section, getMusic(musics, "title.ogg"));
hiScoreTable->run();
delete hiScoreTable;
}
@@ -935,7 +654,7 @@ void Director::runDemoGame()
{
const int playerID = (rand() % 2) + 1;
const int currentStage = 0;
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, screen, asset, input, options, section, nullptr);
demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, screen, asset, input, section, nullptr);
demoGame->run();
delete demoGame;
}
@@ -985,200 +704,6 @@ int Director::run()
return returnCode;
}
// Asigna variables a partir de dos cadenas
bool Director::setOptions(options_t *options, std::string var, std::string value)
{
// Indicador de éxito en la asignación
bool success = true;
// Opciones de video
if (var == "video.mode")
{
options->video.mode = value == std::to_string(SCREEN_VIDEO_MODE_WINDOW) ? SCREEN_VIDEO_MODE_WINDOW : SCREEN_VIDEO_MODE_FULLSCREEN;
}
else if (var == "video.window.size")
{
options->video.window.size = std::stoi(value);
if ((options->video.window.size < 1) || (options->video.window.size > 4))
{
options->video.window.size = 3;
}
}
else if (var == "video.filter")
{
options->video.filter = value == std::to_string(SCREEN_FILTER_NEAREST) ? SCREEN_FILTER_NEAREST : SCREEN_FILTER_LINEAL;
}
else if (var == "video.shaders")
{
options->video.shaders = stringToBool(value);
}
else if (var == "video.vSync")
{
options->video.vSync = stringToBool(value);
}
else if (var == "video.integerScale")
{
options->video.integerScale = stringToBool(value);
}
else if (var == "video.keepAspect")
{
options->video.keepAspect = stringToBool(value);
}
// Opciones de notificaciones
else if (var == "notification.posH")
{
if (value == "pos_left")
{
options->notification.posH = pos_left;
}
else if (value == "pos_middle")
{
options->notification.posH = pos_middle;
}
else
{
options->notification.posH = pos_right;
}
}
else if (var == "notification.posV")
{
options->notification.posV = value == "pos_top" ? pos_top : pos_bottom;
}
else if (var == "notification.sound")
{
options->notification.sound = stringToBool(value);
}
// Opciones de audio
else if (var == "audio.music.enabled")
{
options->audio.music.enabled = stringToBool(value);
}
else if (var == "audio.music.volume")
{
options->audio.music.volume = std::stoi(value);
}
else if (var == "audio.sound.enabled")
{
options->audio.sound.enabled = stringToBool(value);
}
else if (var == "audio.sound.volume")
{
options->audio.sound.volume = std::stoi(value);
}
// Opciones de juego
else if (var == "game.language")
{
options->game.language = std::stoi(value);
}
else if (var == "game.difficulty")
{
options->game.difficulty = std::stoi(value);
}
else if (var == "game.autofire")
{
options->game.autofire = stringToBool(value);
}
// Opciones de mandos
else if (var == "controller1.name")
{
options->controller[0].name = value;
}
else if (var == "controller1.player")
{
options->controller[0].playerId = std::max(1, std::min(2, std::stoi(value)));
}
else if (var == "controller1.button.fire_left")
{
options->controller[0].buttons[0] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller1.button.fire_center")
{
options->controller[0].buttons[1] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller1.button.fire_right")
{
options->controller[0].buttons[2] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller1.button.start")
{
options->controller[0].buttons[3] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller1.button.service")
{
options->controller[0].buttons[4] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller2.name")
{
options->controller[1].name = value;
}
else if (var == "controller2.player")
{
options->controller[1].playerId = std::max(1, std::min(2, std::stoi(value)));
}
else if (var == "controller2.button.fire_left")
{
options->controller[1].buttons[0] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller2.button.fire_center")
{
options->controller[1].buttons[1] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller2.button.fire_right")
{
options->controller[1].buttons[2] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller2.button.start")
{
options->controller[1].buttons[3] = (SDL_GameControllerButton)std::stoi(value);
}
else if (var == "controller2.button.service")
{
options->controller[1].buttons[4] = (SDL_GameControllerButton)std::stoi(value);
}
// Lineas vacias o que empiezan por comentario
else if (var == "" || var.substr(0, 1) == "#")
{
}
else
{
success = false;
}
return success;
}
// Obtiene una fichero a partir de un lang_e
std::string Director::getLangFile(lang::lang_e lang)
{