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
+29 -12
View File
@@ -47,43 +47,60 @@ setupLoop
destroyWindow
$ \window -> bracket
(glCreateContext window)
(\con -> GL.finish >> glDeleteContext con)
$ \_ -> bracket
( \con -> GL.finish >> glDeleteContext con)
$ \_ -> bracket
initParams
paramCleanup
$ \setup -> doLoop setup window startWorld sideEffects eventFn worldFn
$ doLoop window startWorld sideEffects eventFn worldFn
doLoop
:: params
-> Window
:: Window
-> world
-> (params -> world -> IO params)
-> (world -> Event -> Maybe world)
-> (world -> Maybe world)
-> params
-> IO ()
doLoop
setup
window
startWorld
worldSideEffects
eventFn
worldUpdate
startParams
= do
startLoopTicks <- ticks
newParams <- worldSideEffects setup startWorld
startTicks <- ticks
newParams <- worldSideEffects startParams startWorld
glSwapWindow window
events <- pollEvents
maybeUpdatedWorld <- foldM (applyEventIO eventFn) (Just startWorld) events
case maybeUpdatedWorld >>= worldUpdate of
Just updatedWorld -> do
performGC
endLoopTicks <- ticks -- it might be better to use System.Clock (monotonic)
let delay = max 0 (20 + fromIntegral startLoopTicks - fromIntegral endLoopTicks)
endTicks <- ticks -- it might be better to use System.Clock (monotonic)
let delay = max 0 (20 + fromIntegral startTicks - fromIntegral endTicks)
threadDelay (delay * 1000 )
endTime <- ticks
doLoop newParams window updatedWorld worldSideEffects eventFn worldUpdate
doLoop window updatedWorld worldSideEffects eventFn worldUpdate newParams
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 fn mw e = case eventPayload e of
QuitEvent -> return Nothing