So I am trying to make simple ECS system for my game framework.
I decided to make a macro function to have a universal way to create / register components instead of users having to call specific functions for things like int,bool,char and so on. Plus than users will have to make there own functions for custom structs. Macros seem the way to go. The issue is that is so stupid slow. Using it 3 times makes the application stall for 10-15 seconds on startup (release). If I use it to many times the application just crashes. In debug mode 3 takes to long and application crashes.
I am new-Ish to C, so I am probably doing something stupid.
Here is my macro code
Code:
// Macro : Entities Component Initialize Macro
#define CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(TYPE,ID,ARRAY,DEFAULT_VALUE) do\
{\
unsigned int count = _calypso_framework_entities_simple_entity_count_max;\
ARRAY = (TYPE*)malloc(count * sizeof(TYPE));\
for (int i = 0; i < count; i++)\
ARRAY = DEFAULT_VALUE;\
} while (0)
Here is how its being used (Called once)
Code:
calypso_framework_entities_simple_init(1000);
_entity_count_max =
calypso_framework_entities_simple_get_entity_count_max_ptr();
CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(int, "position_x",
_entity_position_x_array,
600);
CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(float, "position_y",
_entity_position_y_array,
300);
CALYPSO_FRAMEWORK_ENTITIES_SIMPLE_REGISTER_COMPONENT(float, "bounds_x",
_entity_bounds_x_array,
130);
Here is just the regular function which is much faster.
Code:
int *calypso_framework_entities_simple_create_entity_data_int_array(int *array,
const int
default_value)
{
const int count = *_calypso_framework_entities_simple_entity_count_max;
array = malloc(count * sizeof(int));
for (int i = 0; i < count; i++)
array = default_value;
return array;
}
Not sure if this is helpful but here is my compile flags for release.
Code:
CC_FLAGS_RELEASE = -std = c99 - O3 - Wall - DNDEBUG