forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SanderMertens#1439 Add query/system callback examples for each and ru…
…n variants
- Loading branch information
1 parent
b55b7c4
commit 5ea2aa1
Showing
42 changed files
with
824 additions
and
22 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...ems/custom_runner/include/custom_runner.h → ...ies/each_callback/include/each_callback.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
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
6 changes: 3 additions & 3 deletions
6
...es/cpp/systems/custom_runner/project.json → ...es/cpp/queries/each_callback/project.json
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"id": "custom_runner", | ||
"id": "each_callback", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
"flecs" | ||
], | ||
"public": false, | ||
"language": "c++" | ||
"language": "c++", | ||
"public": false | ||
} | ||
} |
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,45 @@ | ||
#include <each_callback.h> | ||
#include <iostream> | ||
|
||
// This example shows how to write a query with the each callback, which | ||
// provides the easiest API for iterating over matched components. | ||
|
||
struct Position { | ||
double x, y; | ||
}; | ||
|
||
struct Velocity { | ||
double x, y; | ||
}; | ||
|
||
int main(int, char *[]) { | ||
flecs::world ecs; | ||
|
||
// Create a query for Position, Velocity. | ||
flecs::query<Position, const Velocity> q = | ||
ecs.query<Position, const Velocity>(); | ||
|
||
// Create a few test entities for a Position, Velocity query | ||
ecs.entity("e1") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({1, 2}); | ||
|
||
ecs.entity("e2") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({3, 4}); | ||
|
||
// This entity will not match as it does not have Position, Velocity | ||
ecs.entity("e3") | ||
.set<Position>({10, 20}); | ||
|
||
// Arguments passed to each match query type | ||
q.each([](Position& p, const Velocity& v) { | ||
p.x += v.x; | ||
p.y += v.y; | ||
std::cout << "{" << p.x << ", " << p.y << "}\n"; | ||
}); | ||
|
||
// Output | ||
// {11, 22} | ||
// {13, 24} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/cpp/queries/each_w_entity_callback/include/each_w_entity_callback.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,16 @@ | ||
#ifndef EACH_W_ENTITY_CALLBACK_H | ||
#define EACH_W_ENTITY_CALLBACK_H | ||
|
||
/* This generated file contains includes for project dependencies */ | ||
#include "each_w_entity_callback/bake_config.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
|
24 changes: 24 additions & 0 deletions
24
examples/cpp/queries/each_w_entity_callback/include/each_w_entity_callback/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,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 EACH_W_ENTITY_CALLBACK_BAKE_CONFIG_H | ||
#define EACH_W_ENTITY_CALLBACK_BAKE_CONFIG_H | ||
|
||
/* Headers of public dependencies */ | ||
#include <flecs.h> | ||
|
||
#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,11 @@ | ||
{ | ||
"id": "each_w_entity_callback", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
"flecs" | ||
], | ||
"language": "c++", | ||
"public": false | ||
} | ||
} |
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,45 @@ | ||
#include <each_w_entity_callback.h> | ||
#include <iostream> | ||
|
||
// This example is the same as the each example, but in addition also shows how | ||
// to get access to the matched entity. | ||
|
||
struct Position { | ||
double x, y; | ||
}; | ||
|
||
struct Velocity { | ||
double x, y; | ||
}; | ||
|
||
int main(int, char *[]) { | ||
flecs::world ecs; | ||
|
||
// Create a query for Position, Velocity. | ||
flecs::query<Position, const Velocity> q = | ||
ecs.query<Position, const Velocity>(); | ||
|
||
// Create a few test entities for a Position, Velocity query | ||
ecs.entity("e1") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({1, 2}); | ||
|
||
ecs.entity("e2") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({3, 4}); | ||
|
||
// This entity will not match as it does not have Position, Velocity | ||
ecs.entity("e3") | ||
.set<Position>({10, 20}); | ||
|
||
// Arguments passed to each match components passed to system | ||
q.each([](flecs::entity e, Position& p, const Velocity& v) { | ||
p.x += v.x; | ||
p.y += v.y; | ||
std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n"; | ||
}); | ||
|
||
// Output | ||
// e1: {11, 22} | ||
// e2: {13, 24} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/cpp/queries/each_w_iter_callback/include/each_w_iter_callback.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,16 @@ | ||
#ifndef EACH_W_ITER_CALLBACK_H | ||
#define EACH_W_ITER_CALLBACK_H | ||
|
||
/* This generated file contains includes for project dependencies */ | ||
#include "each_w_iter_callback/bake_config.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
|
24 changes: 24 additions & 0 deletions
24
examples/cpp/queries/each_w_iter_callback/include/each_w_iter_callback/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,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 EACH_W_ITER_CALLBACK_BAKE_CONFIG_H | ||
#define EACH_W_ITER_CALLBACK_BAKE_CONFIG_H | ||
|
||
/* Headers of public dependencies */ | ||
#include <flecs.h> | ||
|
||
#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,11 @@ | ||
{ | ||
"id": "each_w_iter_callback", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
"flecs" | ||
], | ||
"language": "c++", | ||
"public": false | ||
} | ||
} |
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,48 @@ | ||
#include <each_w_iter_callback.h> | ||
#include <iostream> | ||
|
||
// This example is the same as the each example, but in addition also shows how | ||
// to get access to the flecs::iter object. Flecs iterators provide access to a | ||
// lot of information about the currently iterated result, such as the matched | ||
// entities, table, component ids, whether fields are set or not and more. | ||
|
||
struct Position { | ||
double x, y; | ||
}; | ||
|
||
struct Velocity { | ||
double x, y; | ||
}; | ||
|
||
int main(int, char *[]) { | ||
flecs::world ecs; | ||
|
||
// Create a query for Position, Velocity. | ||
flecs::query<Position, const Velocity> q = | ||
ecs.query<Position, const Velocity>(); | ||
|
||
// Create a few test entities for a Position, Velocity query | ||
ecs.entity("e1") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({1, 2}); | ||
|
||
ecs.entity("e2") | ||
.set<Position>({10, 20}) | ||
.set<Velocity>({3, 4}); | ||
|
||
// This entity will not match as it does not have Position, Velocity | ||
ecs.entity("e3") | ||
.set<Position>({10, 20}); | ||
|
||
// Arguments passed to each match components passed to system | ||
q.each([](flecs::iter& it, size_t row, Position& p, const Velocity& v) { | ||
flecs::entity e = it.entity(row); | ||
p.x += v.x; | ||
p.y += v.y; | ||
std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n"; | ||
}); | ||
|
||
// Output | ||
// e1: {11, 22} | ||
// e2: {13, 24} | ||
} |
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
examples/cpp/queries/run/include/run.h → ...cpp/queries/iter_info/include/iter_info.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
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
2 changes: 1 addition & 1 deletion
2
examples/cpp/queries/run/project.json → examples/cpp/queries/iter_info/project.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"id": "run", | ||
"id": "iter_info", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
|
2 changes: 1 addition & 1 deletion
2
examples/cpp/queries/run/src/main.cpp → examples/cpp/queries/iter_info/src/main.cpp
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include <run.h> | ||
#include <iter_info.h> | ||
#include <iostream> | ||
|
||
struct Position { | ||
|
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 RUN_CALLBACK_H | ||
#define RUN_CALLBACK_H | ||
|
||
/* This generated file contains includes for project dependencies */ | ||
#include "run_callback/bake_config.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif | ||
|
24 changes: 24 additions & 0 deletions
24
examples/cpp/queries/run_callback/include/run_callback/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,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 RUN_CALLBACK_BAKE_CONFIG_H | ||
#define RUN_CALLBACK_BAKE_CONFIG_H | ||
|
||
/* Headers of public dependencies */ | ||
#include <flecs.h> | ||
|
||
#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,11 @@ | ||
{ | ||
"id": "run_callback", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
"flecs" | ||
], | ||
"language": "c++", | ||
"public": false | ||
} | ||
} |
Oops, something went wrong.