Cogs.Core
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Cogs::Core::Engine Class Reference

The engine owns all the systems and resource managers, and is responsible for executing different stages of updates in the correct order. More...

#include <Engine.h>

Classes

struct  SystemFlags
 

Public Member Functions

 Engine (Context *context)
 Constructs a new engine instance, living in the given Context.
 
 Engine (const Engine &other)=delete
 Disable copying engine instances.
 
 ~Engine ()
 Destructs the engine instance.
 
Engineoperator= (const Engine &other)=delete
 Disable copy assignment of engine instances.
 
void update ()
 Update the engine, advancing the system state a single frame.
 
void invokeCallback (void(*callback)(void *), void *data)
 Generic callback invoke wrapper for callbacks with just a data-wrapper, proxied from emscripten worker threads.
 
void invokeComponentNotifyCallback (const ComponentModel::Component &component, int notification, const void *data, size_t dataSize)
 Invoke the componentNotifyCallback if it exists, proxied from emscripten worker threads.
 
void invokeResourceLoadCallback (int resourceType, ResourceId id, int code)
 Invoke the resourceLoadCallback if it exists, proxied from emscripten worker threads.
 
bool needsUpdate () const
 Gets if the engine is in need of an update.
 
void triggerUpdate ()
 Triggers an engine update.
 
void setDirty ()
 Triggers an engine update and updates last dirty frame to the current frame.
 
uint32_t getLastDirtyFrame ()
 Get the current frame at the last time setDirty was called.
 
void setNeedsUpdateCallback (NeedsUpdateCallback *callback, void *data)
 Invoked when update state goes from clean to dirty.
 
void updateSystems ()
 Performs updates of all the systems.
 
void registerSystem (ComponentSystemBase *system, int priority, bool registerInStore=true)
 Register the given component system in the engine with the given priority.
 
template<typename SystemType >
SystemType * registerSystem (int priority, SystemType **storage, uint32_t capacity, bool registerInStore=true)
 Utility function for instantiating a system of the given template type and assigning ownership to the engine.
 
void addResourceManager (std::unique_ptr< IResourceManager > &&resourceManager)
 Add the given resource manager to the engine.
 
IValueTypeManagergetResourceManagerByValueType (int valueType)
 
void initializeResources ()
 Initializes all resource managers and default resources.
 
void clearResources ()
 Clears all resource managers pending reinitialization of resources.
 
void runTaskInMainThread (std::function< void()> &&task)
 
void preRender ()
 
void render ()
 
void postRender ()
 
void swapResources ()
 
bool workParallel () const
 True if it is worthwhile to parallelize work.
 
bool isReady () const
 
void checkAndUpdateResources ()
 
void setEditor (class IEditor *editor)
 
class IEditorgetEditor () const
 

Private Member Functions

void setDirtyState ()
 

Private Attributes

Contextcontext = nullptr
 
NeedsUpdateCallback * needsUpdateCallback = nullptr
 
void * needsUpdateData = nullptr
 
std::vector< std::string > resourceManifest
 
std::unique_ptr< class IEditoreditor
 
uint32_t systemFlags = SystemFlags::None
 
uint32_t lastDirtyFrame = 0
 
std::atomic_bool updateRequested = true
 
bool initializing_device = false
 
bool ready = false
 
bool firstInit = true
 
bool workParallelValue = true
 
std::vector< std::unique_ptr< IResourceManager > > resourceManagers
 
std::unique_ptr< struct EngineDatadata
 

Detailed Description

The engine owns all the systems and resource managers, and is responsible for executing different stages of updates in the correct order.

When all systems and resource managers are done updating, rendering will be initiated by notifying the renderer.

The entity store, keeping all top level entities in the running instance, is also owned by the engine.

Definition at line 87 of file Engine.h.

Constructor & Destructor Documentation

◆ Engine()

Cogs::Core::Engine::Engine ( Context context)

Constructs a new engine instance, living in the given Context.

The engine will perform full initialization of all core systems and resource managers, leaving the system ready to start running updates without rendering.

Initialization is performed as follows:

  • The entity store is created and initialized
  • The renderer is created
  • All entity definitions and templates are initialized
  • Resource managers are created
  • Resource managers are initialized
  • Core systems are created

Note: Systems are not initialized until after initializeRendering() is called.

Parameters
contextContext instance the engine instance exists in.

Definition at line 83 of file Engine.cpp.

◆ ~Engine()

Cogs::Core::Engine::~Engine ( )

Destructs the engine instance.

The destruction is performed as follows:

  • Wait for all running tasks to finish
  • Wait for the entity store to clear out all alive entity instances
  • Clear all component systems of internal data
  • Delete all the component systems
  • Clear all resource managers, releasing all data held
  • Process deletion for all resource managers, ensuring any orphaned resources are cleaned up
  • Delete all the resource managers
  • Tell the renderer to clean up and delete itself
  • Delete the renderer
  • Delete the entity store

Definition at line 94 of file Engine.cpp.

References Cogs::Core::ExtensionRegistry::cleanup(), Cogs::Core::Context::clear(), Cogs::Core::Context::memory, Cogs::Core::ExtensionRegistry::remove(), Cogs::Core::Context::resourceUsageLogger, Cogs::Core::Context::taskManager, and Cogs::Core::Context::variables.

Member Function Documentation

◆ addResourceManager()

void Cogs::Core::Engine::addResourceManager ( std::unique_ptr< IResourceManager > &&  resourceManager)

Add the given resource manager to the engine.

The engine assumes ownership over the resource manager instance, destroying it when the engine instance is destructed.

Parameters
resourceManagerUnique pointer to a resource manager.

Definition at line 353 of file Engine.cpp.

◆ checkAndUpdateResources()

void Cogs::Core::Engine::checkAndUpdateResources ( )

Definition at line 273 of file Engine.cpp.

◆ clearResources()

void Cogs::Core::Engine::clearResources ( )

Clears all resource managers pending reinitialization of resources.

Definition at line 563 of file Engine.cpp.

◆ getEditor()

class IEditor * Cogs::Core::Engine::getEditor ( ) const
inline

Definition at line 320 of file Engine.h.

◆ getLastDirtyFrame()

uint32_t Cogs::Core::Engine::getLastDirtyFrame ( )
inline

Get the current frame at the last time setDirty was called.

Useful to determine frames since state changed.

Definition at line 211 of file Engine.h.

◆ getResourceManagerByValueType()

Cogs::Core::IValueTypeManager * Cogs::Core::Engine::getResourceManagerByValueType ( int  valueType)

Definition at line 577 of file Engine.cpp.

◆ initializeResources()

void Cogs::Core::Engine::initializeResources ( )

Initializes all resource managers and default resources.

Definition at line 556 of file Engine.cpp.

◆ invokeCallback()

void Cogs::Core::Engine::invokeCallback ( void(*)(void *)  callback,
void *  data 
)
inline

Generic callback invoke wrapper for callbacks with just a data-wrapper, proxied from emscripten worker threads.

Definition at line 160 of file Engine.h.

◆ invokeComponentNotifyCallback()

void Cogs::Core::Engine::invokeComponentNotifyCallback ( const ComponentModel::Component component,
int  notification,
const void *  data,
size_t  dataSize 
)
inline

Invoke the componentNotifyCallback if it exists, proxied from emscripten worker threads.

Definition at line 163 of file Engine.h.

References Cogs::ComponentModel::Component::getContainer(), Cogs::ComponentModel::Entity::getId(), and Cogs::ComponentModel::Component::getTypeId().

◆ invokeResourceLoadCallback()

void Cogs::Core::Engine::invokeResourceLoadCallback ( int  resourceType,
ResourceId  id,
int  code 
)
inline

Invoke the resourceLoadCallback if it exists, proxied from emscripten worker threads.

Definition at line 170 of file Engine.h.

◆ isReady()

bool Cogs::Core::Engine::isReady ( ) const
inline

Definition at line 315 of file Engine.h.

◆ needsUpdate()

bool Cogs::Core::Engine::needsUpdate ( ) const
inline

Gets if the engine is in need of an update.

Definition at line 180 of file Engine.h.

◆ postRender()

void Cogs::Core::Engine::postRender ( )

Definition at line 538 of file Engine.cpp.

◆ preRender()

void Cogs::Core::Engine::preRender ( )

Definition at line 486 of file Engine.cpp.

◆ registerSystem() [1/2]

void Cogs::Core::Engine::registerSystem ( ComponentSystemBase system,
int  priority,
bool  registerInStore = true 
)

Register the given component system in the engine with the given priority.

The engine assumes ownership over lifetime of the component system, and will perform cleanup and destruction of the system when the engine instance is destructed.

Parameters
systemUnique pointer to a component system based on ComponentSystemBase. Ownership of the pointer will be assumed by the engine.
priorityAn integer stating a priority value for when the system should be run in different update phases. Increasing priority values will move the component system later in the execution queue.
registerInStoreIf the system should be registered in the entity store as candidate to create components of the systems type.
See also
SystemPriority.

Definition at line 543 of file Engine.cpp.

References Cogs::Core::EntityStore::addSystem(), and Cogs::Core::Context::store.

◆ registerSystem() [2/2]

template<typename SystemType >
SystemType * Cogs::Core::Engine::registerSystem ( int  priority,
SystemType **  storage,
uint32_t  capacity,
bool  registerInStore = true 
)
inline

Utility function for instantiating a system of the given template type and assigning ownership to the engine.

Template Parameters
SystemTypeType of component system to instantiate. Must inherit from ComponentSystemBase.
Parameters
priorityInteger priority for ordering system updates.
storagePointer to a pointer where a non-owning pointer to the system should be stored.
registerInStoreIf the system should be automatically registered in the entity store, and creator and destroyer functions for the component type created.

Definition at line 264 of file Engine.h.

References Cogs::Core::Context::memory.

◆ render()

void Cogs::Core::Engine::render ( )

Definition at line 531 of file Engine.cpp.

◆ runTaskInMainThread()

void Cogs::Core::Engine::runTaskInMainThread ( std::function< void()> &&  task)

Definition at line 151 of file Engine.cpp.

◆ setDirty()

void Cogs::Core::Engine::setDirty ( )
inline

Triggers an engine update and updates last dirty frame to the current frame.

Call this to invalidate state and get a full new rendering

Definition at line 203 of file Engine.h.

◆ setDirtyState()

void Cogs::Core::Engine::setDirtyState ( )
private

Definition at line 267 of file Engine.cpp.

◆ setEditor()

void Cogs::Core::Engine::setEditor ( class IEditor editor)

Definition at line 351 of file Engine.cpp.

◆ setNeedsUpdateCallback()

void Cogs::Core::Engine::setNeedsUpdateCallback ( NeedsUpdateCallback *  callback,
void *  data 
)
inline

Invoked when update state goes from clean to dirty.

Definition at line 214 of file Engine.h.

◆ swapResources()

void Cogs::Core::Engine::swapResources ( )

Definition at line 521 of file Engine.cpp.

◆ triggerUpdate()

void Cogs::Core::Engine::triggerUpdate ( )
inline

Triggers an engine update.

If the engine is in the middle of a current update, an additional update will be performed following completion. Call this to trigger just a new engine run, e.g. where rendering is spread over multiple frames. When state has changed, setDirty should be called to trigger a full rendering.

Definition at line 191 of file Engine.h.

◆ update()

void Cogs::Core::Engine::update ( )

Update the engine, advancing the system state a single frame.

During the update all systems will have their different phases of updates executed in the order given by the different systems priorities.

All resource managers are updated, processing queued resources, resources ready for loading, and deleted or orphaned resources.

Finally, the renderer is invoked, producing a single frame of output.

Definition at line 358 of file Engine.cpp.

References Cogs::Core::IRenderer::beginFrame(), Cogs::Core::IRenderer::endFrame(), Cogs::Core::Context::qualityService, Cogs::Core::Context::renderer, Cogs::Core::Context::resourceUsageLogger, and Cogs::Core::Context::variables.

◆ updateSystems()

void Cogs::Core::Engine::updateSystems ( )

Performs updates of all the systems.

Updates are executed according to the order given by the component systems priorities.

The different phases of update are executed for all systems before moving to the next phase;

  • Run preUpdate on all systems
  • Run update on all systems
  • Run postUpdate on all systems

This method is executed automatically by calling update() for a given frame, so it is not needed to invoke this manually. However, it can be used to ensure state is updated before out-of rendering queries such as bounding box queries, requiring that all geometry has been updated before valid results can be had.

Definition at line 442 of file Engine.cpp.

◆ workParallel()

bool Cogs::Core::Engine::workParallel ( ) const
inline

True if it is worthwhile to parallelize work.

Definition at line 313 of file Engine.h.

Member Data Documentation

◆ context

Context* Cogs::Core::Engine::context = nullptr
private

Definition at line 327 of file Engine.h.

◆ data

std::unique_ptr<struct EngineData> Cogs::Core::Engine::data
private

Definition at line 359 of file Engine.h.

◆ editor

std::unique_ptr<class IEditor> Cogs::Core::Engine::editor
private

Definition at line 334 of file Engine.h.

◆ firstInit

bool Cogs::Core::Engine::firstInit = true
private

Definition at line 354 of file Engine.h.

◆ initializing_device

bool Cogs::Core::Engine::initializing_device = false
private

Definition at line 352 of file Engine.h.

◆ lastDirtyFrame

uint32_t Cogs::Core::Engine::lastDirtyFrame = 0
private

Definition at line 347 of file Engine.h.

◆ needsUpdateCallback

NeedsUpdateCallback* Cogs::Core::Engine::needsUpdateCallback = nullptr
private

Definition at line 329 of file Engine.h.

◆ needsUpdateData

void* Cogs::Core::Engine::needsUpdateData = nullptr
private

Definition at line 330 of file Engine.h.

◆ ready

bool Cogs::Core::Engine::ready = false
private

Definition at line 353 of file Engine.h.

◆ resourceManagers

std::vector<std::unique_ptr<IResourceManager> > Cogs::Core::Engine::resourceManagers
private

Definition at line 357 of file Engine.h.

◆ resourceManifest

std::vector<std::string> Cogs::Core::Engine::resourceManifest
private

Definition at line 332 of file Engine.h.

◆ systemFlags

uint32_t Cogs::Core::Engine::systemFlags = SystemFlags::None
private

Definition at line 345 of file Engine.h.

◆ updateRequested

std::atomic_bool Cogs::Core::Engine::updateRequested = true
private

Definition at line 348 of file Engine.h.

◆ workParallelValue

bool Cogs::Core::Engine::workParallelValue = true
private

Definition at line 355 of file Engine.h.


The documentation for this class was generated from the following files: