diff options
Diffstat (limited to 'Demos/Pong')
| -rw-r--r-- | Demos/Pong/Main.hs | 31 |
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 | |||
| 13 | import Spear.Render.Core.Pipeline | 13 | import Spear.Render.Core.Pipeline |
| 14 | import Spear.Render.Core.State | 14 | import Spear.Render.Core.State |
| 15 | import Spear.Render.Immediate | 15 | import Spear.Render.Immediate |
| 16 | import Spear.Sound.Sound | ||
| 17 | import Spear.Sound.State | ||
| 16 | import Spear.Window | 18 | import Spear.Window |
| 17 | 19 | ||
| 18 | import Control.Monad (when) | 20 | import Control.Monad (when) |
| @@ -20,10 +22,11 @@ import Data.Maybe (mapMaybe) | |||
| 20 | 22 | ||
| 21 | 23 | ||
| 22 | data GameState = GameState | 24 | data 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 | |||
| 35 | main :: IO () | 38 | main :: IO () |
| 36 | main = runApp app | 39 | main = runApp app |
| 37 | 40 | ||
| 38 | initGame :: Window -> Game () GameState | 41 | initGame :: AppContext -> Game () GameState |
| 39 | initGame window = do | 42 | initGame 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 | ||
| 43 | endGame :: Game GameState () | 55 | endGame :: Game GameState () |
| 44 | endGame = do | 56 | endGame = do |
| @@ -48,13 +60,12 @@ endGame = do | |||
| 48 | 60 | ||
| 49 | step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool | 61 | step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool |
| 50 | step elapsed dt inputEvents = do | 62 | step 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 | ||
| 60 | processInput :: Window -> Game GameState [GameEvent] | 71 | processInput :: Window -> Game GameState [GameEvent] |
