From 04c4e1941378bf821b822870934090c2b74f7fcd Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Fri, 2 Feb 2024 00:35:27 +0100 Subject: [PATCH] working controller input -> does not work for main menu navigation --- include/Constants.h | 1 + include/KeyboardController.h | 9 ++++++++- src/Game.cpp | 17 +++++++++++++++-- src/KeyboardController.cpp | 19 ++++++++----------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/Constants.h b/include/Constants.h index 20ccf44..2760a73 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -11,6 +11,7 @@ constexpr std::size_t MAX_COMPONENTS = 32; constexpr std::size_t MAX_GROUPS = 32; constexpr std::size_t MAX_STATS = 8; constexpr std::size_t MAX_TEAMS = 8; +constexpr std::size_t MAX_GAME_CONTROLLERS = 4; constexpr int SCREEN_SIZE_HEIGHT = 640; constexpr int SCREEN_SIZE_WIDTH = 800; diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 9079b73..e4f39e0 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -2,10 +2,13 @@ #include #include "Component.h" +#include "SDL_gamecontroller.h" #include "Vector2D.h" #include "Constants.h" #include "SoundManager.h" +#define MIN_JOYSTICK_INPUT 4096 // dpad stuck as -129 after input + class TransformComponent; class SpriteComponent; @@ -13,12 +16,16 @@ class KeyboardController : public Component { public: TransformComponent* transform; + + SDL_GameController* gameController; + /* const uint8_t* keystates = SDL_GetKeyboardState(NULL); SDL_Scancode up; SDL_Scancode down; SDL_Scancode left; SDL_Scancode right; SDL_Scancode fire; + */ SpriteComponent* sprite; @@ -27,7 +34,7 @@ public: uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed KeyboardController() = default; - KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity); + KeyboardController(SDL_GameController* gameController, Vector2D fireVelocity); ~KeyboardController() = default; void init() override; diff --git a/src/Game.cpp b/src/Game.cpp index 33c50c8..e0db44c 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -9,6 +9,8 @@ #include "Entity.h" #include "HealthComponent.h" #include "Map.h" +#include "SDL_gamecontroller.h" +#include "SDL_joystick.h" #include "TextureManager.h" #include "StatEffectsComponent.h" #include "Constants.h" @@ -21,6 +23,7 @@ TextureManager* Game::textureManager = new TextureManager(); SoundManager* Game::soundManager = new SoundManager(); CollisionHandler* Game::collisionHandler = new CollisionHandler(manager); +std::array gameControllers; SDL_Renderer* Game::renderer = nullptr; @@ -37,6 +40,7 @@ Game::~Game() = default; void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { + SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1"); int flags = 0; if (fullscreen) { @@ -50,6 +54,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } + for (int i = 0, c = 0; i < SDL_NumJoysticks(); i++) { + if (SDL_IsGameController(i)) { + gameControllers.at(c++) = SDL_GameControllerOpen(i); + } + if (c > MAX_GAME_CONTROLLERS) { + break; + } + } + if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) { std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl; return; @@ -162,7 +175,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player1.setTeam(TeamLabel::BLUE); player1.addComponent(80,80,2); //posx, posy, scale player1.addComponent(player1Sprite, true); //adds sprite (32x32px), path needed - player1.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added + player1.addComponent(gameControllers.at(0), Vector2D(2, 0));//custom keycontrols can be added player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent(5, Direction::LEFT); player1.addComponent(); @@ -172,7 +185,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player2.setTeam(TeamLabel::RED); player2.addComponent(600, 500, 2); player2.addComponent(player2Sprite, true); - player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); + player2.addComponent(gameControllers.at(1), Vector2D(-2, 0)); player2.addComponent("enemy", 0.8f); player2.addComponent(5, Direction::RIGHT); player2.addComponent(); diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 26324af..275e83c 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -3,15 +3,12 @@ #include "Game.h" #include "Components.h" #include "AssetManager.h" +#include "SDL_gamecontroller.h" #include "SpriteComponent.h" -KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity) +KeyboardController::KeyboardController(SDL_GameController* gameController, Vector2D fireVelocity) { - this->up = up; - this->down = down; - this->left = left; - this->right = right; - this->fire = fire; + this->gameController = gameController; this->fireVelocity = fireVelocity; } @@ -27,30 +24,30 @@ void KeyboardController::update() transform->direction.y = 0; sprite->playAnimation(IDLE); - if (keystates[this->up]) { + if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 1) < MIN_JOYSTICK_INPUT * -1) { // transform->direction.y = -1; sprite->playAnimation(WALK); SoundManager::playSound(STEPS); } - if (keystates[this->left]) { + if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 0) < MIN_JOYSTICK_INPUT * -1) { transform->direction.x = -1; sprite->playAnimation(WALK); sprite->setDirection(Direction::LEFT); SoundManager::playSound(STEPS); } - if (keystates[this->down]) { + if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 1) > MIN_JOYSTICK_INPUT) { transform->direction.y = 1; sprite->playAnimation(WALK); SoundManager::playSound(STEPS); } - if (keystates[this->right]) { + if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 0) > MIN_JOYSTICK_INPUT) { transform->direction.x = 1; sprite->playAnimation(WALK); sprite->setDirection(Direction::RIGHT); SoundManager::playSound(STEPS); } - if (keystates[this->fire]) { + if (SDL_GameControllerGetButton(this->gameController, SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_A)) { Uint32 currentTicks = SDL_GetTicks();