Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include "boost/function.hpp"
  2. template<class T>
  3. void InputManager::registerEvent(SDL_Keycode key,KeyState state,boost::function<void ()> const& function)
  4. {
  5. auto &inputEvents = (state == KeyState::Up) ? m_keyUpEvents : m_keyDownEvents;
  6.  
  7. if(inputEvents.find(key) == inputEvents.end())
  8. {
  9. inputEvents.insert(std::make_pair(key, boost::signals2::signal<void()>()));
  10. m_keyStates[key] = KeyState::Up;
  11. }
  12.  
  13. inputEvents[key].connect(boost::bind(T::function, instance));
  14. }
  15.  
  16. #ifndef SSB_INPUTMANAGER_HPP
  17. #define SSB_INPUTMANAGER_HPP
  18.  
  19. #include <functional>
  20. #include <algorithm>
  21. #include <vector>
  22. #include <map>
  23. #include <SDL_keycode.h>
  24. #include <SDL_events.h>
  25. #include <boost/signals2.hpp>
  26.  
  27. enum KeyState{
  28. Down,
  29. Up
  30. };
  31.  
  32. class InputManager
  33. {
  34. public:
  35. InputManager();
  36. template<class T>
  37. void registerEvent(SDL_Keycode key,KeyState state, boost::function<void ()> const& function);
  38. void pollEvent(SDL_Event event);
  39.  
  40. private:
  41. std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyDownEvents;
  42. std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyUpEvents;
  43. std::map<SDL_Keycode, KeyState> m_keyStates;
  44. };
  45.  
  46. #include "InputManager.tcc"
  47.  
  48. #endif //SSB_INPUTMANAGER_HPP
  49.  
  50. #ifndef SSB_PLAYERCONTROLLER_HPP
  51. #define SSB_PLAYERCONTROLLER_HPP
  52.  
  53. class PlayerController
  54. {
  55. public:
  56. void jump();
  57. private:
  58. Player m_player;
  59. };
  60.  
  61. #endif //SSB_PLAYERCONTROLLER_HPP
  62.  
  63. PlayerController playerController;
  64. InputManager inputController;
  65.  
  66. inputController.registerEvent(SDLK_0, KeyState::Down, playerController.jump());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement