Added - but didn't test - entity creation and tagging

The entities in the API can now be created, deleted and tagged with data of various types. It just needs to be confirmed whether it's working and fixed if buggy.
This commit is contained in:
Eric-Paul Ickhorn 2024-03-03 22:09:04 +01:00
parent 9890a30b5b
commit 48300303a9
3 changed files with 119 additions and 4 deletions

View File

@ -2,7 +2,10 @@
#ifndef MT_ENTITY_H
#define MT_ENTITY_H
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <alphaumlaut/maths.h>
typedef struct MtEntity MtEntity;
@ -21,4 +24,17 @@ struct MtEntity
uint32_t shadow_id;
};
MtEntity mt_summon(void *context_ptr);
void mt_delete(MtEntity entity);
void mt_tag_bool(MtEntity entity, const char *name, bool boolean);
void mt_tag_int(MtEntity entity, const char *name, int64_t integer);
void mt_tag_float(MtEntity entity, const char *name, double real);
void mt_tag_str(MtEntity entity, const char *name, char *string);
void mt_tag_ptr(MtEntity entity, const char *name, void *pointer);
void mt_tag_vec2f(MtEntity entity, const char *name, AuVec2f vec2f);
void mt_tag_vec3f(MtEntity entity, const char *name, AuVec3f vec3f);
void mt_tag_vec4f(MtEntity entity, const char *name, AuVec4f vec4f);
void mt_tag_ref(MtEntity entity, const char *name, MtEntity reference);
#endif // MT_ENTITY_H

View File

@ -16,15 +16,16 @@ typedef struct MtShadowTagSystem MtShadowTagSystem;
typedef enum
{
MT_SHADOW_TAG_UNUSED = 0,
MT_SHADOW_TAG_INTEGER,
MT_SHADOW_TAG_BOOLEAN,
MT_SHADOW_TAG_DECIMAL,
MT_SHADOW_TAG_REAL,
MT_SHADOW_TAG_STRING,
MT_SHADOW_TAG_RAW_POINTER,
MT_SHADOW_TAG_VECTOR_2,
MT_SHADOW_TAG_VECTOR_3,
MT_SHADOW_TAG_VECTOR_4,
MT_SHADOW_TAG_SUB_ENTITY,
MT_SHADOW_TAG_REFERENCE,
MT_SHADOW_TAG_RESOURCE
} MtShadowTagType;
@ -40,13 +41,13 @@ struct MtShadowTag
{
bool boolean;
int64_t integer;
double decimal;
double real;
char *string;
void *raw_pointer;
AuVec2f vec2f;
AuVec3f vec3f;
AuVec4f vec4f;
MtEntity entity;
MtEntity reference;
} data;
};

98
src-c/entity.c Normal file
View File

@ -0,0 +1,98 @@
#include <entity.h>
#include <context.h>
#include <shadow-registry.h>
MtEntity mt_summon(void *context_ptr)
{
MtContext *context = context_ptr;
MtShadow *shadow = mt_create_shadow(context->shadow_registry);
MtEntity entity;
entity.context = context_ptr;
entity.shadow_id = shadow->identifier;
return entity;
}
void mt_delete(MtEntity entity)
{
MtContext *context = entity.context;
MtShadow *shadow = mt_lookup_shadow(context->shadow_registry, entity.shadow_id);
mt_delete_shadow(context->shadow_registry, shadow);
}
MtShadowTag * mt_get_and_name_tag_for_entity(MtEntity entity, const char *name)
{
MtContext *context = entity.context;
MtShadow *shadow = mt_lookup_shadow(context->shadow_registry, entity.shadow_id);
if(shadow->tag_chain_start == NULL)
shadow->tag_chain_start = (entity.context);
else
shadow->tag_chain_start = mt_alloc_shadow_tag_before(shadow->tag_chain_start);
return shadow->tag_chain_start;
}
void mt_tag_bool(MtEntity entity, const char *name, bool boolean)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_BOOLEAN;
tag->data.boolean = boolean;
}
void mt_tag_int(MtEntity entity, const char *name, int64_t integer)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_INTEGER;
tag->data.integer = integer;
}
void mt_tag_float(MtEntity entity, const char *name, double real)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_REAL;
tag->data.real = real;
}
void mt_tag_str(MtEntity entity, const char *name, char *string)
{
// TODO: Allocate the string in an arena allocator which is
// stored in, and deleted with, the entity shadow.
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_STRING;
tag->data.string = string;
}
void mt_tag_ptr(MtEntity entity, const char *name, void *pointer)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_RAW_POINTER;
tag->data.raw_pointer = pointer;
}
void mt_tag_vec2f(MtEntity entity, const char *name, AuVec2f vec2f)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_VECTOR_2;
tag->data.vec2f = vec2f;
}
void mt_tag_vec3f(MtEntity entity, const char *name, AuVec3f vec3f)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_VECTOR_3;
tag->data.vec3f = vec3f;
}
void mt_tag_vec4f(MtEntity entity, const char *name, AuVec4f vec4f)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_VECTOR_4;
tag->data.vec4f = vec4f;
}
void mt_tag_ref(MtEntity entity, const char *name, MtEntity reference)
{
MtShadowTag *tag = mt_get_and_name_tag_for_entity(entity, name);
tag->type = MT_SHADOW_TAG_REFERENCE;
tag->data.reference = reference;
}