Skip to content

Commit 4bde0c1

Browse files
authored
Merge pull request #179 from haskell-game/poll-event-allocs
Make pollEvent not allocate unnecessarily (#178)
2 parents b61f9b1 + 254efa3 commit 4bde0c1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/SDL/Event.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,18 @@ convertRaw (Raw.UnknownEvent t ts) =
746746

747747
-- | Poll for currently pending events. You can only call this function in the thread that set the video mode.
748748
pollEvent :: MonadIO m => m (Maybe Event)
749-
pollEvent = liftIO $ alloca $ \e -> do
750-
n <- Raw.pollEvent e
751-
if n == 0
752-
then return Nothing
753-
else fmap Just (peek e >>= convertRaw)
749+
pollEvent =
750+
liftIO $ do
751+
n <- Raw.pollEvent nullPtr
752+
-- We use NULL first to check if there's an event.
753+
if n == 0
754+
then return Nothing
755+
else alloca $ \e -> do
756+
n <- Raw.pollEvent e
757+
-- Checking 0 again doesn't hurt and it's good to be safe.
758+
if n == 0
759+
then return Nothing
760+
else fmap Just (peek e >>= convertRaw)
754761

755762
-- | Clear the event queue by polling for all pending events.
756763
pollEvents :: (Functor m, MonadIO m) => m [Event]

0 commit comments

Comments
 (0)