Skip to content

Commit

Permalink
SanderMertens#1439 Add query/system callback examples for each and ru…
Browse files Browse the repository at this point in the history
…n variants
  • Loading branch information
SanderMertens authored Nov 15, 2024
1 parent b55b7c4 commit 5ea2aa1
Show file tree
Hide file tree
Showing 42 changed files with 824 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CUSTOM_RUNNER_H
#define CUSTOM_RUNNER_H
#ifndef EACH_CALLBACK_H
#define EACH_CALLBACK_H

/* This generated file contains includes for project dependencies */
#include "custom_runner/bake_config.h"
#include "each_callback/bake_config.h"

#ifdef __cplusplus
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef CUSTOM_RUNNER_BAKE_CONFIG_H
#define CUSTOM_RUNNER_BAKE_CONFIG_H
#ifndef EACH_CALLBACK_BAKE_CONFIG_H
#define EACH_CALLBACK_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
Expand Down
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
}
}
45 changes: 45 additions & 0 deletions examples/cpp/queries/each_callback/src/main.cpp
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}
}
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

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

11 changes: 11 additions & 0 deletions examples/cpp/queries/each_w_entity_callback/project.json
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
}
}
45 changes: 45 additions & 0 deletions examples/cpp/queries/each_w_entity_callback/src/main.cpp
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}
}
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

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

11 changes: 11 additions & 0 deletions examples/cpp/queries/each_w_iter_callback/project.json
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
}
}
48 changes: 48 additions & 0 deletions examples/cpp/queries/each_w_iter_callback/src/main.cpp
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.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef ITER_H
#define ITER_H
#ifndef ITER_INFO_H
#define ITER_INFO_H

/* This generated file contains includes for project dependencies */
#include "run/bake_config.h"
#include "iter_info/bake_config.h"

#ifdef __cplusplus
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef RUN_BAKE_CONFIG_H
#define RUN_BAKE_CONFIG_H
#ifndef ITER_INFO_BAKE_CONFIG_H
#define ITER_INFO_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "run",
"id": "iter_info",
"type": "application",
"value": {
"use": [
Expand Down
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 {
Expand Down
16 changes: 16 additions & 0 deletions examples/cpp/queries/run_callback/include/run_callback.h
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

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

11 changes: 11 additions & 0 deletions examples/cpp/queries/run_callback/project.json
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
}
}
Loading

0 comments on commit 5ea2aa1

Please sign in to comment.