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 | - | class KeyState |
| 7 | + | static class KeyState |
| 8 | {
| |
| 9 | ||
| 10 | private: | |
| 11 | - | KeyState(void) { keys = new BitSet(GLFW_KEY_LAST); keysState = new BitSet(GLFW_KEY_LAST); }
|
| 11 | + | struct Keys |
| 12 | - | ~KeyState(void) { delete keys; delete keysState; }
|
| 12 | + | |
| 13 | BitSet * m_keyVal; | |
| 14 | - | //call this every frame |
| 14 | + | BitSet * m_keyState; |
| 15 | - | void Update() |
| 15 | + | }; |
| 16 | ||
| 17 | - | for(int i = 0; i < GLFW_KEY_LAST; ++i) |
| 17 | + | static Keys * s_inst; |
| 18 | ||
| 19 | - | //update keys |
| 19 | + | |
| 20 | - | if(!(*keysState)[i] && !(*keys)[i]) |
| 20 | + | |
| 21 | - | {
|
| 21 | + | KeyState(void) |
| 22 | - | if(glfwGetKey(i)) keys->SetBit(i); |
| 22 | + | |
| 23 | - | } |
| 23 | + | s_inst = new Keys; |
| 24 | - | else keys->ClearBit(i); |
| 24 | + | s_inst->m_keyVal = new BitSet(GLFW_KEY_LAST); |
| 25 | - | //update states |
| 25 | + | s_inst->m_keyState = new BitSet(GLFW_KEY_LAST); |
| 26 | - | if(glfwGetKey(i)) keysState->SetBit(i); |
| 26 | + | glfwSetKeyCallback(KeyState::UpdateSingle); |
| 27 | - | else keysState->ClearBit(i); |
| 27 | + | |
| 28 | ||
| 29 | ~KeyState(void) { delete s_inst->m_keyVal; delete s_inst->m_keyState; glfwSetKeyCallback(NULL); }
| |
| 30 | ||
| 31 | - | bool GetKey(int key){ return (*keys)[key]; }
|
| 31 | + | static void GLFWCALL UpdateSingle(int key, int val) |
| 32 | {
| |
| 33 | //update values | |
| 34 | - | //bitset arrays via pointers (we have to do it this way) |
| 34 | + | if(!(*(s_inst->m_keyState))[key] && !(*(s_inst->m_keyVal))[key]) |
| 35 | - | BitSet * keys; |
| 35 | + | |
| 36 | - | BitSet * keysState; |
| 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 |