SHARE
TWEET

flecs + tigr

a guest Oct 18th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <flecs.h>
  2. #include <tigr.h>
  3.  
  4. typedef struct Position {
  5.     float x;
  6.     float y;
  7. } Position;
  8.  
  9. typedef struct RenderData {
  10.     Tigr *src_buffer;
  11.     Tigr *dest_buffer;
  12. } RenderData;
  13.  
  14. typedef int32_t Speed;
  15.  
  16. // Move system
  17. void Move(ecs_rows_t *rows) {
  18.     ECS_COLUMN(rows, Position, p, 1);
  19.     ECS_COLUMN(rows, Speed, s, 2);
  20.  
  21.     for (int i = 0; i < rows->count; i ++) {
  22.         p[i].x += s[i] * rows->delta_time;
  23.         p[i].y += s[i] * rows->delta_time;
  24.     }
  25. }
  26.  
  27. void Render(ecs_rows_t *rows) {
  28.     ECS_COLUMN(rows, RenderData, r, 1);
  29.     ECS_COLUMN(rows, Position, p, 2);
  30.  
  31.     for (int i = 0; i < rows->count; i ++) {
  32.         tigrBlit(r[i].dest_buffer, r[i].src_buffer, p[i].x, p[i].y, 0, 0, 512, 512);
  33.         tigrPrint(r[i].dest_buffer, tfont, 10, 20 + 10 * i, tigrRGB(0xff, 0xff, 0xff), "Entity %i: %f,%f", i, p[i].x, p[i].y);
  34.     }
  35. }
  36.  
  37. int main(int argc, char *argv[]) {
  38.     Tigr *screen = tigrWindow(1024, 576, "Tony", TIGR_AUTO);
  39.     Tigr *tiger = tigrLoadImage("assets/images/tiger.png");
  40.  
  41.     ecs_world_t* world = ecs_init_w_args(argc, argv);
  42.  
  43.     ECS_COMPONENT(world, Position);
  44.     ECS_COMPONENT(world, Speed);
  45.     ECS_COMPONENT(world, RenderData);
  46.  
  47.     //ECS_SYSTEM(world, Move, EcsOnUpdate, Position, Speed);
  48.     ECS_SYSTEM(world, Render, EcsOnUpdate, RenderData, Position);
  49.  
  50.     ECS_ENTITY(world, MyEntity, Position, RenderData);
  51.     ecs_set(world, MyEntity, Position, {256, 50});
  52.     ecs_set(world, MyEntity, RenderData, {tiger, screen});
  53.  
  54.     ecs_set_target_fps(world, 60);
  55.  
  56.     while (!tigrClosed(screen))
  57.     {
  58.         tigrClear(screen, tigrRGB(0x80, 0x90, 0xa0));
  59.         ecs_progress(world, 0);
  60.         tigrPrint(screen, tfont, 10, 10, tigrRGB(0xff, 0xff, 0xff), "FT: %.6f", tigrTime());
  61.         tigrUpdate(screen);
  62.     }
  63.  
  64.     tigrFree(tiger);
  65.     tigrFree(screen);
  66.     return ecs_fini(world);
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top