Properly link flecs library
This commit is contained in:
5
engine/libs/flecs/examples/cpp/queries/hierarchy/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/cpp/queries/hierarchy/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef HIERARCHY_H
|
||||
#define HIERARCHY_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "hierarchy/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* 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 HIERARCHY_BAKE_CONFIG_H
|
||||
#define HIERARCHY_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "hierarchy",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include <hierarchy.h>
|
||||
#include <iostream>
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
// Tags for local/world position
|
||||
struct Local { };
|
||||
struct World { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a hierarchy. For an explanation see the entities/hierarchy example
|
||||
flecs::entity sun = ecs.entity("Sun")
|
||||
.add<Position, World>()
|
||||
.set<Position, Local>({1, 1});
|
||||
|
||||
ecs.entity("Mercury")
|
||||
.child_of(sun)
|
||||
.add<Position, World>()
|
||||
.set<Position, Local>({1, 1});
|
||||
|
||||
ecs.entity("Venus")
|
||||
.child_of(sun)
|
||||
.add<Position, World>()
|
||||
.set<Position, Local>({2, 2});
|
||||
|
||||
flecs::entity earth = ecs.entity("Earth")
|
||||
.child_of(sun)
|
||||
.add<Position, World>()
|
||||
.set<Position, Local>({3, 3});
|
||||
|
||||
ecs.entity("Moon")
|
||||
.child_of(earth)
|
||||
.add<Position, World>()
|
||||
.set<Position, Local>({0.1, 0.1});
|
||||
|
||||
// Create a hierarchical query to compute the global position from the
|
||||
// local position and the parent position.
|
||||
flecs::query<const Position, const Position, Position> q =
|
||||
ecs.query_builder<const Position, const Position, Position>()
|
||||
// Modify terms from template to make sure the query selects the
|
||||
// local, world and parent position components.
|
||||
.term_at(1).second<Local>()
|
||||
.term_at(2).second<World>()
|
||||
.term_at(3).second<World>()
|
||||
|
||||
// Extend the 2nd query argument to select it from the parent
|
||||
.term_at(2)
|
||||
// Get from the parent, in breadth-first order (cascade)
|
||||
.parent().cascade()
|
||||
// Make term component optional so we also match the root (sun)
|
||||
.optional()
|
||||
// Finalize the query
|
||||
.build();
|
||||
|
||||
// Do the transform
|
||||
q.iter([](flecs::iter& it,
|
||||
const Position *p, const Position *p_parent, Position *p_out)
|
||||
{
|
||||
for (auto i : it) {
|
||||
p_out[i].x = p[i].x;
|
||||
p_out[i].y = p[i].y;
|
||||
if (p_parent) {
|
||||
p_out[i].x += p_parent->x;
|
||||
p_out[i].y += p_parent->y;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Print world positions
|
||||
ecs.each([](flecs::entity e, flecs::pair<Position, World> p) {
|
||||
std::cout << e.name() << ": {" << p->x << ", " << p->y << "}\n";
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user