AutoSkill cpp_sdl_input_manager_abstraction
Design and implement a singleton InputManager for an SDL-based game engine that abstracts SDL dependencies behind an engine-agnostic API. It encapsulates gamepad logic in a dedicated class, uses unordered maps for robust device management, and tracks input state transitions.
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/cpp_sdl_input_manager_abstraction" ~/.claude/skills/ecnu-icalk-autoskill-cpp-sdl-input-manager-abstraction && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8/cpp_sdl_input_manager_abstraction/SKILL.mdcpp_sdl_input_manager_abstraction
Design and implement a singleton InputManager for an SDL-based game engine that abstracts SDL dependencies behind an engine-agnostic API. It encapsulates gamepad logic in a dedicated class, uses unordered maps for robust device management, and tracks input state transitions.
Prompt
Role & Objective
Act as a C++ Game Engine Architect. Design and implement an InputManager system that abstracts the underlying SDL library. The goal is to provide a clean, engine-agnostic API for input handling while using SDL internally for the actual implementation.
Architecture & Abstraction
- Singleton Pattern: The InputManager must be implemented as a singleton (e.g.,
).GetInstance() - Encapsulation of SDL: The public header file must NOT include SDL headers or expose SDL types (e.g.,
,SDL_Keycode
,SDL_Event
). Keep SDL strictly within theSDL_Joystick
file..cpp - Custom Types: Define custom enums to represent input states, such as
,KeyCode
, andMouseButton
. These must be used in the public API instead of SDL types.GamepadButton - Generality: Do not implement game-specific logic or actions (e.g.,
,Shoot()
,Jump()
). The InputManager should only track and report the state of input devices.MovePlayer() - Device-Centric Design: Design the system to manage the state of one keyboard, one mouse, and multiple gamepads, allowing other systems to query this state generically.
Gamepad Implementation Details
- Class Encapsulation: Encapsulate all gamepad-specific logic (state, SDL controller pointer, updates) into a separate
class. The InputManager should only manage the collection of gamepads and forward events.Gamepad - Robust Storage: Use
to store gamepad instances. Key the map by the SDL Instance ID (not the device index) to handle dynamic connection/disconnection events correctly.std::unordered_map<int, Gamepad> - Resource Management: The
class must manage theGamepad
resource using RAII (close in destructor).SDL_GameController* - Error Handling: Throw exceptions in the
constructor ifGamepad
fails. Catch these exceptions in theSDL_GameControllerOpen
's update loop to prevent crashes.InputManager - Map Safety: Do not use
on the gamepad map in const methods or if the key might not exist (to avoid default construction errors). Useoperator[]
orfind()
instead.insert()
State Management & Workflow
- Update Loop: Implement an
method that polls SDL events and updates internal state maps.Update() - State Tracking: Implement
andcurrent
state maps for buttons.previous
: Returns true if current is true AND previous is false (or missing).IsButtonPressed
: Returns true if current is false (or missing) AND previous is true.IsButtonReleased
- Conversion Logic: Implement private helper functions in the
file to convert SDL-specific codes (like.cpp
) into the custom engine enums.SDLK_...
Anti-Patterns
- Do not include
in the public header file.#include <SDL.h> - Do not use
for gamepad storage if indices are not guaranteed to be stable.std::vector - Do not mix gamepad logic directly into the InputManager class (use the
class).Gamepad - Do not implement game logic methods like
inside the InputManager.HandlePlayerAction() - Do not expose
structures or SDL types to the user of the engine.SDL_Event - Do not assume a specific number of players; handle devices generically.
- Do not rely on device index for long-term identification of a gamepad.
Triggers
- Create an InputManager for my SDL game
- Abstract SDL input in C++
- Refactor my input system to handle gamepads
- Design a device-centric input system
- Implement a singleton input handler