aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-09 12:20:46 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-09 12:20:46 +0000
commitee50719de81145b5859d957da722cf183e0c8c60 (patch)
treee6806d074aaab10103716e632332e02bb4390159
parent677f9396a30754c4030707a8d88009516a20758f (diff)
downloadspace-plat-hs-ee50719de81145b5859d957da722cf183e0c8c60.tar.gz
space-plat-hs-ee50719de81145b5859d957da722cf183e0c8c60.zip
Use ALT + Enter to toggle fullscreen
-rw-r--r--README.md15
-rw-r--r--src/Game.hs7
-rw-r--r--src/Game/Controller.hs11
3 files changed, 30 insertions, 3 deletions
diff --git a/README.md b/README.md
index 4634ae4..9cbc2d9 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,21 @@ Using cabal:
```
cabal run game
```
+
+# Controls
+
+The game supports both keyboard and gamepads.
+
+| Keyboard | Gamepad | Effect |
+| --- | --- | --- |
+| Cursor left | Dpad left | Move left |
+| Cursor right | Dpad right | Move right |
+| z | Button a | Jump |
+| Cursor down + z | Dpad down + Button a | Drop one plaform down |
+
+ESC will exit the game.
+ALT + Enter will toggle between full-screen and windowed mode.
+
## Author
This was made by [Juan J. Martinez](https://www.usebox.net/jjm/about/me/).
diff --git a/src/Game.hs b/src/Game.hs
index 2f9988d..52ba6a7 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -134,8 +134,11 @@ gameLoop :: Env -> IO ()
gameLoop e = do
events <- map SDL.eventPayload <$> SDL.pollEvents
- -- F11 for fullscreen / windowed
- env <- if fromMaybe False $ C.isPressed SDL.KeycodeF11 events then toggleFullscreen e else pure e
+ -- ALT + Enter for fullscreen / windowed
+ env <-
+ if fromMaybe False (C.isPressed SDL.KeycodeReturn events) && C.isModKey C.altMod events
+ then toggleFullscreen e
+ else pure e
let renderer = env.renderer
canvas = env.canvas
diff --git a/src/Game/Controller.hs b/src/Game/Controller.hs
index 01dfd1b..d47963e 100644
--- a/src/Game/Controller.hs
+++ b/src/Game/Controller.hs
@@ -1,4 +1,4 @@
-module Game.Controller (Controls (..), init, update, isPressed) where
+module Game.Controller (Controls (..), init, update, isPressed, isModKey, altMod) where
import Control.Monad
import Data.Int (Int32)
@@ -48,6 +48,9 @@ processControllerEvents controls [] = pure controls
init :: Controls
init = Controls False False False False False False False Nothing Nothing
+altMod :: SDL.KeyModifier
+altMod = SDL.KeyModifier False False False False True False False False False False False
+
updateGamepad :: Controls -> [SDL.EventPayload] -> Controls
updateGamepad controls events =
case controls.joyId of
@@ -92,6 +95,12 @@ isPressed code events
isEventKey expected keycode (SDL.KeyboardEvent (SDL.KeyboardEventData _ motion False ksym)) = expected == motion && SDL.keysymKeycode ksym == keycode
isEventKey _ _ _ = False
+isModKey :: SDL.KeyModifier -> [SDL.EventPayload] -> Bool
+isModKey kmod = any (isModKeyOne kmod)
+ where
+ isModKeyOne m (SDL.KeyboardEvent (SDL.KeyboardEventData _ _ _ ksym)) = SDL.keysymModifier ksym == m
+ isModKeyOne _ _ = False
+
isPressedGamepad :: Int32 -> SDL.ControllerButton -> [SDL.EventPayload] -> Maybe Bool
isPressedGamepad joyId button events
| any (isEventButton SDL.ControllerButtonPressed button) events = Just True