Avara3D 0.2.0
C++ API reference
Runner.h
1//
2// Runner.h
3// avara3d
4//
5// Created by Morgan Davis on 7/10/26.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_RUNNER_H
10#define AVARA3D_RUNNER_H
11
12#include <chrono>
13#include <cstdint>
14#include <functional>
15
16#include "a3d/SimulationConfig.h"
17#include "a3d/profile/FrameStatsHistory.h"
18#include "a3d/profile/Profiler.h"
19
20#include "a3d/TestAccessFwd.h"
21
22namespace a3d {
23
24 struct FrameStats;
25
26 class Scene;
27
45 class Runner {
46
47 public:
48 // [Public Types]
49
51 enum class State {
52 Idle,
53 Running,
54 Stopped
55 };
56
58 struct UpdateInfo {
59
61 std::uint64_t updateIndex {0};
62
64 double elapsedTime {0.0};
65
71 double deltaTime {0.0};
72 };
73
81 using UpdateCallback = std::function<void(Runner& runner, const UpdateInfo& info)>;
82
83 // [Public Lifecycle Functions]
84
94 explicit Runner(Scene& scene, SimulationConfig config = {});
95
96 Runner(const Runner&) = delete;
97 Runner& operator=(const Runner&) = delete;
98
99 Runner(Runner&&) = delete;
100 Runner& operator=(Runner&&) = delete;
101
102 ~Runner();
103
104 // [Public Member Functions]
105
115 void start();
116
127 bool update();
128
134 void stop();
135
137 bool simulationPaused() const;
138
153 void simulationPaused(bool paused);
154
165
178
181
184
186 double timeStep() const;
187
195 void timeStep(double value);
196
198 std::uint32_t maxCatchUpSteps() const;
199
205 void maxCatchUpSteps(std::uint32_t value);
206
208 double timeScale() const;
209
217 void timeScale(double value);
218
220 double simulationTime() const;
221
223 std::uint64_t simulationStepCount() const;
224
226 State state() const;
227
230
232 const Scene& scene() const;
233
234 // [Internal Member Functions]
235
236 bool simulationClockSuspended() const;
237 void simulationClockSuspended(bool suspended);
238
239 private:
240 // [Private Types]
241
242 using Clock = std::chrono::steady_clock;
243 using TimePoint = Clock::time_point;
244
245 // [Private Member Functions]
246
247 void start(TimePoint now);
248 bool update(TimePoint now);
249
250 void advanceSimulation(const UpdateInfo& info, FrameStats& stats);
251 void executePendingSimulationSteps(FrameStats& stats);
252 void executeSimulationStep();
253
254 bool renderFrame(const UpdateInfo& info, FrameStats& stats);
255
256 // [Private Member Variables]
257
258 Scene& _scene;
259 State _state;
260 SimulationConfig _config;
261 double _timeStep;
262 std::uint32_t _maxCatchUpSteps;
263 double _timeScale;
264 double _simulationTimeAccumulator;
265 double _simulationTime;
266 std::uint64_t _simulationStepCount;
267 double _totalDiscardedSimulationTime;
268 bool _simulationPaused;
269 std::uint64_t _pendingSimulationSteps;
270 bool _skipNextUpdateDelta;
271 bool _simulationClockSuspended;
272 TimePoint _startTime;
273 TimePoint _prevUpdateTime;
274 std::uint64_t _updateCount;
275 UpdateCallback _updateCallback;
276 std::uint64_t _renderedFrameCount;
277 Profiler _profiler;
278 FrameStatsHistory _frameStatsHistory;
279
280 // [Test Access]
281
282 friend class testing::RunnerTestAccess;
283 };
284
285}
286
287#endif // AVARA3D_RUNNER_H
Drives host updates, fixed-step simulation, and rendering for a Scene.
Definition: Runner.h:45
Scene & scene()
Returns the Scene driven by this Runner.
State
Runner lifecycle states.
Definition: Runner.h:51
void timeScale(double value)
Changes the scale applied to elapsed host time for automatic simulation stepping.
double timeStep() const
Returns the fixed simulation step duration in seconds.
void updateCallback(UpdateCallback callback)
Replaces the host update callback; an empty callback disables it.
void start()
Starts the Runner using the current monotonic time.
double timeScale() const
Returns the scale applied to elapsed host time for automatic simulation stepping.
void requestSimulationStep()
Queues one fixed-duration simulation step while paused.
void maxCatchUpSteps(std::uint32_t value)
Changes the maximum number of automatic catch-up steps per host update.
std::uint32_t maxCatchUpSteps() const
Returns the maximum number of automatic catch-up steps permitted per host update.
Runner(Scene &scene, SimulationConfig config={})
Creates an idle Runner for scene.
void resetSimulation()
Resets simulation progression without changing scheduling settings.
std::function< void(Runner &runner, const UpdateInfo &info)> UpdateCallback
Callback invoked near the beginning of each host update.
Definition: Runner.h:81
bool update()
Performs one host update using the current monotonic time.
bool simulationPaused() const
Returns true when automatic simulation stepping is paused.
State state() const
Returns the current Runner lifecycle state.
void simulationPaused(bool paused)
Pauses or resumes automatic simulation stepping.
void stop()
Permanently stops the Runner and clears pending requested steps.
const Scene & scene() const
Returns the Scene driven by this Runner.
std::uint64_t simulationStepCount() const
Returns the total number of completed fixed simulation steps.
UpdateCallback updateCallback() const
Returns the currently installed host update callback.
double simulationTime() const
Returns total simulated time in seconds completed by fixed simulation steps.
void timeStep(double value)
Changes the fixed simulation step duration.
Owns a scene graph and the worlds used to simulate and render it.
Definition: Scene.h:44
Timing information supplied to each host update callback.
Definition: Runner.h:58
std::uint64_t updateIndex
Zero-based Runner update index.
Definition: Runner.h:61
double deltaTime
Monotonic seconds since the previous update's start.
Definition: Runner.h:71
double elapsedTime
Monotonic seconds since start(), sampled at this update's start.
Definition: Runner.h:64
Initial simulation scheduling parameters supplied to a Runner.