Cogs.Core
ModelSystem.h
1#pragma once
2
3#include "Systems/ComponentSystem.h"
4
5#include "Components/Core/ModelComponent.h"
6
7#include "Context.h"
8
9#include <map>
10
11#include "DynamicComponentSystem.h"
12
13namespace Cogs
14{
15 namespace Core
16 {
17 typedef std::function<void(Context*, EntityId)> HierarchyChangedCallback;
18 class Context;
19
21 {
22 public:
23 ModelData() = default;
24 ModelData(const ModelData & other) = delete;
25
26 void setLoaded() { modelState |= 1; }
27 bool isLoaded() const { return modelState != 0; }
28
29 // Helper flag used when tracking progress in subresource (mesh) loading
30 void setResourcesLoaded() { modelState |= 2; }
31 bool isResourcesLoaded() const { return modelState & 2; }
32 void clearResourcesLoaded() { modelState = modelState & ~2; }
33
34 void setGeneration(uint8_t generation) { this->generation = generation; }
35 bool isGeneration(uint8_t generation) const { return this->generation == generation; }
36
37 ResourceId getResourceId() { return resourceId; };
38 void setResourceId(ResourceId rid) { resourceId = rid; };
39 MaterialInstanceHandle masterMaterial;
40 std::vector<MaterialInstanceHandle> materials;
41
42 private:
43 uint8_t modelState = 0;
44 uint8_t generation = 0;
45
46 ResourceId resourceId = NoResourceId;
47 };
48
49 class COGSCORE_DLL_API ModelSystem : public ComponentSystemWithDataPools<ModelComponent, ModelData>
50 {
51 public:
52 ModelSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPools(allocator, capacity)
53 {
54 addHierarchyCallback([](Context * context, EntityId id) {
55 if (context->callbacks.hierarchyChangeCallback) {
56 context->callbacks.hierarchyChangeCallback(context, id);
57 }
58 });
59 }
60
61 void initialize(Context * context) override;
62 void update(Context * context) override;
63 void postUpdate(Context * context) override;
64
65 int addHierarchyCallback(HierarchyChangedCallback callback);
66 void removeHierarchyCallback(int id);
67
73 float getLoadProgress(ModelComponent* modelComponent);
74
75 private:
76 void loadModel(Context * context, const ModelComponent & component, ModelData & modelData, Model * model);
77 void clearModel(Context * context, const ModelComponent & component);
78
79 int nextId = 0;
80 std::map<int, HierarchyChangedCallback> callbacks;
81 MessageId hierarchyUpdatedMessage = NoMessage;
82 };
83 }
84}
Component system template with multiple parallel structures per component stored in separate pools si...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Base allocator implementation.
Definition: Allocator.h:30
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Contains a model reference to instance as children to the entity the ModelComponent belongs to.
Model resources define a template for a set of connected entities, with resources such as meshes,...
Definition: Model.h:56