tracer/src/Main.hs

93 lines
2.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE ForeignFunctionInterface #-}
2018-02-07 01:18:16 +01:00
module Main where
import Affection as A
2018-08-10 08:58:26 +02:00
import Data.Ecstasy
2018-02-07 01:18:16 +01:00
import qualified SDL
import NanoVG hiding (V2(..), V3(..))
import Linear
import Foreign.C.Types (CInt(..))
2018-06-24 00:43:09 +02:00
import Data.Maybe (fromJust)
2018-02-07 01:18:16 +01:00
-- internal imports
import Types hiding (draw)
2018-02-07 01:18:16 +01:00
import StateMachine ()
import Init
foreign import ccall unsafe "glewInit"
glewInit :: IO CInt
2018-02-07 01:18:16 +01:00
main :: IO ()
main = do
let config = AffectionConfig
{ initComponents = All
, windowTitle = "Tracer"
, windowConfig = SDL.defaultWindow
{ SDL.windowInitialSize = V2 1280 720
, SDL.windowResizable = True
, SDL.windowOpenGL = Just SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 3
}
}
, canvasSize = Nothing
, preLoop = pre >> smLoad Load
2018-02-07 01:18:16 +01:00
, eventLoop = handle
, updateLoop = update
, drawLoop = draw
, loadState = Init.init
2018-06-24 00:43:09 +02:00
, cleanUp = clean
2018-02-07 01:18:16 +01:00
, initScreenMode = SDL.Windowed
}
withAffection config
pre :: Affection UserData ()
pre = do
ad <- A.get
ud <- getAffection
-- _ <- SDL.glSetAttribute SDL.SDL_GL_SHARE_WITH_CURRENT_CONTEXT 1
2018-07-03 16:19:27 +02:00
threadCtx <- SDL.glCreateContext (drawWindow ad)
SDL.glMakeCurrent (drawWindow ad) (glContext ad)
2018-07-03 16:49:27 +02:00
let Subsystems w m k = subsystems ud
2018-02-07 01:18:16 +01:00
_ <- partSubscribe w (fitViewport (1280/720))
2018-02-18 03:11:41 +01:00
_ <- partSubscribe w exitOnWindowClose
2018-08-10 08:58:26 +02:00
(ws, _) <- yieldSystemT (0, defStorage) (return ())
putAffection ud
2018-07-03 16:19:27 +02:00
{ threadContext = Just threadCtx
2018-08-10 08:58:26 +02:00
, window = Just (drawWindow ad)
, worldState = ws
}
2018-02-07 01:18:16 +01:00
update :: Double -> Affection UserData ()
update dt = do
ud <- getAffection
smUpdate (state ud) dt
draw :: Affection UserData ()
draw = do
ud <- getAffection
liftIO $ beginFrame (nano ud) 1280 720 1
smDraw (state ud)
liftIO $ endFrame (nano ud)
handle :: [SDL.EventPayload] -> Affection UserData ()
handle evs = do
s <- state <$> getAffection
smEvent s evs
exitOnWindowClose :: WindowMessage -> Affection UserData ()
exitOnWindowClose (MsgWindowClose _ _) = do
liftIO $ logIO A.Debug "Window Closed"
quit
exitOnWindowClose _ = return ()
2018-06-24 00:43:09 +02:00
clean :: UserData -> IO ()
2018-07-03 16:19:27 +02:00
clean ud =
2018-06-24 00:43:09 +02:00
SDL.glDeleteContext $ fromJust $ threadContext ud