Add haddock comments
This commit is contained in:
+36
-32
@@ -1,3 +1,9 @@
|
|||||||
|
{-|
|
||||||
|
Module : Loop
|
||||||
|
Description : A minimal game loop
|
||||||
|
|
||||||
|
This module sets up an SDL window which may be updated using a simple game loop.
|
||||||
|
-}
|
||||||
module Loop
|
module Loop
|
||||||
( setupLoop
|
( setupLoop
|
||||||
) where
|
) where
|
||||||
@@ -12,25 +18,17 @@ import Foreign.C
|
|||||||
|
|
||||||
import Control.Lens ((.~),(&),(+~))
|
import Control.Lens ((.~),(&),(+~))
|
||||||
|
|
||||||
winConfig :: Int -> Int -> WindowConfig
|
-- | Create a game loop with an SDL window.
|
||||||
winConfig x y = defaultWindow
|
setupLoop
|
||||||
{ windowGraphicsContext
|
:: (Int,Int) -- ^ The window size.
|
||||||
= OpenGLContext (defaultOpenGL { glProfile = Core Normal 4 3
|
-> IO params -- ^ Initial parameters.
|
||||||
, glColorPrecision = V4 8 8 8 8
|
-> (params -> IO ()) -- ^ Function for cleaning up parameters, applied when exiting loop.
|
||||||
}
|
-> IO world -- ^ Initial simulation state.
|
||||||
)
|
-> (params -> world -> IO params) -- ^ Parameter update, called once per frame. Allows for side effects such as rendering.
|
||||||
, windowInitialSize = V2 (fromIntegral x) (fromIntegral y)
|
|
||||||
, windowResizable =True
|
|
||||||
}
|
|
||||||
|
|
||||||
setupLoop
|
|
||||||
:: (Int,Int)
|
|
||||||
-> IO params
|
|
||||||
-> (params -> IO ())
|
|
||||||
-> IO world
|
|
||||||
-> (params -> world -> IO params)
|
|
||||||
-> (world -> Event -> Maybe world)
|
-> (world -> Event -> Maybe world)
|
||||||
|
-- ^ SDL Event handling, once per frame. Evaluating 'Nothing' exits the loop.
|
||||||
-> (world -> Maybe world)
|
-> (world -> Maybe world)
|
||||||
|
-- ^ Simulation update, once per frame. 'Nothing' exits the loop.
|
||||||
-> IO ()
|
-> IO ()
|
||||||
setupLoop
|
setupLoop
|
||||||
(xSize,ySize)
|
(xSize,ySize)
|
||||||
@@ -53,13 +51,14 @@ setupLoop
|
|||||||
paramCleanup
|
paramCleanup
|
||||||
$ doLoop window startWorld sideEffects eventFn worldFn
|
$ doLoop window startWorld sideEffects eventFn worldFn
|
||||||
|
|
||||||
|
-- | The internal loop.
|
||||||
doLoop
|
doLoop
|
||||||
:: Window
|
:: Window -- ^ The SDL window.
|
||||||
-> world
|
-> world -- ^ Current simulation state.
|
||||||
-> (params -> world -> IO params)
|
-> (params -> world -> IO params) -- ^ Parameter update.
|
||||||
-> (world -> Event -> Maybe world)
|
-> (world -> Event -> Maybe world) -- ^ SDL Event handling.
|
||||||
-> (world -> Maybe world)
|
-> (world -> Maybe world) -- ^ Simulation update
|
||||||
-> params
|
-> params -- ^ Current parameters.
|
||||||
-> IO ()
|
-> IO ()
|
||||||
doLoop
|
doLoop
|
||||||
window
|
window
|
||||||
@@ -92,15 +91,8 @@ applyEventsIO
|
|||||||
-> IO (Maybe world)
|
-> IO (Maybe world)
|
||||||
applyEventsIO fn w = foldM (applyEventIO fn) (Just w)
|
applyEventsIO fn w = foldM (applyEventIO fn) (Just w)
|
||||||
|
|
||||||
--eventCloseOrResize :: Event -> IO (Maybe Event)
|
-- | Handle quit events in a manner to exit the loop. Other events handled as
|
||||||
--eventCloseOrResize e = case eventPayload e of
|
-- determined by the custom function, although resize events also change the viewport.
|
||||||
-- 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
|
||||||
@@ -109,3 +101,15 @@ applyEventIO fn mw e = case eventPayload e of
|
|||||||
-> GL.viewport $= (GL.Position 0 0,GL.Size x y) >> return (mw >>= \w -> fn w e)
|
-> GL.viewport $= (GL.Position 0 0,GL.Size x y) >> return (mw >>= \w -> fn w e)
|
||||||
_ -> return $ mw >>= flip fn e
|
_ -> return $ mw >>= flip fn e
|
||||||
|
|
||||||
|
-- | Create an OpenGL SDL window configuration with a given x and y size.
|
||||||
|
winConfig :: Int -> Int -> WindowConfig
|
||||||
|
winConfig x y = defaultWindow
|
||||||
|
{ windowGraphicsContext
|
||||||
|
= OpenGLContext (defaultOpenGL { glProfile = Core Normal 4 3
|
||||||
|
, glColorPrecision = V4 8 8 8 8
|
||||||
|
}
|
||||||
|
)
|
||||||
|
, windowInitialSize = V2 (fromIntegral x) (fromIntegral y)
|
||||||
|
, windowResizable =True
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user