File tree Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 11#include <string.h>
2+ #include <stdlib.h>
23#include "sdlhelper.h"
34
5+ int SDLHelper_GetEventBufferSize () { return 64 ; }
6+ SDL_Event * SDLHelper_GetEventBuffer () {
7+ static SDL_Event * buffer = NULL ;
8+ if (buffer == NULL ) {
9+ /* leak an inconsequental amount of memory */
10+ buffer = calloc (SDLHelper_GetEventBufferSize (), sizeof (SDL_Event ));
11+ }
12+ return buffer ;
13+ }
14+
415void SDLHelper_JoystickGetDeviceGUID (int device_index , SDL_JoystickGUID * guid )
516{
617 SDL_JoystickGUID t = SDL_JoystickGetDeviceGUID (device_index );
Original file line number Diff line number Diff line change 44#include <stddef.h>
55#include "SDL.h"
66
7+ int SDLHelper_GetEventBufferSize (void );
8+ SDL_Event * SDLHelper_GetEventBuffer (void );
79void SDLHelper_JoystickGetDeviceGUID (int device_index , SDL_JoystickGUID * guid );
810void SDLHelper_JoystickGetGUID (SDL_Joystick * joystick , SDL_JoystickGUID * guid );
911void SDLHelper_JoystickGetGUIDFromString (const char * pchGUID , SDL_JoystickGUID * guid );
Original file line number Diff line number Diff line change @@ -92,6 +92,7 @@ import Data.Text (Text)
9292import Data.Typeable
9393import Foreign hiding (throwIfNeg_ )
9494import Foreign.C
95+ import Foreign.Marshal.Array
9596import GHC.Generics (Generic )
9697import SDL.Vect
9798import SDL.Input.Joystick
@@ -765,11 +766,20 @@ pollEvent =
765766-- Like 'pollEvent' this function should only be called in the OS thread which
766767-- set the video mode.
767768pollEvents :: MonadIO m => m [Event ]
768- pollEvents =
769- do e <- pollEvent
770- case e of
771- Nothing -> return []
772- Just e' -> (e' : ) <$> pollEvents
769+ pollEvents = liftIO $ do
770+ Raw. pumpEvents
771+ peepAllEvents >>= mapM convertRaw where
772+ peepAllEvents = do
773+ numPeeped <- Raw. peepEvents
774+ Raw. eventBuffer
775+ Raw. eventBufferSize
776+ Raw. SDL_GETEVENT
777+ Raw. SDL_FIRSTEVENT
778+ Raw. SDL_LASTEVENT
779+ peeped <- peekArray (fromIntegral numPeeped) Raw. eventBuffer
780+ if numPeeped == Raw. eventBufferSize -- are there more events to peep?
781+ then (peeped ++ ) <$> peepAllEvents
782+ else return peeped
773783
774784-- | Run a monadic computation, accumulating over all known 'Event's.
775785--
Original file line number Diff line number Diff line change @@ -112,7 +112,9 @@ module SDL.Raw.Event (
112112 gameControllerNameForIndex ,
113113 gameControllerOpen ,
114114 gameControllerUpdate ,
115- isGameController
115+ isGameController ,
116+ eventBuffer ,
117+ eventBufferSize
116118) where
117119
118120import Control.Monad.IO.Class
@@ -235,6 +237,9 @@ foreign import ccall "SDL.h SDL_GameControllerOpen" gameControllerOpenFFI :: CIn
235237foreign import ccall " SDL.h SDL_GameControllerUpdate" gameControllerUpdateFFI :: IO ()
236238foreign import ccall " SDL.h SDL_IsGameController" isGameControllerFFI :: CInt -> IO Bool
237239
240+ foreign import ccall " sdlhelper.c SDLHelper_GetEventBufferSize" eventBufferSize :: CInt
241+ foreign import ccall " sdlhelper.c SDLHelper_GetEventBuffer" eventBuffer :: Ptr Event
242+
238243addEventWatch :: MonadIO m => EventFilter -> Ptr () -> m ()
239244addEventWatch v1 v2 = liftIO $ addEventWatchFFI v1 v2
240245{-# INLINE addEventWatch #-}
You can’t perform that action at this time.
0 commit comments