1-- Adapted from the Yampa package.
2-- Displays a square moving in a circle. To move the position drag the mouse.
4-- Requires the SDL package, assuming streamly has already been built, you can
5-- compile it like this:
6-- stack ghc --package SDL circle-mouse.hs
8module Streamly.Examples.CirclingSquare where
11import Graphics.UI.SDL as SDL
15------------------------------------------------------------------------------
17------------------------------------------------------------------------------
25 _ <- SDL.setVideoMode width height 16 [SWSurface]
26 SDL.setCaption "Test" ""
28------------------------------------------------------------------------------
29-- Display a box at a given coordinates
30------------------------------------------------------------------------------
32display :: (Double, Double) -> IO ()
33display (playerX, playerY) = do
34 screen <- getVideoSurface
37 let format = surfaceGetPixelFormat screen
38 bgColor <- mapRGB format 55 60 64
39 _ <- fillRect screen Nothing bgColor
41 -- Paint small red square, at an angle 'angle' with respect to the center
42 foreC <- mapRGB format 212 108 73
46 _ <- fillRect screen (Just (Rect x y side side)) foreC
51------------------------------------------------------------------------------
52-- Wait and update Controller Position if it changes
53------------------------------------------------------------------------------
58updateController :: IORef (Double, Double) -> IO ()
59updateController ref = periodic refreshRate $ do
62 MouseMotion x y _ _ -> do
63 writeIORef ref (fromIntegral x, fromIntegral y)
66------------------------------------------------------------------------------
67-- Periodically refresh the output display
68------------------------------------------------------------------------------
70updateDisplay :: IORef (Double, Double) -> IO ()
71updateDisplay cref = withClock clock refreshRate displaySquare
77 return ((fromIntegral t) * 1000)
81 displaySquare time = do
82 (x, y) <- readIORef cref
83 let t = (fromIntegral time) * speed / 1000000
84 in display (x + cos t * radius, y + sin t * radius)
86circlingSquare :: IO ()
89 cref <- newIORef (0,0)
90 runStreamT $ liftIO (updateController cref) <|> liftIO (updateDisplay cref)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list