Steve's Roc Apps roc-apps on GitHub

Safari Ride Chapters

Chapter 2

One page

Safari Ride works like a flip book. Each frame is one page, and the ride shows a new page thirty times a second. Here's one page, held still. It's the page at step 1,529, with the cat out on the road:

Step 1,529 of the ride, held still.

This chapter makes a first pass over that page. It names the page's parts in the code. Later chapters show how each part is made.

In the code, a page starts as one value of the FrameView type:

FrameView.roc, in FrameView
# ...
FrameView := {
	## The sky, the sun and the grass, which everything else is painted over.
	backdrop : FrameView.Backdrop,

	## The mountains, darkened with the sky.
	mountain_shapes : List(Shapes.Shape),

	## The road itself and its ponds.
	ground_patch_shapes : List(Shapes.Shape),

	## The things on and beside the road.
	scene : Scene,

	## The camera's roll. The camera turns one way, so the whole frame, backdrop
	## and shapes, turns the other way by the same angle, about the screen's
	## centre.
	roll : Angle.Lean,
}.{
# ...

You can read it as English for now. A # ... line marks a gap in the quoted code. Four of the fields are parts of the picture you can see: the backdrop of sky and grass, with the sun when it's in view; the mountains; the road and its ponds; and the scene, which holds the things in view on and beside the road. The fifth field is the camera's roll: how far the whole page leans when the bike leans. The roll comes from the rider, so the second half of the book explains it.

One function gives back the FrameView for any moment of the ride:

FrameView.roc
frame_view_at : Route, RideInstant -> FrameView

That line is a type annotation: the function's name, a colon, and its type. frame_view_at takes a Route, the whole road, and a RideInstant, one moment of the ride, such as the one at step 1,529. After the arrow comes what it gives back, the moment's FrameView. One more function turns the FrameView, backdrop and all, into a single list of shapes, leaned by the roll:

FrameView.roc
leaned_shapes : FrameView -> List(Shapes.Shape)

Roc code runs on a platform, and Safari Ride's platform paints that list on the screen. So each page starts from one moment of the ride and ends as one list of shapes.

In the first half of the book, we hold this page still and build it part by part, starting with the things beside the road. In the second half, the pages turn. The chapters ahead teach Roc slowly, one idea at a time, so the syntax here can wait for them. In the next chapter, we break the ride into the pieces we build.