This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Cse536 Functional Programming
104/11/23
Lecture #13, Nov. 08, 2004•Today’s Topics
–Simple animations
–Buffered graphics
–Animations in Haskell
–Complex animations
–Lifting primitives to animations
–Behaviors
–Type classes, animations, and Behaviors
–Time translation
•Reading Assignment–Read chapter 13 - A Module of Simple Animations
Cse536 Functional Programming
204/11/23
Animations• An animation is a “moving” graphic.
– Sometimes we say a time dependent graphic, since where it “moves” to is dependent upon time.
• To create the illusion of “movement” we need draw frames with a different picture each frame. – A frame rate of about 30 frames a second is optimal
– less than 15-20 appears to flicker
– greater than 30 gives no apparent improvement
• To draw a frame we need to erase the old frame before drawing the new frame.
• All our drawings have been accumulative (we never erase anything, just draw “over” what’s already there).
• There exist several strategies for frame drawing.
Cse536 Functional Programming
304/11/23
Buffered graphics• Display devices display the information
stored in the video memory.
• Buffered graphics use two sets of memory, instantaneously switching from one memory to the other, so quickly that the flicker effect is unobservable.
H IHI
H I
THERETHERE
This video memory free forwriting while the other is displayed
Cse536 Functional Programming
404/11/23
Haskell interface to buffered graphics
• getWindowTick– getWindowTick :: Window -> IO()
– Every window has an internal timer. getWindowTick “waits” for the next “tick” (since the last call to getWindowTick) before it returns. If the next “tick” has already occurred it returns immediately.
• timeGetTime– timegetTime :: IO Word32
– Returns the current time. This time has no real bearing on anything tangible. It is just a big number, and measures the time in milliseconds. The “difference” between successive calls accurately measures elapsed time.
– Writes the graphic into the “free” video graphic buffer. At the next frame “tick” what’s in the “free” video buffer will be drawn, and the current buffer will become the free buffer.
Usual tick rate = 30times per second
Cse536 Functional Programming
504/11/23
Old interface:
openWindow :: String -> Point -> IO Window
e.g. openWindow “title” (width,height)
Richer interface:
openWindowEx :: String -> Maybe Point
Maybe Point -> (Graphic -> DrawFun) ->
Maybe word32 -> IO Window
openWindowEx “title”
(Just(x,y)) -- upper left corner
(Just(width,height))
drawFun
(Just 30) -- refresh rate
Interface to the richer window interface.
Cse536 Functional Programming
604/11/23
Animations in Haskelltype Animation a = Time -> a
type Time = Float
rubberBall :: Animation Shape
rubberBall t = Ellipse (sin t) (cos t)
animate :: String -> Animation a ->
(a -> IO Graphic) -> IO ()
main1 = animate
"Animation of a Shape"
rubberBall
(return . withColor Blue . shapeToGraphic)
Cse536 Functional Programming
704/11/23
ExampleShape pulses from
this
to this
to this
Cse536 Functional Programming
804/11/23
The animate functionanimate :: String -> Animation a -> (a -> IO Graphic) -> IO ()
animate title anim toGraphic = runGraphics (
do w <- openWindowEx title (Just (0,0)) (Just(xWin,yWin))
drawBufferedGraphic (Just 30)
t0 <- timeGetTime
let loop =
do t <- timeGetTime
let ft =intToFloat (word32ToInt (t-t0)) / 1000
gr <- toGraphic (anim ft)
setGraphic w gr
getWindowTick w
loop
loop)
Cse536 Functional Programming
904/11/23
Complex Animations
revolvingBall :: Animation Region
revolvingBall t
= let ball = Shape (Ellipse 0.2 0.2)
in Translate (sin t, cos t) ball
planets :: Animation Picture
planets t
= let p1 = Region Red (Shape (rubberBall t))
p2 = Region Yellow (revolvingBall t)
in p1 `Over` p2
tellTime :: Animation String
tellTime t = "The time is: " ++ show t
Cse536 Functional Programming
1004/11/23
Telling Timemain2 = animate "Animated Text”
tellTime
(return . text (100,200))
The time changesas time advances
Cse536 Functional Programming
1104/11/23
Revolving CircleregionToGraphic :: Region -> Graphic