Skip to content

Instantly share code, notes, and snippets.

@profan
Created May 22, 2014 21:40
Show Gist options
  • Save profan/dc9cf860e5bb1f8482fc to your computer and use it in GitHub Desktop.
Save profan/dc9cf860e5bb1f8482fc to your computer and use it in GitHub Desktop.
Key Event Manager
#ifndef VOX_KEYEVENTMANAGER_HPP
#define VOX_KEYEVENTMANAGER_HPP
#include "easylogging++.h"
#include "Keyboard.hpp"
#include "KeyNew.hpp"
#include <SDL2/SDL.h>
#include <vector>
#include "Profiler.h"
namespace vox {
class KeyEventManager {
public:
KeyEventManager() :
pressedkeys_(Keyboard::getKeyboardState())
{
LOG(INFO) << "KeyEventManager created.";
}
~KeyEventManager() {
LOG(INFO) << "KeyEventManager destroyed.";
}
void addKeyListener(SDL_Scancode key, std::function<void(void)> callback) {
KeyNew newkey{key, callback};
listeners_.push_back(newkey);
}
void handleEvent() {
for(int i = 0; i < listeners_.size(); ++i) {
if (pressedkeys_[listeners_[i].scancode]) {
listeners_[i].callback();
}
}
}
private:
const uint8_t* pressedkeys_;
std::vector<KeyNew> listeners_;
}; //KeyEventManager
} //vox
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment