Ja es pot gastar el teclat com a control independent del primer mando

Ja pot jugar un jugador amb teclat i altre amb mando
Es pot asignar el teclat a qualsevol dels dos jugadors
Continua podentse gastar mando i teclat a l'hora per al mateix jugador
This commit is contained in:
2024-11-03 11:07:58 +01:00
parent a1ccb6102a
commit 86cd7b0f16
11 changed files with 306 additions and 293 deletions

View File

@@ -4,7 +4,7 @@
#include <fstream> // para basic_ostream, operator<<, basi...
#include <iostream> // para cout
#include <vector> // para vector
#include "input.h" // para inputs_e, INPUT_USE_ANY, INPUT_...
#include "input.h" // para inputs_e, InputDeviceToUse::ANY, INPUT_...
#include "lang.h" // para lang_e
#include "screen.h" // para ScreenVideoMode, ScreenFilter
#include "utils.h" // para OptionsController, Options, op_...
@@ -45,37 +45,11 @@ void initOptions()
options.game.autofire = true;
// Opciones de control
options.controller.clear();
OptionsController c;
constexpr int num_players = 2;
for (int index = 0; index < num_players; ++index)
{
c.index = index;
c.player_id = index + 1;
c.device_type = INPUT_USE_GAMECONTROLLER;
c.name = "NO NAME";
c.plugged = false;
// Inputs que se guardan en las opciones y, por tanto, a disco
c.inputs.clear();
c.inputs.push_back(InputType::FIRE_LEFT);
c.inputs.push_back(InputType::FIRE_CENTER);
c.inputs.push_back(InputType::FIRE_RIGHT);
c.inputs.push_back(InputType::START);
c.inputs.push_back(InputType::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.at(0).device_type = INPUT_USE_ANY; // El primer jugador puede usar tanto el teclado como el primer mando
options.controllers.clear();
options.controllers.resize(2);
options.controllers.at(0).player_id = 1;
options.controllers.at(1).player_id = 2;
setKeyboardToPlayer(1);
}
// Carga el fichero de configuración
@@ -129,12 +103,11 @@ bool loadOptionsFile(std::string file_path)
options.video.mode = ScreenVideoMode::WINDOW;
}
if (options.video.window.size < 1 || options.video.window.size > 4)
{
options.video.window.size = 3;
}
options.video.window.size = std::clamp(options.video.window.size, 1, 4);
if (options.game.language != lang::Code::en_UK && options.game.language != lang::Code::ba_BA && options.game.language != lang::Code::es_ES)
if (options.game.language != lang::Code::en_UK &&
options.game.language != lang::Code::ba_BA &&
options.game.language != lang::Code::es_ES)
{
options.game.language = lang::Code::en_UK;
}
@@ -156,73 +129,54 @@ bool saveOptionsFile(std::string file_path)
std::cout << "Writing file: " << getFileName(file_path) << std::endl;
// Opciones de video
const auto value_video_mode_winow = std::to_string(static_cast<int>(ScreenVideoMode::WINDOW));
const auto value_video_mode_fullscreen = std::to_string(static_cast<int>(ScreenVideoMode::FULLSCREEN));
const auto value_filter_nearest = std::to_string(static_cast<int>(ScreenFilter::NEAREST));
const auto value_filter_lineal = std::to_string(static_cast<int>(ScreenFilter::LINEAL));
file << "## VIDEO\n";
file << "## video.mode [" << value_video_mode_winow << ": window, " << value_video_mode_fullscreen << ": fullscreen]\n";
file << "## video.filter [" << value_filter_nearest << ": nearest, " << value_filter_lineal << ": lineal]\n";
file << "## video.mode [" << static_cast<int>(ScreenVideoMode::WINDOW) << ": window, " << static_cast<int>(ScreenVideoMode::FULLSCREEN) << ": fullscreen]\n";
file << "## video.filter [" << static_cast<int>(ScreenFilter::NEAREST) << ": nearest, " << static_cast<int>(ScreenFilter::LINEAL) << ": lineal]\n";
file << "\n";
const auto valueVideoMode = std::to_string(static_cast<int>(options.video.mode));
file << "video.mode=" << valueVideoMode << "\n";
file << "video.window.size=" + std::to_string(options.video.window.size) + "\n";
const auto valueFilter = std::to_string(static_cast<int>(options.video.filter));
file << "video.filter=" << valueFilter << "\n";
file << "video.v_sync=" + boolToString(options.video.v_sync) + "\n";
file << "video.integer_scale=" + boolToString(options.video.integer_scale) + "\n";
file << "video.shaders=" + boolToString(options.video.shaders) + "\n";
file << "video.mode=" << static_cast<int>(options.video.mode) << "\n";
file << "video.window.size=" << options.video.window.size << "\n";
file << "video.filter=" << static_cast<int>(options.video.filter) << "\n";
file << "video.v_sync=" << boolToString(options.video.v_sync) << "\n";
file << "video.integer_scale=" << boolToString(options.video.integer_scale) << "\n";
file << "video.shaders=" << boolToString(options.video.shaders) << "\n";
// Opciones de audio
file << "\n\n## AUDIO\n";
file << "## volume [0 .. 100]\n";
file << "\n";
file << "audio.enabled=" + boolToString(options.audio.enabled) + "\n";
file << "audio.volume=" + std::to_string(options.audio.volume) + "\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";
file << "audio.enabled=" << boolToString(options.audio.enabled) << "\n";
file << "audio.volume=" << options.audio.volume << "\n";
file << "audio.music.enabled=" << boolToString(options.audio.music.enabled) << "\n";
file << "audio.music.volume=" << options.audio.music.volume << "\n";
file << "audio.sound.enabled=" << boolToString(options.audio.sound.enabled) << "\n";
file << "audio.sound.volume=" << options.audio.sound.volume << "\n";
// Opciones del juego
const auto value_difficulty_easy = std::to_string(static_cast<int>(GameDifficulty::EASY));
const auto value_difficulty_normal = std::to_string(static_cast<int>(GameDifficulty::NORMAL));
const auto value_difficulty_hard = std::to_string(static_cast<int>(GameDifficulty::HARD));
file << "\n\n## GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\n";
file << "## game.difficulty [" << value_difficulty_easy << ": easy, " << value_difficulty_normal << ": normal, " << value_difficulty_hard << ": hard]\n";
file << "## game.difficulty [" << static_cast<int>(GameDifficulty::EASY) << ": easy, " << static_cast<int>(GameDifficulty::NORMAL) << ": normal, " << static_cast<int>(GameDifficulty::HARD) << ": hard]\n";
file << "\n";
file << "game.language=" + std::to_string(static_cast<int>(options.game.language)) + "\n";
file << "game.difficulty=" + std::to_string(static_cast<int>(options.game.difficulty)) + "\n";
file << "game.autofire=" + boolToString(options.game.autofire) + "\n";
file << "game.language=" << static_cast<int>(options.game.language) << "\n";
file << "game.difficulty=" << static_cast<int>(options.game.difficulty) << "\n";
file << "game.autofire=" << boolToString(options.game.autofire) << "\n";
// Opciones de mandos
file << "\n\n## CONTROLLERS\n";
file << "\n";
const int num_players = 2;
for (int index = 0; index < num_players; ++index)
for (const auto &controller : options.controllers)
{
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].player_id) + "\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 < num_players - 1)
{
file << "\n";
}
file << "\n";
file << "controller." << controller.index << ".name=" << controller.name << "\n";
file << "controller." << controller.index << ".player=" << controller.player_id << "\n";
file << "controller." << controller.index << ".type=" << static_cast<int>(controller.type) << "\n";
file << "controller." << controller.index << ".button.fire_left=" << controller.buttons.at(0) << "\n";
file << "controller." << controller.index << ".button.fire_center=" << controller.buttons.at(1) << "\n";
file << "controller." << controller.index << ".button.fire_right=" << controller.buttons.at(2) << "\n";
file << "controller." << controller.index << ".button.start=" << controller.buttons.at(3) << "\n";
file << "controller." << controller.index << ".button.service=" << controller.buttons.at(4) << "\n";
}
// Cierra el fichero
@@ -242,7 +196,6 @@ bool setOptions(const std::string &var, const std::string &value)
{
options.video.mode = static_cast<ScreenVideoMode>(std::stoi(value));
}
else if (var == "video.window.size")
{
options.video.window.size = std::stoi(value);
@@ -251,22 +204,18 @@ bool setOptions(const std::string &var, const std::string &value)
options.video.window.size = 3;
}
}
else if (var == "video.filter")
{
options.video.filter = static_cast<ScreenFilter>(std::stoi(value));
}
else if (var == "video.shaders")
{
options.video.shaders = stringToBool(value);
}
else if (var == "video.integer_scale")
{
options.video.integer_scale = stringToBool(value);
}
else if (var == "video.v_sync")
{
options.video.v_sync = stringToBool(value);
@@ -277,7 +226,6 @@ bool setOptions(const std::string &var, const std::string &value)
{
options.audio.enabled = stringToBool(value);
}
else if (var == "audio.volume")
{
options.audio.volume = std::stoi(value);
@@ -286,17 +234,14 @@ bool setOptions(const std::string &var, const std::string &value)
{
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);
@@ -307,93 +252,85 @@ bool setOptions(const std::string &var, const std::string &value)
{
options.game.language = static_cast<lang::Code>(std::stoi(value));
}
else if (var == "game.difficulty")
{
options.game.difficulty = static_cast<GameDifficulty>(std::stoi(value));
}
else if (var == "game.autofire")
{
options.game.autofire = stringToBool(value);
}
// Opciones de mandos
else if (var == "controller1.name")
else if (var == "controller.0.name")
{
options.controller[0].name = value;
options.controllers.at(0).name = value;
}
else if (var == "controller1.player")
else if (var == "controller.0.player")
{
options.controller[0].player_id = std::max(1, std::min(2, std::stoi(value)));
options.controllers.at(0).player_id = std::clamp(std::stoi(value), 1, 2);
}
else if (var == "controller1.button.fire_left")
else if (var == "controller.0.type")
{
options.controller[0].buttons[0] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(0).type = static_cast<InputDeviceToUse>(std::stoi(value));
}
else if (var == "controller1.button.fire_center")
else if (var == "controller.0.button.fire_left")
{
options.controller[0].buttons[1] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(0).buttons.at(0) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller1.button.fire_right")
else if (var == "controller.0.button.fire_center")
{
options.controller[0].buttons[2] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(0).buttons.at(1) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller1.button.start")
else if (var == "controller.0.button.fire_right")
{
options.controller[0].buttons[3] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(0).buttons.at(2) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller1.button.service")
else if (var == "controller.0.button.start")
{
options.controller[0].buttons[4] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(0).buttons.at(3) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller2.name")
else if (var == "controller.0.button.service")
{
options.controller[1].name = value;
options.controllers.at(0).buttons.at(4) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller2.player")
else if (var == "controller.1.name")
{
options.controller[1].player_id = std::max(1, std::min(2, std::stoi(value)));
options.controllers.at(1).name = value;
}
else if (var == "controller2.button.fire_left")
else if (var == "controller.1.player")
{
options.controller[1].buttons[0] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(1).player_id = std::clamp(std::stoi(value), 1, 2);
}
else if (var == "controller2.button.fire_center")
else if (var == "controller.1.type")
{
options.controller[1].buttons[1] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(1).type = static_cast<InputDeviceToUse>(std::stoi(value));
}
else if (var == "controller2.button.fire_right")
else if (var == "controller.1.button.fire_left")
{
options.controller[1].buttons[2] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(1).buttons.at(0) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller2.button.start")
else if (var == "controller.1.button.fire_center")
{
options.controller[1].buttons[3] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(1).buttons.at(1) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller2.button.service")
else if (var == "controller.1.button.fire_right")
{
options.controller[1].buttons[4] = (SDL_GameControllerButton)std::stoi(value);
options.controllers.at(1).buttons.at(2) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller.1.button.start")
{
options.controllers.at(1).buttons.at(3) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller.1.button.service")
{
options.controllers.at(1).buttons.at(4) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
// Lineas vacias o que empiezan por comentario
else if (var.empty() || var.starts_with("#"))
{
}
else
{
success = false;
@@ -407,4 +344,46 @@ int to_JA_volume(int vol)
{
vol = vol * 1.28f;
return std::clamp(vol, 0, 128);
}
// Asigna el teclado al jugador
void setKeyboardToPlayer(int player_id)
{
for (auto &controller : options.controllers)
{
if (controller.player_id == player_id)
{
controller.type = InputDeviceToUse::ANY;
}
else
{
controller.type = InputDeviceToUse::CONTROLLER;
}
}
}
// Intercambia el teclado de jugador
void swapOptionsKeyboard()
{
std::swap(options.controllers.at(0).type, options.controllers.at(1).type);
}
// Intercambia los jugadores asignados a los dos primeros mandos
void swapOptionsControllers()
{
std::swap(options.controllers.at(0).player_id, options.controllers.at(1).player_id);
std::swap(options.controllers.at(0).type, options.controllers.at(1).type);
}
// Averigua quien está usando el teclado
int getPlayerWhoUsesKeyboard()
{
for (auto &controller : options.controllers)
{
if (controller.type == InputDeviceToUse::ANY)
{
return controller.player_id;
}
}
return 0;
}