Skip to content

Commit

Permalink
Implement flecs script math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 7, 2024
1 parent 894344f commit ce89718
Show file tree
Hide file tree
Showing 21 changed files with 967 additions and 187 deletions.
418 changes: 299 additions & 119 deletions distr/flecs.c

Large diffs are not rendered by default.

119 changes: 88 additions & 31 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

/** @def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

/** \def FLECS_CUSTOM_BUILD
* This macro lets you customize which addons to build flecs with.
* Without any addons Flecs is just a minimal ECS storage, but addons add
Expand Down Expand Up @@ -175,38 +185,29 @@
*/
// #define FLECS_CUSTOM_BUILD

/** @def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
#define FLECS_MODULE /**< Module support */
#define FLECS_SCRIPT /**< ECS data definition format */
#define FLECS_STATS /**< Track runtime statistics */
#define FLECS_METRICS /**< Expose component data as statistics */
#define FLECS_ALERTS /**< Monitor conditions for errors */
#define FLECS_SYSTEM /**< System support */
#define FLECS_PIPELINE /**< Pipeline support */
#define FLECS_TIMER /**< Timer support */
#define FLECS_META /**< Reflection support */
#define FLECS_UNITS /**< Builtin standard units */
#define FLECS_JSON /**< Parsing JSON to/from component values */
#define FLECS_DOC /**< Document entities & components */
#define FLECS_LOG /**< When enabled ECS provides more detailed logs */
#define FLECS_APP /**< Application addon */
#define FLECS_OS_API_IMPL /**< Default implementation for OS API */
#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI */
#define FLECS_REST /**< REST API for querying application data */
// #define FLECS_JOURNAL /**< Journaling addon (disabled by default) */
// #define FLECS_PERF_TRACE /**< Enable performance tracing (disabled by default) */
#define FLECS_ALERTS /**< Monitor conditions for errors */
#define FLECS_APP /**< Application addon */
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
#define FLECS_DOC /**< Document entities & components */
// #define FLECS_JOURNAL /**< Journaling addon (disabled by default) */
#define FLECS_JSON /**< Parsing JSON to/from component values */
#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI */
#define FLECS_LOG /**< When enabled ECS provides more detailed logs */
#define FLECS_META /**< Reflection support */
#define FLECS_METRICS /**< Expose component data as statistics */
#define FLECS_MODULE /**< Module support */
#define FLECS_OS_API_IMPL /**< Default implementation for OS API */
// #define FLECS_PERF_TRACE /**< Enable performance tracing (disabled by default) */
#define FLECS_PIPELINE /**< Pipeline support */
#define FLECS_REST /**< REST API for querying application data */
#define FLECS_SCRIPT /**< Flecs entity notation language */
// #define FLECS_SCRIPT_MATH /**< Math functions for flecs script (may require linking with libm) */
#define FLECS_SYSTEM /**< System support */
#define FLECS_STATS /**< Track runtime statistics */
#define FLECS_TIMER /**< Timer support */
#define FLECS_UNITS /**< Builtin standard units */
#endif // ifndef FLECS_CUSTOM_BUILD

/** @def FLECS_LOW_FOOTPRINT
Expand Down Expand Up @@ -10395,6 +10396,9 @@ int ecs_value_move_ctor(
#ifdef FLECS_NO_SCRIPT
#undef FLECS_SCRIPT
#endif
#ifdef FLECS_NO_SCRIPT_MATH
#undef FLECS_SCRIPT_MATH
#endif
#ifdef FLECS_NO_STATS
#undef FLECS_STATS
#endif
Expand Down Expand Up @@ -14211,6 +14215,59 @@ void FlecsUnitsImport(

#endif

#ifdef FLECS_SCRIPT_MATH
#ifdef FLECS_NO_SCRIPT_MATH
#error "FLECS_NO_SCRIPT_MATH failed: SCRIPT_MATH is required by other addons"
#endif
/**
* @file addons/script_math.h
* @brief Math functions for flecs script.
*/

#ifdef FLECS_SCRIPT_MATH

#ifndef FLECS_SCRIPT
#define FLECS_SCRIPT
#endif

/**
* @defgroup c_addons_script_math Script Math
* @ingroup c_addons
* Math functions for flecs script.
* @{
*/

#ifndef FLECS_SCRIPT_MATH_H
#define FLECS_SCRIPT_MATH_H

#ifdef __cplusplus
extern "C" {
#endif

/** Script math import function.
* Usage:
* @code
* ECS_IMPORT(world, FlecsScriptMath)
* @endcode
*
* @param world The world.
*/
FLECS_API
void FlecsScriptMathImport(
ecs_world_t *world);

#ifdef __cplusplus
}
#endif

#endif

/** @} */

#endif

#endif

#ifdef FLECS_SCRIPT
#ifdef FLECS_NO_SCRIPT
#error "FLECS_NO_SCRIPT failed: SCRIPT is required by other addons"
Expand Down
63 changes: 32 additions & 31 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

/** @def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

/** \def FLECS_CUSTOM_BUILD
* This macro lets you customize which addons to build flecs with.
* Without any addons Flecs is just a minimal ECS storage, but addons add
Expand Down Expand Up @@ -173,38 +183,29 @@
*/
// #define FLECS_CUSTOM_BUILD

/** @def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
#define FLECS_MODULE /**< Module support */
#define FLECS_SCRIPT /**< ECS data definition format */
#define FLECS_STATS /**< Track runtime statistics */
#define FLECS_METRICS /**< Expose component data as statistics */
#define FLECS_ALERTS /**< Monitor conditions for errors */
#define FLECS_SYSTEM /**< System support */
#define FLECS_PIPELINE /**< Pipeline support */
#define FLECS_TIMER /**< Timer support */
#define FLECS_META /**< Reflection support */
#define FLECS_UNITS /**< Builtin standard units */
#define FLECS_JSON /**< Parsing JSON to/from component values */
#define FLECS_DOC /**< Document entities & components */
#define FLECS_LOG /**< When enabled ECS provides more detailed logs */
#define FLECS_APP /**< Application addon */
#define FLECS_OS_API_IMPL /**< Default implementation for OS API */
#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI */
#define FLECS_REST /**< REST API for querying application data */
// #define FLECS_JOURNAL /**< Journaling addon (disabled by default) */
// #define FLECS_PERF_TRACE /**< Enable performance tracing (disabled by default) */
#define FLECS_ALERTS /**< Monitor conditions for errors */
#define FLECS_APP /**< Application addon */
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
#define FLECS_DOC /**< Document entities & components */
// #define FLECS_JOURNAL /**< Journaling addon (disabled by default) */
#define FLECS_JSON /**< Parsing JSON to/from component values */
#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI */
#define FLECS_LOG /**< When enabled ECS provides more detailed logs */
#define FLECS_META /**< Reflection support */
#define FLECS_METRICS /**< Expose component data as statistics */
#define FLECS_MODULE /**< Module support */
#define FLECS_OS_API_IMPL /**< Default implementation for OS API */
// #define FLECS_PERF_TRACE /**< Enable performance tracing (disabled by default) */
#define FLECS_PIPELINE /**< Pipeline support */
#define FLECS_REST /**< REST API for querying application data */
#define FLECS_SCRIPT /**< Flecs entity notation language */
// #define FLECS_SCRIPT_MATH /**< Math functions for flecs script (may require linking with libm) */
#define FLECS_SYSTEM /**< System support */
#define FLECS_STATS /**< Track runtime statistics */
#define FLECS_TIMER /**< Timer support */
#define FLECS_UNITS /**< Builtin standard units */
#endif // ifndef FLECS_CUSTOM_BUILD

/** @def FLECS_LOW_FOOTPRINT
Expand Down
46 changes: 46 additions & 0 deletions include/flecs/addons/script_math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file addons/script_math.h
* @brief Math functions for flecs script.
*/

#ifdef FLECS_SCRIPT_MATH

#ifndef FLECS_SCRIPT
#define FLECS_SCRIPT
#endif

/**
* @defgroup c_addons_script_math Script Math
* @ingroup c_addons
* Math functions for flecs script.
* @{
*/

#ifndef FLECS_SCRIPT_MATH_H
#define FLECS_SCRIPT_MATH_H

#ifdef __cplusplus
extern "C" {
#endif

/** Script math import function.
* Usage:
* @code
* ECS_IMPORT(world, FlecsScriptMath)
* @endcode
*
* @param world The world.
*/
FLECS_API
void FlecsScriptMathImport(
ecs_world_t *world);

#ifdef __cplusplus
}
#endif

#endif

/** @} */

#endif
10 changes: 10 additions & 0 deletions include/flecs/private/addons.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifdef FLECS_NO_SCRIPT
#undef FLECS_SCRIPT
#endif
#ifdef FLECS_NO_SCRIPT_MATH
#undef FLECS_SCRIPT_MATH
#endif
#ifdef FLECS_NO_STATS
#undef FLECS_STATS
#endif
Expand Down Expand Up @@ -161,6 +164,13 @@
#include "../addons/units.h"
#endif

#ifdef FLECS_SCRIPT_MATH
#ifdef FLECS_NO_SCRIPT_MATH
#error "FLECS_NO_SCRIPT_MATH failed: SCRIPT_MATH is required by other addons"
#endif
#include "../addons/script_math.h"
#endif

#ifdef FLECS_SCRIPT
#ifdef FLECS_NO_SCRIPT
#error "FLECS_NO_SCRIPT failed: SCRIPT is required by other addons"
Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ flecs_src = files(
'src/addons/rest.c',
'src/addons/script/template.c',
'src/addons/script/ast.c',
'src/addons/script/builtin_functions.c',
'src/addons/script/function.c',
'src/addons/script/functions_builtin.c',
'src/addons/script/functions_math.c',
'src/addons/script/interpolate.c',
'src/addons/script/parser.c',
'src/addons/script/query_parser.c',
Expand Down
2 changes: 1 addition & 1 deletion project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"lang.c": {
"${os linux}": {
"lib": ["rt", "pthread"],
"lib": ["rt", "pthread", "m"],
"${cfg debug}": {
"export-symbols": true
},
Expand Down
3 changes: 2 additions & 1 deletion src/addons/script/expr/visit_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ int flecs_expr_function_visit_type(

try_function:
if (!is_method) {
ecs_entity_t func = ecs_lookup(world, node->function_name);
ecs_entity_t func = desc->lookup_action(
world, node->function_name, desc->lookup_ctx);
if (!func) {
flecs_expr_visit_error(script, node,
"unresolved function identifier '%s'",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file addons/script/builtin_functions.c
* @brief Builtin script functions.
* @brief Flecs functions for flecs script.
*/

#include "flecs.h"
Expand Down
Loading

0 comments on commit ce89718

Please sign in to comment.