Skip to content

Instantly share code, notes, and snippets.

@ivanperez-keera
Created April 6, 2017 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanperez-keera/f419915d352a1229c2e8b44996ac8d18 to your computer and use it in GitHub Desktop.
Save ivanperez-keera/f419915d352a1229c2e8b44996ac8d18 to your computer and use it in GitHub Desktop.
Yampa SDL 1.2 Back-end using monadic Signals/Sinks
{-# 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