diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-04 21:20:12 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-04 21:20:12 +0000 |
commit | 2103dc0dcf42fd2489d5f9e4fec46146f7cc9db5 (patch) | |
tree | 81fae8446820a0dd8c728230d8e99018edebc836 /src/Game/Utils.hs | |
download | space-plat-hs-2103dc0dcf42fd2489d5f9e4fec46146f7cc9db5.tar.gz space-plat-hs-2103dc0dcf42fd2489d5f9e4fec46146f7cc9db5.zip |
Initial import
Diffstat (limited to 'src/Game/Utils.hs')
-rw-r--r-- | src/Game/Utils.hs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Game/Utils.hs b/src/Game/Utils.hs new file mode 100644 index 0000000..acfab6f --- /dev/null +++ b/src/Game/Utils.hs @@ -0,0 +1,19 @@ +module Game.Utils (rect, isPressed) where + +import Foreign.C.Types (CInt) +import qualified SDL +import SDL.Vect (V2 (..)) + +rect :: Int -> Int -> Int -> Int -> SDL.Rectangle CInt +rect x y w h = SDL.Rectangle (SDL.P $ V2 (fromIntegral x) (fromIntegral y)) (V2 (fromIntegral w) (fromIntegral h)) + +isPressed :: SDL.Keycode -> [SDL.EventPayload] -> Maybe Bool +isPressed code events + -- TODO: gamepad support + | any (isEventKey SDL.Pressed code) events = Just True + | any (isEventKey SDL.Released code) events = Just False + | otherwise = Nothing + where + isEventKey :: SDL.InputMotion -> SDL.Keycode -> SDL.EventPayload -> Bool + isEventKey expected keycode (SDL.KeyboardEvent (SDL.KeyboardEventData _ motion False ksym)) = expected == motion && SDL.keysymKeycode ksym == keycode + isEventKey _ _ _ = False |