edge-0.9.1.0: ResourceTracker.hs
{-|
Module : ResourcesTracker
Description : Stores image and sound resources for use in the program
Copyright : (c) Christopher Howard, 2016
License : GPL-3
Maintainer : ch.howard@zoho.com
Resources (images and sounds) are preloaded into the resource tracker,
so that the rt can be passed into objects, rather than requiring each
object to load its own resources from disk.
-}
module ResourceTracker ( ResourceTracker()
, RT
, emptyResourceTracker
, getImage
, storeImage
, getSound
, storeSound
, protectedGetImage
) where
import Prelude (String, Show)
import Graphics.Gloss.Data.Picture ( Picture(..) )
import Graphics.Gloss.Data.Color ( white )
import Data.Map ( Map, empty, insert )
import qualified Data.Map as M ( lookup )
import Sound.ALUT ( Buffer )
import Data.Maybe ( fromMaybe )
data ResourceTracker = ResourceTracker { images :: Map String Picture
, sounds :: Map String Buffer
}
deriving (Show)
type RT = ResourceTracker
emptyResourceTracker = ResourceTracker { images = empty
, sounds = empty
}
getImage r n = M.lookup n (images r)
storeImage r n p = r { images = insert n p (images r) }
getSound r n = M.lookup n (sounds r)
storeSound r n b = r { sounds = insert n b (sounds r) }
protectedGetImage a b = fromMaybe
(Scale 0.1 0.1
(Color white
(Text "missing image")))
(getImage a b)