150 lines
3.5 KiB
C
150 lines
3.5 KiB
C
/**
|
|
* Copyright (c) 2026 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "entityrenderable.h"
|
|
#include "entity/entitymanager.h"
|
|
#include "display/shader/shaderunlit.h"
|
|
#include "display/mesh/cube.h"
|
|
|
|
void entityRenderableInit(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->type = ENTITY_RENDERABLE_TYPE_MATERIAL;
|
|
r->mesh = &CUBE_MESH_SIMPLE;
|
|
r->shader = &SHADER_UNLIT;
|
|
r->material.unlit.color = COLOR_WHITE;
|
|
}
|
|
|
|
entityrenderabletype_t entityRenderableGetType(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
return r->type;
|
|
}
|
|
|
|
void entityRenderableSetType(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const entityrenderabletype_t type
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->type = type;
|
|
}
|
|
|
|
mesh_t * entityRenderableGetMesh(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
return r->mesh;
|
|
}
|
|
|
|
void entityRenderableSetMesh(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
mesh_t *mesh
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->mesh = mesh;
|
|
}
|
|
|
|
shader_t * entityRenderableGetShader(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
return r->shader;
|
|
}
|
|
|
|
void entityRenderableSetShader(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
shader_t *shader
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->shader = shader;
|
|
}
|
|
|
|
shadermaterial_t * entityRenderableGetShaderMaterial(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
return &r->material;
|
|
}
|
|
|
|
void entityRenderableSetColor(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const color_t color
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->material.unlit.color = color;
|
|
}
|
|
|
|
void entityRenderableSpriteBatchAdd(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId,
|
|
const spritebatchsprite_t *sprite
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
if(r->spritebatch.spriteCount >= ENTITY_RENDERABLE_SPRITEBATCH_SPRITES_MAX) return;
|
|
r->spritebatch.sprites[r->spritebatch.spriteCount++] = *sprite;
|
|
}
|
|
|
|
void entityRenderableSpriteBatchClear(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
r->spritebatch.spriteCount = 0;
|
|
}
|
|
|
|
void entityRenderableDispose(
|
|
const entityid_t entityId,
|
|
const componentid_t componentId
|
|
) {
|
|
entityrenderable_t *r = componentGetData(
|
|
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
|
);
|
|
if(
|
|
r->type == ENTITY_RENDERABLE_TYPE_CALLBACK &&
|
|
r->userFree &&
|
|
r->user
|
|
) {
|
|
r->userFree(r->user);
|
|
r->user = NULL;
|
|
}
|
|
r->mesh = NULL;
|
|
r->shader = NULL;
|
|
}
|