Cogs.Core
EntityDefinition.h
1#pragma once
2
3/*
4 * Include this header to access struct EntityDefinition and struct FieldValue.
5 * Common usage is to define custom entities in extensions.
6 *
7 * Alternative is defining the entity in JSON and adding to Store using Cogs::Core::readEntityDefinition.
8 */
9#include "Base.h"
10
11#include "ComponentFunctions.h"
12#include "Types.h"
13
14#include "Foundation/Reflection/Type.h"
15
16#include <glm/vec2.hpp>
17#include <glm/vec3.hpp>
18#include <glm/vec4.hpp>
19#include <glm/mat4x4.hpp>
20#include <glm/ext/quaternion_float.hpp>
21
22#include <vector>
23#include <functional>
24#include <span>
25#include <unordered_map>
26
27namespace Cogs
28{
29 namespace Core
30 {
35 {
36 Unknown = 0,
37 Bool,
38 Float,
39 Int,
40 Enum,
41 Vec2,
42 Vec3,
43 Vec4,
44 Mat4,
45 DVec2,
46 DVec3,
47 DVec4,
48 Quaternion,
50 Model,
51 Asset,
52 Mesh,
53 Texture,
54 Gui,
55 String,
56 MultiString,
57 IntArray,
58 FloatArray,
59 Vec2Array,
60 Vec3Array,
61 Vec4Array,
62 Entity,
63 EntityArray,
65 };
66
67 struct ResourceDefinition;
68 using MaterialInstanceDefinition = ResourceDefinition;
69
76 {
77 FieldValue() {}
78
79 FieldValue(const FieldValue& original) {
80 componentId = original.componentId;
81 fieldId = original.fieldId;
82 type = original.type;
83
84 std::memcpy(storage, original.storage, sizeof(storage));
85
86 value = original.value;
87 values = original.values;
88 intValues = original.intValues;
89 floatValues = original.floatValues;
90 vec3Values = original.vec3Values;
91 vec2Values = original.vec2Values;
92 vec4Values = original.vec4Values;
93 }
94
97
100
102 DefaultValueType type = DefaultValueType::Unknown;
103
104 union
105 {
106 bool boolValue;
107 float floatValue;
108 double doubleValue;
109 int intValue;
110 glm::vec2 float2;
111 glm::vec3 float3;
112 glm::vec4 float4;
113 glm::quat quat;
114 glm::dvec2 double2;
115 glm::dvec3 double3;
116 glm::dvec4 double4;
117 glm::mat4x4 mat4;
118#ifdef DEBUG
119 // Odd size because we must accommodate MaterialInstanceDefinition in debug mode.
120 char storage[512+32] = { 0 };
121#else
122 char storage[512] = { 0 };
123#endif
124 };
125
126 std::string value;
127 std::vector<std::string> values;
128 std::vector<int> intValues;
129 std::vector<float> floatValues;
130 std::vector<glm::vec3> vec3Values;
131 std::vector<glm::vec2> vec2Values;
132 std::vector<glm::vec4> vec4Values;
133
134 const MaterialInstanceDefinition * asMaterialInstance() const { return reinterpret_cast<const MaterialInstanceDefinition *>(storage); }
135
136 FieldValue& operator =(const FieldValue& rhs) {
137 componentId = rhs.componentId;
138 fieldId = rhs.fieldId;
139 type = rhs.type;
140
141 std::memcpy(storage, rhs.storage, sizeof(storage));
142
143 value = rhs.value;
144 values = rhs.values;
145 intValues = rhs.intValues;
146 floatValues = rhs.floatValues;
147 vec3Values = rhs.vec3Values;
148 vec2Values = rhs.vec2Values;
149 vec4Values = rhs.vec4Values;
150
151 return *this;
152 }
153 };
154
160 {
161 EntityDefinition() = default;
162
165 std::string name;
166
168 std::string description;
169
171 std::vector<std::string> components;
172
175 bool compiled = false;
176
179 std::vector<ComponentCreator *> creators;
180
182 std::vector<FieldValue> defaultValues;
183 };
184
191 void applyFieldValues(class Context * context, std::span<FieldValue> entityDefinition, ComponentModel::Entity * entity);
192
193 struct MaterialInstance;
194
198 void updateMaterialInstance(Context * context, MaterialInstance * materialInstance, const MaterialInstanceDefinition & value);
199 }
200}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
void updateMaterialInstance(Context *context, MaterialInstance *materialInstance, const MaterialInstanceDefinition &materialInstanceValue)
Apply material instance values.
void applyFieldValues(class Context *context, std::span< FieldValue > entityDefinition, ComponentModel::Entity *entity)
Apply defaults from the given entityDefinition to the given entity.
DefaultValueType
Defines value types for default values.
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
constexpr FieldId NoField
No field id.
Definition: Name.h:60
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Defines how to construct entities of a certain type by a list of components to instantiate and defaul...
std::string description
Description of the entity. For in-line documentation.
std::vector< ComponentCreator * > creators
std::vector< std::string > components
Names of the component types to instantiate when creating an entity from this definition.
std::vector< FieldValue > defaultValues
Set of default values to apply after constructing entities from this definition.
Defines an extension to Cogs.Core and provides methods to override in order to initialize extension c...
Defines a value to apply to a field.
Reflection::TypeId componentId
Type id of the component the value should be applied to.
Reflection::FieldId fieldId
Field id of the field in the given component the value should be applied to.
DefaultValueType type
Type of the field.
Material instances represent a specialized Material combined with state for all its buffers and prope...
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
Definition: Mesh.h:265
Model resources define a template for a set of connected entities, with resources such as meshes,...
Definition: Model.h:56
Texture resources contain raster bitmap data to use for texturing.
Definition: Texture.h:91