aboutsummaryrefslogtreecommitdiff
path: root/Demos/Pong/Main.hs
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-08-15 22:49:21 -0700
committer3gg <3gg@shellblade.net>2024-08-15 22:49:21 -0700
commit678a4631a36b55face6541c473d5dfb854225547 (patch)
treec673a3597edd8a42a3b0cb15e9d6f8ea1a2235f7 /Demos/Pong/Main.hs
parentae90f69c9fe6f21f698305232b453fcfbd3fdb02 (diff)
Better event handling.
Diffstat (limited to 'Demos/Pong/Main.hs')
-rw-r--r--Demos/Pong/Main.hs16
1 files changed, 9 insertions, 7 deletions
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
14import Spear.Render.Immediate 14import Spear.Render.Immediate
15import Spear.Window 15import Spear.Window
16 16
17import Control.Monad (when)
17import Data.Maybe (mapMaybe) 18import Data.Maybe (mapMaybe)
18 19
19 20
@@ -44,7 +45,8 @@ endGame = do
44step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool 45step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool
45step elapsed dt inputEvents = do 46step elapsed dt inputEvents = do
46 gs <- getGameState 47 gs <- getGameState
47 let events = translateEvents inputEvents 48 events <- processInput (window gs)
49 --when (events /= []) $ gameIO . putStrLn $ show events
48 modifyGameState $ \gs -> 50 modifyGameState $ \gs ->
49 gs 51 gs
50 { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gs) 52 { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gs)
@@ -112,11 +114,11 @@ resize (ResizeEvent w h) =
112 viewProjection = Matrix4.ortho left right bottom top (-1) 1 114 viewProjection = Matrix4.ortho left right bottom top (-1) 1
113 } 115 }
114 116
115translateEvents = mapMaybe translateEvents' 117
116 where translateEvents' (KeyDown KEY_A) = Just MoveLeft 118processInput :: Window -> Game GameState [GameEvent]
117 translateEvents' (KeyDown KEY_D) = Just MoveRight 119processInput window = processKeys window
118 translateEvents' (KeyUp KEY_A) = Just StopLeft 120 [ (KEY_A, MoveLeft)
119 translateEvents' (KeyUp KEY_D) = Just StopRight 121 , (KEY_D, MoveRight)
120 translateEvents' _ = Nothing 122 ]
121 123
122exitRequested = elem (KeyDown KEY_ESC) 124exitRequested = elem (KeyDown KEY_ESC)