Nameless Engine
|
#include <GameManager.h>
Public Member Functions | |
GameManager (const GameManager &)=delete | |
GameManager & | operator= (const GameManager &)=delete |
void | setGarbageCollectorRunInterval (long long iGcRunIntervalInSec) |
void | queueGarbageCollection (bool bForce, const std::optional< std::function< void()> > &onFinished={}) |
void | addDeferredTask (const std::function< void()> &task) |
void | addTaskToThreadPool (const std::function< void()> &task) |
void | createWorld (const std::function< void(const std::optional< Error > &)> &onCreated, size_t iWorldSize=Globals::getDefaultWorldSize()) |
void | loadNodeTreeAsWorld (const std::function< void(const std::optional< Error > &)> &onLoaded, const std::filesystem::path &pathToNodeTree, size_t iWorldSize=Globals::getDefaultWorldSize()) |
sgc::GcPtr< Node > | getWorldRootNode () |
float | getWorldTimeInSeconds () |
size_t | getWorldSize () |
size_t | getTotalSpawnedNodeCount () |
size_t | getCalledEveryFrameNodeCount () |
Window * | getWindow () const |
GameInstance * | getGameInstance () const |
CameraManager * | getCameraManager () const |
float | getTimeSincePrevFrameInSec () const |
long long | getGarbageCollectorRunIntervalInSec () const |
bool | isNodeSpawned (size_t iNodeId) |
bool | isBeingDestroyed () const |
Static Public Member Functions | |
static GameManager * | get () |
Private Member Functions | |
GameManager (Window *pWindow) | |
std::optional< Error > | initialize (std::optional< RendererType > preferredRenderer) |
void | destroy () |
template<typename MyGameInstance > requires std::derived_from<MyGameInstance, GameInstance> | |
void | setGameInstance () |
void | onGameStarted () |
void | onBeforeNewFrame (float timeSincePrevCallInSec) |
void | onKeyboardInput (KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) |
void | onMouseInput (MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) |
void | onMouseMove (double xOffset, double yOffset) |
void | onMouseScrollMove (int iOffset) |
void | onWindowFocusChanged (bool bIsFocused) const |
void | onFramebufferSizeChanged (int iWidth, int iHeight) const |
void | onWindowClose () const |
void | onTickFinished () |
void | runGarbageCollection (bool bForce=false) |
void | executeDeferredTasks () |
void | triggerActionEvents (std::variant< KeyboardKey, MouseButton > key, KeyboardModifiers modifiers, bool bIsPressedDown) |
void | triggerAxisEvents (KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) |
void | destroyAndCleanExistingWorld () |
Private Attributes | |
Window * | pWindow |
std::unique_ptr< GameInstance > | pGameInstance |
std::pair< std::recursive_mutex, std::unique_ptr< World > > | mtxWorld |
std::unique_ptr< Renderer > | pRenderer |
std::unique_ptr< CameraManager > | pCameraManager |
ThreadPool | threadPool |
std::pair< std::recursive_mutex, std::queue< std::function< void()> > > | mtxDeferredTasks |
InputManager | inputManager |
std::chrono::steady_clock::time_point | lastGcRunTime |
float | timeSincePrevFrameInSec = 0.0F |
long long | iGcRunIntervalInSec = 120 |
std::thread::id | mainThreadId |
bool | bShouldAcceptNewDeferredTasks = true |
bool | bIsInitialized = false |
bool | bIsBeingDestroyed = false |
Static Private Attributes | |
static const char * | sGcLeakReasons |
Friends | |
class | Window |
Controls main game objects: game instance, input manager, renderer, audio engine, physics engine and etc.
|
private |
Creates uninitialized GameManager.
pWindow | Window that owns this object. |
void ne::GameManager::addDeferredTask | ( | const std::function< void()> & | task | ) |
Adds a function to be executed on the main thread next time onBeforeNewFrame is called.
gc
pointers in std::function
, this is not supported and will cause memory leaks/crashes!task | Function to execute. |
void ne::GameManager::addTaskToThreadPool | ( | const std::function< void()> & | task | ) |
Adds a function to be executed on the thread pool.
gc
pointers in std::function
, this is not supported and will cause memory leaks/crashes!task | Function to execute. |
void ne::GameManager::createWorld | ( | const std::function< void(const std::optional< Error > &)> & | onCreated, |
size_t | iWorldSize = Globals::getDefaultWorldSize() |
||
) |
Adds a deferred task (see addDeferredTask) to create a new world that contains only one node - root node.
create/load world task
the engine will finish all other tasks and only when deferred tasks queue is empty start to create/load world so you don't need to care about the order of deferred tasks.onCreated | Callback function that will be called on the main thread after the world is created. Contains optional error (if world creation failed) as the only argument. Use GameInstance member functions as callback functions for created worlds, because all nodes and other game objects will be destroyed while the world is changing. |
iWorldSize | Size of the new world in game units. Must be power of 2 (128, 256, 512, 1024, 2048, etc.). World size needs to be specified for internal purposes such as Directional Light shadow map size. You don't need to care why we need this information, you only need to know that if you leave world bounds lighting or physics may be incorrect (the editor or engine will warn you if something is leaving world bounds, pay attention to the logs). |
|
private |
Contains destructor logic: runs GC for the last time, destroys game instance, etc.
|
private |
Destroys the current world (if exists) and runs GC to clean everything up.
|
private |
Executes all deferred tasks from mtxDeferredTasks.
|
static |
Returns the last created game manager object.
nullptr
use isBeingDestroyed.nullptr
if no game manager object was created yet, otherwise pointer to game manager object. size_t ne::GameManager::getCalledEveryFrameNodeCount | ( | ) |
Returns the current amount of spawned nodes that are marked as "should be called every frame".
CameraManager * ne::GameManager::getCameraManager | ( | ) | const |
Returns camera manager.
GameInstance * ne::GameManager::getGameInstance | ( | ) | const |
Returns game instance that this game is using (Game owns GameInstance).
long long ne::GameManager::getGarbageCollectorRunIntervalInSec | ( | ) | const |
Returns the current interval after which we need to run garbage collector again.
float ne::GameManager::getTimeSincePrevFrameInSec | ( | ) | const |
Returns the last time value that was passed to onBeforeNewFrame function(s).
size_t ne::GameManager::getTotalSpawnedNodeCount | ( | ) |
Returns total amount of currently spawned nodes.
Window * ne::GameManager::getWindow | ( | ) | const |
Returns window that owns this object.
sgc::GcPtr< Node > ne::GameManager::getWorldRootNode | ( | ) |
Returns a pointer to world's root node.
size_t ne::GameManager::getWorldSize | ( | ) |
Returns world size in game units.
float ne::GameManager::getWorldTimeInSeconds | ( | ) |
Returns time since world creation (in seconds).
|
private |
Initializes the manager.
preferredRenderer | Preferred renderer to be used. |
bool ne::GameManager::isBeingDestroyed | ( | ) | const |
Tells whether Game's destruction process was started or not.
bool ne::GameManager::isNodeSpawned | ( | size_t | iNodeId | ) |
Tells if a node with the specified ID is currently spawned or not.
iNodeId | ID of the node to check. |
true
if the node is spawned, false
otherwise. void ne::GameManager::loadNodeTreeAsWorld | ( | const std::function< void(const std::optional< Error > &)> & | onLoaded, |
const std::filesystem::path & | pathToNodeTree, | ||
size_t | iWorldSize = Globals::getDefaultWorldSize() |
||
) |
Adds a deferred task (see addDeferredTask) to load and deserialize a node tree to be used as the new world.
Node tree's root node will be used as world's root node.
create/load world task
the engine will finish all other tasks and only when deferred tasks queue is empty start to create/load world so you don't need to care about the order of deferred tasks.onLoaded | Callback function that will be called on the main thread after the world is loaded. Contains optional error (if world loading failed) as the only argument. Use GameInstance member functions as callback functions for loaded worlds, because all nodes and other game objects will be destroyed while the world is changing. |
pathToNodeTree | Path to the file that contains a node tree to load, the ".toml" extension will be automatically added if not specified. |
iWorldSize | Size of the world in game units. Must be power of 2 (128, 256, 512, 1024, 2048, etc.). World size needs to be specified for internal purposes such as Directional Light shadow map size. You don't need to care why we need this information, you only need to know that if you leave world bounds lighting or physics may be incorrect (the editor or engine will warn you if something is leaving world bounds, pay attention to the logs). |
|
private |
Called before a new frame is rendered.
timeSincePrevCallInSec | Time in seconds that has passed since the last call to this function. |
|
private |
Called when the framebuffer size was changed.
iWidth | New width of the framebuffer (in pixels). |
iHeight | New height of the framebuffer (in pixels). |
|
private |
Called by owner Window to notify game instance about game being started (everything is set up).
|
private |
Called when the window (that owns this object) receives keyboard input.
key | Keyboard key. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the key down event occurred or key up. |
|
private |
Called when the window (that owns this object) receives mouse input.
button | Mouse button. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the button down event occurred or button up. |
|
private |
Called when the window received mouse movement.
xOffset | Mouse X movement delta in pixels (plus if moved to the right, minus if moved to the left). |
yOffset | Mouse Y movement delta in pixels (plus if moved up, minus if moved down). |
|
private |
Called when the window receives mouse scroll movement.
iOffset | Movement offset. |
|
private |
Called by the owner when a tick is fully finished.
|
private |
Called when a window that owns this game instance was requested to close (no new frames will be rendered). Prefer to do your destructor logic here.
|
private |
Called when the window focus was changed.
bIsFocused | Whether the window has gained or lost the focus. |
void ne::GameManager::queueGarbageCollection | ( | bool | bForce, |
const std::optional< std::function< void()> > & | onFinished = {} |
||
) |
Queues a request to run a garbage collection as a deferred task on the main thread using addDeferredTask.
bForce | Force run garbage collection even if the last garbage collection was run not so long ago. |
onFinished | Optional callback that will be triggered on the main thread when garbage collection is finished (queued as addDeferredTask). |
|
private |
Runs garbage collection if enough time has passed since the last garbage collection (see setGarbageCollectorRunInterval).
bForce | Force run garbage collection even if the last garbage collection was run not so long ago. |
|
inlineprivate |
Set GameInstance derived class to react to user inputs, window events and etc.
void ne::GameManager::setGarbageCollectorRunInterval | ( | long long | iGcRunIntervalInSec | ) |
Modifies the interval after which we need to run garbage collector again. The current value can be retrieved using getGarbageCollectorRunIntervalInSec.
iGcRunIntervalInSec | Interval in seconds. |
|
private |
Triggers action events from keyboard/mouse input.
key | Keyboard/mouse key. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the key down event occurred or key up. |
|
private |
Triggers axis events from keyboard input.
key | Keyboard key. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the key down event occurred or key up. |
|
private |
Whether destroy was called or not.
|
private |
Whether initialize was called or not.
|
private |
Whether addDeferredTask should accept new tasks or not.
|
private |
Interval in seconds after which we need to run garbage collector again.
|
private |
Binds action/axis names with input keys.
|
private |
Last time we run garbage collector.
|
private |
ID of the main thread.
|
private |
Mutex for read/write operations on deferred tasks queue. Queue of functions to call on the main thread before each frame is rendered.
|
private |
Game world, stores world's node tree.
|
private |
Determines what camera is used as in-game eyes.
|
private |
Reacts to user input, window events and etc.
|
private |
Draws graphics on window.
|
private |
Do not delete this pointer. Window-owner of this Game.
|
inlinestaticprivate |
Description of reasons why a leak may occur.
|
private |
Thread pool to execute tasks.
|
private |
The last time value that was passed to onBeforeNewFrame.