aboutsummaryrefslogtreecommitdiff
path: root/Demos
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-01-02 09:58:14 -0800
committer3gg <3gg@shellblade.net>2025-01-02 09:58:14 -0800
commitae29688a6bcd05daf9154fb5792861723afdb1ed (patch)
treec9d739407f97dee97930620aeefd1ba554958740 /Demos
parentd5edea871f9c8cf52014ff7e726a2c9f84813715 (diff)
Introduce HasState to simplify state programming.
Diffstat (limited to 'Demos')
-rw-r--r--Demos/Pong/Main.hs20
1 files changed, 5 insertions, 15 deletions
diff --git a/Demos/Pong/Main.hs b/Demos/Pong/Main.hs
index 993c0ff..22b1021 100644
--- a/Demos/Pong/Main.hs
+++ b/Demos/Pong/Main.hs
@@ -7,7 +7,7 @@ import Pong
7import Spear.App 7import Spear.App
8import Spear.Game 8import Spear.Game
9import Spear.Math.AABB 9import Spear.Math.AABB
10import Spear.Math.Matrix4 as Matrix4 hiding (position) 10import Spear.Math.Matrix4 as Matrix4
11import Spear.Math.Spatial 11import Spear.Math.Spatial
12import Spear.Math.Spatial2 12import Spear.Math.Spatial2
13import Spear.Math.Vector 13import Spear.Math.Vector
@@ -20,12 +20,10 @@ import Spear.Sound.State
20import Spear.Window 20import Spear.Window
21 21
22import Control.Monad (when) 22import Control.Monad (when)
23import Data.Maybe (mapMaybe)
24 23
25 24
26data Pong = Pong 25data Pong = Pong
27 { immRenderState :: ImmRenderState 26 { viewProjection :: Matrix4
28 , viewProjection :: Matrix4
29 , backgroundMusic :: SoundSource 27 , backgroundMusic :: SoundSource
30 , world :: [GameObject] 28 , world :: [GameObject]
31 } 29 }
@@ -43,10 +41,6 @@ main = runApp app
43 41
44initGame :: Game AppContext Pong 42initGame :: Game AppContext Pong
45initGame = do 43initGame = do
46 renderCoreState <- contextRenderCoreState <$> get
47 (immRenderState, renderCoreState') <- runSiblingGame renderCoreState newImmRenderer
48 -- TODO: This can work if we use FlexibleContexts and change the function signatures.
49 --immRenderState <- newImmRenderer
50 music <- siblingGame $ do 44 music <- siblingGame $ do
51 musicBuffer <- loadAudioFile "/home/jeanne/Casual Tiki Party Main.wav" 45 musicBuffer <- loadAudioFile "/home/jeanne/Casual Tiki Party Main.wav"
52 music <- makeSoundSource 46 music <- makeSoundSource
@@ -54,13 +48,10 @@ initGame = do
54 setSoundLoopMode music Loop 48 setSoundLoopMode music Loop
55 playSounds [music] 49 playSounds [music]
56 return music 50 return music
57 return $ Pong immRenderState Matrix4.id music newWorld 51 return $ Pong Matrix4.id music newWorld
58 52
59endGame :: Game GameState () 53endGame :: Game GameState ()
60endGame = do 54endGame = return ()
61 renderCoreState <- appRenderCoreState <$> get
62 game <- getGameState
63 exec' runSiblingGame renderCoreState (deleteImmRenderer $ immRenderState game)
64 55
65 56
66step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool 57step :: Elapsed -> Dt -> [InputEvent] -> Game GameState Bool
@@ -86,7 +77,7 @@ exitRequested = elem (KeyDown KEY_ESC)
86render :: Game GameState () 77render :: Game GameState ()
87render = do 78render = do
88 gameState <- getGameState 79 gameState <- getGameState
89 immRenderState' <- exec runSiblingGame (immRenderState gameState) $ do 80 siblingGame $ do
90 immStart 81 immStart
91 immSetViewProjectionMatrix (viewProjection gameState) 82 immSetViewProjectionMatrix (viewProjection gameState)
92 -- Clear the background to a different colour than the playable area to make 83 -- Clear the background to a different colour than the playable area to make
@@ -95,7 +86,6 @@ render = do
95 clearBuffers [ColourBuffer] 86 clearBuffers [ColourBuffer]
96 render' $ world gameState 87 render' $ world gameState
97 immEnd 88 immEnd
98 putGameState $ gameState { immRenderState = immRenderState' }
99 89
100render' :: [GameObject] -> Game ImmRenderState () 90render' :: [GameObject] -> Game ImmRenderState ()
101render' world = do 91render' world = do