项目作者: librg

项目描述 :
Header-only C & C++ ZPL Entity System module.
高级语言: C
项目地址: git://github.com/librg/zpl-ent.git
创建时间: 2017-08-15T13:40:13Z
项目社区:https://github.com/librg/zpl-ent

开源协议:

下载


ZPL - Entity System module [ABANDONED]

npm version

This module offers zero-cost speed low memory footprint entity allocator and de-allocator.
On top of that, user has the ability to attach components onto entities and iterate over them.

Important note

  • Component pool of type X can be used ONLY BY ONE entity pool.
  • Entity pool can use MULTIPLE component pools of type X.
  • Entity handles SHALL NOT BE mixed-up with DIFFERENT entity pools / component pools.
  • Entity’s components HAVE TO BE detached BEFORE the entity gets destroyed.

Usage

  1. #define ZPL_IMPLEMENTATION
  2. #define ZPLE_IMPLEMENTATION
  3. #include <zpl.h>
  4. #include <zpl_ent.h>
  5. typedef struct {
  6. f32 x,y,z;
  7. } ZPLE_COMP_DECLARE(transform);
  8. void transform_print_cb(zple_id_t handle, transform_t *tran) {
  9. zpl_printf("%f, %f, %f\n", tran->x, tran->y, tran->z);
  10. }
  11. // NOTE(ZaKlaus): Put this EXACTLY to one source file!
  12. ZPLE_COMP_DEFINE(transform);
  13. int main(void) {
  14. // NOTE(ZaKlaus): Initialize our entity pool
  15. zple_pool pool = {0};
  16. zple_init(&pool, zpl_heap_allocator(), 100);
  17. // NOTE(ZaKlaus): Initialize our component pool
  18. // WARN(ZaKlaus): Component pool of type X can be used EXACTLY by ONE entity pool EXCLUSIVELY!!!
  19. transform_pool tranpool = {0};
  20. transform_init(&tranpool, &pool, zpl_heap_allocator());
  21. // NOTE(ZaKlaus): Generate some entities.
  22. for (isize i = 0; i < 15; ++i) {
  23. zple_id_t h = zple_create(&pool);
  24. transform_attach(&tranpool, h, (transform_t){i, i*2, i*i});
  25. }
  26. // NOTE(ZaKlaus): Do foreach on our component pool
  27. zple_foreach((&tranpool), transform, transform_print_cb);
  28. zple_id_t t = zple_create(&pool);
  29. ZPL_ASSERT(t.id == 15); // NOTE(ZaKlaus): This entity should be 16th.
  30. // TODO(ZaKlaus): Add component to entity
  31. transform_t *data = transform_attach(&tranpool, t, (transform_t) { 1, 2, 3 });
  32. // NOTE(ZaKlaus): Check on the data.
  33. transform_t *d = transform_fetch(&tranpool, t);
  34. ZPL_ASSERT(d->x == 1 && d->y == 2 && d->z == 3);
  35. // NOTE(ZaKlaus): Detach component from entity.
  36. // WARN(ZaKlaus): Do this each time when you destroy the entity!
  37. transform_detach(&tranpool, t);
  38. // TODO(ZaKlaus): Remove entity
  39. zple_destroy(&pool, t);
  40. // NOTE(ZaKlaus): Add new entity
  41. t = zple_create(&pool);
  42. ZPL_ASSERT(t.id == 15 && t.generation == 1); // NOTE(ZaKlaus): This should be re-used entity.
  43. // NOTE(ZaKlaus): Release our resources
  44. transform_free(&tranpool);
  45. zple_free(&pool);
  46. return 0;
  47. }