dmuratshin 9 years ago
parent
commit
056b2d4044
3 changed files with 34 additions and 3 deletions
  1. 8 0
      oxygine/src/core/oxygine.cpp
  2. 21 0
      oxygine/src/key.cpp
  3. 5 3
      oxygine/src/key.h

+ 8 - 0
oxygine/src/core/oxygine.cpp

@@ -5,6 +5,7 @@
 #include "res/CreateResourceContext.h"
 #include "res/CreateResourceContext.h"
 #include "res/Resources.h"
 #include "res/Resources.h"
 
 
+
 #include "res/ResBuffer.h"
 #include "res/ResBuffer.h"
 #include "res/ResFontBM.h"
 #include "res/ResFontBM.h"
 #include "res/ResAtlas.h"
 #include "res/ResAtlas.h"
@@ -174,6 +175,11 @@ namespace oxygine
     }
     }
 #endif
 #endif
 
 
+    namespace key
+    {
+        void update();
+    }
+
     namespace core
     namespace core
     {
     {
         void focusLost()
         void focusLost()
@@ -715,6 +721,8 @@ namespace oxygine
 
 
         bool update()
         bool update()
         {
         {
+            key::update();
+
             timeMS duration = IVideoDriver::_stats.duration;
             timeMS duration = IVideoDriver::_stats.duration;
             IVideoDriver::_stats = IVideoDriver::Stats();
             IVideoDriver::_stats = IVideoDriver::Stats();
             IVideoDriver::_stats.duration = duration;
             IVideoDriver::_stats.duration = duration;

+ 21 - 0
oxygine/src/key.cpp

@@ -6,11 +6,30 @@ namespace oxygine
 {
 {
     namespace key
     namespace key
     {
     {
+        int _counter = 0;
+
         const int KEYS = 256;
         const int KEYS = 256;
         unsigned char _keys[KEYS];
         unsigned char _keys[KEYS];
 
 
+        void init()
+        {
+            if (!_counter)
+                memset(_keys, 0, sizeof(_keys));
+
+            _counter++;
+        }
+
+        void release()
+        {
+            _counter--;
+            OX_ASSERT(_counter >= 0);
+        }
+
         void update()
         void update()
         {
         {
+            if (!_counter)
+                return;
+
             int num = 0;
             int num = 0;
             const Uint8* data = SDL_GetKeyboardState(&num);
             const Uint8* data = SDL_GetKeyboardState(&num);
             num = std::min(num, KEYS);
             num = std::min(num, KEYS);
@@ -20,12 +39,14 @@ namespace oxygine
 
 
         bool wasPressed(keycode k)
         bool wasPressed(keycode k)
         {
         {
+            OX_ASSERT(_counter);
             const Uint8* data = SDL_GetKeyboardState(0);
             const Uint8* data = SDL_GetKeyboardState(0);
             return data[k] && !_keys[k];
             return data[k] && !_keys[k];
         }
         }
 
 
         bool wasReleased(keycode key)
         bool wasReleased(keycode key)
         {
         {
+            OX_ASSERT(_counter);
             const Uint8* data = SDL_GetKeyboardState(0);
             const Uint8* data = SDL_GetKeyboardState(0);
             return !data[key] && _keys[key];
             return !data[key] && _keys[key];
         }
         }

+ 5 - 3
oxygine/src/key.h

@@ -5,13 +5,15 @@ namespace oxygine
 {
 {
     namespace key
     namespace key
     {
     {
-        /**should be called before core::update*/
-        void update();
-
         typedef int keycode;
         typedef int keycode;
 
 
+        void init();
+        void release();
+
         bool wasPressed(keycode);
         bool wasPressed(keycode);
         bool wasReleased(keycode);
         bool wasReleased(keycode);
+
+        /*could be used without key::init()**/
         bool isPressed(keycode);
         bool isPressed(keycode);
     }
     }
 }
 }