Skip to content
Flecs v4.1
app.h
Go to the documentation of this file.
1/**
2 * @file addons/app.h
3 * @brief App addon.
4 *
5 * The app addon is a wrapper around the application's main loop. Its main
6 * purpose is to provide a hook to modules that need to take control of the
7 * main loop, as is for example the case with native applications that use
8 * Emscripten with WebGL.
9 */
10
11#ifdef FLECS_APP
12
13#ifndef FLECS_PIPELINE
14#define FLECS_PIPELINE
15#endif
16
17#ifndef FLECS_APP_H
18#define FLECS_APP_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/**
25 * @defgroup c_addons_app App
26 * @ingroup c_addons
27 * Optional addon for running the main application loop.
28 *
29 * @{
30 */
31
32/** Callback type for init action. */
34 ecs_world_t *world);
35
36/** Used with ecs_app_run(). */
37typedef struct ecs_app_desc_t {
38 ecs_ftime_t target_fps; /**< Target FPS. */
39 ecs_ftime_t delta_time; /**< Frame time increment (0 for measured values). */
40 int32_t threads; /**< Number of threads. */
41 int32_t frames; /**< Number of frames to run (0 for infinite). */
42 bool enable_rest; /**< Enables ECS access over HTTP, necessary for the explorer. */
43 bool enable_stats; /**< Periodically collects statistics. */
44 uint16_t port; /**< HTTP port used by REST API. */
45
46 ecs_app_init_action_t init; /**< If set, the function is run before starting the
47 * main loop. */
48
49 void *ctx; /**< Reserved for custom run and frame actions. */
51
52/** Callback type for run action. */
54 ecs_world_t *world,
55 ecs_app_desc_t *desc);
56
57/** Callback type for frame action. */
59 ecs_world_t *world,
60 const ecs_app_desc_t *desc);
61
62/** Run application.
63 * This will run the application with the parameters specified in desc. After
64 * the application quits (ecs_quit() is called), the world will be cleaned up.
65 *
66 * If a custom run action is set, it will be invoked by this operation. The
67 * default run action calls the frame action in a loop until it returns a
68 * non-zero value.
69 *
70 * @param world The world.
71 * @param desc Application parameters.
72 * @return Zero if success, non-zero if failed.
73 */
74FLECS_API
76 ecs_world_t *world,
77 ecs_app_desc_t *desc);
78
79/** Default frame callback.
80 * This operation will run a single frame. By default, this operation will invoke
81 * ecs_progress() directly, unless a custom frame action is set.
82 *
83 * @param world The world.
84 * @param desc The desc struct passed to ecs_app_run().
85 * @return Value returned by ecs_progress().
86 */
87FLECS_API
89 ecs_world_t *world,
90 const ecs_app_desc_t *desc);
91
92/** Set custom run action.
93 * See ecs_app_run().
94 *
95 * @param callback The run action.
96 * @return Zero if success, non-zero if failed.
97 */
98FLECS_API
100 ecs_app_run_action_t callback);
101
102/** Set custom frame action.
103 * See ecs_app_run_frame().
104 *
105 * @param callback The frame action.
106 * @return Zero if success, non-zero if failed.
107 */
108FLECS_API
110 ecs_app_frame_action_t callback);
111
112/** @} */
113
114#ifdef __cplusplus
115}
116#endif
117
118#endif
119
120#endif // FLECS_APP
int(*) ecs_app_frame_action_t(ecs_world_t *world, const ecs_app_desc_t *desc)
Callback type for frame action.
Definition app.h:58
int(*) ecs_app_init_action_t(ecs_world_t *world)
Callback type for init action.
Definition app.h:33
FLECS_API int ecs_app_run(ecs_world_t *world, ecs_app_desc_t *desc)
Run application.
int(*) ecs_app_run_action_t(ecs_world_t *world, ecs_app_desc_t *desc)
Callback type for run action.
Definition app.h:53
FLECS_API int ecs_app_run_frame(ecs_world_t *world, const ecs_app_desc_t *desc)
Default frame callback.
FLECS_API int ecs_app_set_run_action(ecs_app_run_action_t callback)
Set custom run action.
FLECS_API int ecs_app_set_frame_action(ecs_app_frame_action_t callback)
Set custom frame action.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:438
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
Used with ecs_app_run().
Definition app.h:37
uint16_t port
HTTP port used by REST API.
Definition app.h:44
bool enable_stats
Periodically collects statistics.
Definition app.h:43
ecs_ftime_t target_fps
Target FPS.
Definition app.h:38
ecs_ftime_t delta_time
Frame time increment (0 for measured values).
Definition app.h:39
ecs_app_init_action_t init
If set, the function is run before starting the main loop.
Definition app.h:46
int32_t frames
Number of frames to run (0 for infinite).
Definition app.h:41
void * ctx
Reserved for custom run and frame actions.
Definition app.h:49
bool enable_rest
Enables ECS access over HTTP, necessary for the explorer.
Definition app.h:42
int32_t threads
Number of threads.
Definition app.h:40