From 678a4631a36b55face6541c473d5dfb854225547 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 15 Aug 2024 22:49:21 -0700 Subject: Better event handling. --- Demos/Pong/Main.hs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'Demos/Pong/Main.hs') diff --git a/Demos/Pong/Main.hs b/Demos/Pong/Main.hs index c768142..ee55622 100644 --- a/Demos/Pong/Main.hs +++ b/Demos/Pong/Main.hs @@ -14,6 +14,7 @@ import Spear.Render.Core.State import Spear.Render.Immediate import Spear.Window +import Control.Monad (when) import Data.Maybe (mapMaybe) @@ -44,7 +45,8 @@ endGame = do step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool step elapsed dt inputEvents = do gs <- getGameState - let events = translateEvents inputEvents + events <- processInput (window gs) + --when (events /= []) $ gameIO . putStrLn $ show events modifyGameState $ \gs -> gs { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gs) @@ -112,11 +114,11 @@ resize (ResizeEvent w h) = viewProjection = Matrix4.ortho left right bottom top (-1) 1 } -translateEvents = mapMaybe translateEvents' - where translateEvents' (KeyDown KEY_A) = Just MoveLeft - translateEvents' (KeyDown KEY_D) = Just MoveRight - translateEvents' (KeyUp KEY_A) = Just StopLeft - translateEvents' (KeyUp KEY_D) = Just StopRight - translateEvents' _ = Nothing + +processInput :: Window -> Game GameState [GameEvent] +processInput window = processKeys window + [ (KEY_A, MoveLeft) + , (KEY_D, MoveRight) + ] exitRequested = elem (KeyDown KEY_ESC) -- cgit v1.2.3