This is my first time actually trying to wrap the the SDL event handling functions in a class of my own. I'm trying to achieve a design similar to unity's where you have this static Input class and you can call it's functions whenever to check for various input events.
Here is the class:
class InputManager
{
private:
InputManager() = default;
InputManager(const InputManager& c) = delete;
private:
static SDL_Event event;
static bool isKeyPressed;
static bool isKeyReleased;
static bool isMousePressed;
static bool isMouseReleased;
static int keyIndex;
static int buttonIndex;
public:
static InputManager* GetInstance();
void Update();
public:
bool KeyDown(int iKeyIndex);
bool KeyStillDown(int iKeyIndex);
bool KeyUp(int iKeyIndex);
bool KeyStillUp(int iKeyIndex);
bool MouseDown(int iButton);
bool MouseStillDown(int iButton);
bool MouseUp(int iButton);
bool MouseStillUp(int iButton);
};
and it's implementations:
SDL_Event InputManager::event;
bool InputManager::isMousePressed = false;
bool InputManager::isMouseReleased = false;
bool InputManager::isKeyPressed = false;
bool InputManager::isKeyReleased = false;
InputManager* InputManager::GetInstance()
{
static InputManager* instace = new InputManager();
return instace;
}
void InputManager::Update()
{
SDL_PollEvent(&event);
}
bool InputManager::KeyDown(int iKeyIndex)
{
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == iKeyIndex && isKeyPressed == false)
{
isKeyPressed = true;
return true;
}
else if(event.type == SDL_KEYUP)
{
isKeyPressed = false;
return false;
}
else
return false;
}
bool InputManager::KeyStillDown(int iKeyIndex)
{
if(event.type == SDL_KEYDOWN &&
event.key.keysym.sym == iKeyIndex)
return true;
else
return false;
}
bool InputManager::KeyUp(int iKeyIndex)
{
if(event.type == SDL_KEYUP && event.key.keysym.sym == iKeyIndex && isKeyReleased == false)
{
isKeyReleased = true;
return true;
}
else
{
isKeyReleased = false;
return false;
}
}
bool InputManager::KeyStillUp(int iKeyIndex)
{
if(event.type == SDL_KEYUP &&
event.key.keysym.sym == iKeyIndex && event.key.repeat == 0)
return true;
else
return false;
}
bool InputManager::MouseDown(int iButton)
{
if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == iButton && isMousePressed == false)
{
isMousePressed = true;
return true;
}
else if(event.type == SDL_MOUSEBUTTONUP)
{
isMousePressed = false;
return false;
}
else
return false;
}
bool InputManager::MouseStillDown(int iButton)
{
if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == iButton)
return true;
else
return false;
}
bool InputManager::MouseUp(int iButton)
{
if(event.type == SDL_MOUSEBUTTONUP && event.button.button == iButton && isMouseReleased == false)
{
isMousePressed = true;
return true;
}
else
{
isMousePressed = false;
return false;
}
}
bool InputManager::MouseStillUp(int iButton)
{
if(event.type == SDL_MOUSEBUTTONUP && event.button.button == iButton)
return true;
else
return false;
}
Now I know this class has it's flaws and it certainly does, for instance the KeyUp(int iKeyIndex)
and MouseUp(int iButton)
functions seem to always be true unless I move the mouse or do any type of event. I'm aware of issue and why it's happening but I can't seem to find a solution for it. I would like to know how to fix these issues.