165 lines
4.2 KiB
C
165 lines
4.2 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "entitybase.h"
|
|
#include "error/error.h"
|
|
|
|
#include <yyjson.h>
|
|
|
|
#define X(enumName, type, field, init, dispose, render, serialize, deserialize) \
|
|
// do nothing
|
|
#include "componentlist.h"
|
|
#undef X
|
|
|
|
typedef union {
|
|
#define X(enumName, type, field, init, dispose, render, serialize, deserialize) \
|
|
type field;
|
|
#include "componentlist.h"
|
|
#undef X
|
|
} componentdata_t;
|
|
|
|
typedef struct {
|
|
const char_t *enumName;
|
|
const char_t *name;
|
|
void (*init)(const entityid_t, const componentid_t);
|
|
void (*dispose)(const entityid_t, const componentid_t);
|
|
errorret_t (*render)(const entityid_t, const componentid_t);
|
|
void (*serialize)(
|
|
const entityid_t,
|
|
const componentid_t,
|
|
yyjson_mut_doc *,
|
|
yyjson_mut_val *
|
|
);
|
|
errorret_t (*deserialize)(
|
|
const entityid_t,
|
|
const componentid_t,
|
|
yyjson_val *
|
|
);
|
|
} componentdefinition_t;
|
|
|
|
typedef enum {
|
|
COMPONENT_TYPE_NULL,
|
|
|
|
#define X(enumName, type, field, init, dispose, render, serialize, deserialize) \
|
|
COMPONENT_TYPE_##enumName,
|
|
#include "componentlist.h"
|
|
#undef X
|
|
|
|
COMPONENT_TYPE_COUNT
|
|
} componenttype_t;
|
|
|
|
typedef struct {
|
|
componenttype_t type;
|
|
componentdata_t data;
|
|
} component_t;
|
|
|
|
extern componentdefinition_t COMPONENT_DEFINITIONS[];
|
|
|
|
/**
|
|
* Initializes a component of the given type for the entity with component ID.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param type The type of the component to initialize.
|
|
*/
|
|
void componentInit(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const componenttype_t type
|
|
);
|
|
|
|
/**
|
|
* Gets the pointer to the data of a component for the entity with component ID.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param type The type of the component to get, only used for assertion.
|
|
* @return A pointer to the component data.
|
|
*/
|
|
void * componentGetData(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const componenttype_t type
|
|
);
|
|
|
|
/**
|
|
* Gets the index of a component for the entity with component ID.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @return The index of the component in the component array.
|
|
*/
|
|
componentindex_t componentGetIndex(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Gets the entity IDs of all entities with a component of the given type.
|
|
*
|
|
* @param type The type of the component to get entities for.
|
|
* @param outEntities An array to write the entity IDs to, must be at least
|
|
* ENTITY_COUNT_MAX in size.
|
|
* @param outComponents An array to write the component IDs to.
|
|
* @return The number of entity IDs written to outEntities.
|
|
*/
|
|
entityid_t componentGetEntitiesWithComponent(
|
|
const componenttype_t type,
|
|
entityid_t outEntities[ENTITY_COUNT_MAX],
|
|
componentid_t outComponents[ENTITY_COUNT_MAX]
|
|
);
|
|
|
|
/**
|
|
* Disposes of a component for the entity with component ID.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
*/
|
|
void componentDispose(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
);
|
|
|
|
/**
|
|
* Calls the render callback on every active component that defines one.
|
|
* Iterates all active entities and all their component slots. No-op for
|
|
* components whose definition has render == NULL.
|
|
*
|
|
* @return Error state.
|
|
*/
|
|
errorret_t componentRenderAll(void);
|
|
|
|
/**
|
|
* Serializes a component's configurable state into a JSON object. The caller
|
|
* is responsible for creating and owning the doc and obj.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param doc The mutable JSON document to allocate values from.
|
|
* @param obj The JSON object to write fields into.
|
|
*/
|
|
void componentSerialize(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
yyjson_mut_doc *doc,
|
|
yyjson_mut_val *obj
|
|
);
|
|
|
|
/**
|
|
* Deserializes a component's configurable state from a JSON object.
|
|
*
|
|
* @param entityId The entity ID.
|
|
* @param componentId The component ID.
|
|
* @param obj The JSON object to read fields from.
|
|
* @return Error state.
|
|
*/
|
|
errorret_t componentDeserialize(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
yyjson_val *obj
|
|
); |