Cogs.Core
TwinVisualsExtension.cpp
1#include "Context.h"
2#include "ExtensionRegistry.h"
3#include "ResourceManifest.h"
4#include "Resources/ResourceStore.h"
5#include "Serialization/EntityReader.h"
6
7#include "Renderer/Tasks/RenderTaskFactory.h"
8
9#include "../../TexAtlas/Source/TexAtlasSystem.h"
10
11#include "TwinVisualsComponent.h"
12#include "TwinVisualsSystem.h"
13#include "TwinCadModelComponent.h"
14#include "TwinCadModelSystem.h"
15#include "TwinVisualsTexAtlasRenderTask.h"
16
17namespace Cogs::Core {
18
19 static RenderTask* createTwinVisualsTexAtlasRenderTask(RenderTaskContext* renderTaskContext, const RenderTaskDefinition& renderTaskDefinition, const PipelineOptions& pipelineOptions)
20 {
21 TexAtlasSystem* texAtlasSystem = ExtensionRegistry::getExtensionSystem<TexAtlasSystem>(renderTaskContext->context);
22 TwinVisualsSystem* twinVisualsSystem = ExtensionRegistry::getExtensionSystem<TwinVisualsSystem>(renderTaskContext->context);
23 if (texAtlasSystem && twinVisualsSystem) {
24 return new TwinVisualsTexAtlasRenderTask(renderTaskContext, texAtlasSystem, twinVisualsSystem, renderTaskDefinition, pipelineOptions);
25 }
26 return nullptr;
27 }
28
29 static void destroyTwinVisualsTexAtlasRenderTask(RenderTask** task, RenderTaskContext*)
30 {
31 if (TwinVisualsTexAtlasRenderTask* myTask = dynamic_cast<TwinVisualsTexAtlasRenderTask*>(*task); myTask) {
32 delete myTask;
33 *task = nullptr;
34 }
35 }
36
38 {
40 ExtensionRegistry::add(this, COGS_CORE_VERSION_STRING);
41 }
42
43 bool initializeStatic() override;
44 bool initialize(Context* context) override;
45 void cleanup(Context* context) override;
46
47 const char* getExtensionKey() const override { return "TwinVisuals"; }
48 } twinVisualsExtension;
49
50}
51
52
54{
55 TwinVisualsComponent::registerType();
56 TwinCadModelComponent::registerType();
57 return true;
58}
59
61{
62 const std::string resourceArchive = "Cogs.Core.Extensions.TwinVisuals.zip";
63#ifdef __EMSCRIPTEN__
64 bool resourceArchiveAdded = false;
65 std::vector<std::string> manifest = getResourceManifest(context);
66 for (auto& item : manifest) {
67 if (item.find(resourceArchive) != std::string::npos) {
68 context->resourceStore->addResourceArchive(item);
69 resourceArchiveAdded = true;
70 break;
71 }
72 }
73 if (!resourceArchiveAdded) {
74 context->resourceStore->addResourceArchive(resourceArchive);
75 }
76#else
77 context->resourceStore->addSearchPath("../Extensions/TwinVisuals/Data/");
78 context->resourceStore->addResourceArchive(resourceArchive);
79#endif
80
81 readEntityDefinition(R"(
82{ "name": "TwinVisuals",
83 "components" : [
84 "TwinVisualsComponent"
85 ]
86})", context->store);
87
88 readEntityDefinition(R"(
89{ "name": "TwinCadModel",
90 "components": [
91 "TransformComponent",
92 "SceneComponent",
93 "AssetComponent",
94 "RenderComponent",
95 "TwinCadModelComponent"
96 ]
97})", context->store);
98
99 ExtensionRegistry::registerExtensionSystem<TwinVisualsSystem>(context, SystemPriority::PreRendering, 1);
100 ExtensionRegistry::registerExtensionSystem<TwinCadModelSystem>(context, SystemPriority::PreRendering, 1);
101 addTaskType("TwinVisualsTexAtlasRender", createTwinVisualsTexAtlasRenderTask, destroyTwinVisualsTexAtlasRenderTask);
102 return true;
103}
104
106{
107}
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
std::unique_ptr< class ResourceStore > resourceStore
ResourceStore service instance.
Definition: Context.h:210
static void add(Extension *extension, StringView version)
Adds the given extension to the registry, ensuring the initialization methods are called at appropria...
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Defines an extension to Cogs.Core and provides methods to override in order to initialize extension c...
@ PreRendering
Run before rendering is performed.
Definition: Engine.h:74
bool initializeStatic() override
Initialize extension statically.
const char * getExtensionKey() const override
Get the extensions unique key, used to check for extension presence and retrieve extension specific d...
bool initialize(Context *context) override
Initialize extension for the given context.
void cleanup(Context *context) override
Cleanup context bound extension content.