Last active
March 19, 2021 07:33
-
-
Save SanderMertens/40bf6cd98dc658443651125e6dee5c28 to your computer and use it in GitHub Desktop.
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
// Draft for low-level macro-less API | |
// | |
// The goal of this draft is to explore a full-featured minimal API that does not rely on macro's. That should make it easier to port | |
// flecs to other languages, and allow for more straightforward integration with other frameworks / engines. This is not meant as a | |
// full replacement for an application-facing API, though should be able to coexist with one. | |
// | |
// Inspiration for naming conventions & declarative design comes from Sokol Gfx | |
// Create world as usual | |
ecs_world_t *world = ecs_init(); | |
// Set world parameters | |
ecs_update_world(world, &(ecs_world_desc){ | |
.target_fps = 60, | |
.thread_count = 4, | |
.trace_level = 3 | |
}); | |
// Register component | |
ecs_entity_t ecs_typeid(Position) = ecs_make_entity(world, &(ecs_entity_desc){ | |
.name = "Position", | |
.components = {ecs_typeid(EcsComponent)}, | |
.values = { | |
&(EcsComponent){sizeof(Position), alignof(Position)} | |
} | |
}); | |
// Register tag, which is equivalent to an empty entity | |
ecs_entity_t Dirty = ecs_make_entity(world, &(ecs_entity_desc){ | |
.name = "Dirty" | |
}); | |
// Create a prefab entity, by adding the Prefab tag | |
ecs_entity_t base = ecs_make_entity(world, &(ecs_entity_desc){ | |
.name = "myPrefab", | |
.components = {EcsPrefab, ecs_typeid(Mass)}, | |
.values = { | |
NULL, | |
&(Mass){10} | |
} | |
}); | |
// Create regular entity & initialize components | |
ecs_entity_t e1 = ecs_make_entity(world, &(ecs_entity_desc){ | |
.name = "e1", | |
.components = {ecs_typeid(Position), ecs_typeid(Velocity), ECS_INSTANCEOF | base} | |
.values = { | |
&(Position){10, 20}, | |
&(Velocity){1, 2}, | |
} | |
}); | |
// Add & set component | |
ecs_update_entity(world, e1, &(ecs_entity_desc){ | |
.add = {ecs_typeid(Mass), Dirty}, | |
.values = { | |
&(Mass){10} | |
} | |
}); | |
// Create system, which is a query + a callback | |
ecs_make_query(world, &(ecs_query_desc){ | |
.name = "Move", | |
.callback = Move, | |
.components = {EcsOnUpdate}, | |
.signature = { | |
{EcsInOut, ecs_typeid(Position)}, | |
{EcsIn, ecs_typeid(Velocity)} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment