From c92f2a13aa41027d59819f2eb0bd3ecd25a67d9e Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 2 Oct 2024 13:43:32 +0200 Subject: [PATCH] - [NEW] Afegit menu per a redefinir els botons del gamepad --- source/config.cpp | 14 +++++++ source/config.h | 3 ++ source/m_menu.cpp | 31 +++++++++----- source/m_menu.h | 5 ++- source/m_menu_gamepad.cpp | 85 +++++++++++++++++++++++++++++++++++++++ source/m_menu_gamepad.h | 19 +++++++++ source/m_menu_tecles.cpp | 2 - source/main.cpp | 27 ++++++++++--- 8 files changed, 166 insertions(+), 20 deletions(-) create mode 100644 source/m_menu_gamepad.cpp create mode 100644 source/m_menu_gamepad.h diff --git a/source/config.cpp b/source/config.cpp index 0c4e309..e0ed993 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -7,6 +7,7 @@ namespace config bool musicEnabled = true; int soundMode = SOUND_ALL; uint8_t keys[6] = {SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_SPACE, SDL_SCANCODE_RETURN}; + int8_t pad_btns[6] = {0, 1, 2, 3, 4, 5}; void setMusic(const bool value) { @@ -55,4 +56,17 @@ namespace config { return keys[which]; } + + void definePadBtn(const int which, const int btn) + { + static const char* nomtecles[6] = {"btnup", "btndown", "btnleft", "btnright", "btnjump", "btnpick"}; + pad_btns[which] = btn; + char tmp[5]; + file::setConfigValue(nomtecles[which], SDL_itoa(btn, tmp, 10)); + } + + const int getPadBtn(const int which) + { + return keys[which]; + } } diff --git a/source/config.h b/source/config.h index 182a3f1..92a5d4b 100644 --- a/source/config.h +++ b/source/config.h @@ -24,4 +24,7 @@ namespace config void defineKey(const int which, const int key); const int getKey(const int which); + void definePadBtn(const int which, const int btn); + const int getPadBtn(const int which); + } \ No newline at end of file diff --git a/source/m_menu.cpp b/source/m_menu.cpp index d2319b0..c59ca34 100644 --- a/source/m_menu.cpp +++ b/source/m_menu.cpp @@ -34,10 +34,10 @@ namespace modules return OPTION_EIXIR; } if (input::keyPressed(SDL_SCANCODE_DOWN) || input::keyPressed(config::getKey(KEY_DOWN))) - selected_option = (selected_option+1)&3; + selected_option++; if (selected_option==5) selected_option=0; if (input::keyPressed(SDL_SCANCODE_UP) || input::keyPressed(config::getKey(KEY_UP))) - selected_option = (selected_option-1)&3; + selected_option--; if (selected_option<0) selected_option=4; if (input::keyPressed(SDL_SCANCODE_SPACE) || input::keyPressed(SDL_SCANCODE_RETURN) || input::keyPressed(config::getKey(KEY_JUMP)) || input::keyPressed(config::getKey(KEY_PICK))) { @@ -60,26 +60,37 @@ namespace modules case OPTION_JUGAR: draw::print2("fg JUGAR AL JOC", 11, 14, YELLOW, FONT_ZOOM_VERTICAL); draw::print2("de REDEFINIR TECLES", 11, 17, GREEN, FONT_ZOOM_NONE); - draw::print2("de CONFIGURAR AUDIO", 11, 19, GREEN, FONT_ZOOM_NONE); - draw::print2("de EIXIR", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de REDEFINIR GAMEPAD", 11, 19, GREEN, FONT_ZOOM_NONE); + draw::print2("de CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE); break; case OPTION_TECLES: draw::print2("de JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE); draw::print2("fg REDEFINIR TECLES", 11, 16, YELLOW, FONT_ZOOM_VERTICAL); - draw::print2("de CONFIGURAR AUDIO", 11, 19, GREEN, FONT_ZOOM_NONE); - draw::print2("de EIXIR", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de REDEFINIR GAMEPAD", 11, 19, GREEN, FONT_ZOOM_NONE); + draw::print2("de CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE); + break; + case OPTION_GAMEPAD: + draw::print2("de JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE); + draw::print2("de REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE); + draw::print2("fg REDEFINIR GAMEPAD", 11, 18, YELLOW, FONT_ZOOM_VERTICAL); + draw::print2("de CONFIGURAR AUDIO", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE); break; case OPTION_AUDIO: draw::print2("de JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE); draw::print2("de REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE); - draw::print2("fg CONFIGURAR AUDIO", 11, 18, YELLOW, FONT_ZOOM_VERTICAL); - draw::print2("de EIXIR", 11, 21, GREEN, FONT_ZOOM_NONE); + draw::print2("de REDEFINIR GAMEPAD", 11, 18, GREEN, FONT_ZOOM_NONE); + draw::print2("fg CONFIGURAR AUDIO", 11, 20, YELLOW, FONT_ZOOM_VERTICAL); + draw::print2("de EIXIR", 11, 23, GREEN, FONT_ZOOM_NONE); break; case OPTION_EIXIR: draw::print2("de JUGAR AL JOC", 11, 14, GREEN, FONT_ZOOM_NONE); draw::print2("de REDEFINIR TECLES", 11, 16, GREEN, FONT_ZOOM_NONE); - draw::print2("de CONFIGURAR AUDIO", 11, 18, GREEN, FONT_ZOOM_NONE); - draw::print2("fg EIXIR", 11, 20, YELLOW, FONT_ZOOM_VERTICAL); + draw::print2("de REDEFINIR GAMEPAD", 11, 18, GREEN, FONT_ZOOM_NONE); + draw::print2("de CONFIGURAR AUDIO", 11, 20, GREEN, FONT_ZOOM_NONE); + draw::print2("fg EIXIR", 11, 22, YELLOW, FONT_ZOOM_VERTICAL); break; }; diff --git a/source/m_menu.h b/source/m_menu.h index 40b83dc..ac15835 100644 --- a/source/m_menu.h +++ b/source/m_menu.h @@ -7,8 +7,9 @@ namespace modules #define OPTION_NONE -1 #define OPTION_JUGAR 0 #define OPTION_TECLES 1 - #define OPTION_AUDIO 2 - #define OPTION_EIXIR 3 + #define OPTION_GAMEPAD 2 + #define OPTION_AUDIO 3 + #define OPTION_EIXIR 4 void init(); int loop(); diff --git a/source/m_menu_gamepad.cpp b/source/m_menu_gamepad.cpp new file mode 100644 index 0000000..c923738 --- /dev/null +++ b/source/m_menu_gamepad.cpp @@ -0,0 +1,85 @@ +#include "m_menu_gamepad.h" +#include "jdraw.h" +#include "jinput.h" +#include "config.h" +#include + +namespace modules +{ + namespace menu_gamepad + { + enum states { STATE_SELECTING, STATE_REDEFINING }; + + int selected_option = MENU_GAMEPAD_AMUNT; + states state = STATE_SELECTING; + char name[6] = "PAD00"; + + const char *getPadBtnName(const int8_t key) + { + name[3] = (key/10)+48; + name[4] = (key%10)+48; + return name; + } + + void init() + { + selected_option = MENU_GAMEPAD_AMUNT; + } + + int loop() + { + if (state == STATE_SELECTING) + { + if (input::keyPressed(SDL_SCANCODE_ESCAPE)) { + return MENU_GAMEPAD_TORNAR; + } + if (input::keyPressed(SDL_SCANCODE_DOWN) || input::keyPressed(config::getKey(KEY_DOWN))) + { selected_option++; if (selected_option>6) selected_option=0; } + + if (input::keyPressed(SDL_SCANCODE_UP) || input::keyPressed(config::getKey(KEY_UP))) + { selected_option--; if (selected_option<0) selected_option=6; } + + if (input::keyPressed(SDL_SCANCODE_SPACE) || input::keyPressed(SDL_SCANCODE_RETURN) || + input::keyPressed(config::getKey(KEY_JUMP)) || input::keyPressed(config::getKey(KEY_PICK))) { + if (selected_option==MENU_GAMEPAD_TORNAR) + return MENU_GAMEPAD_TORNAR; + else + state = STATE_REDEFINING; + } + } else { + const int8_t btn = input::getPadBtnPressed(); + if (btn != SDL_CONTROLLER_BUTTON_INVALID) { + config::definePadBtn(selected_option, btn); + state = STATE_SELECTING; + } + if (input::keyPressed(SDL_SCANCODE_ESCAPE)) state = STATE_SELECTING; + } + + draw::cls(2); + draw::color(1); + + draw::print2("REDEFINIR GAMEPAD", 12, 3, TEAL, FONT_ZOOM_VERTICAL); + + int pos = 10; + static const char *textos[6] = { "AMUNT:", "AVALL:", "ESQUERRA:", "DRETA:", "BOTAR: ", "AGAFAR: " }; + + for (int i=0; i<6; ++i) + { + const bool selected = (i==selected_option); + draw::print2(selected?"fg":"de", 8, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE); + draw::print2(textos[i], 13, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE); + if (!selected || state == STATE_SELECTING) draw::print2(getPadBtnName(config::getPadBtn(i)), 23, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE); + pos += selected?3:2; + } + + const bool selected = (selected_option==MENU_GAMEPAD_TORNAR); + draw::print2(selected?"fg":"de", 8, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE); + draw::print2("TORNAR", 13, pos, selected?YELLOW:GREEN, selected?FONT_ZOOM_VERTICAL:FONT_ZOOM_NONE); + + draw::print2("(C) JAILDOCTOR 2024", 11, 28, TEAL, FONT_ZOOM_NONE); + + draw::render(); + return MENU_GAMEPAD_NONE; + } + } +} diff --git a/source/m_menu_gamepad.h b/source/m_menu_gamepad.h new file mode 100644 index 0000000..bb91a99 --- /dev/null +++ b/source/m_menu_gamepad.h @@ -0,0 +1,19 @@ +#pragma once + +namespace modules +{ + namespace menu_gamepad + { + #define MENU_GAMEPAD_NONE -1 + #define MENU_GAMEPAD_AMUNT 0 + #define MENU_GAMEPAD_AVALL 1 + #define MENU_GAMEPAD_ESQUERRA 2 + #define MENU_GAMEPAD_DRETA 3 + #define MENU_GAMEPAD_BOTAR 4 + #define MENU_GAMEPAD_AGAFAR 5 + #define MENU_GAMEPAD_TORNAR 6 + + void init(); + int loop(); + } +} diff --git a/source/m_menu_tecles.cpp b/source/m_menu_tecles.cpp index 66add19..0481cc9 100644 --- a/source/m_menu_tecles.cpp +++ b/source/m_menu_tecles.cpp @@ -50,8 +50,6 @@ namespace modules if (input::keyPressed(SDL_SCANCODE_SPACE) || input::keyPressed(SDL_SCANCODE_RETURN) || input::keyPressed(config::getKey(KEY_JUMP)) || input::keyPressed(config::getKey(KEY_PICK))) { - //if (selected_option==MENU_AUDIO_MUSICA) config::toggleMusic(); - //if (selected_option==MENU_AUDIO_SO) config::toggleSound(); if (selected_option==MENU_TECLES_TORNAR) return MENU_TECLES_TORNAR; else diff --git a/source/main.cpp b/source/main.cpp index 0542aae..35e3944 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -18,6 +18,7 @@ #include "m_gameover.h" #include "m_catslife.h" #include "m_menu_tecles.h" +#include "m_menu_gamepad.h" #include "m_menu_audio.h" #include "m_editor_map.h" #include "m_editor_templates.h" @@ -32,12 +33,13 @@ #define M_GAMEOVER 4 #define M_CATSLIFE 5 #define M_MENU_TECLES 6 -#define M_MENU_AUDIO 7 -#define M_EDITOR_MAP 8 -#define M_EDITOR_TEMPLATES 9 -#define M_EDITOR_COLORS 10 -#define M_EDITOR_BITMAP_FILE 11 -#define M_EDITOR_BITMAP 12 +#define M_MENU_GAMEPAD 7 +#define M_MENU_AUDIO 8 +#define M_EDITOR_MAP 9 +#define M_EDITOR_TEMPLATES 10 +#define M_EDITOR_COLORS 11 +#define M_EDITOR_BITMAP_FILE 12 +#define M_EDITOR_BITMAP 13 int current_module = M_LOGO; @@ -69,6 +71,13 @@ void loadConfig() const int value = SDL_atoi( file::getConfigValue(nomtecles[i]).c_str() ); if (value != 0) config::defineKey(i, value); } + + static const char* nombotons[6] = {"btnup", "btndown", "btnleft", "btnright", "btnjump", "btnpick"}; + for (int i=0; i<6; ++i) + { + const int value = SDL_atoi( file::getConfigValue(nombotons[i]).c_str() ); + if (value != 0) config::defineKey(i, value); + } } void game::init() @@ -127,6 +136,7 @@ bool game::loop() if (option == OPTION_EIXIR) return false; if (option == OPTION_JUGAR) { modules::game::init(); current_module = M_GAME; } if (option == OPTION_TECLES) { modules::menu_tecles::init(); current_module = M_MENU_TECLES; } + if (option == OPTION_GAMEPAD) { modules::menu_gamepad::init(); current_module = M_MENU_GAMEPAD; } if (option == OPTION_AUDIO) { modules::menu_audio::init(); current_module = M_MENU_AUDIO; } } break; @@ -210,6 +220,11 @@ bool game::loop() modules::menu::init(); current_module = M_MENU; } break; + case M_MENU_GAMEPAD: + if (modules::menu_gamepad::loop() == MENU_GAMEPAD_TORNAR) { + modules::menu::init(); current_module = M_MENU; + } + break; case M_MENU_AUDIO: if (modules::menu_audio::loop() == MENU_AUDIO_TORNAR) { modules::menu::init(); current_module = M_MENU;