-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#663 add unit examples, add get value functions to meta cursor
- Loading branch information
1 parent
27412b6
commit 427ed1c
Showing
16 changed files
with
772 additions
and
38 deletions.
There are no files selected for viewing
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,5 @@ | ||
.bake_cache | ||
.DS_Store | ||
.vscode | ||
gcov | ||
bin |
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 UNITS_H | ||
#define UNITS_H | ||
|
||
/* This generated file contains includes for project dependencies */ | ||
#include "units/bake_config.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#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,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 UNITS_BAKE_CONFIG_H | ||
#define UNITS_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,9 @@ | ||
{ | ||
"id": "units", | ||
"type": "application", | ||
"value": { | ||
"use": [ | ||
"flecs" | ||
] | ||
} | ||
} |
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,71 @@ | ||
#include <units.h> | ||
#include <stdio.h> | ||
|
||
typedef struct { | ||
double temperature; | ||
double pressure; | ||
double precipitation; | ||
} WeatherStation; | ||
|
||
void print_value(ecs_world_t *world, const ecs_meta_cursor_t *cur) { | ||
// Get unit entity and component | ||
ecs_entity_t u = ecs_meta_get_unit(cur); | ||
const EcsUnit *u_data = ecs_get(world, u, EcsUnit); | ||
|
||
// Print value with unit symbol | ||
printf("%s: %.1f %s\n", ecs_meta_get_member(cur), ecs_meta_get_float(cur), | ||
u_data->symbol); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
ecs_world_t *ecs = ecs_init_w_args(argc, argv); | ||
|
||
// Import units module. | ||
ECS_IMPORT(ecs, FlecsUnits); | ||
|
||
ECS_COMPONENT(ecs, WeatherStation); | ||
|
||
// Register reflection data with units. This can improve the way information | ||
// is visualized in tools, such as the explorer. | ||
ecs_struct_init(ecs, &(ecs_struct_desc_t) { | ||
.entity.entity = ecs_id(WeatherStation), | ||
.members = { | ||
{ | ||
.name = "temperature", | ||
.type = ecs_id(ecs_f64_t), | ||
.unit = EcsCelsius | ||
}, | ||
{ | ||
.name = "pressure", | ||
.type = ecs_id(ecs_f64_t), | ||
.unit = EcsBar | ||
}, | ||
{ | ||
.name = "precipitation", | ||
.type = ecs_id(ecs_f64_t), | ||
.unit = EcsMilliMeters | ||
}, | ||
} | ||
}); | ||
|
||
ecs_entity_t e = ecs_set(ecs, 0, WeatherStation, {24, 1.2, 0.5}); | ||
|
||
// Use cursor API to print values with units | ||
WeatherStation *ptr = ecs_get_mut(ecs, e, WeatherStation, 0); | ||
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, ecs_id(WeatherStation), ptr); | ||
|
||
ecs_meta_push(&cur); | ||
print_value(ecs, &cur); | ||
ecs_meta_next(&cur); | ||
print_value(ecs, &cur); | ||
ecs_meta_next(&cur); | ||
print_value(ecs, &cur); | ||
ecs_meta_pop(&cur); | ||
|
||
return ecs_fini(ecs); | ||
|
||
// Output: | ||
// temperature: 24.0 °C | ||
// pressure: 1.2 bar | ||
// precipitation: 0.5 mm | ||
} |
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 UNITS_H | ||
#define UNITS_H | ||
|
||
/* This generated file contains includes for project dependencies */ | ||
#include "units/bake_config.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#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,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 UNITS_BAKE_CONFIG_H | ||
#define UNITS_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": "units", | ||
"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,50 @@ | ||
#include <units.h> | ||
#include <iostream> | ||
|
||
struct WeatherStation { | ||
double temperature; | ||
double pressure; | ||
double precipitation; | ||
}; | ||
|
||
void print_value(const flecs::meta::cursor& cur) { | ||
// Get unit entity and component | ||
flecs::entity u = cur.get_unit(); | ||
const flecs::Unit *u_data = u.get<flecs::Unit>(); | ||
|
||
// Print value with unit symbol | ||
std::cout << cur.get_member() << ": " << cur.get_float() | ||
<< " " << u_data->symbol << "\n"; | ||
} | ||
|
||
int main(int, char *[]) { | ||
flecs::world ecs; | ||
|
||
// Import units module. | ||
ecs.import<flecs::units>(); | ||
|
||
// Register reflection data with units. This can improve the way information | ||
// is visualized in tools, such as the explorer. | ||
ecs.component<WeatherStation>() | ||
.member<double, flecs::units::temperature::Celsius>("temperature") | ||
.member<double, flecs::units::pressure::Bar>("pressure") | ||
.member<double, flecs::units::length::MilliMeters>("precipitation"); | ||
|
||
auto e = ecs.entity().set<WeatherStation>({24, 1.2, 0.5}); | ||
|
||
// Use cursor API to print values with units | ||
WeatherStation *ptr = e.get_mut<WeatherStation>(); | ||
auto cur = ecs.cursor<WeatherStation>(ptr); | ||
cur.push(); | ||
print_value(cur); | ||
cur.next(); | ||
print_value(cur); | ||
cur.next(); | ||
print_value(cur); | ||
cur.pop(); | ||
|
||
// Output: | ||
// temperature: 24 °C | ||
// pressure: 1.2 bar | ||
// precipitation: 0.5 mm | ||
} |
Oops, something went wrong.