From 8984aede0162f6bdcfc2dc0a54f563a3b1ff5684 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 31 Dec 2024 17:02:31 -0800 Subject: Add enough audio for background music. --- Demos/Pong/Main.hs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'Demos/Pong/Main.hs') 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 import Spear.Render.Core.Pipeline import Spear.Render.Core.State import Spear.Render.Immediate +import Spear.Sound.Sound +import Spear.Sound.State import Spear.Window import Control.Monad (when) @@ -20,10 +22,11 @@ import Data.Maybe (mapMaybe) data GameState = GameState - { window :: Window + { context :: AppContext , renderCoreState :: RenderCoreState , immRenderState :: ImmRenderState , viewProjection :: Matrix4 + , backgroundMusic :: SoundSource , world :: [GameObject] } @@ -35,10 +38,19 @@ app = App options initGame endGame step render resize main :: IO () main = runApp app -initGame :: Window -> Game () GameState -initGame window = do +initGame :: AppContext -> Game () GameState +initGame context = do (immRenderState, renderCoreState) <- runSiblingGame newImmRenderer newRenderCoreState - return $ GameState window renderCoreState immRenderState Matrix4.id newWorld + (music, soundState') <- flip runSiblingGame (appSoundState context) $ do + musicBuffer <- loadAudioFile "/home/jeanne/Casual Tiki Party Main.wav" + music <- makeSoundSource + liftIO $ do + setSoundSourceBuffer music musicBuffer + setSoundLoopMode music Loop + playSounds [music] + return music + let context' = context { appSoundState = soundState' } + return $ GameState context' renderCoreState immRenderState Matrix4.id music newWorld endGame :: Game GameState () endGame = do @@ -48,13 +60,12 @@ endGame = do step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool step elapsed dt inputEvents = do - gs <- get - events <- processInput (window gs) + gameState <- get + events <- processInput (appWindow . context $ gameState) --when (events /= []) $ liftIO . putStrLn $ show events - modify $ \gs -> - gs - { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gs) - } + modify $ \gameState -> gameState + { world = stepWorld (realToFrac elapsed) (realToFrac dt) events (world gameState) + } return (not $ exitRequested inputEvents) processInput :: Window -> Game GameState [GameEvent] -- cgit v1.2.3