Skip to content
Flecs v4.1
systems/target_fps

  • C

    View on GitHub

    1#include <target_fps.h>
    2#include <stdio.h>
    3
    4void PrintDeltaTime(ecs_iter_t *it) {
    5 // Print delta_time. The same value is passed to all systems.
    6 printf("delta_time: %f\n", (double)it->delta_time);
    7}
    8
    9int main(int argc, char *argv[]) {
    10 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    11
    12 // Create system that prints delta_time. This system doesn't query for any
    13 // components which means it won't match any entities, but will still be ran
    14 // once for each call to ecs_progress.
    15 ECS_SYSTEM(ecs, PrintDeltaTime, EcsOnUpdate, 0);
    16
    17 // Set target FPS to 1 frame per second
    18 ecs_set_target_fps(ecs, 1);
    19
    20 // Run 5 frames
    21 for (int i = 0; i < 5; i ++) {
    22 // To make sure the frame doesn't run faster than the specified target
    23 // FPS ecs_progress will insert a sleep if the measured delta_time is
    24 // smaller than 1 / target_fps.
    25 //
    26 // By default ecs_progress uses the sleep function provided by the OS
    27 // which is not always very accurate. If more accuracy is required the
    28 // sleep function of the OS API can be overridden with a custom one.
    29 //
    30 // If a value other than 0 is provided to the delta_time argument of
    31 // ecs_progress, this value will be used to calculate the length of
    32 // the sleep to insert.
    33 ecs_progress(ecs, 0);
    34 }
    35
    36 return ecs_fini(ecs);
    37}
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API void ecs_set_target_fps(ecs_world_t *world, ecs_ftime_t fps)
    Set target frames per second (FPS) for an application.
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:263
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Iterator.
    Definition flecs.h:1183
    float delta_time
    Time elapsed since last frame.
    Definition flecs.h:1228

  • C++

    View on GitHub

    1#include <target_fps.h>
    2#include <iostream>
    3
    4int main(int, char *[]) {
    5 flecs::world ecs;
    6
    7 // Create system that prints delta_time. This system doesn't query for any
    8 // components which means it won't match any entities, but will still be ran
    9 // once for each call to ecs_progress.
    10 ecs.system()
    11 .run([](flecs::iter& it) {
    12 std::cout << "delta_time: " << it.delta_time() << std::endl;
    13 });
    14
    15 // Set target FPS to 1 frame per second
    16 ecs.set_target_fps(1);
    17
    18 // Run 5 frames
    19 for (int i = 0; i < 5; i ++) {
    20 // To make sure the frame doesn't run faster than the specified target
    21 // FPS ecs_progress will insert a sleep if the measured delta_time is
    22 // smaller than 1 / target_fps.
    23 //
    24 // By default ecs_progress uses the sleep function provided by the OS
    25 // which is not always very accurate. If more accuracy is required the
    26 // sleep function of the OS API can be overridden with a custom one.
    27 //
    28 // If a value other than 0 is provided to the delta_time argument of
    29 // ecs_progress, this value will be used to calculate the length of
    30 // the sleep to insert.
    31 ecs.progress();
    32 }
    33}
    void set_target_fps(ecs_ftime_t target_fps) const
    Set the target FPS.
    Definition impl.hpp:37
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    Class for iterating over query results.
    Definition iter.hpp:68
    ecs_ftime_t delta_time() const
    Get the time elapsed since the last frame.
    Definition iter.hpp:132
    system & run(Func &&func)
    Replace the system's run callback.
    Definition impl.hpp:108
    The world.
    Definition world.hpp:129