Skip to content

The Scene Package

Patrick Stephen edited this page Dec 4, 2021 · 2 revisions

To manage scenes and operations between scenes, Oak has the scene subpackage. It defines function types for building scenes, a map type for managing a set of scenes, and transition operations to manipulate the screen from one scene to the next.

Scene Types

A scene.Scene is a struct composed of a scene.Start, scene.Loop, and scene.End, where operations on a scene expect the start to function to be called, then the loop function until it returns to stop calling it, and finally the end function. When AddScene is called to register a scene, none of these functions need to be provided, and reasonable defaults will replace them.

// Start is called when a scene begins, including contextual information like
// what scene came before this one and a direct reference to clean data structures
// for event handling and rendering
Start func(ctx *Context)
// If Loop returns true, the scene will continue
// If Loop returns false, End will be called to determine the next scene
Loop func() (cont bool)
// End is a function returning the next scene and a SceneResult of
// input settings for the next scene.
End func() (nextScene string, result *Result)

The ctx.SceneInput passed into a Start is the same as the NextSceneInput supplied in the previous End's `Result:

// A Result is a set of options for what should be passed into the next
// scene and how the next scene should be transitioned to.
type Result struct {
    NextSceneInput interface{}
    Transition
}

Scene Transitions

A scene.Transition is a function which manipulates the last visible frame of a scene to transition into the next scene:

type Transition func(*image.RGBA, int) bool

At each frame following a scene end, the transition will be passed in the image data of the screen at the end of the last scene and how many frames have passed since the last scene ended. If the transition manipulates this image data, that same manipulated image data will be passed to the next frame of transitioning. Transitions will not stop until they return false, so the next scene will not begin until they return false. While the Transition is ongoing, the next scene's Start will run.

Oak offers a couple of built in Transitions: Fade and Zoom, both of which are rather simple and will operate for a defined number of frames.

Scene Maps

When oak.AddScene is called, this forwards to the default window's w.SceneMap.AddScene, on the scene.Map type.

In addition to AddScene, maps support Get and GetCurrent, both of which are locking calls around an internal map[string]Scene. As Scenes are not used as pointers, modifying these results will not modify the scene map itself.