Skip to content

Commit

Permalink
#55 add example demonstrating optional columns
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Oct 6, 2019
1 parent 4d129f6 commit e2a5737
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/22_optional/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.bake_cache
.DS_Store
.vscode
gcov
bin
16 changes: 16 additions & 0 deletions examples/22_optional/include/optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef OPTIONAL_H
#define OPTIONAL_H

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

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif

44 changes: 44 additions & 0 deletions examples/22_optional/include/optional/bake_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* 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 OPTIONAL_BAKE_CONFIG_H
#define OPTIONAL_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>

/* Headers of private dependencies */
#ifdef OPTIONAL_IMPL
/* No dependencies */
#endif

/* Convenience macro for exporting symbols */
#ifndef OPTIONAL_STATIC
#if OPTIONAL_IMPL && (defined(_MSC_VER) || defined(__MINGW32__))
#define OPTIONAL_EXPORT __declspec(dllexport)
#elif OPTIONAL_IMPL
#define OPTIONAL_EXPORT __attribute__((__visibility__("default")))
#elif defined _MSC_VER
#define OPTIONAL_EXPORT __declspec(dllimport)
#else
#define OPTIONAL_EXPORT
#endif
#else
#define OPTIONAL_EXPORT
#endif

#endif

12 changes: 12 additions & 0 deletions examples/22_optional/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "optional",
"type": "application",
"value": {
"author": "Jane Doe",
"description": "A simple hello world flecs application",
"public": false,
"use": [
"flecs"
]
}
}
51 changes: 51 additions & 0 deletions examples/22_optional/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <optional.h>

typedef float Health;
typedef float Stamina;
typedef float Mana;

void Regenerate(ecs_rows_t *rows) {
ECS_COLUMN(rows, Health, health, 1);
ECS_COLUMN(rows, Stamina, stamina, 2);
ECS_COLUMN(rows, Mana, mana, 3);

for (int i = 0; i < rows->count; i ++) {
if (health) {
health[i] ++;
printf("%d: process health\n", (int)rows->entities[i]);
}

if (stamina) {
stamina[i] ++;
printf("%d: process stamina\n", (int)rows->entities[i]);
}

if (mana) {
mana[i] ++;
printf("%d: process mana\n", (int)rows->entities[i]);
}
}
}

int main(int argc, char *argv[]) {
/* Create the world, pass arguments for overriding the number of threads,fps
* or for starting the admin dashboard (see flecs.h for details). */
ecs_world_t *world = ecs_init_w_args(argc, argv);

ECS_COMPONENT(world, Health);
ECS_COMPONENT(world, Stamina);
ECS_COMPONENT(world, Mana);

ECS_SYSTEM(world, Regenerate, EcsOnUpdate, ?Health, ?Stamina, ?Mana);

/* Create three entities that will all match with the Regenerate system */
ecs_new(world, Health);
ecs_new(world, Stamina);
ecs_new(world, Mana);

/* Run systems */
ecs_progress(world, 0);

/* Cleanup */
return ecs_fini(world);
}

0 comments on commit e2a5737

Please sign in to comment.