4#include "MemoryContext.h"
6#include "Services/TaskManager.h"
8#include "Foundation/Memory/Allocator.h"
16 class IGraphicsDevice;
22 class ComponentSystemBase;
23 class IResourceManager;
24 class IValueTypeManager;
26 typedef void NeedsUpdateCallback(
void* data);
152#if defined(EMSCRIPTEN) && !defined(COGS_SINGLETHREADED)
153 void invokeCallback(
void(*callback)(
void*),
void* data);
155 void invokeComponentNotifyCallback(
const ComponentModel::Component& component,
int notification,
const void* data,
size_t dataSize);
157 void invokeResourceLoadCallback(
int resourceType, ResourceId
id,
int code);
160 void invokeCallback(
void(*callback)(
void*),
void* data) {
if (callback) { callback(data); } }
164 if (context->callbacks.componentNotifyCallback) {
165 context->callbacks.componentNotifyCallback(context, component.
getTypeId(), component.
getContainer()->
getId(), notification, data, dataSize);
171 if (context->callbacks.resourceLoadCallback) {
172 context->callbacks.resourceLoadCallback(context, resourceType,
id, code);
182 return updateRequested;
193 bool wasRequested = updateRequested.exchange(
true);
194 if ((wasRequested ==
false) && needsUpdateCallback) {
195 invokeCallback(needsUpdateCallback, needsUpdateData);
205 if (!updateRequested) setDirtyState();
214 void setNeedsUpdateCallback(NeedsUpdateCallback* callback,
void* data) { needsUpdateCallback = callback; needsUpdateData = data; }
230 void updateSystems();
250 void registerSystem(
ComponentSystemBase * system,
int priority,
bool registerInStore =
true);
263 template<
typename SystemType>
264 SystemType *
registerSystem(
int priority, SystemType ** storage, uint32_t capacity,
bool registerInStore =
true)
266 auto system = Memory::create<SystemType>(
267 context->
memory->baseAllocator,
268 context->
memory->componentAllocator,
275 registerSystem(system, priority, registerInStore);
288 void addResourceManager(std::unique_ptr<IResourceManager> && resourceManager);
295 void initializeResources();
300 void clearResources();
302 void runTaskInMainThread(std::function<
void()>&& task);
308 void swapResources();
315 bool isReady()
const {
return ready; }
317 void checkAndUpdateResources();
319 void setEditor(
class IEditor * editor);
320 class IEditor * getEditor()
const {
return editor.get(); }
325 void setDirtyState();
327 Context * context =
nullptr;
329 NeedsUpdateCallback* needsUpdateCallback =
nullptr;
330 void* needsUpdateData =
nullptr;
332 std::vector<std::string> resourceManifest;
334 std::unique_ptr<class IEditor> editor;
345 uint32_t systemFlags = SystemFlags::None;
347 uint32_t lastDirtyFrame = 0;
348 std::atomic_bool updateRequested =
true;
352 bool initializing_device =
false;
354 bool firstInit =
true;
355 bool workParallelValue =
true;
357 std::vector<std::unique_ptr<IResourceManager>> resourceManagers;
359 std::unique_ptr<struct EngineData> data;
Base class for Component instances.
class Entity * getContainer() const
Get the container currently owning this component instance.
constexpr Reflection::TypeId getTypeId() const
Get the Reflection::TypeId of the component.
constexpr size_t getId() const noexcept
Get the unique identifier of this entity.
Base class for component systems.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< struct MemoryContext > memory
Memory and allocation info.
The engine owns all the systems and resource managers, and is responsible for executing different sta...
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...
void invokeResourceLoadCallback(int resourceType, ResourceId id, int code)
Invoke the resourceLoadCallback if it exists, proxied from emscripten worker threads.
void setNeedsUpdateCallback(NeedsUpdateCallback *callback, void *data)
Invoked when update state goes from clean to dirty.
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 invokeCallback(void(*callback)(void *), void *data)
Generic callback invoke wrapper for callbacks with just a data-wrapper, proxied from emscripten worke...
void triggerUpdate()
Triggers an engine update.
Engine & operator=(const Engine &other)=delete
Disable copy assignment of engine instances.
bool workParallel() const
True if it is worthwhile to parallelize work.
bool needsUpdate() const
Gets if the engine is in need of an update.
Engine(const Engine &other)=delete
Disable copying engine instances.
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.
Contains all Cogs related functionality.
Contains system priority enumeration.
ESystemPriority
Defines priority values for systems.
@ DispatchViewDependent
Quickly dispatch view dependent async tasks before serial view dependent tasks.
@ PreTransform
Run before transformations are updated.
@ Transform
Run at the time of transformations.
@ PostGeometry
Run after geometry updates are performed, e.g to perform calculations on vertex data.
@ PreView
Run after transformations, but before view data is updated.
@ DynamicComponents
Run at the same time as the dynamic components.
@ PreRendering
Run before rendering is performed.
@ PostView
Run after view data has been updated. Anything after this is appropriate for geometry depending on e....
@ PreDynamicComponents
Run before the dynamic components.
@ View
Run at the time view data is updated.
@ LightView
Run at the time light views are updated.
@ Geometry
Run at the time geometry data is updated.
@ PreLightView
Run after view, but before the time light views are updated.
@ PostTransform
Run immediately after transformations are updated.
@ Rendering
Run at the time rendering is performed.
@ PreGeometry
Run before geometry updates are performed.