affection/examples/example00.hs

139 lines
4 KiB
Haskell
Raw Normal View History

2017-12-15 16:48:12 +00:00
{-# LANGUAGE MultiParamTypeClasses #-}
2016-11-04 15:06:16 +00:00
2016-05-29 16:01:23 +00:00
import Affection
2016-11-04 15:06:16 +00:00
import qualified SDL
2017-12-13 14:19:53 +00:00
import Control.Concurrent.STM
2017-12-15 16:48:12 +00:00
import Control.Monad.IO.Class (liftIO)
2016-12-26 13:06:51 +00:00
2017-12-15 17:12:16 +00:00
newtype StateData = StateData
2017-12-13 14:19:53 +00:00
{ sdSubs :: Subsystems
}
2016-11-04 15:06:16 +00:00
2017-12-13 14:19:53 +00:00
data Subsystems = Subsystems
2017-12-15 16:48:12 +00:00
{ subWindow :: Window
, subMouse :: Mouse
, subKeyboard :: Keyboard
2017-12-13 14:19:53 +00:00
}
2016-05-29 16:01:23 +00:00
2017-12-15 17:12:16 +00:00
newtype Window = Window (TVar [(UUID, WindowMessage -> Affection StateData ())])
newtype Mouse = Mouse (TVar [(UUID, MouseMessage -> Affection StateData ())])
newtype Keyboard = Keyboard (TVar [(UUID, KeyboardMessage -> Affection StateData ())])
2017-12-15 16:48:12 +00:00
instance Participant Window WindowMessage StateData where
partSubscribers (Window t) = do
2017-12-15 17:12:16 +00:00
subTups <- liftIO $ readTVarIO t
2017-12-15 16:48:12 +00:00
return $ map snd subTups
2017-12-23 23:39:52 +00:00
partSubscribe (Window t) funct = do
uuid <- genUUID
liftIO $ atomically $ modifyTVar' t ((uuid, funct) :)
return $ MsgId uuid MsgWindowEmptyEvent
2017-12-15 16:48:12 +00:00
2017-12-23 23:39:52 +00:00
partUnSubscribe (Window t) (MsgId uuid _) =
2017-12-15 16:48:12 +00:00
liftIO $ atomically $ modifyTVar' t (filter (\(u, _) -> u /= uuid))
instance SDLSubsystem Window StateData where
consumeSDLEvents = consumeSDLWindowEvents
instance Participant Mouse MouseMessage StateData where
partSubscribers (Mouse t) = do
2017-12-15 17:12:16 +00:00
subTups <- liftIO $ readTVarIO t
2017-12-15 16:48:12 +00:00
return $ map snd subTups
2017-12-23 23:39:52 +00:00
partSubscribe (Mouse t) funct = do
uuid <- genUUID
liftIO $ atomically $ modifyTVar' t ((uuid, funct) :)
return $ MsgId uuid MsgMouseEmptyEvent
2017-12-15 16:48:12 +00:00
2017-12-23 23:39:52 +00:00
partUnSubscribe (Mouse t) (MsgId uuid _) =
2017-12-15 16:48:12 +00:00
liftIO $ atomically $ modifyTVar' t (filter (\(u, _) -> u /= uuid))
instance SDLSubsystem Mouse StateData where
consumeSDLEvents = consumeSDLMouseEvents
instance Participant Keyboard KeyboardMessage StateData where
partSubscribers (Keyboard t) = do
2017-12-15 17:12:16 +00:00
subTups <- liftIO $ readTVarIO t
2017-12-15 16:48:12 +00:00
return $ map snd subTups
2017-12-23 23:39:52 +00:00
partSubscribe (Keyboard t) funct = do
uuid <- genUUID
liftIO $ atomically $ modifyTVar' t ((uuid, funct) :)
return $ MsgId uuid MsgKeyboardEmptyEvent
2017-12-15 16:48:12 +00:00
2017-12-23 23:39:52 +00:00
partUnSubscribe (Keyboard t) (MsgId uuid _) =
2017-12-15 16:48:12 +00:00
liftIO $ atomically $ modifyTVar' t (filter (\(u, _) -> u /= uuid))
instance SDLSubsystem Keyboard StateData where
consumeSDLEvents = consumeSDLKeyboardEvents
2016-05-29 16:01:23 +00:00
main :: IO ()
2016-11-04 15:06:16 +00:00
main = do
2017-12-13 14:19:53 +00:00
logIO Debug "Starting"
let conf = AffectionConfig
{ initComponents = All
, windowTitle = "affection: example00"
, windowConfig = SDL.defaultWindow
2017-12-15 16:48:12 +00:00
{ SDL.windowOpenGL = Just SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 3
}
}
2017-12-13 14:19:53 +00:00
, initScreenMode = SDL.Windowed
, canvasSize = Nothing
, loadState = load
, preLoop = pre
, eventLoop = handle
, updateLoop = update
, drawLoop = draw
, cleanUp = clean
}
2016-11-04 15:06:16 +00:00
withAffection conf
2017-12-13 14:19:53 +00:00
load :: IO StateData
2017-03-23 03:27:57 +00:00
load = do
2017-12-13 14:19:53 +00:00
empty1 <- newTVarIO [] -- ([] :: [(UUID, WindowMessage -> Affection StateData ())])
empty2 <- newTVarIO [] -- ([] :: [(UUID, MouseMessage -> Affection StateData ())])
empty3 <- newTVarIO [] -- ([] :: [(UUID, KeyboardMessage -> Affection StateData ())])
return $ StateData $ Subsystems
2017-12-15 16:48:12 +00:00
(Window empty1)
(Mouse empty2)
(Keyboard empty3)
2017-12-13 14:19:53 +00:00
pre :: Affection StateData ()
pre = do
sd <- getAffection
_ <- partSubscribe (subKeyboard $ sdSubs sd) exitOnQ
2017-12-16 10:57:58 +00:00
_ <- partSubscribe (subWindow $ sdSubs sd) exitOnWindowClose
2017-12-13 14:19:53 +00:00
return ()
2016-11-04 15:06:16 +00:00
2017-12-13 14:19:53 +00:00
exitOnQ :: KeyboardMessage -> Affection StateData ()
exitOnQ (MsgKeyboardEvent _ _ _ _ sym) =
case SDL.keysymKeycode sym of
2017-12-15 16:58:55 +00:00
SDL.KeycodeQ -> do
liftIO $ logIO Debug "Yo dog I heard..."
quit
_ -> return ()
2016-11-04 15:06:16 +00:00
2017-12-16 10:57:58 +00:00
exitOnWindowClose :: WindowMessage -> Affection StateData ()
exitOnWindowClose wm =
case wm of
MsgWindowClose _ _ -> do
liftIO $ logIO Debug "I heard another one..."
quit
_ -> return ()
2017-12-13 14:19:53 +00:00
handle :: [SDL.EventPayload] -> Affection StateData ()
handle es = do
(Subsystems a b c) <- sdSubs <$> getAffection
_ <- consumeSDLEvents a es
_ <- consumeSDLEvents b es
_ <- consumeSDLEvents c es
return ()
2017-12-13 14:19:53 +00:00
update _ = return ()
2017-12-13 14:19:53 +00:00
draw = return ()
2016-11-13 12:46:23 +00:00
clean _ = return ()