Cogs.Core
Scene.cpp
1#include "Scene.h"
2
3#include "Foundation/Logging/Logger.h"
4
5#include "Context.h"
6#include "Engine.h"
7#include "EntityStore.h"
8
9#include "Services/Variables.h"
10#include "Services/TaskManager.h"
11
12#include "Systems/Core/TransformSystem.h"
13#include "Systems/Core/CameraSystem.h"
14#include "Systems/Core/FogSystem.h"
15#include "Systems/Core/EnvironmentSystem.h"
16
17#include "Serialization/AssetReader.h"
18
19namespace
20{
21 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("Scene");
22}
23
24Cogs::Core::Scene::Scene(Context * context) : context(context)
25{
26
27}
28
29void Cogs::Core::Scene::setup(bool readDefaultScene)
30{
31 if (readDefaultScene && context->variables->get("noDefaultScene")->isEmpty()) {
32 Cogs::Core::readAsset(context, context->variables->get("defaultScene", "Scenes/Default.scene"), AssetLoadFlags::NoDefault);
33 }
34
35 EntityPtr camera = context->store->getEntity("Camera", false);
36
37 if (!camera) {
38 LOG_WARNING(logger, "Default camera missing. Creating fallback camera.");
39
40 camera = context->store->createEntity("Camera", "Camera");
41 }
42
43 ComponentHandle cameraComponent = camera->getComponentHandle<CameraComponent>();
44
45 if (!cameraComponent) {
46 LOG_ERROR(logger, "Default camera invalid. No CameraComponent. Adding fallback.");
47
48 cameraComponent = context->cameraSystem->createComponent();
49 camera->addComponent(cameraComponent);
50 }
51
52 if (!camera->getComponent<TransformComponent>()) {
53 LOG_ERROR(logger, "Default camera invalid. No TransformComponent. Adding fallback.");
54
55 camera->addComponent(context->transformSystem->createComponent());
56 }
57
58 context->cameraSystem->setMainCamera(cameraComponent);
59 context->getDefaultView()->setCamera(camera);
60
61 cameraComponent.resolveComponent<CameraComponent>()->viewportSize = context->getDefaultView()->getSize();
62
63 EntityPtr fog = context->store->getEntity("Fog");
64 ComponentHandle fogComponent;
65 if (fog) {
66 fogComponent = fog->getComponentHandle<FogComponent>();
67 if (!fogComponent) {
68 LOG_ERROR(logger, "Entity 'Fog' expected to have FogComponent.");
69 }
70 }
71
72 context->fogSystem->setGlobalFog(fogComponent);
73
74 EntityPtr env = context->store->getEntity("Environment");
75 ComponentHandle environmentComponent;
76 if (env) {
77 environmentComponent = env->getComponentHandle<EnvironmentComponent>();
78 if (!environmentComponent) {
79 LOG_ERROR(logger, "Entity 'Environment' expected to have EnvironmentComponent.");
80 }
81 }
82
83 context->environmentSystem->setGlobalEnvironment(environmentComponent);
84}
85
87{
88 sceneResources.clear();
89
90 context->taskManager->waitAll();
91
92 context->store->clear();
93
94 context->engine->clearResources();
95
96 context->engine->initializeResources();
97}
98
100{
101 sceneResources.emplace_back(resource);
102}
ComponentHandle createComponent() override
class EntityStore * store
Entity store.
Definition: Context.h:231
std::unique_ptr< class Variables > variables
Variables service instance.
Definition: Context.h:180
EntityPtr getEntity(const StringView &name, bool logIfNotFound=true) const
Retrieve a reference to the shared entity pointer to the Entity with the given name.
EntityPtr createEntity(const StringView &name, const StringView &type, bool storeOwnership=true)
Create a new Entity.
Contains data to describe fog.
Definition: FogComponent.h:17
void clear()
Clear the scene contents, emptying the entity store of top level entities.
Definition: Scene.cpp:86
void setup(bool readDefaultScene)
Setup default scene components.
Definition: Scene.cpp:29
void addResource(ResourceHandleBase &resource)
Adds a resource to be handled with the lifetime of the scene.
Definition: Scene.cpp:99
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
void setCamera(const Cogs::Core::EntityPtr &renderCamera)
Sets or updates the camera of this ViewContext instance.
Log implementation class.
Definition: LogManager.h:139
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
@ NoDefault
Don't load the default scene. Highly recommended as not setting this flag cause extra scene parse.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Handle to a Component instance.
Definition: Component.h:67
ComponentType * resolveComponent() const
Definition: Component.h:90
Resource handle base class handling reference counting of resources derived from ResourceBase.