aboutsummaryrefslogtreecommitdiff
path: root/Demos
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-12-31 17:02:31 -0800
committer3gg <3gg@shellblade.net>2024-12-31 17:02:31 -0800
commit8984aede0162f6bdcfc2dc0a54f563a3b1ff5684 (patch)
treea80cbe5897edc2e2e6b06e87173624e0f4f4a377 /Demos
parentbbc4e34213147f30068a00bb56ebb871114e26dd (diff)
Add enough audio for background music.
Diffstat (limited to 'Demos')
-rw-r--r--Demos/Pong/Main.hs31
1 files changed, 21 insertions, 10 deletions
diff --git a/Demos/Pong/Main.hs b/Demos/Pong/Main.hs
index b93325d..f77136f 100644
--- a/Demos/Pong/Main.hs
+++ b/Demos/Pong/Main.hs
@@ -13,6 +13,8 @@ import Spear.Physics.Collision
13import Spear.Render.Core.Pipeline 13import Spear.Render.Core.Pipeline
14import Spear.Render.Core.State 14import Spear.Render.Core.State
15import Spear.Render.Immediate 15import Spear.Render.Immediate
16import Spear.Sound.Sound
17import Spear.Sound.State
16import Spear.Window 18import Spear.Window
17 19
18import Control.Monad (when) 20import Control.Monad (when)
@@ -20,10 +22,11 @@ import Data.Maybe (mapMaybe)
20 22
21 23
22data GameState = GameState 24data GameState = GameState
23 { window :: Window 25 { context :: AppContext
24 , renderCoreState :: RenderCoreState 26 , renderCoreState :: RenderCoreState
25 , immRenderState :: ImmRenderState 27 , immRenderState :: ImmRenderState
26 , viewProjection :: Matrix4 28 , viewProjection :: Matrix4
29 , backgroundMusic :: SoundSource
27 , world :: [GameObject] 30 , world :: [GameObject]
28 } 31 }
29 32
@@ -35,10 +38,19 @@ app = App options initGame endGame step render resize
35main :: IO () 38main :: IO ()
36main = runApp app 39main = runApp app
37 40
38initGame :: Window -> Game () GameState 41initGame :: AppContext -> Game () GameState
39initGame window = do 42initGame context = do
40 (immRenderState, renderCoreState) <- runSiblingGame newImmRenderer newRenderCoreState 43 (immRenderState, renderCoreState) <- runSiblingGame newImmRenderer newRenderCoreState
41 return $ GameState window renderCoreState immRenderState Matrix4.id newWorld 44 (music, soundState') <- flip runSiblingGame (appSoundState context) $ do
45 musicBuffer <- loadAudioFile "/home/jeanne/Casual Tiki Party Main.wav"
46 music <- makeSoundSource
47 liftIO $ do
48 setSoundSourceBuffer music musicBuffer
49 setSoundLoopMode music Loop
50 playSounds [music]
51 return music
52 let context' = context { appSoundState = soundState' }
53 return $ GameState context' renderCoreState immRenderState Matrix4.id music newWorld
42 54
43endGame :: Game GameState () 55endGame :: Game GameState ()
44endGame = do 56endGame = do
@@ -48,13 +60,12 @@ endGame = do
48 60
49step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool 61step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool
50step elapsed dt inputEvents = do 62step elapsed dt inputEvents = do
51 gs <- get 63 gameState <- get
52 events <- processInput (window gs) 64 events <- processInput (appWindow . context $ gameState)
53 --when (events /= []) $ liftIO . putStrLn $ show events 65 --when (events /= []) $ liftIO . putStrLn $ show events
54 modify $ \gs -> 66 modify $ \gameState -> gameState
55 gs 67 { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gameState)
56 { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gs) 68 }
57 }
58 return (not $ exitRequested inputEvents) 69 return (not $ exitRequested inputEvents)
59 70
60processInput :: Window -> Game GameState [GameEvent] 71processInput :: Window -> Game GameState [GameEvent]