tracer/src/Main.hs

137 lines
4.1 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)
2020-05-05 10:26:16 +02:00
import Control.Monad
import Control.Concurrent.MVar
2022-08-04 18:13:53 +02:00
import Control.Monad.Trans.Resource (allocate)
2018-09-25 00:13:09 +02:00
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
2018-10-12 14:26:06 +02:00
import Util
2018-02-07 01:18:16 +01:00
foreign import ccall unsafe "glewInit"
glewInit :: IO CInt
2020-05-05 10:26:16 +02:00
instance Affectionate UserData where
preLoop = (\ud -> pre ud >> smLoad Load ud)
handleEvents = handle
update = Main.update
draw = Main.draw
loadState = Init.init
cleanUp = clean
hasNextStep = liftIO . readMVar . doNextStep
2018-02-07 01:18:16 +01:00
main :: IO ()
main = do
let config = AffectionConfig
2019-10-28 18:20:34 +01:00
{ initComponents = All
, windowTitle = "Tracer"
, windowConfigs =
[ ( 0
, SDL.defaultWindow
{ SDL.windowInitialSize = V2 1280 720
, SDL.windowResizable = True
, SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 3
}
}
2020-05-05 10:26:16 +02:00
, SDL.Windowed
2019-10-28 18:20:34 +01:00
)
]
2020-05-05 10:26:16 +02:00
} :: AffectionConfig UserData
2018-02-07 01:18:16 +01:00
withAffection config
2020-05-05 10:26:16 +02:00
pre :: UserData -> Affection ()
pre ud = do
ad <- A.get
2022-08-04 18:13:53 +02:00
let (AffectionWindow awwindow _ _) = head $ drawWindows ad
threadCtx <- SDL.glCreateContext awwindow
SDL.glMakeCurrent awwindow (acContext $ head $ glContext ad)
2020-05-05 10:26:16 +02:00
let Subsystems w _ k j _ = subsystems ud
2018-02-07 01:18:16 +01:00
_ <- partSubscribe w (fitViewport (1280/720))
2020-05-05 10:26:16 +02:00
_ <- partSubscribe w (exitOnWindowClose ud)
2018-09-18 05:35:40 +02:00
_ <- partSubscribe k toggleFullScreen
2020-05-05 10:26:16 +02:00
_ <- partSubscribe k (quitGame ud)
u <- partSubscribe j (cacheJoypad ud)
2018-08-10 08:58:26 +02:00
(ws, _) <- yieldSystemT (0, defStorage) (return ())
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (threadContext ud) (Just threadCtx)
2022-08-04 18:13:53 +02:00
void $ liftIO $ swapMVar (window ud) (Just awwindow)
2020-05-05 10:26:16 +02:00
void $ liftIO $ putMVar (worldState ud) ws
void $ liftIO $ putMVar (joyUUID ud) u
quitGame :: UserData -> KeyboardMessage -> Affection ()
quitGame ud (MsgKeyboardEvent _ _ SDL.Pressed False sym)
| SDL.keysymKeycode sym == SDL.KeycodeEscape =
void $ liftIO $ swapMVar (doNextStep ud) False
2018-09-25 00:13:09 +02:00
| SDL.keysymKeycode sym == SDL.KeycodeF5 = do
2018-09-21 22:25:23 +02:00
ad <- A.get
2020-05-05 10:26:16 +02:00
curState <- liftIO $ readMVar (state ud)
when (curState == Main WorldMap || curState == Main MindMap) $ do
2018-10-08 23:36:52 +02:00
let Subsystems w m k j t = subsystems ud
2020-05-05 10:26:16 +02:00
curUUID <- liftIO $ readMVar (uuid ud)
mapM_ (partUnSubscribe w) curUUID
mapM_ (partUnSubscribe m) curUUID
mapM_ (partUnSubscribe k) curUUID
mapM_ (partUnSubscribe j) curUUID
mapM_ (partUnSubscribe t) curUUID
2019-10-28 18:20:34 +01:00
SDL.glMakeCurrent
2022-08-04 18:13:53 +02:00
(awWindow $ head $ drawWindows ad)
(acContext $ head $ glContext ad)
2018-09-25 00:13:09 +02:00
(ws, _) <- yieldSystemT (0, defStorage) (return ())
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (worldState ud) ws
void $ liftIO $ swapMVar (state ud) Load
smLoad Load ud
| otherwise = return ()
quitGame _ _ = return ()
toggleFullScreen :: KeyboardMessage -> Affection ()
2018-09-18 05:35:40 +02:00
toggleFullScreen (MsgKeyboardEvent _ _ SDL.Pressed False sym)
2020-05-05 10:26:16 +02:00
| SDL.keysymKeycode sym == SDL.KeycodeF11 = toggleScreen 0
2018-09-18 05:35:40 +02:00
| otherwise = return ()
toggleFullScreen _ = return ()
2020-05-05 10:26:16 +02:00
update :: UserData -> Double -> Affection ()
update ud dt = do
curState <- liftIO $ readMVar (state ud)
smUpdate curState ud dt
2018-02-07 01:18:16 +01:00
2020-05-05 10:26:16 +02:00
draw :: UserData -> Affection ()
draw ud = do
curState <- liftIO $ readMVar (state ud)
2018-02-07 01:18:16 +01:00
liftIO $ beginFrame (nano ud) 1280 720 1
2020-05-05 10:26:16 +02:00
smDraw curState ud
2018-02-07 01:18:16 +01:00
liftIO $ endFrame (nano ud)
2020-05-05 10:26:16 +02:00
handle :: UserData -> [SDL.EventPayload] -> Affection ()
handle ud evs = do
s <- liftIO $ readMVar (state ud)
smEvent s ud evs
2018-02-07 01:18:16 +01:00
2020-05-05 10:26:16 +02:00
exitOnWindowClose :: UserData -> WindowMessage -> Affection ()
exitOnWindowClose ud (MsgWindowClose _ _) = do
2018-02-07 01:18:16 +01:00
liftIO $ logIO A.Debug "Window Closed"
2020-05-05 10:26:16 +02:00
void $ liftIO $ swapMVar (doNextStep ud) False
exitOnWindowClose _ _ = return ()
2018-06-24 00:43:09 +02:00
clean :: UserData -> IO ()
2020-05-05 10:26:16 +02:00
clean ud = do
tContext <- readMVar (threadContext ud)
SDL.glDeleteContext $ fromJust tContext