From 44ec3cc8e03fef4a73b809eae72e757d25875fbe Mon Sep 17 00:00:00 2001 From: nek0 Date: Mon, 4 May 2020 05:26:49 +0200 Subject: [PATCH] comments and formatting --- src/Affection.hs | 84 ++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/src/Affection.hs b/src/Affection.hs index bb44eac..c589c36 100644 --- a/src/Affection.hs +++ b/src/Affection.hs @@ -80,7 +80,9 @@ withAffection AffectionConfig{..} = do -- get current time execTime <- getTime Monotonic liftIO $ logIO Debug "Loading initial data container" + -- construct game data object from provided Affectionate instance gameData <- loadState @us + -- build state container let initContainer = AffectionData { drawWindows = windows , glContext = contexts @@ -89,45 +91,51 @@ withAffection AffectionConfig{..} = do , sysTime = execTime , pausedTime = False } - (_, nState) <- runStateT ( A.runState $ do - liftIO $ logIO Debug "Starting Loop" - preLoop gameData - whileM_ (hasNextStep gameData) + -- initialize and run state + (_, nState) <- runStateT + (A.runState (do - -- get state - ad <- get - -- Measure time difference form last run - now <- liftIO $ getTime Monotonic - let lastTime = sysTime ad - -- compute dt and update elapsedTime - let !dt = fromIntegral - (toNanoSecs $ diffTimeSpec lastTime now) / (10 ^ (9 :: Int)) - !ne = elapsedTime ad + dt - put $ ad - { elapsedTime = ne - , deltaTime = dt - } - -- poll events - evs <- preHandleEvents =<< liftIO SDL.pollEvents - -- handle events - handleEvents gameData evs - -- execute user defined update loop - unless (pausedTime ad) (update gameData dt) - -- execute user defined draw loop - liftIO $ GL.clear [GL.ColorBuffer, GL.DepthBuffer, GL.StencilBuffer] - draw gameData - liftIO GL.flush - -- actual displaying of newly drawn frame - mapM_ (SDL.glSwapWindow . (\(_,y,_) -> y)) windows - -- save new time - ad3 <- get - when (sysTime ad == sysTime ad3) ( - put ad3 - { sysTime = now - } - ) - ) - ) initContainer + liftIO $ logIO Debug "Starting Loop" + -- run preLoop function from Affectionate + preLoop gameData + whileM_ (hasNextStep gameData) + (do + -- get state + ad <- get + -- Measure time difference form last run + now <- liftIO $ getTime Monotonic + let lastTime = sysTime ad + -- compute dt and update elapsedTime + let !dt = fromIntegral + (toNanoSecs $ diffTimeSpec lastTime now) / (10 ^ (9 :: Int)) + !ne = elapsedTime ad + dt + -- update state data object with new time values + put $ ad + { elapsedTime = ne + , deltaTime = dt + } + -- poll events + evs <- preHandleEvents =<< liftIO SDL.pollEvents + -- handle events + handleEvents gameData evs + -- execute user defined update loop + unless (pausedTime ad) (update gameData dt) + -- clear GL buffer >> execute user defined draw loop >> flush GL buffer + liftIO $ GL.clear [GL.ColorBuffer, GL.DepthBuffer, GL.StencilBuffer] + draw gameData + liftIO GL.flush + -- actual displaying of newly drawn frame + mapM_ (SDL.glSwapWindow . (\(_,y,_) -> y)) windows + -- -- save new time + -- ad3 <- get + -- when (sysTime ad == sysTime ad3) ( + -- put ad3 + -- { sysTime = now + -- } + -- ) + ) + ) + ) initContainer liftIO $ logIO Debug "Loop ended. Cleaning" cleanUp gameData liftIO $ logIO Debug "Destroying Window"