Add (persistent) volume control

This commit is contained in:
jgk
2021-04-08 11:06:02 +02:00
parent f1214c0584
commit ea0dfbe1df
8 changed files with 76 additions and 54 deletions
+39
View File
@@ -0,0 +1,39 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StrictData #-}
module Dodge.Config.Data (
Configuration (..)
, defaultConfig
, volume_master
, volume_sound
, volume_music
) where
import Data.Aeson
import Foreign.C.Types
import GHC.Generics
import qualified GHC.Int
import qualified SDL
import System.Directory
import Control.Lens
data Configuration = Configuration
{ _volume_master :: Float
, _volume_sound :: Float
, _volume_music :: Float
}
deriving (Generic, Show)
makeLenses ''Configuration
instance ToJSON Configuration where
toEncoding = genericToEncoding defaultOptions
instance FromJSON Configuration
defaultConfig = Configuration
{ _volume_master = 1
, _volume_sound = 1
, _volume_music = 1
}
+10 -7
View File
@@ -1,10 +1,10 @@
{-|
IO actions that apply config side effects and save it to disk.
IO actions that apply config side effects and save configuration settings to disk.
-}
module Dodge.Config.Update
where
import Dodge.Data
import Dodge.LoadConfig
import Dodge.Config.Data
import Sound
import Data.Aeson (encodeFile)
@@ -12,11 +12,14 @@ import Control.Monad (when)
updateDodgeConfig :: World -> IO ()
updateDodgeConfig w = case _menuLayers w of
(ConfigSaveScreen _: _) -> do
putStrLn "Saving config"
(ConfigSaveScreen : _) -> do
putStrLn "Saving config to data/dodge.config.json"
encodeFile "data/dodge.config.json" cfig
_ -> when (_configNeedsUpdate w) $ do
setSoundVolume ( _volume_master cfig * _volume_sound cfig)
setMusicVolume ( _volume_master cfig * _volume_music cfig)
_ -> when (_configNeedsUpdate w) (setVolume cfig)
where
cfig = _config w
setVolume :: Configuration -> IO ()
setVolume cfig = do
setSoundVolume ( _volume_master cfig * _volume_sound cfig)
setMusicVolume ( _volume_master cfig * _volume_music cfig)