# Scenes and time


A video is a list of scenes on a timeline, plus named moments called cues. The renderer asks the video for one frame at a time; the video works out which scenes are on screen at that time and calls them in order.

## The video

```odin
@(export)
effects_video :: proc() -> ^fx.Video {
	return fx.video({
		name    = "Spot",
		scenes  = SCENES[:],
		cues    = CUES[:],
		audio   = "audio/mix.wav",
		overlay = proc(t: f32) {fx.vignette(0.35); fx.grain(0.035)},
	})
}
```

`fx.video` registers the description with the renderer and returns it. It copies the scene, cue and mark lists it is given, registers the bundled fonts, and fills in defaults.

| Field | Meaning | Default |
| --- | --- | --- |
| `name` | Shown in the editor. | |
| `width`, `height` | The design size: the coordinate system your scenes draw in. | 1920 × 1080 |
| `fps` | Frames per second of a render. | 60 |
| `duration` | Length in seconds. | Until the last scene, or the voice-over, ends |
| `background` | The color every frame is cleared to. | Opaque black |
| `scenes` | The timeline. | |
| `cues` | Named times. | |
| `marks` | Times the editor draws on its ruler and snaps to: beats, usually. | |
| `audio` | One soundtrack from time 0, relative to the project. Short for a single `Sound`. | |
| `sounds` | Sounds on the timeline. See [sound](/docs/sound/). | |
| `vo` | A `timeline.json` with voice-over word timings. See [voice-over](/docs/voice-over/). | |
| `overlay` | `proc(t: f32)` drawn above all scenes. | |
| `master` | `proc(t: f32) -> Xf`: a group around all scenes. | |

The design size is not the output size. Scenes always draw on the design stage; `effects render --scale 0.5` renders the same picture at 960×540, and the editor fits it to its window.

## Scenes

```odin
SCENES := [?]fx.Scene {
	{name = "intro", start = 0,   dur = 3.4, draw = intro},
	{name = "dash",  start = 3.0, dur = 6,   draw = dash},
	{name = "outro", vo = "outro", draw = outro}, // start and length from the voice-over
}
```

| Field | Meaning |
| --- | --- |
| `name` | Shown on the editor's timeline, and the key under which a moved scene is saved in `timing.json`. Keep names unique. |
| `start`, `dur` | When it begins and how long it lasts, in seconds. A scene is drawn while `start <= t < start + dur`. |
| `draw` | The procedure that draws it: `proc(s: fx.Shot)`. |
| `vo` | The id of a voice-over scene to take `start` (and `dur`, if you leave it 0) from. |
| `color` | Its color on the editor's timeline. |
| `data` | A pointer handed to `draw` as `s.data`, for scenes built in a loop. |

Scenes are drawn in list order, later ones on top, and they may overlap. Overlap is how transitions work: for half a second both scenes are drawn, and each decides how visible it is.

The table has to be a global (or otherwise outlive the call to `fx.video`): the slice is copied, but the array literal it points into must still exist when it is.

A scene list can also be built in code. A music edit might make one scene per bass hit, all sharing one `draw` and told apart by `data`.

## Shot

A scene's `draw` receives a `Shot`: everything it needs to know about time.

```odin
Shot :: struct {
	t, lt, p:   f32,
	start, dur: f32,
	data:       rawptr,
}
```

| Field | Meaning |
| --- | --- |
| `t` | Video time in seconds. The same number for every scene drawn in this frame. |
| `lt` | Local time: seconds since this scene started. `t - start`. |
| `p` | Progress through this scene, 0 to 1. `lt / dur`. |
| `start`, `dur` | This scene's place on the timeline, after any move made in the editor. |
| `data` | The scene's `data`. |

```odin
scenes_shot :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	rows := [?]struct {
		name:  string,
		v, hi: f32,
	}{{"s.t", s.t, s.dur}, {"s.lt", s.lt, s.dur}, {"s.p", s.p, 1}}
	for r, i in rows {
		y := 300 + f32(i) * 200
		fx.label(r.name, {200, y}, fx.mono(56), fx.theme.nt, {0, 0.5})
		fx.rrect({460, y - 8, 1000, 16}, 8, fx.theme.nb)
		fx.rrect({460, y - 8, 1000 * r.v / r.hi, 16}, 8, fx.theme.na)
		fx.label(fmt.tprintf("%.2f", r.v), {1520, y}, fx.mono(56), fx.theme.nm, {0, 0.5})
	}
}
```

Use `lt` for animation inside a scene, so the scene keeps working when it is moved. Use `t` with `start` when you write a time as an offset from the scene's start (`fx.ep(s.t, s.start, s.start + 0.6)`), and `p` when something should span the whole scene whatever its length.

A few procedures read the video's time themselves instead of being handed it, [`letters`](/docs/type/) for one. Their times are video times: write `s.start + 0.3`, not `0.3`.

## Fade

```odin
fade :: proc(s: Shot, fi: f32 = 0.4, fo: f32 = 0.4) -> f32
```

How visible a scene is: 0 when it starts, 1 after `fi` seconds, back to 0 over the last `fo` seconds. It is [`win`](/docs/anim/#windows-and-staggers) over the scene's span, and it usually goes straight into a group's opacity:

```odin
if fx.group({o = fx.fade(s)}) {
	// the scene
}
```

```odin
scenes_fade :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	fi, fo := knob("fade in", 0.4, 0.05, 1.5), knob("fade out", 0.4, 0.05, 1.5)
	o := fx.fade(s, fi, fo)
	if fx.group({o = o}) {
		fx.caption("fx.fade(s)", {960, 470}, 190, fx.theme.nt, {0.5, 0.5})
	}
	// the value itself, drawn over the scene's span
	fx.rrect({260, 860, 1400, 6}, 3, fx.theme.nb)
	fx.rrect({260, 860, 1400 * s.p, 6}, 3, fx.theme.ns)
	fx.circle({260 + 1400 * s.p, 863 - 180 * o}, 14, fx.theme.na)
}
```

A group that is fully transparent draws nothing and skips its contents, so a faded-out scene costs nothing.

## Cues

A cue is a time with a name. Scenes ask for it instead of hard-coding the number, so the moment can be moved in one place, or dragged in the editor, without touching the scenes that use it.

```odin
CUES := [?]fx.Cue{{"click", 6.2}, {"drop", 11.56}}

dash :: proc(s: fx.Shot) {
	click := fx.cue("click")
	pressed := fx.ep(s.t, click, click + 0.12)
	// …
}
```

`fx.cue(name)` returns the cue's time, or 0 (and a message on stderr) when there is none by that name.

## Timing that lives outside the code

Scenes, cues and sounds dragged in the editor are saved to `timing.json` in the project directory. The file holds only what differs from the code:

```json
{
  "scenes": { "dash": { "start": 3.2, "dur": 5.6 } },
  "cues": { "click": 6.45 },
  "sounds": { "boom": 11.5 }
}
```

The renderer applies it on top of the code's times every time it loads the project. Delete an entry (or select the item in the editor and press Delete) to go back to the time in code. Keep the file in version control: it is part of the edit.

## Master and overlay

Two hooks apply to the whole film.

`master` returns an [`Xf`](/docs/group/) for a group wrapped around every scene, the background included: the film's grade, its glow, its lens, a camera move. It is called once per frame with the video time.

```odin
master :: proc(t: f32) -> fx.Xf {
	return {glow = fx.glow(26, 0.9, 0.45), post = fx.lens(0.05, 5), contrast = 0.08}
}
```

`overlay` draws above everything, outside the master group: vignette, grain, a watermark, letterbox bars. Grain belongs here rather than inside the master, so it is not blurred into the glow.

```odin
scenes_master :: proc(s: fx.Shot) {
	// what Video.master does for a whole film: one group around every scene
	master := fx.Xf{glow = fx.glow(knob("glow", 26, 0, 60), 0.9, 0.45), post = fx.lens(0.05, knob("fringe", 5, 0, 16))}
	if fx.group(master) {
		fx.backdrop(.Night)
		p := fx.ep(s.lt, 0.3, 1.6)
		fx.rrect({360, 520, 1200 * p, 10}, 5, fx.theme.na)
		fx.caption("the film's look", {360, 330}, 150, fx.theme.nt)
	}
	fx.vignette(0.4) // what Video.overlay does: drawn above everything
	fx.grain(0.04)
}
```

## Reference

| Name | What it is |
| --- | --- |
| `video(v)` | Registers a `Video` with the renderer and returns it. |
| `Video` | The description of a film: size, rate, scenes, cues, sounds, hooks. |
| `Scene` | A named span on the timeline with a `draw` procedure. |
| `Shot` | What `draw` is handed: `t`, `lt`, `p`, `start`, `dur`, `data`. |
| `Cue` | A named time. |
| `cue(name)` | A cue's time. |
| `fade(s, fi, fo)` | A scene's visibility, fading at both ends. |
| `video_end(v)`, `scenes_end(v)` | Where the last scene (or the voice-over) ends. |
| `stage()` | The design stage as a `Rect`. |
| `now()` | The time of the frame being drawn. |
