The functional animation language FAL was defined in Chapter 15. In this chapter we will explore a method to render FAL programs in a graphics window. The fundamental challenge will be interfacing FAL, arguably the most abstract programming style introduced in this textbook, to the relatively low-level interface to graphics windows provided by the Graphics Library.
module Reactimate where
import Fal
import SOEGraphics hiding (Region, Event)
import qualified SOEGraphics as G (Region, Event)
import Draw (xWin, yWin, intToFloat)
import Win32Misc (timeGetJime)
import Word (word32ToInt)
import Channel
Preliminaries
Analogous to the function animate in Chapter 13, our goal is to define a function, which I will call reactimate, having type:
reactimate :: String → Behavior a → (a → IO Graphic) → IO ()
Before doing this, however, there is an important secondary task to be carried out. We need a mechanism to extract a stream of time-stamped user actions (lifted into the Maybe data type) from the operating system. We want the stream of user actions to be infinite, and return Just ua if there is a user action ua present at the time we ask for it, and Nothing if there is not. The problem is the phrase “at the time we ask for it.” How can we make a stream dependent on the execution time of a program? Surely we will need to go through the IO monad to achieve this dependency, and, in fact, I will use the Channel abstraction discussed in Chapter 16 to do so. For now, however, let's assume that we have a function:
windowUser :: Window → IO (([Maybe UserAction], [Time]), IO ())
which behaves as follows. Executing the command:
((us, ts), addEvents) ← windowUser w
in a particular window environment yields a user action stream us and time stream ts. In addition, it yields an IO action addEvents that causes the operating system to update us with whatever user actions happen to be pending at that time, followed by a Nothing to indicate that there are no more user actions pending.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.