# HG changeset patch # User Emmanuel Gil Peyrot # Date 1398362502 -7200 # Node ID 1abb6f7db8cc560cd797c01c60cb040c1994fe54 # Parent 63440d1e07177eadfd432e01ebe2c735654d974b Don’t retrieve the keyboard_state pointer at each frame. diff --git a/pytouhou/lib/sdl.pxd b/pytouhou/lib/sdl.pxd --- 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 * diff --git a/pytouhou/lib/sdl.pyx b/pytouhou/lib/sdl.pyx --- 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(data, len(data)) diff --git a/pytouhou/ui/gamerunner.pyx b/pytouhou/ui/gamerunner.pyx --- 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