tracer/src/Load.hs

166 lines
5.9 KiB
Haskell
Raw Normal View History

module Load where
import Affection as A
import qualified SDL
import Graphics.Rendering.OpenGL.GL.FlushFinish (finish)
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar
import Control.Monad (when)
import qualified Data.Map as M
import Data.Ecstasy
import Data.Maybe
import System.Exit (exitFailure)
import NanoVG hiding (V2(..))
-- internal imports
import Types
2018-06-28 19:07:58 +00:00
import MainGame.WorldMap
import Util
loadLoad :: Affection UserData ()
loadLoad = do
2018-07-06 15:17:12 +00:00
ad <- A.get
ud <- getAffection
progress <- liftIO $ newMVar 0
future <- liftIO $ newEmptyMVar
_ <- liftIO $ createFont (nano ud) "bedstead"
(FileName "assets/font/Bedstead-Semicondensed.ttf")
_ <- liftIO $ forkIO $
loadFork
(worldState ud)
2018-07-03 14:19:27 +00:00
(fromJust $ window ud)
(fromJust $ threadContext ud)
(nano ud)
future
progress
2018-07-06 15:17:12 +00:00
SDL.glMakeCurrent (fromJust $ window ud) (glContext ad)
putAffection ud
{ stateMVar = future
, stateProgress = progress
, state = Load
, assetFonts = M.fromList
[ (FontBedstead, "bedstead")
]
}
loadFork
:: (SystemState Entity IO)
2018-07-03 14:19:27 +00:00
-> SDL.Window
-> SDL.GLContext
-> Context
-> MVar (SystemState Entity IO, StateData)
-> MVar Float
-> IO ()
2018-07-03 14:19:27 +00:00
loadFork ws win glc nvg future progress = do
let stateSteps = 22
increment = 1 / stateSteps
SDL.glMakeCurrent win glc
modifyMVar_ progress (return . (+ increment))
mwallasc <- createImage nvg (FileName "assets/walls/wall_asc.png") 0
modifyMVar_ progress (return . (+ increment))
mwalldesc <- createImage nvg (FileName "assets/walls/wall_desc.png") 0
modifyMVar_ progress (return . (+ increment))
mwallcornern <- createImage nvg (FileName "assets/walls/wall_corner_n.png") 0
modifyMVar_ progress (return . (+ increment))
mwallcornere <- createImage nvg (FileName "assets/walls/wall_corner_e.png") 0
modifyMVar_ progress (return . (+ increment))
mwallcorners <- createImage nvg (FileName "assets/walls/wall_corner_s.png") 0
modifyMVar_ progress (return . (+ increment))
mwallcornerw <- createImage nvg (FileName "assets/walls/wall_corner_w.png") 0
modifyMVar_ progress (return . (+ increment))
mwalltne <- createImage nvg (FileName "assets/walls/wall_t_ne.png") 0
modifyMVar_ progress (return . (+ increment))
mwalltse <- createImage nvg (FileName "assets/walls/wall_t_se.png") 0
modifyMVar_ progress (return . (+ increment))
mwalltsw <- createImage nvg (FileName "assets/walls/wall_t_sw.png") 0
modifyMVar_ progress (return . (+ increment))
mwalltnw <- createImage nvg (FileName "assets/walls/wall_t_nw.png") 0
modifyMVar_ progress (return . (+ increment))
mwallcross <- createImage nvg (FileName "assets/walls/wall_cross.png") 0
modifyMVar_ progress (return . (+ increment))
mmiscbox1 <- createImage nvg (FileName "assets/misc/box1.png") 0
modifyMVar_ progress (return . (+ increment))
mmisctable1 <- createImage nvg (FileName "assets/misc/table1.png") 0
modifyMVar_ progress (return . (+ increment))
mmisctable2 <- createImage nvg (FileName "assets/misc/table2.png") 0
modifyMVar_ progress (return . (+ increment))
mmisctable3 <- createImage nvg (FileName "assets/misc/table3.png") 0
modifyMVar_ progress (return . (+ increment))
mmisctable4 <- createImage nvg (FileName "assets/misc/table4.png") 0
modifyMVar_ progress (return . (+ increment))
mmisctableC <- createImage nvg (FileName "assets/misc/tableCorner.png") 0
modifyMVar_ progress (return . (+ increment))
let mimgs = [ mwallasc, mwalldesc,
mwallcornern, mwallcornere, mwallcorners, mwallcornerw,
mwalltne, mwalltse, mwalltsw, mwalltnw, mwallcross,
mmiscbox1,
mmisctable1, mmisctable2, mmisctable3, mmisctable4, mmisctableC
]
when (any isNothing mimgs) $ do
liftIO $logIO Error "Failed to load image assets"
exitFailure
-- playerImgs <- loadPlayerSprite "assets/intruder.png" 64 74 nvg
-- (zipWith (\a b -> (a, [b])) [0..] $ [ImgIntrE .. ImgIntrN] ++ [ImgIntrNE])
let imgs = zipWith (\a b -> (a, fromJust b)) [ImgWallAsc ..] mimgs
directions = [E .. N] ++ [NE]
standIds var = map (AnimId var "standing") directions
walkIds var = map (AnimId var "walking") directions
standConfigs = map
(\i -> AnimationConfig (0, i * 74) (64, 74) 1 0 APLoop)
[0 .. length (standIds 0) - 1]
walkConfigs = map
(\i -> AnimationConfig (64, i * 74) (64, 74) 6 1.5 APLoop)
[0 .. length (walkIds 0) - 1]
playerStanding <- loadAnimationSprites "assets/intruder.png" nvg
(zip (standIds 0) standConfigs)
liftIO $modifyMVar_ progress (return . (+ increment))
playerWalking <- loadAnimationSprites "assets/intruder.png" nvg
(zip (walkIds 0) walkConfigs)
modifyMVar_ progress (return . (+ increment))
jdoemStanding <- loadAnimationSprites "assets/jdoem.png" nvg
(zip (standIds 1) standConfigs)
liftIO $modifyMVar_ progress (return . (+ increment))
jdoemWalking <- loadAnimationSprites "assets/jdoem.png" nvg
(zip (walkIds 1) walkConfigs)
modifyMVar_ progress (return . (+ increment))
finish
putMVar future
( ws
, LoadData
{ loadAssetImages = M.fromList imgs
, loadAssetAnims = M.fromList
(playerStanding ++ playerWalking ++ jdoemStanding ++ jdoemWalking)
}
)
drawLoad :: Affection UserData ()
drawLoad = do
ud <- getAffection
progress <- liftIO $ readMVar (stateProgress ud)
liftIO $ do
logIO A.Verbose ("LoadProgress: " ++ show progress)
drawLoadScreen ud progress
updateLoad :: Double -> Affection UserData ()
updateLoad _ = do
ud <- getAffection
mwsld <- liftIO $ tryTakeMVar (stateMVar ud)
case mwsld of
Just (_, ld) -> do
liftIO $ logIO A.Debug "loaded assets, entering menu"
putAffection ud
{ assetImages = loadAssetImages ld
, assetAnimations = loadAssetAnims ld
2018-06-28 19:07:58 +00:00
, state = Main WorldMap
, stateData = None
}
loadMap
2018-07-03 14:19:27 +00:00
Nothing ->
return ()