SHOW:
|
|
- or go back to the newest paste.
| 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 | - | struct Keys |
| 11 | + | static BitSet * m_keyVal; |
| 12 | - | {
|
| 12 | + | static BitSet * m_keyState; |
| 13 | - | BitSet * m_keyVal; |
| 13 | + | |
| 14 | - | BitSet * m_keyState; |
| 14 | + | //key update |
| 15 | - | }; |
| 15 | + | |
| 16 | {
| |
| 17 | - | static Keys * s_inst; |
| 17 | + | //update value |
| 18 | if(!(*(m_keyState))[key] && val) | |
| 19 | m_keyVal->SetBit(key); | |
| 20 | - | //constructs and destructs, |
| 20 | + | |
| 21 | - | KeyState(void) |
| 21 | + | //update state |
| 22 | if(val) | |
| 23 | - | s_inst = new Keys; |
| 23 | + | m_keyState->SetBit(key); |
| 24 | - | s_inst->m_keyVal = new BitSet(GLFW_KEY_LAST); |
| 24 | + | |
| 25 | - | s_inst->m_keyState = new BitSet(GLFW_KEY_LAST); |
| 25 | + | m_keyState->ClearBit(key); |
| 26 | - | glfwSetKeyCallback(KeyState::UpdateSingle); |
| 26 | + | |
| 27 | ||
| 28 | public: | |
| 29 | - | ~KeyState(void) { delete s_inst->m_keyVal; delete s_inst->m_keyState; glfwSetKeyCallback(NULL); }
|
| 29 | + | //construct |
| 30 | KeyState(void) | |
| 31 | {
| |
| 32 | m_keyVal = new BitSet(GLFW_KEY_LAST); | |
| 33 | - | //update values |
| 33 | + | m_keyState = new BitSet(GLFW_KEY_LAST); |
| 34 | - | if(!(*(s_inst->m_keyState))[key] && !(*(s_inst->m_keyVal))[key]) |
| 34 | + | |
| 35 | - | {
|
| 35 | + | //destruct |
| 36 | - | if(glfwGetKey(key)) |
| 36 | + | ~KeyState(void) |
| 37 | - | s_inst->m_keyVal->SetBit(key); |
| 37 | + | |
| 38 | - | } |
| 38 | + | delete m_keyVal; |
| 39 | delete m_keyState; | |
| 40 | - | s_inst->m_keyVal->ClearBit(key); |
| 40 | + | glfwSetKeyCallback(NULL); |
| 41 | } | |
| 42 | - | //update states |
| 42 | + | |
| 43 | - | if(glfwGetKey(key)) |
| 43 | + | //needed to be done after GL init, so it works correctly |
| 44 | - | s_inst->m_keyState->SetBit(key); |
| 44 | + | void InitCallBack() { glfwSetKeyCallback(KeyState::UpdateSingle); }
|
| 45 | ||
| 46 | - | s_inst->m_keyState->ClearBit(key); |
| 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 (*(s_inst->m_keyVal))[key]; }
|
| 50 | + | bool GetKey(const int key) const { return (*(m_keyVal))[key]; }
|
| 51 | ||
| 52 | - | }; |
| 52 | + | }KeyState; |
| 53 | ||
| 54 | - | KeyState::Keys* KeyState::s_inst = NULL; |
| 54 | + | //without these we get linker errors, |
| 55 | BitSet * KeyState::m_keyVal = NULL; | |
| 56 | BitSet * KeyState::m_keyState = NULL; | |
| 57 | ||
| 58 | #endif |