flix

0.81.0

Sketch

The frame loop.

Surface below it owns the window and the toolkit; this module owns time. It reads the monotonic clock, decides how many logical ticks a frame owes, runs them, and paints -- and that one System.nanoTime is the only Java it touches.

Everything above it -- sketches and the game -- sees Canvas, Input, and pure functions, and never learns that a window exists.

Definitions

def clampHz(hz: Int32): Int32§ Source

A simulation rate the frame loop's arithmetic can survive.

Config is a public record anyone can build, and an hz of nought divides by zero before the window even opens. A negative one runs the clock backwards, and anything above a billion rounds stepNs down to nought and divides by zero again on the frame-timing arithmetic. None of those are worth a crash in a game, so the rate is brought into a range a display can plausibly be asked for.

def defaultConfig(): Config§ Source

A 640x480 sketch at 60 Hz with a fixed seed and no frame limit.

def freshSeed(): Int64 \ IO§ Source

Generates a fresh random seed from system nanoTime or reads FLIX_INVADERS_SEED.

def maxFramesFromEnv(): Option[Int32] \ IO§ Source

Reads a frame limit from the FLIX_SKETCH_MAX_FRAMES environment variable.

This is how the cross-platform smoke test bounds a windowed run so it terminates on its own. Anything unset, unparseable, or non-positive means "run until closed".

def parseSeed(raw: String): Option[Int64]§ Source

The parsing half, split out so it can be tested without an environment.

def seedFromEnv(): Option[Int64] \ IO§ Source

The seed a run starts from: random unless FLIX_INVADERS_SEED names one.

The game was seeded with a constant, so every launch played the identical game and the attract screen replayed the identical demo. Reproducibility is worth keeping, but it belongs to the tests -- which call Game.initial(seed) directly and are unaffected by this -- rather than to the thing a person sits down in front of.

System.nanoTime rather than the wall clock: two launches in the same millisecond would collide on currentTimeMillis, and nothing here needs the value to mean a date. The note elsewhere about nanoTime not being wall-clock is about measuring time; an arbitrary origin is exactly what a seed wants.

The F3 overlay shows the seed of whichever world is on screen, so a game worth keeping can be read off and played again by setting the variable. That is the whole reason the override exists: without it, a bug found once could not be shown to anyone. Note the attract screen's SEED is the demo's, which Session.demoSeed offsets by 977 so it never mirrors a real game -- feeding that one back gives a different game, not the demo you were watching.

def start(cfg: Config, init: Int64 -> s, step: s -> (Snapshot -> s \ Sound), view: s -> (Stats -> Unit \ Canvas)): s \ IO§ Source

Drives a window from a pure state function.

Opens the window, instantiates the sound engine, and runs until the window closes.

init builds the starting world from cfg's seed, step advances it by exactly one logical tick, and view describes what to draw. step may use only Sound and view only Canvas; neither can reach IO. That is the whole point of the boundary, and it is enforced by these types rather than by convention.

A step with nothing to play is lifted with Sound.silent.

The simulation advances on a fixed timestep, so behaviour does not depend on frame rate or machine speed. That is what makes a recorded input script replay exactly.

Returns the state the window closed on, so a caller can persist something the player earned. It is read after Surface.loop returns, by which point no frame is running.

view is also handed the runtime's own performance figures, because the frame loop is the only thing that can measure them and the view is the only thing that can show them. They describe the previous frame -- this one is not finished being drawn -- which is what any such display shows.

def stepNsFor(hz: Int32): Int64§ Source

How long one logical tick covers, in nanoseconds.