Last active
December 27, 2015 20:29
-
-
Save funrep/7385446 to your computer and use it in GitHub Desktop.
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 RankNTypes #-} | |
import Reactive.Banana | |
import Reactive.Banana.Frameworks | |
import qualified Graphics.UI.SDL as SDL | |
import qualified Graphics.UI.SDL.Image as Image | |
main = do | |
SDL.init [SDL.InitEverything] | |
SDL.rawSetCaption (Just "foobar") Nothing | |
SDL.setVideoMode 640 480 32 [] | |
img <- Image.load "foo.png" | |
source <- newAddHandler | |
network <- setupNetwork img source | |
actuate network | |
eventLoop source | |
eventLoop :: EventSource (Int, Int) -> IO () | |
eventLoop esMouse = loop | |
where | |
loop = do | |
(SDL.getMouseState >>= \(x, y, _) -> return (x, y)) >>= snd esMouse | |
loop | |
type EventSource a = (AddHandler a, a -> IO ()) | |
setupNetwork :: SDL.Surface -> EventSource (Int, Int) -> IO EventNetwork | |
setupNetwork img esMouse = compile $ do | |
eMouse <- fromAddHandler $ fst esMouse | |
reactimate $ fmap (render img) eMouse | |
render :: SDL.Surface -> (Int, Int) -> IO () | |
render img (x, y) = do | |
surf <- SDL.getVideoSurface | |
SDL.blitSurface img (Just (SDL.Rect (x-25) (y-25) 50 50)) surf Nothing | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment