I am making a C++ SDL based game engine, currently and finishing the final touches of my…
- User
I am making a C++ SDL based game engine, currently and finishing the final touches of my classes, In my InputManager class, you said to me in another instance:
- You may want to use SDL_Scancode instead of SDL_Keycode for key mapping to abstract away the specific keyboard layout.
- Key Identifier: Using a std::string as a key might seem flexible, but it’s less performant due to string comparison and hashing overhead. If your key identifiers can be reduced to an enum or integer identifiers, it would be more efficient. If you have a predefined set of keys, consider an enum (which can also map to strings for configurable keys).
- Methods like KeyPressed and KeyReleased will be called frequently, potentially multiple times per frame. It’s crucial to make these methods as efficient as possible. This might include optimizing your controls data structure for quick lookups.
How can I do this?
public: ~InputManager() = default; InputManager(const InputManager&) = delete; InputManager operator=(const InputManager&) = delete; static InputManager& GetInstance(); void Update(); bool KeyPressed(const std::string& key, int player = 0); bool KeyReleased(const std::string& key, int player = 0); void AddKey(const std::string& key, SDL_Keycode code, int player = 0); void AddKeys(const std::unordered_map<std::string, SDL_Keycode>& keyMap, int player = 0); void ModifyKey(const std::string& key, SDL_Keycode newCode, int player = 0);
private: InputManager(); void CheckNewPlayers(int player); std::vector<std::unordered_map<std::string, SDL_Keycode>> controls; std::vector<std::vector<Uint8>> currentState; std::vector<std::vector<Uint8>> previousState; - Assistant
- User
I don't want to fix 2) like this, just keep the other two fixes but not 2, since I want the flexibility of not defining the GameKey since I want to support any game
- Assistant
- User
I want to abstract SDL and take away SDL_Keycode or SDL_Scancode, or anything related to SDL, how can I do that without exposing it?
- Assistant