Try to move sound into world, slightly broken now

This commit is contained in:
2021-09-08 19:12:54 +01:00
parent 04e5b9b39f
commit c3d567353c
9 changed files with 33 additions and 56 deletions
+14 -37
View File
@@ -21,7 +21,6 @@ import Sound.Data
import Music import Music
import Data.Preload import Data.Preload
import Data.Preload.Render import Data.Preload.Render
import Shader.Poke
import Control.Lens import Control.Lens
--import Foreign (Word32) --import Foreign (Word32)
@@ -33,59 +32,37 @@ import Graphics.Rendering.OpenGL hiding (color, rotate, scale, translate)
import qualified SDL import qualified SDL
import qualified SDL.Mixer as Mix import qualified SDL.Mixer as Mix
main' :: IO ()
main' = do
(sizex,sizey) <- loadConfig
theKeyConfig <- loadKeyConfig
dodgeConfig <- loadDodgeConfig
setupLoop
(sizex,sizey)
(SDL.cursorVisible $= False >> doPreload >>= applyWorldConfig dodgeConfig)
(\x -> (SDL.cursorVisible $= True) >> cleanUpPreload x)
(fmap (setWindowSize sizex sizey . (keyConfig .~ theKeyConfig) . (config .~ dodgeConfig)) firstWorld)
doSideEffects
handleEvent
(Just . update)
main :: IO () main :: IO ()
main = do main = do
(sizex,sizey) <- loadConfig (sizex,sizey) <- loadConfig
theKeyConfig <- loadKeyConfig
dodgeConfig <- loadDodgeConfig
setupLoop' setupLoop'
(sizex,sizey) (sizex,sizey)
theCleanup theCleanup
(firstWorldLoad (sizex,sizey) theKeyConfig dodgeConfig) (firstWorldLoad (sizex,sizey))
theUpdateStep theUpdateStep
handleEvent handleEvent
-- (SDL.cursorVisible $= False >> doPreload >>= applyWorldConfig dodgeConfig)
-- (fmap (setWindowSize sizex sizey . (keyConfig .~ theKeyConfig) . (config .~ dodgeConfig)) firstWorld)
-- doSideEffects
theCleanup :: World -> IO () theCleanup :: World -> IO ()
theCleanup w = SDL.cursorVisible $= True >> cleanUpPreload (_preloadData w) theCleanup w = SDL.cursorVisible $= True >> cleanUpPreload (_preloadData w)
--firstWorldLoad :: Configuration -> IO World firstWorldLoad :: (Int,Int) -> IO World
firstWorldLoad (sizex,sizey) theKeyConfig theConfig = do firstWorldLoad (sizex,sizey) = do
SDL.cursorVisible $= False SDL.cursorVisible $= False
theKeyConfig <- loadKeyConfig
theConfig <- loadDodgeConfig
pdata <- doPreload >>= applyWorldConfig theConfig pdata <- doPreload >>= applyWorldConfig theConfig
w <- fmap (setWindowSize sizex sizey . (keyConfig .~ theKeyConfig) . (config .~ theConfig)) firstWorld w <- fmap (setWindowSize sizex sizey . (keyConfig .~ theKeyConfig) . (config .~ theConfig)) firstWorld
return $ w & preloadData .~ pdata return $ w & preloadData .~ pdata
theUpdateStep :: World -> IO World theUpdateStep :: World -> IO World
theUpdateStep = applySideEffects . update theUpdateStep = doSideEffects . update
applySideEffects :: World -> IO (World) doSideEffects :: World -> IO World
applySideEffects w = do doSideEffects w = do
newpdata <- doSideEffects (_preloadData w) w let preData = _preloadData w
return $ w & preloadData .~ newpdata
doSideEffects :: PreloadData SoundOrigin -> World -> IO (PreloadData SoundOrigin)
doSideEffects preData w = do
--startTicks <- SDL.ticks
void $ doDrawing (_renderData preData) w void $ doDrawing (_renderData preData) w
playPositionalSoundQueue (_loadedChunks $ _soundData preData) (_soundQueue w) playPositionalSoundQueue (_loadedChunks $ _soundData preData) (_soundQueue w)
newPlayingSounds <- playSoundAndUpdate (_soundData preData) (_sounds w) newPlayingSounds <- playSoundAndUpdate (_soundData preData) (_playingSounds w) (_sounds w)
endTicks <- SDL.ticks endTicks <- SDL.ticks
let lastFrameTicks = _frameTimer preData let lastFrameTicks = _frameTimer preData
@@ -96,14 +73,14 @@ doSideEffects preData w = do
. translate (-0.5) (-0.8) . scale 0.0005 0.0005 . translate (-0.5) (-0.8) . scale 0.0005 0.0005
. text $ "ms/frame " ++ show (endTicks - lastFrameTicks) . text $ "ms/frame " ++ show (endTicks - lastFrameTicks)
) )
foldr (=<<) (return (preData & soundData . playingSounds .~ newPlayingSounds newpdata <- foldr (=<<) (return (preData & frameTimer .~ endTicks)) (_doneSideEffects w)
& frameTimer .~ endTicks)) (_doneSideEffects w) return $ w & preloadData .~ newpdata
doPreload :: IO (PreloadData a) doPreload :: IO PreloadData
doPreload = do doPreload = do
lChunks <- loadSounds lChunks <- loadSounds
lMusic <- loadMusic lMusic <- loadMusic
let sData = SoundData {_loadedChunks = lChunks, _playingSounds = M.empty} let sData = SoundData {_loadedChunks = lChunks}
mData = MusicData {_loadedMusic = lMusic} mData = MusicData {_loadedMusic = lMusic}
Mix.playMusic Mix.Forever (lMusic IM.! 0) Mix.playMusic Mix.Forever (lMusic IM.! 0)
rData <- preloadRender rData <- preloadRender
+2 -2
View File
@@ -8,9 +8,9 @@ import Music
import Control.Lens import Control.Lens
import GHC.Word (Word32) import GHC.Word (Word32)
data PreloadData a = PreloadData data PreloadData = PreloadData
{ _renderData :: RenderData { _renderData :: RenderData
, _soundData :: SoundData a , _soundData :: SoundData
, _musicData :: MusicData , _musicData :: MusicData
, _frameTimer :: Word32 , _frameTimer :: Word32
} }
+4 -4
View File
@@ -14,7 +14,7 @@ import Data.Aeson (encodeFile)
{- | {- |
Write the current world configuration to disk as a json file. Write the current world configuration to disk as a json file.
-} -}
saveConfig :: Configuration -> PreloadData SoundOrigin -> IO (PreloadData SoundOrigin) saveConfig :: Configuration -> PreloadData -> IO PreloadData
saveConfig cfig d = do saveConfig cfig d = do
putStrLn "Saving config to data/dodge.config.json" putStrLn "Saving config to data/dodge.config.json"
encodeFile "data/dodge.config.json" cfig encodeFile "data/dodge.config.json" cfig
@@ -23,7 +23,7 @@ saveConfig cfig d = do
{- | {- |
Apply the volume settings from the world configuration to the running game. Apply the volume settings from the world configuration to the running game.
-} -}
setVol :: Configuration -> PreloadData SoundOrigin -> IO (PreloadData SoundOrigin) setVol :: Configuration -> PreloadData -> IO PreloadData
setVol cfig d = do setVol cfig d = do
setSoundVolume ( _volume_master cfig * _volume_sound cfig) setSoundVolume ( _volume_master cfig * _volume_sound cfig)
setMusicVolume ( _volume_master cfig * _volume_music cfig) setMusicVolume ( _volume_master cfig * _volume_music cfig)
@@ -34,8 +34,8 @@ Apply /all/ of the values in the world configuration to the running game.
-} -}
applyWorldConfig applyWorldConfig
:: Configuration :: Configuration
-> PreloadData SoundOrigin -> PreloadData
-> IO (PreloadData SoundOrigin) -> IO PreloadData
applyWorldConfig cfig pdata = do applyWorldConfig cfig pdata = do
setVol cfig pdata >>= pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y setVol cfig pdata >>= pdataResizeUpdate (x `div` divRes) (y `div` divRes) x y
where where
+4 -3
View File
@@ -78,6 +78,7 @@ data World = World
, _buttons :: IM.IntMap Button , _buttons :: IM.IntMap Button
, _soundQueue :: [(Int,Int16)] , _soundQueue :: [(Int,Int16)]
, _sounds :: M.Map SoundOrigin Sound , _sounds :: M.Map SoundOrigin Sound
, _playingSounds :: M.Map SoundOrigin Sound
, _decorations :: IM.IntMap Picture , _decorations :: IM.IntMap Picture
, _foregroundDecorations :: [Polyhedra] , _foregroundDecorations :: [Polyhedra]
, _foregroundEdgeVerx :: [Point3] , _foregroundEdgeVerx :: [Point3]
@@ -102,14 +103,14 @@ data World = World
, _selLocation :: Int , _selLocation :: Int
, _keyConfig :: KeyConfigSDL , _keyConfig :: KeyConfigSDL
, _config :: Configuration , _config :: Configuration
, _sideEffects :: [PreloadData SoundOrigin -> IO (PreloadData SoundOrigin)] , _sideEffects :: [PreloadData -> IO PreloadData]
, _doneSideEffects :: [PreloadData SoundOrigin -> IO (PreloadData SoundOrigin)] , _doneSideEffects :: [PreloadData -> IO PreloadData]
, _debugFlags :: DebugFlags , _debugFlags :: DebugFlags
, _inventoryMode :: InventoryMode , _inventoryMode :: InventoryMode
, _lClickHammer :: HammerPosition , _lClickHammer :: HammerPosition
, _radDistortion :: [(Point2,Point2,Point2,Float)] , _radDistortion :: [(Point2,Point2,Point2,Float)]
, _gameRooms :: [GameRoom] , _gameRooms :: [GameRoom]
, _preloadData :: PreloadData SoundOrigin , _preloadData :: PreloadData
} }
data OptionScreenFlag = NormalOptions | GameOverOptions data OptionScreenFlag = NormalOptions | GameOverOptions
data ScreenLayer data ScreenLayer
+1 -1
View File
@@ -50,10 +50,10 @@ defaultWorld = World
, _buttons = IM.empty , _buttons = IM.empty
, _soundQueue = [] , _soundQueue = []
, _sounds = M.empty , _sounds = M.empty
, _playingSounds = M.empty
, _corpses = IM.empty , _corpses = IM.empty
, _decorations = IM.empty , _decorations = IM.empty
, _storedLevel = Nothing , _storedLevel = Nothing
--, _menuLayers = [levelMenu 1]
, _menuLayers = [] , _menuLayers = []
, _worldState = M.empty , _worldState = M.empty
, _worldTriggers = S.empty , _worldTriggers = S.empty
+2 -2
View File
@@ -9,10 +9,10 @@ import Sound.Data
import qualified SDL.Mixer as Mix import qualified SDL.Mixer as Mix
cleanUpPreload :: PreloadData a -> IO () cleanUpPreload :: PreloadData -> IO ()
cleanUpPreload pd = do cleanUpPreload pd = do
cleanUpRenderPreload $ _renderData pd cleanUpRenderPreload $ _renderData pd
cleanUpSoundPreload $ _soundData pd cleanUpSoundPreload $ _soundData pd
cleanUpSoundPreload :: SoundData a -> IO () cleanUpSoundPreload :: SoundData -> IO ()
cleanUpSoundPreload _ = Mix.closeAudio cleanUpSoundPreload _ = Mix.closeAudio
+2 -2
View File
@@ -15,8 +15,8 @@ pdataResizeUpdate
-> Int -- ^ Scaled height -> Int -- ^ Scaled height
-> Int -- ^ Full width -> Int -- ^ Full width
-> Int -- ^ Full height -> Int -- ^ Full height
-> PreloadData a -> PreloadData
-> IO (PreloadData a) -> IO PreloadData
pdataResizeUpdate xsize ysize xfull yfull pdata = do pdataResizeUpdate xsize ysize xfull yfull pdata = do
rd <- renderDataResizeUpdate xsize ysize xfull yfull (_renderData pdata) rd <- renderDataResizeUpdate xsize ysize xfull yfull (_renderData pdata)
return (pdata {_renderData = rd}) return (pdata {_renderData = rd})
+3 -3
View File
@@ -49,9 +49,9 @@ In the update:
3. apply 'Just' sound position effects, set value to 'Nothing' 3. apply 'Just' sound position effects, set value to 'Nothing'
4. remove sounds that have stopped playing from the map. 4. remove sounds that have stopped playing from the map.
-} -}
playSoundAndUpdate :: Ord a => SoundData a -> M.Map a Sound -> IO (M.Map a Sound) playSoundAndUpdate :: Ord a => SoundData -> M.Map a Sound -> M.Map a Sound -> IO (M.Map a Sound)
playSoundAndUpdate sData newSounds playSoundAndUpdate sData oldSounds newSounds
= updateSounds (_loadedChunks sData) (M.unionWith mergeSound (_playingSounds sData) newSounds) = updateSounds (_loadedChunks sData) (M.unionWith mergeSound oldSounds newSounds)
mergeSound :: Sound -> Sound -> Sound mergeSound :: Sound -> Sound -> Sound
mergeSound oldS newS mergeSound oldS newS
+1 -2
View File
@@ -16,9 +16,8 @@ data SoundStatus
| ToStart | ToStart
deriving (Eq,Ord,Show) deriving (Eq,Ord,Show)
data SoundData a = SoundData data SoundData = SoundData
{_loadedChunks :: IM.IntMap Mix.Chunk {_loadedChunks :: IM.IntMap Mix.Chunk
,_playingSounds :: M.Map a Sound
} }
data Sound = Sound data Sound = Sound
{ _soundTime :: Maybe Int { _soundTime :: Maybe Int