I'm using a class with static methods and two static variables, currentKeyState and lastKeyState. What I'm trying to do is call this class's update method every frame, setting the lastKeyState = currentKeyState and setting currentKeyState = SDL_GetKeyState(NULL);
Then I want to run a KeyPressed() method that returns true if currentKeyState[key] && !lastKeyState[key]
I will show the code I'm using and I ask you guys to look it over and maybe offer some advice. Thanks in advance.
Code: Select all
#ifndef INPUTHANDLER_H
#define INPUTHANDLER_H
#include "SDL\SDL.h"
class InputHandler
{
public:
static void Update();
static bool IsKeyDown(SDLKey);
static bool IsKeyUp(SDLKey);
static bool KeyPressed(SDLKey);
static bool KeyReleased(SDLKey);
private:
InputHandler();
~InputHandler();
static Uint8* currentKeyState;
static Uint8* lastKeyState;
};
#endif
Code: Select all
#include "InputHandler.h"
InputHandler::InputHandler() {}
InputHandler::~InputHandler() {}
void InputHandler::Update()
{
lastKeyState = currentKeyState;
currentKeyState = SDL_GetKeyState(NULL);
}
bool InputHandler::IsKeyDown(SDLKey key)
{
return currentKeyState[key];
}
bool InputHandler::IsKeyUp(SDLKey key)
{
return !currentKeyState[key];
}
bool InputHandler::KeyPressed(SDLKey key)
{
return currentKeyState[key] && !lastKeyState[key];
}
bool InputHandler::KeyReleased(SDLKey key)
{
return lastKeyState[key] && !currentKeyState[key];
}
Uint8* InputHandler::currentKeyState = SDL_GetKeyState(NULL);
Uint8* InputHandler::lastKeyState = SDL_GetKeyState(NULL);