Skip to content

Commit

Permalink
Reduce usage of auto in C++ examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jul 10, 2023
1 parent 424c893 commit de5754c
Show file tree
Hide file tree
Showing 57 changed files with 182 additions and 163 deletions.
4 changes: 2 additions & 2 deletions examples/cpp/entities/basics/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int, char *[]) {
flecs::world ecs;

// Create an entity with name Bob
auto bob = ecs.entity("Bob")
flecs::entity bob = ecs.entity("Bob")
// The set operation finds or creates a component, and sets it.
// Components are automatically registered with the world.
.set<Position>({10, 20})
Expand All @@ -27,7 +27,7 @@ int main(int, char *[]) {
bob.set<Position>({20, 30});

// Create another named entity
auto alice = ecs.entity("Alice")
flecs::entity alice = ecs.entity("Alice")
.set<Position>({10, 20});

// Add a tag after entity is created
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/entities/hierarchy/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(int, char *[]) {
// Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
// create entities as children of other entities.

auto sun = ecs.entity("Sun")
flecs::entity sun = ecs.entity("Sun")
.add<Star>()
.set<Position>({1, 1});

Expand All @@ -47,12 +47,12 @@ int main(int, char *[]) {
.add<Planet>()
.set<Position>({2, 2});

auto earth = ecs.entity("Earth")
flecs::entity earth = ecs.entity("Earth")
.child_of(sun)
.add<Planet>()
.set<Position>({3, 3});

auto moon = ecs.entity("Moon")
flecs::entity moon = ecs.entity("Moon")
.child_of(earth)
.add<Moon>()
.set<Position>({0.1, 0.1});
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/entities/hooks/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int, char *[]) {

flecs::log::set_level(0);

auto e = ecs.entity("Entity");
flecs::entity e = ecs.entity("Entity");

flecs::log::push("e.add<String>()");
e.add<String>();
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/entities/iterate_components/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void iterate_components(flecs::entity e) {
int main(int, char *[]) {
flecs::world ecs;

auto bob = ecs.entity()
flecs::entity bob = ecs.entity()
.set<Position>({10, 20})
.set<Velocity>({1, 1})
.add<Human>()
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/entities/multi_set_get/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int, char *[]) {
flecs::world ecs;

// Create new entity, set Position and Mass component
auto e = ecs.entity()
flecs::entity e = ecs.entity()
.set([](Position& p, Mass& m) {
p.x = 10;
p.y = 20;
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/entities/prefab/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int, char *[]) {
flecs::world ecs;

// Create a prefab hierarchy.
auto spaceship = ecs.prefab("Spaceship")
flecs::entity spaceship = ecs.prefab("Spaceship")
// Add components to prefab entity as usual
.set<ImpulseSpeed>({50})
.set<Defense>({50})
Expand All @@ -27,15 +27,15 @@ int main(int, char *[]) {
// copy of the component.
.override<Position>();

auto freighter = ecs.prefab("Freighter")
flecs::entity freighter = ecs.prefab("Freighter")
// Short for .add(flecs::IsA, spaceship). This ensures the entity
// inherits all components from spaceship.
.is_a(spaceship)
.add<HasFTL>()
.set<FreightCapacity>({100})
.set<Defense>({100});

auto mammoth_freighter = ecs.prefab("MammothFreighter")
flecs::entity mammoth_freighter = ecs.prefab("MammothFreighter")
.is_a(freighter)
.set<FreightCapacity>({500})
.set<Defense>({300});
Expand All @@ -50,7 +50,7 @@ int main(int, char *[]) {
// Create a regular entity from a prefab.
// The instance will have a private copy of the Position component, because
// of the override in the spaceship entity. All other components are shared.
auto inst = ecs.entity("my_mammoth_freighter")
flecs::entity inst = ecs.entity("my_mammoth_freighter")
.is_a(mammoth_freighter);

// Inspect the type of the entity. This outputs:
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/explorer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ int main(int argc, char *argv[]) {
.member<double, mass::KiloGrams>("value");

// Simple hierarchy
auto Sun = world.entity("Sun")
flecs::entity Sun = world.entity("Sun")
.set<Mass>({1.988500e31});

auto Earth = world.scope(Sun).entity("Earth")
flecs::entity Earth = world.scope(Sun).entity("Earth")
.set<Mass>({5.9722e24});

world.scope(Earth).entity("Moon")
Expand Down
10 changes: 5 additions & 5 deletions examples/cpp/game_mechanics/inventory_system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ void transfer_item(flecs::entity container, flecs::entity item) {
if (amt) {
// If item has amount we need to check if the container already has an
// item of this kind, and increase the value.
auto ecs = container.world();
auto ik = item_kind(item);
auto dst_item = find_item_w_kind(container, ik);
flecs::world ecs = container.world();
flecs::entity ik = item_kind(item);
flecs::entity dst_item = find_item_w_kind(container, ik);
if (dst_item) {
// If a matching item was found, increase its amount
Amount *dst_amt = dst_item.get_mut<Amount>();
Expand Down Expand Up @@ -302,14 +302,14 @@ int main(int, char *[]) {
.set_override<Health>({ 20 });

// Create a loot box with items
auto loot_box = ecs.entity("Chest").add<Container>().with<ContainedBy>([&]{
flecs::entity loot_box = ecs.entity("Chest").add<Container>().with<ContainedBy>([&]{
ecs.entity().is_a<IronSword>();
ecs.entity().is_a<WoodenArmor>();
ecs.entity().add<Coin>().set<Amount>({ 30 });
});

// Create a player entity with an inventory
auto player = ecs.entity("Player").set<Health>({10}).add<Inventory>(
flecs::entity player = ecs.entity("Player").set<Health>({10}).add<Inventory>(
ecs.entity().add<Container>().with<ContainedBy>([&]{
ecs.entity().add<Coin>().set<Amount>({ 20 });
})
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/game_mechanics/scene_management/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void reset_scene(flecs::world& ecs) {
void menu_scene(flecs::iter& it, size_t, ActiveScene) {
std::cout << "\n>> ActiveScene has changed to `MenuScene`\n\n";

auto ecs = it.world();
auto scene = ecs.component<SceneRoot>();
flecs::world ecs = it.world();
flecs::entity scene = ecs.component<SceneRoot>();

reset_scene(ecs);

Expand All @@ -48,8 +48,8 @@ void menu_scene(flecs::iter& it, size_t, ActiveScene) {
void game_scene(flecs::iter& it, size_t, ActiveScene) {
std::cout << "\n>> ActiveScene has changed to `GameScene`\n\n";

auto ecs = it.world();
auto scene = ecs.component<SceneRoot>();
flecs::world ecs = it.world();
flecs::entity scene = ecs.component<SceneRoot>();

reset_scene(ecs);

Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/hello_world/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int, char *[]) {
});

// Create an entity with name Bob, add Position and food preference
auto Bob = ecs.entity("Bob")
flecs::entity Bob = ecs.entity("Bob")
.set(Position{0, 0})
.set(Velocity{1, 2})
.add<Eats, Apples>();
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/queries/basics/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int, char *[]) {

// Create a query for Position, Velocity. Queries are the fastest way to
// iterate entities as they cache results.
auto q = ecs.query<Position, const 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")
Expand Down
15 changes: 8 additions & 7 deletions examples/cpp/queries/change_tracking/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ int main(int, char *[]) {
// when query::changed() is called.
// Each query has its own private dirty state which is reset only when the
// query is iterated.
auto q_read = ecs.query<const Position>();
flecs::query<const Position> q_read = ecs.query<const Position>();

// Create a query that writes the component based on a Dirty state.
auto q_write = ecs.query_builder<const Dirty, Position>()
.term_at(1).up() // Only match Dirty from prefab
.instanced() // Instanced iteration is faster (see example)
.build();
flecs::query<const Dirty, Position> q_write =
ecs.query_builder<const Dirty, Position>()
.term_at(1).up() // Only match Dirty from prefab
.instanced() // Instanced iteration is faster (see example)
.build();

// Create two prefabs with a Dirty component. We can use this to share a
// single Dirty value for all entities in a table.
auto p1 = ecs.prefab("p1").set<Dirty>({false});
auto p2 = ecs.prefab("p2").set<Dirty>({true});
flecs::entity p1 = ecs.prefab("p1").set<Dirty>({false});
flecs::entity p2 = ecs.prefab("p2").set<Dirty>({true});

// Create instances of p1 and p2. Because the entities have different
// prefabs, they end up in different tables.
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/queries/group_by/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main() {
ecs.component<Third>();

// Grouped query
auto q = ecs.query_builder<Position>()
flecs::query<Position> q = ecs.query_builder<Position>()
.group_by<Group>(group_by_relation)
.build();

Expand Down Expand Up @@ -92,7 +92,7 @@ int main() {
//

q.iter([&](flecs::iter& it, Position *p) {
auto group = ecs.entity(it.group_id());
flecs::entity group = ecs.entity(it.group_id());
std::cout << " - group " << group.path() << ": table ["
<< it.table().str() << "]\n";

Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/queries/group_by_callbacks/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main() {
ecs.component<Third>();

// Grouped query
auto q = ecs.query_builder<Position>()
flecs::query<Position> q = ecs.query_builder<Position>()
.group_by<Group>()

// Callback invoked when a new group is created
Expand Down Expand Up @@ -106,7 +106,7 @@ int main() {
std::cout << "\n";

q.iter([&](flecs::iter& it, Position *p) {
auto group = ecs.entity(it.group_id());
flecs::entity group = ecs.entity(it.group_id());
group_ctx *ctx = static_cast<group_ctx*>(q.group_ctx(group));
std::cout << " - group " << group.path() << ": table ["
<< it.table().str() << "]\n";
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/queries/group_by_custom/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main() {
ecs.component<Third>();

// Grouped query
auto q = ecs.query_builder<Position>()
flecs::query<Position> q = ecs.query_builder<Position>()
.group_by<Group>(group_by_relation)
.build();

Expand Down Expand Up @@ -78,7 +78,7 @@ int main() {
//

q.iter([&](flecs::iter& it, Position *p) {
auto group = ecs.entity(it.group_id());
flecs::entity group = ecs.entity(it.group_id());
std::cout << " - group " << group.path() << ": table ["
<< it.table().str() << "]\n";

Expand Down
6 changes: 3 additions & 3 deletions examples/cpp/queries/group_iter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ int main() {
.add<Soldier>()
.add<Npc>();

auto q = ecs.query_builder<Npc>()
flecs::query<Npc> q = ecs.query_builder<Npc>()
.group_by<WorldCell>(group_by_relation)
.build();

// Iterate all tables
std::cout << "All tables:\n";
q.iter([&](flecs::iter& it) {
auto group = ecs.entity(it.group_id());
flecs::entity group = ecs.entity(it.group_id());
std::cout << " - group " << group.path() << ": table ["
<< it.table().str() << "]\n";
});
Expand All @@ -89,7 +89,7 @@ int main() {
// Only iterate entities in cell 1_0
std::cout << "Tables for cell 1_0:\n";
q.iter().set_group<Cell_1_0>().iter([&](flecs::iter& it) {
auto group = ecs.entity(it.group_id());
flecs::entity group = ecs.entity(it.group_id());
std::cout << " - group " << group.path() << ": table ["
<< it.table().str() << "]\n";
});
Expand Down
33 changes: 17 additions & 16 deletions examples/cpp/queries/hierarchy/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(int, char *[]) {
flecs::world ecs;

// Create a hierarchy. For an explanation see the entities/hierarchy example
auto sun = ecs.entity("Sun")
flecs::entity sun = ecs.entity("Sun")
.add<Position, World>()
.set<Position, Local>({1, 1});

Expand All @@ -27,7 +27,7 @@ int main(int, char *[]) {
.add<Position, World>()
.set<Position, Local>({2, 2});

auto earth = ecs.entity("Earth")
flecs::entity earth = ecs.entity("Earth")
.child_of(sun)
.add<Position, World>()
.set<Position, Local>({3, 3});
Expand All @@ -39,21 +39,22 @@ int main(int, char *[]) {

// Create a hierarchical query to compute the global position from the
// local position and the parent position.
auto 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>()
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();
// 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,
Expand Down
11 changes: 6 additions & 5 deletions examples/cpp/queries/instancing/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ int main(int, char *[]) {

// Create a query for Position, Velocity. We'll create a few entities that
// have Velocity as owned and shared component.
auto q = ecs.query_builder<Position, const Velocity>()
.term_at(1).self() // Position must always be owned by the entity
.instanced() // create instanced query
.build();
flecs::query<Position, const Velocity> q =
ecs.query_builder<Position, const Velocity>()
.term_at(1).self() // Position must always be owned by the entity
.instanced() // create instanced query
.build();

// Create a prefab with Velocity. Prefabs are not matched with queries.
auto prefab = ecs.prefab("p")
flecs::entity prefab = ecs.prefab("p")
.set<Velocity>({1, 2});

// Create a few entities that own Position & share Velocity from the prefab.
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/queries/iter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ int main(int, char *[]) {
flecs::world ecs;

// Create a query for Position, Velocity.
auto q = ecs.query<Position, const 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")
Expand Down
7 changes: 4 additions & 3 deletions examples/cpp/queries/singleton/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ int main(int, char *[]) {
world.entity("e3").set<Velocity>({0, 2});

// Create query that matches Gravity as singleton
auto q = world.query_builder<Velocity, const Gravity>()
.term_at(2).singleton()
.build();
flecs::query<Velocity, const Gravity> q =
world.query_builder<Velocity, const Gravity>()
.term_at(2).singleton()
.build();

// In a query string expression you can use the $ shortcut for singletons:
// Velocity, Gravity($)
Expand Down
Loading

0 comments on commit de5754c

Please sign in to comment.