Skip to content
Flecs v4.1
frame.h
Go to the documentation of this file.
1/**
2 * @file addons/frame.h
3 * @brief Frame management.
4 */
5
6#ifdef FLECS_FRAME
7
8#if !defined(FLECS_OS_API_IMPL) && !defined(FLECS_NO_OS_API_IMPL)
9#define FLECS_OS_API_IMPL
10#endif
11
12#ifndef FLECS_FRAME_H
13#define FLECS_FRAME_H
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/**
20 * @defgroup c_addons_frame Frame
21 * @ingroup c_addons
22 * Frame management.
23 *
24 * @{
25 */
26
27/** Begin frame.
28 * When an application does not use ecs_progress() to control the main loop, it
29 * can still use Flecs features such as FPS limiting and time measurements. This
30 * operation needs to be invoked whenever a new frame is about to get processed.
31 *
32 * Calls to ecs_frame_begin() must always be followed by ecs_frame_end().
33 *
34 * The function accepts a delta_time parameter, which will get passed to
35 * systems. This value is also used to compute the amount of time the function
36 * needs to sleep to ensure it does not exceed the target_fps, when it is set.
37 * When 0 is provided for delta_time, the time will be measured.
38 *
39 * This function should only be run from the main thread.
40 *
41 * @param world The world.
42 * @param delta_time Time elapsed since the last frame.
43 * @return The provided delta_time, or measured time if 0 was provided.
44 */
45FLECS_API
47 ecs_world_t *world,
48 ecs_ftime_t delta_time);
49
50/** End frame.
51 * This operation must be called at the end of the frame, and always after
52 * ecs_frame_begin().
53 *
54 * @param world The world.
55 */
56FLECS_API
58 ecs_world_t *world);
59
60/** Register an action to be executed once after the frame.
61 * Post frame actions are typically used for calling operations that cannot be
62 * invoked during iteration, such as changing the number of threads.
63 *
64 * @param world The world.
65 * @param action The function to execute.
66 * @param ctx Userdata to pass to the function.
67 */
68FLECS_API
70 ecs_world_t *world,
71 ecs_fini_action_t action,
72 void *ctx);
73
74/** Signal exit.
75 * This operation signals that the application should quit. It will cause
76 * ecs_progress() to return false.
77 *
78 * @param world The world to quit.
79 */
80FLECS_API
82 ecs_world_t *world);
83
84/** Return whether a quit has been requested.
85 *
86 * @param world The world.
87 * @return Whether a quit has been requested.
88 * @see ecs_quit()
89 */
90FLECS_API
92 const ecs_world_t *world);
93
94/** Measure frame time.
95 * Frame time measurements measure the total time passed in a single frame, and
96 * how much of that time was spent on systems and on merging.
97 *
98 * Frame time measurements add a small constant-time overhead to an application.
99 * When an application sets a target FPS, frame time measurements are enabled by
100 * default.
101 *
102 * @param world The world.
103 * @param enable Whether to enable or disable frame time measuring.
104 */
105FLECS_API
107 ecs_world_t *world,
108 bool enable);
109
110/** Measure system time.
111 * System time measurements measure the time spent in each system.
112 *
113 * System time measurements add overhead to every system invocation and
114 * therefore have a small but measurable impact on application performance.
115 * System time measurements must be enabled before obtaining system statistics.
116 *
117 * @param world The world.
118 * @param enable Whether to enable or disable system time measuring.
119 */
120FLECS_API
122 ecs_world_t *world,
123 bool enable);
124
125/** Set target frames per second (FPS) for an application.
126 * Setting the target FPS ensures that ecs_progress() is not invoked faster than
127 * the specified FPS. When enabled, ecs_progress() tracks the time passed since
128 * the last invocation, and sleeps the remaining time of the frame (if any).
129 *
130 * This feature ensures systems are run at a consistent interval, as well as
131 * conserving CPU time by not running systems more often than required.
132 *
133 * Note that ecs_progress() only sleeps if there is time left in the frame. Both
134 * time spent in Flecs and time spent outside of Flecs are taken into account.
135 *
136 * @param world The world.
137 * @param fps The target FPS.
138 */
139FLECS_API
141 ecs_world_t *world,
142 ecs_ftime_t fps);
143
144/** Set time scale.
145 * Increase or decrease simulation speed by the provided multiplier.
146 *
147 * @param world The world.
148 * @param scale The scale to apply (default = 1).
149 */
150FLECS_API
152 ecs_world_t *world,
153 ecs_ftime_t scale);
154
155/** Reset world clock.
156 * Reset the clock that keeps track of the total time passed in the simulation.
157 *
158 * @param world The world.
159 */
160FLECS_API
162 ecs_world_t *world);
163
164/** @} */
165
166#ifdef __cplusplus
167}
168#endif
169
170#endif
171
172#endif // FLECS_FRAME
FLECS_API void ecs_measure_frame_time(ecs_world_t *world, bool enable)
Measure frame time.
FLECS_API void ecs_set_time_scale(ecs_world_t *world, ecs_ftime_t scale)
Set time scale.
FLECS_API ecs_ftime_t ecs_frame_begin(ecs_world_t *world, ecs_ftime_t delta_time)
Begin frame.
FLECS_API void ecs_frame_end(ecs_world_t *world)
End frame.
FLECS_API void ecs_quit(ecs_world_t *world)
Signal exit.
FLECS_API void ecs_run_post_frame(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register an action to be executed once after the frame.
FLECS_API void ecs_reset_clock(ecs_world_t *world)
Reset world clock.
FLECS_API bool ecs_should_quit(const ecs_world_t *world)
Return whether a quit has been requested.
FLECS_API void ecs_measure_system_time(ecs_world_t *world, bool enable)
Measure system time.
FLECS_API void ecs_set_target_fps(ecs_world_t *world, ecs_ftime_t fps)
Set target frames per second (FPS) for an application.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
void(*) ecs_fini_action_t(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:655
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59