I am making a C++ SDL based game engine, currently doing the InputManager, check my…
- User
I am making a C++ SDL based game engine, currently doing the InputManager, check my class and make the necessary fixes, things I know are wrong:
- at method
- KeyPressed and KeyReleased are wrong.
- SetMouseKey not used yet, don't know if I need it.
- Only one gamepad is on support, I want to support X gamepads.
- other fixes.
class InputManager { public: ~InputManager() = default; InputManager(const InputManager&) = delete; InputManager operator=(const InputManager&) = delete; static InputManager& GetInstance(); void Update(); bool IsKeyPressed(Input::KeyCode key) const; bool IsKeyReleased(Input::KeyCode key) const; bool IsMouseButtonPressed(Input::MouseButton button) const; Point GetMousePosition() const; bool IsGamepadButtonDown(Input::GamepadButton button, int gamepadIndex) const;
private: InputManager(); void KeyPressed(uint8_t keyPressed); void KeyReleased(uint8_t keyReleased); void SetMouseState(uint8_t mouseButton, bool pressed); SDL_GameController* GetGamepad(); std::unordered_map<Input::KeyCode, bool> currentKeyboardState; std::unordered_map<Input::KeyCode, bool> previousKeyboardState; std::unordered_map<Input::MouseButton, bool> mouseButtonsState; SDL_GameController* gamepad;};
#include "InputManager.h"
#include #include
uint8_t KeyToSDL(Input::KeyCode keyCode); Input::KeyCode KeyFromSDL(uint8_t sdlKeyCode); uint8_t MouseButtonToSDL(Input::MouseButton mouseButton); Input::MouseButton MouseButtonFromSDL(uint8_t sdlMouseButton); SDL_GameControllerButton GamepadButtonToSDL(Input::GamepadButton gamepadButton); Input::GamepadButton GamepadButtonFromSDL(SDL_GameControllerButton sdlGamepadButton);
InputManager::InputManager() { }
InputManager& InputManager::GetInstance() { static InputManager instance;
return instance;}
void InputManager::Update() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: { KeyPressed(event.key.keysym.sym); break; } case SDL_KEYUP: { KeyReleased(event.key.keysym.sym); break; } case SDL_MOUSEBUTTONUP: { SetMouseState(event.button.button, false); break; } case SDL_MOUSEBUTTONDOWN: { SetMouseState(event.button.button, true); break; } case SDL_CONTROLLERDEVICEADDED: { if (!gamepad) { gamepad = SDL_GameControllerOpen(event.cdevice.which); } break; } case SDL_CONTROLLERDEVICEREMOVED: { if (gamepad && event.cdevice.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad))) { SDL_GameControllerClose(gamepad); gamepad = GetGamepad(); } break; } } } }
bool InputManager::IsKeyPressed(Input::KeyCode key) const { return currentKeyboardState.at(key) && !previousKeyboardState.at(key); }
bool InputManager::IsKeyReleased(Input::KeyCode key) const { return !currentKeyboardState.at(key) && previousKeyboardState.at(key); }
- Assistant
- User
It is looking good, now finish the methods you forgot to do
- Assistant
- User
OK, now is taking form, now I need to help me ifnis hthe two update methods, the key update I currently have are:
void InputManager::KeyPressed(uint8_t keyPressed) { previousKeyboardState.at(KeyFromSDL(keyPressed)) = false; currentKeyboardState.at(KeyFromSDL(keyPressed)) = true; }
void InputManager::KeyReleased(uint8_t keyReleased) { previousKeyboardState.at(KeyFromSDL(keyReleased)) = true; currentKeyboardState.at(KeyFromSDL(keyReleased)) = false; }
I don't know how to translate them to the UpdateKeyboard you suggested
- Assistant
- User
Do we really need to store the GamepadStates? currently we can ask the pressed button directly, should we store it? If so we need to change the UpdateGamepadState() to reflect that. Another thing is the SetMouseState, why didn't you wrote it? Should we keep it?
- Assistant
- User
The mouse is done but now I am torn apart between adding the previousGamepadState or not, I do plan on support varying games but mostly will be RPG, If I ever decide to do a platform game or an action RPG game, would I be needing the Button pressed and button released methods for the gamepad?
- Assistant
- User
this is how I had with the keyboard and you changed both methods to Update, but now you keep both methods (pressed and released) for the gamepad. why?
- Assistant
Apologies for the confusion. The methodology I provided earlier for the keyboard should apply consistently across all input devices, including gamepads, for the sake of uniformity and maintainability. The approach for gamepad input should mimic that for the keyboard. The individual and methods were a remnant of an earlier concept.
- User
OK I will keep the older way of separated events, what I do with the old code: std::vector<SDL_GameController*> gamepads; SDL_GameController* GetGamepad(int index);
case SDL_CONTROLLERDEVICEADDED: { int index = event.cdevice.which; if (index < static_cast(gamepads.size()) && !gamepads[index]) { gamepads[index] = SDL_GameControllerOpen(index); } else if (index >= static_cast(gamepads.size())) { gamepads.resize(index + 1); gamepads[index] = SDL_GameControllerOpen(index); } break; } case SDL_CONTROLLERDEVICEREMOVED: { int index = event.cdevice.which; if (index < static_cast(gamepads.size()) && gamepads[index]) { SDL_GameControllerClose(gamepads[index]); gamepads[index] = nullptr; } break; }
- Assistant
Given that you want to handle gamepad button events separately and maintain the ability to access any gamepad using an index, here's how we can refine the
SDL_CONTROLLERDEVICEADDEDand code blocks to work with the separated event-driven model: - User
No, you missunderstood what I meant, the old code is working correctly and you just re copied it again, I want to know how can I connect with the new button pressed code if there is a way to connect it or not, or if the code will only be used for connection and disconnections and nothing else
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.