Cleanup preload, add basic frame time checking

This commit is contained in:
2021-03-24 18:41:29 +01:00
parent f45ca2dd24
commit 3a52f3feaf
5 changed files with 40 additions and 43 deletions
+9 -8
View File
@@ -60,16 +60,17 @@ main = do
(worldPictures w) (worldPictures w)
blendFunc $= (SrcAlpha,OneMinusSrcAlpha) blendFunc $= (SrcAlpha,OneMinusSrcAlpha)
renderFoldable (_renderData preData) (picToLTree Nothing $ fixedCoordPictures w) renderFoldable (_renderData preData) (picToLTree Nothing $ fixedCoordPictures w)
endRenderTicks <- SDL.ticks
playSoundQueue (_soundData preData) (_soundQueue w) playSoundQueue (_soundData preData) (_soundQueue w)
newSoundData <- playAndUpdate (_sounds w) (_soundData preData) newSoundData <- playAndUpdate (_sounds w) (_soundData preData)
endMixerTicks <- SDL.ticks
return $ (preData {_soundData = newSoundData}) endTicks <- SDL.ticks
& renderTime +~ (endRenderTicks - startTicks) let lastFrameTicks = _frameTimer preData
& mixerTime +~ (endMixerTicks - endRenderTicks) renderFoldable (_renderData preData)
& renderLighting +~ (lightTicks - startTicks) (picToLTree Nothing . setLayer 1 . setDepth (-1)
& renderPicture +~ (endRenderTicks - lightTicks) . translate 0 (-0.8) . scale 0.0005 0.0005
& pokeTime +~ timeSpentPoking . text $ show (endTicks - lastFrameTicks))
return $ preData & soundData .~ newSoundData
& frameTimer .~ endTicks
) )
(flip $ menuEvents $ flip handleEvent) (flip $ menuEvents $ flip handleEvent)
(\w -> Just $ update w) (\w -> Just $ update w)
+27 -10
View File
@@ -51,39 +51,56 @@ setupLoop
$ \_ -> bracket $ \_ -> bracket
initParams initParams
paramCleanup paramCleanup
$ \setup -> doLoop setup window startWorld sideEffects eventFn worldFn $ doLoop window startWorld sideEffects eventFn worldFn
doLoop doLoop
:: params :: Window
-> Window
-> world -> world
-> (params -> world -> IO params) -> (params -> world -> IO params)
-> (world -> Event -> Maybe world) -> (world -> Event -> Maybe world)
-> (world -> Maybe world) -> (world -> Maybe world)
-> params
-> IO () -> IO ()
doLoop doLoop
setup
window window
startWorld startWorld
worldSideEffects worldSideEffects
eventFn eventFn
worldUpdate worldUpdate
startParams
= do = do
startLoopTicks <- ticks startTicks <- ticks
newParams <- worldSideEffects setup startWorld newParams <- worldSideEffects startParams startWorld
glSwapWindow window glSwapWindow window
events <- pollEvents events <- pollEvents
maybeUpdatedWorld <- foldM (applyEventIO eventFn) (Just startWorld) events maybeUpdatedWorld <- foldM (applyEventIO eventFn) (Just startWorld) events
case maybeUpdatedWorld >>= worldUpdate of case maybeUpdatedWorld >>= worldUpdate of
Just updatedWorld -> do Just updatedWorld -> do
performGC performGC
endLoopTicks <- ticks -- it might be better to use System.Clock (monotonic) endTicks <- ticks -- it might be better to use System.Clock (monotonic)
let delay = max 0 (20 + fromIntegral startLoopTicks - fromIntegral endLoopTicks)
let delay = max 0 (20 + fromIntegral startTicks - fromIntegral endTicks)
threadDelay (delay * 1000 ) threadDelay (delay * 1000 )
endTime <- ticks
doLoop newParams window updatedWorld worldSideEffects eventFn worldUpdate doLoop window updatedWorld worldSideEffects eventFn worldUpdate newParams
Nothing -> return () Nothing -> return ()
applyEventsIO
:: (world -> Event -> Maybe world)
-> world
-> [Event]
-> IO (Maybe world)
applyEventsIO fn w es = foldM (applyEventIO fn) (Just w) es
--eventCloseOrResize :: Event -> IO (Maybe Event)
--eventCloseOrResize e = case eventPayload e of
-- QuitEvent -> return Nothing
-- WindowClosedEvent _ -> return Nothing
-- WindowSizeChangedEvent (WindowSizeChangedEventData {windowSizeChangedEventSize = V2 x y})
-- -> GL.viewport $= (GL.Position 0 0,GL.Size x y) >> return (Just e)
-- _ -> return $ Just e
--
applyEventIO :: (world -> Event -> Maybe world) -> Maybe world -> Event -> IO (Maybe world) applyEventIO :: (world -> Event -> Maybe world) -> Maybe world -> Event -> IO (Maybe world)
applyEventIO fn mw e = case eventPayload e of applyEventIO fn mw e = case eventPayload e of
QuitEvent -> return Nothing QuitEvent -> return Nothing
+1 -13
View File
@@ -17,21 +17,9 @@ doPreload :: IO (PreloadData a)
doPreload = do doPreload = do
sData <- preloadSound sData <- preloadSound
rData <- preloadRender rData <- preloadRender
return $ PreloadData rData sData 0 0 0 0 0 0 0 0 0 return $ PreloadData rData sData 0
cleanUpPreload :: PreloadData a -> IO () cleanUpPreload :: PreloadData a -> IO ()
cleanUpPreload pd = do cleanUpPreload pd = do
cleanUpRenderPreload $ _renderData pd cleanUpRenderPreload $ _renderData pd
cleanUpSoundPreload $ _soundData pd cleanUpSoundPreload $ _soundData pd
showTiming :: PreloadData a -> IO ()
showTiming pd = do
putStrLn $ "Time rendering: " ++ show (_renderTime pd)
putStrLn $ "Time renderingLighting: " ++ show (_renderLighting pd)
putStrLn $ "Time renderingPicture: " ++ show (_renderPicture pd)
putStrLn $ "Time sim-ing: " ++ show (_simTime pd)
putStrLn $ "Time mixing: " ++ show (_mixerTime pd)
putStrLn $ "Time idle: " ++ show (_idleTime pd)
putStrLn $ "Time gc: " ++ show (_gcTime pd)
putStrLn $ "Time pokeing: " ++ show (_pokeTime pd)
+1 -9
View File
@@ -10,15 +10,7 @@ import Sound.Preload
data PreloadData a = PreloadData data PreloadData a = PreloadData
{ _renderData :: RenderData { _renderData :: RenderData
, _soundData :: SoundData a , _soundData :: SoundData a
, _currentTime :: Word32 , _frameTimer :: Word32
, _renderTime :: Word32
, _renderLighting :: Word32
, _renderPicture :: Word32
, _simTime :: Word32
, _mixerTime :: Word32
, _idleTime :: Word32
, _gcTime :: Word32
, _pokeTime :: Word32
} }
makeLenses ''PreloadData makeLenses ''PreloadData
-1
View File
@@ -12,7 +12,6 @@ import qualified Data.Map as M
import Control.Lens import Control.Lens
playSoundQueue :: SoundData a -> [Int] -> IO () playSoundQueue :: SoundData a -> [Int] -> IO ()
playSoundQueue sd ns = playSoundQueue sd ns =
forM_ ns $ \n -> playIfFree (_loadedChunks sd IM.! n) Mix.Once forM_ ns $ \n -> playIfFree (_loadedChunks sd IM.! n) Mix.Once