Created
April 6, 2017 11:02
-
-
Save ivanperez-keera/f419915d352a1229c2e8b44996ac8d18 to your computer and use it in GitHub Desktop.
Yampa SDL 1.2 Back-end using monadic Signals/Sinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module FRP.Yampa.Backends.SDL where | |
import Data.IORef | |
import FRP.Yampa.Signal as Yampa | |
import Graphics.UI.SDL as SDL | |
-- * Signals | |
-- ** SDL clock as Yampa input (source) | |
data SDLClock = SDLClock (IORef Int) | |
instance Signal SDLClock Double IO where | |
initializeSg = do | |
timeRef <- newIORef (0 :: Int) | |
return (SDLClock timeRef, 0) | |
pollSg (SDLClock timeRef) = do | |
-- Obtain new number of ticks since initialisation | |
ts <- fmap fromIntegral getTicks | |
-- Calculate time delta | |
pt <- readIORef timeRef | |
let dt = ts - pt | |
dtY = fromIntegral dt / 100 | |
-- Update number of tickts | |
writeIORef timeRef ts | |
-- Return time delta as floating point number | |
return (dtY, dtY) | |
data SDLSignal a = SDLSignal | |
{ sdlClock :: SDLClock | |
, sdlValue :: a | |
} | |
instance Source a v IO => Signal (SDLSignal a) v IO where | |
initializeSg = do | |
(clk,_) <- initializeSg | |
so <- initializeSo | |
c <- pollSo so | |
return (SDLSignal clk so, c) | |
-- Sense new input | |
pollSg (SDLSignal clock input) = do | |
(dt,_) <- pollSg clock | |
c' <- pollSo input | |
return (dt, c') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment