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:
class InputManager { public: ~InputManager(); 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 IsGamepadButtonPressed(Input::GamepadButton button, int gamepadIndex) const; bool IsGamepadButtonReleased(Input::GamepadButton button, int gamepadIndex) const;
private: InputManager(); void UpdateKeyboardState(); void UpdateGamepadState(); void SetMouseState(uint8_t mouseButton, bool pressed); SDL_GameController* GetGamepad(int index); void GamepadButtonPressed(uint8_t button, int gamepadIndex); void GamepadButtonReleased(uint8_t button, int gamepadIndex); std::unordered_map<Input::KeyCode, bool> currentKeyboardState; std::unordered_map<Input::KeyCode, bool> previousKeyboardState; std::unordered_map<Input::MouseButton, bool> mouseButtonsState; std::vector<SDL_GameController*> gamepads; std::vector<std::unordered_map<Input::GamepadButton, bool>> currentGamepadState; std::vector<std::unordered_map<Input::GamepadButton, bool>> previousGamepadState;};
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() { for (SDL_GameController* gamepad : gamepads) { if (gamepad) { SDL_GameControllerClose(gamepad); } } }
InputManager& InputManager::GetInstance() { static InputManager instance;
return instance;}
void InputManager::Update() { previousKeyboardState = currentKeyboardState;
SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_MOUSEBUTTONUP: { SetMouseState(event.button.button, false); break; } case SDL_MOUSEBUTTONDOWN: { SetMouseState(event.button.button, true); break; } case SDL_CONTROLLERDEVICEADDED: { int index = event.cdevice.which; if (index < static_cast<int>(gamepads.size()) && !gamepads[index]) { gamepads[index] = SDL_GameControllerOpen(index); } else if (index >= static_cast<int>(gamepads.size())) { gamepads.resize(index + 1); gamepads[index] = SDL_GameControllerOpen(index); } break; } case SDL_CONTROLLERDEVICEREMOVED: { int index = event.cdevice.which; if (index < static_cast<int>(gamepads.size()) && gamepads[index]) { SDL_GameControllerClose(gamepads[index]); gamepads[index] = nullptr; } break; } case SDL_CONTROLLERBUTTONDOWN: { GamepadButtonPressed(event.cbutton.button, event.cbutton.which); break; } case SDL_CONTROLLERBUTTONUP: { GamepadButtonReleased(event.cbutton.button, event.cbutton.which); break; } } } UpdateKeyboardState(); - Assistant
- User
- Assistant
The updated code looks much better, and it's great that you've resolved the initial issues and concerns. Your decision to use
uint8_tto abstract away SDL-specific types is reasonable, and it seems you've carefully addressed the feedback so far.Just a couple more tips to make sure your is robust: