changeset 538:1abb6f7db8cc

Don’t retrieve the keyboard_state pointer at each frame.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 24 Apr 2014 20:01:42 +0200
parents 63440d1e0717
children d6d9a711253d
files pytouhou/lib/sdl.pxd pytouhou/lib/sdl.pyx pytouhou/ui/gamerunner.pyx
diffstat 3 files changed, 7 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/lib/sdl.pxd
+++ b/pytouhou/lib/sdl.pxd
@@ -42,6 +42,8 @@ cdef SDL_EventType KEYDOWN
 cdef SDL_EventType QUIT
 cdef SDL_EventType WINDOWEVENT
 
+cdef const Uint8 *keyboard_state
+
 
 cdef class Window:
     cdef SDL_Window *window
@@ -111,7 +113,6 @@ cdef void mix_init(int flags) except *
 cdef void ttf_init() except *
 cdef void gl_set_attribute(SDL_GLattr attr, int value) except *
 cdef list poll_events()
-cdef const Uint8* get_keyboard_state() nogil
 cdef Surface load_png(file_)
 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=*, Uint32 gmask=*, Uint32 bmask=*, Uint32 amask=*)
 cdef void mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize) except *
--- a/pytouhou/lib/sdl.pyx
+++ b/pytouhou/lib/sdl.pyx
@@ -52,12 +52,16 @@ class SDL(object):
         self.sound = sound
 
     def __enter__(self):
+        global keyboard_state
+
         IF UNAME_SYSNAME == "Windows":
             SDL_SetMainReady()
         init(SDL_INIT_VIDEO)
         img_init(IMG_INIT_PNG)
         ttf_init()
 
+        keyboard_state = SDL_GetKeyboardState(NULL)
+
         if self.sound:
             mix_init(0)
             try:
@@ -285,10 +289,6 @@ cdef list poll_events():
     return ret
 
 
-cdef const Uint8* get_keyboard_state() nogil:
-    return SDL_GetKeyboardState(NULL)
-
-
 cdef Surface load_png(file_):
     data = file_.read()
     rwops = SDL_RWFromConstMem(<char*>data, len(data))
--- a/pytouhou/ui/gamerunner.pyx
+++ b/pytouhou/ui/gamerunner.pyx
@@ -129,7 +129,7 @@ cdef class GameRunner(Runner):
                         self.window.set_size(event[2], event[3])
         if self.replay_level is None:
             #TODO: allow user settings
-            keys = sdl.get_keyboard_state()
+            keys = sdl.keyboard_state
             keystate = 0
             if keys[sdl.SCANCODE_Z]:
                 keystate |= 1