Advertisement
Paul17041993

KeyState.h

Jul 25th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef _KEYSTATE_H_
  2. #define _KEYSTATE_H_
  3.  
  4. #include "PaulMath.h"
  5. #include "GL/glfw.h"
  6.  
  7. static class KeyState
  8. {
  9. private:
  10.     //arrays
  11.     static BitSet * m_keyVal;
  12.     static BitSet * m_keyState;
  13.    
  14.     //key update
  15.     static void GLFWCALL UpdateSingle(int key, int val)
  16.     {
  17.         //update value
  18.         if(!(*(m_keyState))[key] && val)
  19.             m_keyVal->SetBit(key);
  20.  
  21.         //update state
  22.         if(val)
  23.             m_keyState->SetBit(key);
  24.         else
  25.             m_keyState->ClearBit(key);
  26.     }
  27.  
  28. public:
  29.     //construct
  30.     KeyState(void)
  31.     {
  32.         m_keyVal = new BitSet(GLFW_KEY_LAST);
  33.         m_keyState = new BitSet(GLFW_KEY_LAST);
  34.     }
  35.     //destruct
  36.     ~KeyState(void)
  37.     {
  38.         delete m_keyVal;
  39.         delete m_keyState;
  40.         glfwSetKeyCallback(NULL);
  41.     }
  42.  
  43.     //needed to be done after GL init, so it works correctly
  44.     void InitCallBack() { glfwSetKeyCallback(KeyState::UpdateSingle); }
  45.  
  46.     //call at end of every cycle to get desired logic
  47.     void Clear() { m_keyVal->ClearAllBits(); }
  48.  
  49.     //for getting a key
  50.     bool GetKey(const int key) const { return (*(m_keyVal))[key]; }
  51.  
  52. }KeyState;
  53.  
  54. //without these we get linker errors,
  55. BitSet * KeyState::m_keyVal = NULL;
  56. BitSet * KeyState::m_keyState = NULL;
  57.  
  58. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement