Cogs.Core
ComponentFunctions.cpp
1#include "ComponentFunctions.h"
2
3#include "Foundation/Logging/Logger.h"
4
5#include "Context.h"
6#include "Engine.h"
7#include "EntityStore.h"
8
9#include "Systems/Core/TransformSystem.h"
10#include "Systems/Core/SceneSystem.h"
11#include "Systems/Core/RenderSystem.h"
12#include "Systems/Core/CameraSystem.h"
13#include "Systems/Core/DynamicComponentSystem.h"
14
15using namespace Cogs::Core;
16
17namespace
18{
19 Cogs::Logging::Log logger = Cogs::Logging::getLogger("ComponentFunctions");
20
21 void entityError(const char* method, const EntityId entityId)
22 {
23 LOG_ERROR(logger, "%s: Invalid entity id %zd.", method, size_t(entityId));
24 }
25}
26
27void getLocalTransform(BridgeContext* ctx, EntityId entityId, float * data)
28{
29 auto context = static_cast<Context*>(ctx);
30 auto entity = context->store->getEntityPtr(entityId);
31 if (!entity) {
32 entityError("getLocalTransform", entityId);
33 return;
34 }
35
36 auto transformComponent = entity->getComponent<TransformComponent>();
37
38 if (!transformComponent) {
39 LOG_ERROR(logger, "Could not retrieve transform component for entity %zd.", entity->getId());
40 return;
41 }
42
43 context->transformSystem->updateLocalTransform(*transformComponent);
44
45 *reinterpret_cast<glm::mat4 *>(data) = context->transformSystem->getLocalTransform(transformComponent);
46}
47
48void getWorldTransform(BridgeContext* ctx, EntityId entityId, float * data)
49{
50 auto context = static_cast<Context*>(ctx);
51 auto entity = context->store->getEntityPtr(entityId);
52 if (!entity) {
53 entityError("getWorldTransform", entityId);
54 return;
55 }
56
57 auto transformComponent = entity->getComponent<TransformComponent>();
58
59 if (!transformComponent) {
60 LOG_ERROR(logger, "Could not retrieve transform component for entity %zd.", entity->getId());
61 return;
62 }
63
64 context->transformSystem->updateTransformData(*transformComponent);
65
66 *reinterpret_cast<glm::mat4 *>(data) = context->transformSystem->getLocalToWorld(transformComponent);
67}
68
69void getViewMatrix(BridgeContext* ctx, EntityId entityId, float * data)
70{
71 auto context = static_cast<Context*>(ctx);
72 auto entity = context->store->getEntityPtr(entityId);
73 if (!entity) {
74 entityError("getViewMatrix", entityId);
75 return;
76 }
77
78 auto cameraComponent = entity->getComponent<CameraComponent>();
79
80 if (!cameraComponent) {
81 LOG_ERROR(logger, "Could not retrieve camera component for entity %zd.", entity->getId());
82 return;
83 }
84
85 auto cameraData = &context->cameraSystem->getData(cameraComponent);
86
87 if (!cameraData) {
88 LOG_ERROR(logger, "Could not get camera data for entity %zd.", entity->getId());
89 return;
90 }
91
92 *reinterpret_cast<glm::mat4 *>(data) = cameraData->viewMatrix;
93}
94
95void getProjectionMatrix(BridgeContext* ctx, EntityId entityId, float * data)
96{
97 auto context = static_cast<Context*>(ctx);
98 auto entity = context->store->getEntityPtr(entityId);
99 if (!entity) {
100 entityError("getProjectionMatrix", entityId);
101 return;
102 }
103
104 auto cameraComponent = entity->getComponent<CameraComponent>();
105
106 if (!cameraComponent) {
107 LOG_ERROR(logger, "Could not retrieve camera component for entity %zd.", entity->getId());
108 return;
109 }
110
111 auto cameraData = &context->cameraSystem->getData(cameraComponent);
112
113 if (!cameraData) {
114 LOG_ERROR(logger, "Could not get camera data for entity %zd.", entity->getId());
115 return;
116 }
117
118 *reinterpret_cast<glm::mat4 *>(data) = cameraData->projectionMatrix;
119}
120
121CogsBool getVisibility(BridgeContext* ctx, EntityId entityId, const int mask)
122{
123 auto context = static_cast<Context*>(ctx);
124 auto entity = context->store->getEntityPtr(entityId);
125 if (!entity) {
126 entityError("getVisibility", entityId);
127 return false;
128 }
129
130 auto renderComponent = entity->getComponent<RenderComponent>();
131
132 if (!renderComponent) {
133 LOG_ERROR(logger, "Could not retrieve render component for entity %zd.", entity->getId());
134 return false;
135 }
136
137 return renderComponent->isVisibleInLayer((RenderLayers)mask);
138}
139
140void sendMessage(BridgeContext* ctx, EntityId entityId, const char * message, void * arg)
141{
142 auto context = static_cast<Context*>(ctx);
143 auto entity = context->store->getEntityPtr(entityId);
144 if (!entity) {
145 entityError("sendMessage", entityId);
146 return;
147 }
148
149 auto messageId = context->dynamicComponentSystem->getMessageId(Cogs::StringView(message));
150 context->dynamicComponentSystem->sendMessage(entity, messageId, arg);
151 context->engine->triggerUpdate();
152}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class EntityStore * store
Entity store.
Definition: Context.h:231
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
Base component for all rendering content.
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
Log implementation class.
Definition: LogManager.h:139
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
RenderLayers
Contains common render layers.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180