tracer/src/Init.hs

80 lines
2.1 KiB
Haskell
Raw Normal View History

2018-02-07 00:18:16 +00:00
{-# LANGUAGE ForeignFunctionInterface #-}
module Init where
import Affection as A
import NanoVG hiding (V2(..), V3(..))
2018-05-20 22:40:40 +00:00
import NanoVG.Internal.Image (ImageFlags(..))
2018-02-07 00:18:16 +00:00
2018-05-20 22:40:40 +00:00
import Codec.Picture as CP
import Codec.Picture.Extra
2018-02-07 00:18:16 +00:00
import Control.Concurrent.STM
import qualified Data.Set as S
import qualified Data.Map.Strict as M
2018-05-20 22:40:40 +00:00
import Data.ByteString.Lazy (toStrict)
2018-02-25 09:30:13 +00:00
import System.Exit (exitFailure)
2018-02-07 00:18:16 +00:00
import Foreign.C.Types (CInt(..))
import Types
foreign import ccall unsafe "glewInit"
glewInit :: IO CInt
init :: IO UserData
init = do
2018-02-07 00:18:16 +00:00
subs <- Subsystems
2018-10-08 16:54:23 +00:00
<$> (SubWindow <$> newTVarIO [])
<*> (SubMouse <$> newTVarIO [])
<*> (SubKeyboard <$> newTVarIO [])
<*> (SubJoypad <$> newTVarIO [])
2018-10-08 21:36:52 +00:00
<*> (SubTranslator <$> newTVarIO [])
2018-06-07 22:29:46 +00:00
_ <- glewInit
nvg <- createGL3 (S.fromList [NanoVG.Debug, Antialias, StencilStrokes])
2018-02-07 00:18:16 +00:00
return UserData
{ state = Load
2018-05-30 14:20:58 +00:00
, subsystems = subs
, assetImages = M.empty
, assetAnimations = M.empty
, assetFonts = M.empty
2018-10-08 21:36:52 +00:00
, assetIcons = M.empty
2018-05-30 14:20:58 +00:00
, nano = nvg
, uuid = []
, stateData = None
, threadContext = Nothing
2018-10-08 21:36:52 +00:00
, joystick = Nothing
2018-02-07 00:18:16 +00:00
}
2018-05-20 22:40:40 +00:00
loadPlayerSprite
:: FilePath -- Path to spritemap
-> Int -- width of single sprite
-> Int -- height of single sprite
-> Context -- Nanovg context
-> [(Int, [ImgId])] -- [(picture row, Image IDs)]
-> IO [(ImgId, NanoVG.Image)]
loadPlayerSprite fp w h nvg rids = do
eimg <- readImage fp
case eimg of
Left err -> do
logIO Error err
exitFailure
Right dimg -> do
let img = convertRGBA8 dimg
2018-07-03 14:19:27 +00:00
ret <- mapM (\(row, ids) ->
mapM (\(num, i) -> do
2018-05-20 22:40:40 +00:00
let cr = crop (num * w) (row * h) w h img
2018-05-30 14:20:58 +00:00
mresimg <-
2018-07-03 14:19:27 +00:00
createImageMem nvg ImagePremultiplied (toStrict $ encodePng cr)
2018-05-20 22:40:40 +00:00
case mresimg of
Nothing -> do
2018-07-03 14:19:27 +00:00
logIO Error ("Failed to load: " ++ fp ++ " " ++ show i)
2018-05-20 22:40:40 +00:00
exitFailure
2018-07-03 14:19:27 +00:00
Just resimg -> return (i, resimg)
2018-05-20 22:40:40 +00:00
) (zip [0..] ids)
) rids
return $ concat ret