Ductane v1.6.0
Contents

Docs / Rendering

Loading screens

LoadingScreen is what stands in front of a world while it loads, so a player is never put into one that is half there. It keeps the state: progress that only goes forward and climbs smoothly however lumpy the reports are, the moment the world is ready, a short hold, a fade, and the moment to give the player their controls. It also has a plain look, a title, a subtitle and a thin bar drawn through the HUD, so a game has a working loading screen before it has a designed one.

Why a game wants one

A world drawn while it streams in shows its ground first, then everything standing on it arriving at once, with the player already in it and able to walk where the walls are not solid yet. A loading screen hides that. Its settle hides one more thing: once the world is whole, the screen stays opaque for a moment while the finished world draws behind it. Those are the frames where every pipeline is first used and every texture first read, the hitch a player would otherwise feel in their first step. So it is worth having even when loading is fast.

Using it

Like the splash, it is an overlay and a clock, not a mode. Advance it from your simulation step, draw it from your HUD, last, and drop it when it is done:

// your game
private var loading: LoadingScreen? = LoadingScreen(title: class="p">"The Harbor", subtitle: class="p">"Find the keeper")

func step(deltaTime: Float) {
    if loading != nil {
        loading?.advance(by: deltaTime)
        if loading?.isFinished == true { loading = nil }
    }
    let playerHasControl = loading?.isLifting ?? true
    // … the rest of the step runs as usual
}

func encodeHUD(into encoder: MTL4RenderCommandEncoder, frameSlot: Int, interpolation: Float) {
    guard let hud else { return }
    hud.begin(viewportSize: viewportSize)
    // … the game's own interface
    loading?.draw(into: hud, viewportSize: viewportSize)   // last, over everything
    hud.draw(into: encoder, frameSlot: frameSlot)
}

Keep the frame loop running behind it, scene and all. The settle only warms anything up if the world is actually drawn while the screen is opaque. What your game holds back until isLifting (the player's controls, a mission's clock, enemies waking) is its own decision. isLifting turns true as the fade begins: before that, the screen is opaque and a press would move someone nobody can see.

Reporting progress

What counts as loaded is your game's to say. Call report(_:) with the share done, 0 to 1, as stages complete, and finish() when the world is whole. A world renderer loading its props in the background says how many of how many it has read:

// your game
world.onLoadingProgress = { [weak self] done, total in
    if total > 0 { self?.loading?.report(Float(done) / Float(total)) }
}
world.loadPropsInBackground { [weak self] failures, retry in
    self?.loading?.finish()
}
if !world.isLoadingProps { loading?.finish() }   // nothing needed reading

Two details in that: the last progress call is (0, 0), which is why it checks total, and loadPropsInBackground(_:) calls nothing back when there was nothing to load, which is why the last line is there.

The bar is honest and calm by construction:

The timing

PropertyDefaultMeaning
climbRate1.5the most the bar climbs a second, in shares of the whole
settle0.35 sheld at full strength once ready and full
fadeOut0.6 sfading away

progress, shownProgress, percent (0 to 100, rounded down, so 100 only when full), isReady, elapsed, sinceFull, opacity, isLifting and isFinished report where it is. Even an instant load is on screen for a moment: the bar takes two thirds of a second to fill from empty, then the settle and the fade. Raise climbRate if that is too long.

The settle is counted from the moment within a step that the bar filled, and the fades are linear ramps, so the picture at a given moment is the same at 30 steps a second as at 240. The tests check that.

The plain look and your own

draw(into:viewportSize:) fills the viewport with background (#0B0D10, near-black so it reads as a surface), puts the title in capitals low on the left with the subtitle under it, and runs a thin bar across the foot with the percentage over its right end, all in foreground. Sizes scale against a 1080-unit-high viewport. Either line can be nil, and all of these are vars you can set after LoadingScreen(title:subtitle:).

The title and subtitle are drawn in the HUD's font, so any character outside ASCII must be in the atlas's additionalCharacters, or it draws nothing. The title is capitalized first, so include the capital forms.

A game with its own art draws its own screen from shownProgress, percent and opacity, and keeps using advance(by:), report(_:) and finish() for the state. There is no skip: it lasts as long as the load.

With the splash

The splash credits the engine at launch and runs on a fixed clock a key can cut short; the loading screen lasts exactly as long as a world takes. A game usually loads its first world during the splash. If both are up at once, draw the loading screen and then the splash, so the credit stays on top while the load carries on behind it.

What it does not do