Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "boost/function.hpp"
- template<class T>
- void InputManager::registerEvent(SDL_Keycode key,KeyState state,boost::function<void ()> const& function)
- {
- auto &inputEvents = (state == KeyState::Up) ? m_keyUpEvents : m_keyDownEvents;
- if(inputEvents.find(key) == inputEvents.end())
- {
- inputEvents.insert(std::make_pair(key, boost::signals2::signal<void()>()));
- m_keyStates[key] = KeyState::Up;
- }
- inputEvents[key].connect(boost::bind(T::function, instance));
- }
- #ifndef SSB_INPUTMANAGER_HPP
- #define SSB_INPUTMANAGER_HPP
- #include <functional>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <SDL_keycode.h>
- #include <SDL_events.h>
- #include <boost/signals2.hpp>
- enum KeyState{
- Down,
- Up
- };
- class InputManager
- {
- public:
- InputManager();
- template<class T>
- void registerEvent(SDL_Keycode key,KeyState state, boost::function<void ()> const& function);
- void pollEvent(SDL_Event event);
- private:
- std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyDownEvents;
- std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyUpEvents;
- std::map<SDL_Keycode, KeyState> m_keyStates;
- };
- #include "InputManager.tcc"
- #endif //SSB_INPUTMANAGER_HPP
- #ifndef SSB_PLAYERCONTROLLER_HPP
- #define SSB_PLAYERCONTROLLER_HPP
- class PlayerController
- {
- public:
- void jump();
- private:
- Player m_player;
- };
- #endif //SSB_PLAYERCONTROLLER_HPP
- PlayerController playerController;
- InputManager inputController;
- inputController.registerEvent(SDLK_0, KeyState::Down, playerController.jump());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement