Advertisement
Paul17041993

KeyState.h

Jul 25th, 2013
71
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.  
  10. private:
  11.         struct Keys
  12.         {
  13.             BitSet * m_keyVal;
  14.             BitSet * m_keyState;
  15.         };
  16.  
  17.     static Keys * s_inst;
  18.  
  19. public:
  20.     //constructs and destructs,
  21.     KeyState(void) 
  22.     {
  23.         s_inst = new Keys;
  24.         s_inst->m_keyVal = new BitSet(GLFW_KEY_LAST);
  25.         s_inst->m_keyState = new BitSet(GLFW_KEY_LAST);
  26.         glfwSetKeyCallback(KeyState::UpdateSingle);
  27.     }
  28.  
  29.     ~KeyState(void) { delete s_inst->m_keyVal; delete s_inst->m_keyState; glfwSetKeyCallback(NULL); }
  30.  
  31.     static void GLFWCALL UpdateSingle(int key, int val)
  32.     {
  33.         //update values
  34.         if(!(*(s_inst->m_keyState))[key] && !(*(s_inst->m_keyVal))[key])
  35.         {
  36.             if(glfwGetKey(key))
  37.                 s_inst->m_keyVal->SetBit(key);
  38.         }
  39.         else
  40.             s_inst->m_keyVal->ClearBit(key);
  41.  
  42.         //update states
  43.         if(glfwGetKey(key))
  44.             s_inst->m_keyState->SetBit(key);
  45.         else
  46.             s_inst->m_keyState->ClearBit(key);
  47.     }
  48.  
  49.     //for getting a key
  50.     bool GetKey(const int key) const { return (*(s_inst->m_keyVal))[key]; }
  51.  
  52. };
  53.  
  54. KeyState::Keys* KeyState::s_inst = NULL;
  55.  
  56. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement