I am making a C++ SDL based game engine, currently in the middle of abstracting the…
- User
I am making a C++ SDL based game engine, currently in the middle of abstracting the SDL_Key class into my own Key class, help me change this so I don't need to depend on the SDL_key, my idea is to use that for the InputManager I already have:
class InputManager { 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); // Need to hide away SDL_Keycode void AddKeys(const std::unordered_map<std::string, SDL_Keycode>& keyMap, int player = 0); // Need to hide away SDL_Keycode void ModifyKey(const std::string& key, SDL_Keycode newCode, int player = 0); // Need to hide away SDL_Keycode
private: InputManager(); void CheckNewPlayers(int player); std::vector<std::unordered_map<std::string, SDL_Keycode>> controls; std::vector<std::vector<uint8_t>> currentState; std::vector<std::vector<uint8_t>> previousState;};
- Assistant
- User
OK I like the idea, should we keep the name as KeyCode or change it to something else like in C# Keys? How is this enum named in other frameworks or game engines?
- Assistant
The naming conventions for key codes or keys in other frameworks and game engines can vary, but many adhere to straightforward and self-explanatory names. The term "KeyCode" is widely understood among developers, as it clearly refers to the codes representing keys on a keyboard. Similarly, "Keys" is also a commonly used term, especially in environments like .NET/C# with XNA/MonoGame.
Here are some examples of how different frameworks or engines name their key code classes or enums:
- Unity (C#): Unity uses the
KeyCodeenum for representing key codes. This terminology is quite familiar to Unity developers.
- Unity (C#): Unity uses the
- User
OK I will keep this KeyCode, now you brought an interesting thing too, in SFML they distinguish the keyboard keys, now if I wanted to implement other keys like Mouse or Josticks, how can I accomplish this, do I have to add MouseKey and JostickKey or something?
- Assistant
- User
Perfect, should InputEvent be a class or an struct, why did you choose struct first?
- Assistant
In C++, the primary difference between
classandstructis the default access level for their members and base classes: everything isprivateby default inclass, while instruct, it ispublic. That's the only technical difference between the two in the language.Choosing between a
structand aclassusually comes down to convention and the intent of use: - User
OK I got it, now complete the HandleEvents method
- Assistant
- User
You left the code commented again, finish it without commenting on what to do but doing it directly
- Assistant
- User
No you missunderstood what I meant, I don't want to check the events like this, just use the InputManager to deal with the InputEvent instead, also don't add any implementation of the game like shoot or something, I want my engine to be as general as possible:
#include "InputManager.h"
#include #include
InputManager::InputManager() { }
InputManager& InputManager::GetInstance() { static InputManager instance;
return instance; - Assistant
- User
NO scratch all of the last part, you are not understanding me, I want to use SDL internally to handle the inputs in the InputManager, but I don't want the SDL to be exposed OUTSIDE the InputManager class, I don't want my users to know I am using SDL internally but I want to use it to deal with the Keyboard, Mouse and Jostick presses.
- Assistant
- User
Should we move the key codes of each player to a player class instead, and use this InputManager to only handle the events agnostically?
- Assistant
- User
- Assistant
- User
I was thinking to myself, maybe I should make the InputManager agnostic to players or player input, and just have one keyboard, one mouse, and one or more gamepads, but not using any player or anything like that, what do you think of this idea? Maybe separating this will make it more broader, but I don't know if this is correct or not, I have read other InputManager from other developers and they deal with the keyboard states and or mouse states and that's all. Maybe my design was wrong from the start and I should take everything else and leave the broader input events
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.