From fa764894490a35ab1e13f8f27031fd3af7b56455 Mon Sep 17 00:00:00 2001 From: Alfie King Date: Fri, 13 Sep 2024 12:12:31 +0100 Subject: [PATCH] controller and mouse support --- .gitignore | 3 ++- include/input.h | 25 +++++++++++++++++++++ include/vector.h | 9 ++++++++ src/example-game.cpp | 18 +++++++++------ src/input.cpp | 52 +++++++++++++++++++++++++++++++++++++------- 5 files changed, 91 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 6a8bc10..45dba51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode -build \ No newline at end of file +build +main.x86_64 \ No newline at end of file diff --git a/include/input.h b/include/input.h index bab3c4b..7858f0c 100644 --- a/include/input.h +++ b/include/input.h @@ -2,6 +2,27 @@ #include #include +struct Controller +{ + SDL_GameController* controller; + std::string name; + std::map buttons; + Sint16 leftStickX = 0; + Sint16 leftStickY = 0; + Sint16 rightStickX = 0; + Sint16 rightStickY = 0; + Sint16 leftTrigger = 0; + Sint16 rightTrigger = 0; +}; + +struct Mouse +{ + int x; + int y; + int wheel; + std::map buttons; +}; + class Input { private: @@ -9,6 +30,10 @@ class Input public: bool exit; + std::map controllers; std::map activeKeys; + Mouse mouse; + + Input(); void update(); }; \ No newline at end of file diff --git a/include/vector.h b/include/vector.h index 8f660f6..76c938d 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,9 +1,14 @@ +// prevent multiple inclusion #pragma once +// class for 2D float vector class float_vec2 { + // public members public: + // x and y coordinates float x, y; + // operator functions float_vec2 operator+(float_vec2 vec) { return { x + vec.x, y + vec.y }; @@ -26,10 +31,14 @@ class float_vec2 } }; +// class for 2D integer vector class int_vec2 { + // public members public: + // x and y coordinates int x, y; + // operator functions int_vec2 operator+(int_vec2 vec) { return { x + vec.x, y + vec.y }; diff --git a/src/example-game.cpp b/src/example-game.cpp index b934570..27fd56e 100644 --- a/src/example-game.cpp +++ b/src/example-game.cpp @@ -77,29 +77,33 @@ int main(int argc, char* argv[]) if (engine->input.exit || engine->input.activeKeys[SDLK_ESCAPE]) { quit(); - } + } - if (engine->input.activeKeys[SDLK_d]) + if (engine->input.activeKeys[SDLK_d] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] || engine->input.controllers[0].leftStickX > 8000) { animations->setAnimation("walk_right"); sprite->move({ 1, 0 }); } - if (engine->input.activeKeys[SDLK_a]) + if (engine->input.activeKeys[SDLK_a] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] || engine->input.controllers[0].leftStickX < -8000) + { animations->setAnimation("walk_left"); sprite->move({ -1, 0 }); } - if (engine->input.activeKeys[SDLK_w]) + if (engine->input.activeKeys[SDLK_w] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] || engine->input.controllers[0].leftStickY < -8000) { animations->setAnimation("walk_back"); sprite->move({ 0, -1 }); } - if (engine->input.activeKeys[SDLK_s]) + if (engine->input.activeKeys[SDLK_s] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] || engine->input.controllers[0].leftStickY > 8000) { animations->setAnimation("walk_front"); sprite->move({ 0, 1 }); } - if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s]) + if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s] + && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] + && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] + && engine->input.controllers[0].leftStickX == 0 && engine->input.controllers[0].leftStickY == 0) { animations->setAnimation("idle"); } @@ -113,7 +117,7 @@ int main(int argc, char* argv[]) // Draw sprite animations->draw(engine->renderer); - // Update screen + // Update screen engine->render(); } return 0; diff --git a/src/input.cpp b/src/input.cpp index eceb96a..3bab838 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,20 +1,56 @@ #include "input.h" +Input::Input() +{ + SDL_Init(SDL_INIT_JOYSTICK); + exit = false; +} + void Input::update() { while (SDL_PollEvent(&event)) { - if (event.type == SDL_KEYDOWN) - { - activeKeys[event.key.keysym.sym] = true; - } - else if (event.type == SDL_KEYUP) + switch (event.type) { + case SDL_CONTROLLERBUTTONDOWN: + controllers[event.cbutton.which].buttons[event.cbutton.button] = true; + break; + case SDL_CONTROLLERBUTTONUP: + controllers[event.cbutton.which].buttons[event.cbutton.button] = false; + break; + case SDL_MOUSEBUTTONDOWN: + mouse.buttons[event.button.button] = true; + break; + case SDL_MOUSEBUTTONUP: + mouse.buttons[event.button.button] = false; + break; + case SDL_KEYDOWN: + activeKeys[event.key.keysym.sym] = true; + break; + case SDL_KEYUP: activeKeys.erase(event.key.keysym.sym); - } - else if (event.type == SDL_QUIT) - { + break; + case SDL_CONTROLLERDEVICEADDED: + controllers[SDL_NumJoysticks() - 1] = { + SDL_GameControllerOpen(SDL_NumJoysticks() - 1), + SDL_GameControllerName(SDL_GameControllerOpen(SDL_NumJoysticks() - 1)) + }; + printf("Controller added: %s\n", controllers[SDL_NumJoysticks() - 1].name.c_str()); + break; + case SDL_QUIT: exit = true; + break; } + for (auto& controller : controllers) + { + controller.second.leftStickX = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_LEFTX); + controller.second.leftStickY = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_LEFTY); + controller.second.rightStickX = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_RIGHTX); + controller.second.rightStickY = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_RIGHTY); + controller.second.leftTrigger = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT); + controller.second.rightTrigger = SDL_GameControllerGetAxis(controller.second.controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT); + } + SDL_GetMouseState(&mouse.x, &mouse.y); + mouse.wheel = event.wheel.y; } } \ No newline at end of file