The frame, layers and time
What happens inside one step_frame() and how your code takes part: layers (conjure/engine/layer.h,
conjure/engine/layer_stack.h, conjure/engine/game_layer.h, conjure/engine/imgui_layer.h,
conjure/engine/debug_overlay_layer.h), subsystems (conjure/engine/engine_subsystem.h), time
(conjure/engine/engine_time.h, conjure/engine/fixed_step_clock.h, conjure/engine/engine_phase.h) and the
per-frame statistics and scratch memory (conjure/engine/frame_stats.h, conjure/engine/frame_memory.h).
| Name | The frame, layers and time |
| Module | Engine |
#include |
conjure/engine/debug_overlay_layer.h, conjure/engine/engine_phase.h, conjure/engine/engine_subsystem.h, conjure/engine/engine_time.h, conjure/engine/fixed_step_clock.h, conjure/engine/frame_memory.h, conjure/engine/frame_stats.h, conjure/engine/game_layer.h, conjure/engine/imgui_layer.h, conjure/engine/layer.h, conjure/engine/layer_stack.h |
| Engineering Handbook | Engineering handbook |
Syntax
Usage and complete examples are described in the reference sections below.
At a glance
| You want to... | Use |
|---|---|
| React to window events, draw ImGui, run code once per frame outside the ECS | derive from layer, push it with application::layers().push_layer() |
| Add a module object with systems of its own (a plugin's subsystem) | derive from engine_subsystem, application::add_subsystem() before initialise() |
| Read the frame delta, the fixed delta, the frame index | application::time() (an engine_time), or system_context::frame() inside a system |
| Know whether a phase is a fixed one | is_fixed_phase(phase), FIXED_PHASES, to_string(phase) |
| Profile the last frames | application::stats().latest(), history(), average_frame_milliseconds(n) |
| Allocate scratch memory that dies at the next frame | context.frameMemory->allocate_span<T>(count) |
| Quit when the window closes | nothing: the engine quits with exit_code::OK unless a layer keeps the window open |
| Ask the player before the window closes (unsaved work) | override layer::on_close_request and return true |
Variables and Properties
Types, fields, and state are described in the reference sections below.
The frame in order
Every phase is an ecs::system_phase; the engine calls scheduler.run_phase(phase, frame_info) and the
scheduler flushes command buffers at every stage boundary, so a structural change made in one phase is visible
in the next, never in the same one.
begin_frame (engine_time) -> frame_memory.reset -> pump events (a close: layers.on_close_request), layers.on_event,
input.end_frame
-> main-thread queue -> subsystems.begin_frame, layers.on_frame_begin
-> FRAME_BEGIN
-> [FIXED_UPDATE, PHYSICS_STEP, FIXED_END] x fixed steps this frame (skipped while PAUSED)
-> UPDATE -> layers.on_update -> LATE_UPDATE -> TRANSFORM -> SYNC
-> layers.on_imgui, layers.on_imgui_end -> EXTRACT -> render_frame
-> FRAME_END -> subsystems.end_frame, layers.on_frame_end, layers.apply_pending -> pacer -> stats.commit
While simulation_mode::PAUSED the fixed loop is skipped and UPDATE / LATE_UPDATE run only the systems flagged
RUNS_WHILE_PAUSED; TRANSFORM, SYNC and EXTRACT keep running so an editor still renders edits. STEP_ONCE runs
one frame and returns to PAUSED; application::step_fixed(n) runs exactly n fixed steps in the next frame, even
while PAUSED, and cancel_fixed_steps() drops such a request before that frame consumes it (the editor's
play.stop does, so a step never runs over the restored editing scene).
Layers
A layer is a named object with virtual hooks: on_attach (startup step 19; a failure fails initialise()
with LAYER_ATTACH_FAILED), on_detach, on_frame_begin, on_update (after UPDATE, also while paused),
on_imgui and on_imgui_end (editor builds, with the ImGui frame open), on_frame_end, on_event, which
returns true to consume a window or input event before it reaches the input state and the layers below, and
on_close_request, which returns true to keep the window open (see Closing the window).
layer_stack keeps ordinary layers below overlays: push_layer and push_overlay (a request made inside a
frame is applied at the frame boundary), pop(layer), dispatch_event (top-down until consumed),
dispatch_close_request (top-down until a layer keeps the window open), layers(), overlay_count(),
pending_count(). The three built-in layers:
| Layer | What it does |
|---|---|
game_layer |
sees the window-close event and calls request_quit(OK) without consuming it; close_requests() counts them |
imgui_layer |
editor builds only: owns the one ImGuiContext, begins the frame in on_frame_begin, ends it in on_imgui_end and hands the draw data to the renderer; context(), is_frame_open(), stats(), clipboard helpers |
debug_overlay_layer |
outside Distribution: the frame-time overlay; text(), set_visible(), set_log_interval_frames(), format_sample(context) |
#include "conjure/engine/layer.h"
class fps_layer final : public conjure::engine::layer {
public:
fps_layer() : layer("fps") {}
public:
void on_frame_end(conjure::engine::engine_context &context) override {
if (context.time->frameIndex % 60 == 0) {
const conjure::engine::frame_sample &last = context.stats->latest();
CONJURE_LOG_INFO(LOG_GAME, "{:.2f} ms/frame", last.frameMilliseconds);
}
}
};
app.layers().push_layer(std::make_unique<fps_layer>()); // before initialise(), or at any frame boundary
Warning
Layer hooks run on the main thread between phases. Mutating the world from on_update is allowed (no system
is running), mutating it from a job is not.
Closing the window
When the window is asked to close, the engine asks the layers first, top-down, during the event pump of the frame:
- On a
close_event(the title-bar button, Alt+F4), before any layer'son_eventsees that event. - On
window::should_close()at the end of the pump when noclose_eventcame, which is how awindow::request_close()from code is noticed.
on_close_request returns false by default, and the application then quits with exit_code::OK. A layer that
returns true keeps the window open. The engine withdraws the window's close request (window::cancel_close()),
requests no quit, and the close_event reaches no layer's on_event and not the input state. The layers below
the one that kept the window open are not asked. The layer then asks the player and quits later with
context.app->request_quit(), or not at all. The editor does this while its scene has unsaved changes.
#include "conjure/engine/application.h"
#include "conjure/engine/layer.h"
// Keeps the window open while there is unsaved work, so the game can ask first.
class save_prompt_layer final : public conjure::engine::layer {
private:
bool mAsking = false;
public:
bool unsavedWork = false;
public:
save_prompt_layer() :
layer("save_prompt") {}
public:
[[nodiscard]] bool on_close_request(conjure::engine::engine_context &context) override {
CONJURE_UNUSED(context);
if (!unsavedWork) {
return false; // nothing to ask: the application quits with exit_code::OK
}
mAsking = true;
return true; // the window stays open
}
// True while the game should show its prompt; the prompt's Quit button calls context.app->request_quit().
[[nodiscard]] bool is_asking() const noexcept {
return mAsking;
}
};
Warning
A layer that returns true owns the quit. If it never calls application::request_quit, the window cannot be
closed. Return false once there is nothing left to ask about.
Subsystems
engine_subsystem is the adapter interface the engine uses for its own modules and offers to plugins and
executables: name(), startup(context) (create the module object, publish its pointer), post_startup(context)
(after every startup), register_systems(scheduler, context) (before the scheduler is finalised),
begin_frame, end_frame and shutdown(context), which must be safe after a failed or skipped startup.
Executables add subsystems with application::add_subsystem() before initialise(); a module entry adds them
through the application service during startup. The bindings module's scripting_bindings_subsystem is the
first user (📘 API: Scripting from C#).
Time
engine_time is written once per frame and read everywhere: time and unscaledTime (seconds since the first
frame), deltaTime and unscaledDeltaTime (clamped to maxFrameDeltaSeconds), fixedDeltaTime, fixedTime,
timeScale, interpolationAlpha (where the frame sits between the last two fixed steps, valid from UPDATE on),
frameIndex, fixedStepIndex, fixedStepsThisFrame, inFixedStep and droppedFixedTime (the spiral-of-death
guard fired). The first frame of an application uses the fixed delta whatever the clock says, so every run starts
the same way. make_frame_info(time) builds the ecs::frame_info a system sees.
fixed_step_clock is the accumulator behind the fixed loop: advance(scaledDelta) returns how many steps to
run and the new alpha, caps the steps at maxStepsPerFrame (dropping the remainder and flagging it),
set_fixed_delta, reset. engine_phase.h adds to_string(phase), is_fixed_phase(phase), phase_index(phase)
and the FIXED_PHASES span.
Note
With time_settings::forcedFrameDeltaSeconds > 0 (the headless default, 1/60) the wall clock is never read,
so a headless run is repeatable frame for frame; jobs.workerCount = 0 makes it repeatable across machines too.
Frame statistics and scratch memory
frame_stats is a ring of 240 frame_samples committed at the end of each frame: frameMilliseconds,
phaseMilliseconds[phase], submitMilliseconds, pacerSleepMilliseconds, fixedSteps, jobsExecuted,
frameMemoryHighWater, droppedFixedTime. latest(), sample_at(age), history(),
average_frame_milliseconds(count), total_committed() and total_frames_with_dropped_time() (the number of
frames in which the spiral-of-death guard dropped fixed time, one per frame) read it; the editor's profiler panel
and the C# Diagnostics.Stats show the same numbers. frameMilliseconds includes the pacer sleep: the debug
overlay prints cpu as frameMilliseconds - pacerSleepMilliseconds next to the whole frame time.
frame_memory is per-frame scratch over the core frame allocator: allocate(bytes, alignment),
allocate_span<T>(count), construct<T>(args...); everything is released by reset() at the start of the next
frame. Jobs of the scheduler may allocate from it; an overflow falls back to the heap and is counted
(stats().overflowCount), never fatal.
Caution
A pointer into frame memory is valid until the next reset(), which the engine performs at step 2 of the next
frame. Keeping one across frames is a use-after-free.
Functions
| Signature (short) | Purpose |
|---|---|
class layer { virtual on_attach / on_detach / on_frame_begin / on_update / on_imgui / on_imgui_end / on_frame_end / on_event / on_close_request } |
the layer hooks |
bool layer::on_close_request(engine_context &) |
true keeps the window open when it is asked to close; the default false quits |
layer_stack::push_layer(unique_ptr<layer>) / push_overlay / pop(layer &) / dispatch_event / dispatch_close_request |
the stack |
class engine_subsystem { name, startup, post_startup, register_systems, begin_frame, end_frame, shutdown } |
the adapter interface |
struct engine_time / make_frame_info(const engine_time &) |
the frame clock |
fixed_step_result fixed_step_clock::advance(f32) |
steps and alpha for this frame |
bool is_fixed_phase(system_phase) / std::string_view to_string(system_phase) |
phase helpers |
const frame_sample &frame_stats::latest() / span<const frame_sample> history() |
profiling |
void *frame_memory::allocate(usize, usize) / span<T> allocate_span<T>(usize) / T *construct<T>(...) |
scratch memory |
See also:
- 📘 API: Running the engine, Queries, views and systems, Jobs and threading
- 🛠 Handbook: Engine module, Data-oriented design