-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#55 add periodic system C example, fix issue in flecs.hpp
- Loading branch information
1 parent
a25ba88
commit d9e6c6d
Showing
5 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
44 changes: 44 additions & 0 deletions
44
examples/c/35_periodic_system/include/periodic_system/bake_config.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters