Guest User

Untitled

a guest
Dec 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. {-# LANGUAGE Arrows #-}
  2. module Main where
  3.  
  4. import Prelude hiding ((.), id)
  5. import Control.Applicative
  6. import Control.Arrow
  7. import Control.Wire
  8. import Control.Exception
  9. import Control.Monad.Loops
  10. import qualified Control.Monad as M (when)
  11. import Debug.Trace (trace)
  12.  
  13. import Graphics.UI.SDL as SDL
  14.  
  15. type EventWire = EventM IO [SDL.Event]
  16. type SDLWire = WireM IO [SDL.Event]
  17.  
  18. main :: IO ()
  19. main = bracket_ (SDL.init [InitVideo]) SDL.quit $ do
  20. screen <- setVideoMode 800 600 0 [DoubleBuf, HWSurface]
  21. sess <- clockSession
  22. loop sess $ system screen
  23. where
  24. loop s w = do
  25. ev <- unfoldWhileM (/= NoEvent) pollEvent
  26. (v, w', s') <- stepSessionM_ s ev w
  27. M.when v $ loop s' w'
  28.  
  29. system :: Surface -> SDLWire Bool
  30. system screen = proc ev -> do
  31. g <- hold (iterateW negate 1 . keyUp SDLK_SPACE) <|> pure (-1) -< ev
  32. q <- quitEv -< ev
  33.  
  34. execute . periodically (1/50) <|> pure () -< (fillRect screen Nothing $ Pixel 0) >> (fillRect screen (Just $ Rect 0 (300 - g * 100) 800 10) $ Pixel 0xffffffff) >> SDL.flip screen
  35.  
  36. returnA -< not q
  37.  
  38. quitEv :: SDLWire Bool
  39. quitEv = pure True . (when (elem Quit)) <|> pure False
  40.  
  41. keyDown :: SDLKey -> EventWire
  42. keyDown k = when $ not . null . filter f
  43. where
  44. f (KeyDown (Keysym k' _ _)) | k == k' = True
  45. f _ = False
  46.  
  47. keyUp :: SDLKey -> EventWire
  48. keyUp k = when $ not . null . filter f
  49. where
  50. f (KeyUp (Keysym k' _ _)) | k == k' = True
  51. f _ = False
Add Comment
Please, Sign In to add comment