Skip to content

Commit

Permalink
#55 add periodic system C example, fix issue in flecs.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jan 12, 2020
1 parent a25ba88 commit d9e6c6d
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 2 deletions.
16 changes: 16 additions & 0 deletions examples/c/35_periodic_system/include/periodic_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PERIODIC_SYSTEM_H
#define PERIODIC_SYSTEM_H

/* This generated file contains includes for project dependencies */
#include "periodic_system/bake_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef PERIODIC_SYSTEM_BAKE_CONFIG_H
#define PERIODIC_SYSTEM_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>

/* Headers of private dependencies */
#ifdef PERIODIC_SYSTEM_IMPL
/* No dependencies */
#endif

/* Convenience macro for exporting symbols */
#ifndef PERIODIC_SYSTEM_STATIC
#if PERIODIC_SYSTEM_IMPL && (defined(_MSC_VER) || defined(__MINGW32__))
#define PERIODIC_SYSTEM_EXPORT __declspec(dllexport)
#elif PERIODIC_SYSTEM_IMPL
#define PERIODIC_SYSTEM_EXPORT __attribute__((__visibility__("default")))
#elif defined _MSC_VER
#define PERIODIC_SYSTEM_EXPORT __declspec(dllimport)
#else
#define PERIODIC_SYSTEM_EXPORT
#endif
#else
#define PERIODIC_SYSTEM_EXPORT
#endif

#endif

12 changes: 12 additions & 0 deletions examples/c/35_periodic_system/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "periodic_system",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"public": false,
"use": [
"flecs"
]
}
}
69 changes: 69 additions & 0 deletions examples/c/35_periodic_system/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <periodic_system.h>

/* Component types */
typedef struct Vector2D {
float x;
float y;
} Vector2D;

typedef Vector2D Position;
typedef Vector2D Velocity;

void Move(ecs_rows_t *rows) {
ECS_COLUMN(rows, Position, p, 1);
ECS_COLUMN(rows, Velocity, v, 2);

for (int i = 0; i < rows->count; i ++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}

void PrintPosition(ecs_rows_t *rows) {
ECS_COLUMN(rows, Position, p, 1);

for (int i = 0; i < rows->count; i ++) {
printf("%s position = {.x = %f, .y = %f}\n",
ecs_get_id(rows->world, rows->entities[i]),
p[i].x, p[i].y);
}
}

int main(int argc, char *argv[]) {
/* Create the world, pass arguments for overriding the number of threads,fps
* or for starting the admin dashboard (see flecs.h for details). */
ecs_world_t *world = ecs_init_w_args(argc, argv);

/* Register components */
ECS_COMPONENT(world, Position);
ECS_COMPONENT(world, Velocity);

/* Define a system called Move that is executed every frame, and subscribes
* for the 'Position' and 'Velocity' components */
ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Velocity);

/* Create a system that prints the position of an entity once per second */
ECS_SYSTEM(world, PrintPosition, EcsOnUpdate, Position);
ecs_set_period(world, PrintPosition, 1.0);

/* Create an entity with Position and Velocity. Creating an entity with the
* ECS_ENTITY macro is an easy way to create entities with multiple
* components. Additionally, entities created with this macro can be looked
* up by their name ('MyEntity'). */
ECS_ENTITY(world, MyEntity, Position, Velocity);

/* Initialize values for the entity */
ecs_set(world, MyEntity, Position, {0, 0});
ecs_set(world, MyEntity, Velocity, {1, 1});

/* Run at 60FPS */
ecs_set_target_fps(world, 60);

printf("Application move_system is running, press CTRL-C to exit...\n");

/* Run systems */
while ( ecs_progress(world, 0));

/* Cleanup */
return ecs_fini(world);
}
16 changes: 14 additions & 2 deletions include/flecs/flecs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ class component : public entity {
component_base<T>::s_type,
component_base<T>::s_name);

entity(world, component_base<T>::s_entity);
m_id = component_base<T>::s_entity;
m_world = world.c_ptr();
}
};

Expand Down Expand Up @@ -1188,7 +1189,8 @@ class system final : public entity {
public:
system(const world& world, const char *name = nullptr)
: m_kind(static_cast<EcsSystemKind>(OnUpdate))
, m_name(name)
, m_name(name)
, m_period(0.0)
, m_on_demand(false)
, m_hidden(false) {
m_world = world.c_ptr();
Expand All @@ -1204,6 +1206,11 @@ class system final : public entity {
return *this;
}

system& period(float period) {
m_period = period;
return *this;
}

system& on_demand() {
m_on_demand = true;
return *this;
Expand Down Expand Up @@ -1304,6 +1311,10 @@ class system final : public entity {

ecs_set_system_context(m_world, e, ctx);

if (m_period) {
ecs_set_period(m_world, e, m_period);
}

m_id = e;

return *this;
Expand Down Expand Up @@ -1334,6 +1345,7 @@ class system final : public entity {
EcsSystemKind m_kind;
const char *m_name;
const char *m_signature = nullptr;
float m_period;
bool m_on_demand;
bool m_hidden;
};
Expand Down

0 comments on commit d9e6c6d

Please sign in to comment.