Nameless Engine
|
#include <GameInstance.h>
Public Member Functions | |
GameInstance (Window *pGameWindow, GameManager *pGameManager, InputManager *pInputManager) | |
GameInstance (const GameInstance &)=delete | |
GameInstance & | operator= (const GameInstance &)=delete |
void | addDeferredTask (const std::function< void()> &task) const |
void | addTaskToThreadPool (const std::function< void()> &task) const |
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()) |
void | queueGarbageCollection (bool bForce, const std::optional< std::function< void()> > &onFinished={}) |
void | setGarbageCollectorRunInterval (long long iGcRunIntervalInSec) |
void | executeShaderManagerSelfValidation () const |
sgc::GcPtr< Node > | getWorldRootNode () const |
float | getWorldTimeInSeconds () const |
size_t | getWorldSize () const |
size_t | getTotalSpawnedNodeCount () |
size_t | getCalledEveryFrameNodeCount () |
Window * | getWindow () const |
CameraManager * | getCameraManager () const |
InputManager * | getInputManager () const |
long long | getGarbageCollectorRunIntervalInSec () |
Static Public Member Functions | |
static float | getTotalApplicationTimeInSec () |
Protected Member Functions | |
Timer * | createTimer (const std::string &sTimerName) |
void | stopAndDisableCreatedTimers () |
virtual void | onGameStarted () |
virtual void | onBeforeNewFrame (float timeSincePrevCallInSec) |
virtual void | onKeyboardInput (KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) |
virtual void | onMouseInput (MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) |
virtual void | onMouseMove (double xOffset, double yOffset) |
virtual void | onMouseScrollMove (int iOffset) |
virtual void | onWindowFocusChanged (bool bIsFocused) |
virtual void | onFramebufferSizeChanged (int iWidth, int iHeight) |
virtual void | onWindowClose () |
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, bool)> > > * | getActionEventBindings () |
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, float)> > > * | getAxisEventBindings () |
Private Member Functions | |
void | onInputActionEvent (unsigned int iActionId, KeyboardModifiers modifiers, bool bIsPressedDown) |
void | onInputAxisEvent (unsigned int iAxisEventId, KeyboardModifiers modifiers, float input) |
Private Attributes | |
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, bool)> > > | mtxBindedActionEvents |
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, float)> > > | mtxBindedAxisEvents |
std::pair< std::recursive_mutex, std::vector< std::unique_ptr< Timer > > > | mtxCreatedTimers |
bool | bAllowCreatingTimers = true |
Window * | pGameWindow = nullptr |
GameManager * | pGameManager = nullptr |
InputManager * | pInputManager = nullptr |
Friends | |
class | GameManager |
Main game class, exists while the game window is not closed (i.e. while the game is not closed).
Reacts to user inputs, window events and etc. Owned by Game object.
ne::GameInstance::GameInstance | ( | Window * | pGameWindow, |
GameManager * | pGameManager, | ||
InputManager * | pInputManager | ||
) |
Constructor.
pGameWindow | Window that owns this game instance. |
pGameManager | GameManager that owns this game instance. |
pInputManager | Input manager of the owner Game object. |
void ne::GameInstance::addDeferredTask | ( | const std::function< void()> & | task | ) | const |
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::GameInstance::addTaskToThreadPool | ( | const std::function< void()> & | task | ) | const |
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. |
|
protected |
Creates a new timer and saves it inside of this GameInstance.
removeTimer
function but it may appear in the future (although there's really no point in removing a timer so don't care about it).sTimerName | Name of this timer (used for logging). Don't add "timer" word to your timer's name as it will be appended in the logs. |
nullptr
if something went wrong, otherwise a non-owning pointer to the created timer that is guaranteed to be valid while this object is alive. void ne::GameInstance::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.
gc
pointers to nodes in your game instance, make sure you clear them (set to nullptr
) before calling this function.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). |
void ne::GameInstance::executeShaderManagerSelfValidation | ( | ) | const |
Analyzes the current state to see if any shader-related errors have place (like unused shaders in memory or etc.). Fixes errors and reports them in log.
|
protected |
Returns map of action events that this GameInstance is binded to (must be used with mutex). Binded callbacks will be automatically called when an action event is triggered.
Example:
|
protected |
Returns map of axis events that this GameInstance is binded to (must be used with mutex). Binded callbacks will be automatically called when an axis event is triggered.
Example:
size_t ne::GameInstance::getCalledEveryFrameNodeCount | ( | ) |
Returns the current amount of spawned nodes that are marked as "should be called every frame".
CameraManager * ne::GameInstance::getCameraManager | ( | ) | const |
Returns a reference to the camera manager this game is using.
long long ne::GameInstance::getGarbageCollectorRunIntervalInSec | ( | ) |
Returns the current interval after which we need to run garbage collector again.
InputManager * ne::GameInstance::getInputManager | ( | ) | const |
Returns a reference to the input manager this game instance is using. Input manager allows binding IDs with multiple input keys that you can receive in onInputActionEvent.
|
static |
Returns the time in seconds that has passed since the very first window was created.
size_t ne::GameInstance::getTotalSpawnedNodeCount | ( | ) |
Returns total amount of currently spawned nodes.
Window * ne::GameInstance::getWindow | ( | ) | const |
Returns a reference to the window this game instance is using.
sgc::GcPtr< Node > ne::GameInstance::getWorldRootNode | ( | ) | const |
Returns a pointer to world's root node.
size_t ne::GameInstance::getWorldSize | ( | ) | const |
Returns world size in game units.
float ne::GameInstance::getWorldTimeInSeconds | ( | ) | const |
Returns time since world creation (in seconds).
void ne::GameInstance::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.
gc
pointers to nodes in game instance, make sure you set nullptr
to them before calling this function.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). |
|
inlineprotectedvirtual |
Called before a new frame is rendered.
timeSincePrevCallInSec | Time in seconds that has passed since the last call to this function. |
Reimplemented in ne::EditorGameInstance.
|
inlineprotectedvirtual |
Called when the framebuffer size was changed.
iWidth | New width of the framebuffer (in pixels). |
iHeight | New height of the framebuffer (in pixels). |
|
inlineprotectedvirtual |
Called after GameInstance's constructor is finished and created GameInstance object was saved in GameManager (that owns GameInstance).
At this point you can create and interact with the game world and etc.
Reimplemented in ne::EditorGameInstance.
|
private |
Called when a window that owns this game instance receives user input and the input key exists as an action event in the InputManager. Called after onKeyboardInput.
iActionId | Unique ID of the input action event (from input manager). |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the key down event occurred or key up. |
|
private |
Called when a window that owns this game instance receives user input and the input key exists as an axis event in the InputManager. Called after onKeyboardInput and after onInputActionEvent.
iAxisEventId | Unique ID of the input axis event (from input manager). |
modifiers | Keyboard modifier keys. |
input | A value in range [-1.0f; 1.0f] that describes input. |
|
inlineprotectedvirtual |
Called when the window receives keyboard input. Called before onInputActionEvent. Prefer to use onInputActionEvent instead of this function.
key | Keyboard key. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the key down event occurred or key up. |
|
inlineprotectedvirtual |
Called when the window receives mouse input. Called before onInputActionEvent. Prefer to use onInputActionEvent instead of this function.
button | Mouse button. |
modifiers | Keyboard modifier keys. |
bIsPressedDown | Whether the button down event occurred or button up. |
|
inlineprotectedvirtual |
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). |
|
inlineprotectedvirtual |
Called when the window receives mouse scroll movement.
iOffset | Movement offset. |
|
inlineprotectedvirtual |
Called when the window that owns this game instance was requested to close (no new frames will be rendered).
Prefer to have your destructor logic here, because after this function is finished the world will be destroyed and will be inaccessible (nullptr
).
|
inlineprotectedvirtual |
Called when the window focus was changed.
bIsFocused | Whether the window has gained or lost the focus. |
void ne::GameInstance::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). |
void ne::GameInstance::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. |
|
protected |
Called by owner object to stop and disable all created timers, additionally does not allows creating any more timers.
|
private |
Whether createTimer works or not.
|
private |
Map of action events that this GameInstance is binded to. Must be used with mutex.
|
private |
Map of axis events that this GameInstance is binded to. Must be used with mutex.
|
private |
Timers creates via createTimer.
|
private |
Do not delete. Owner of this object.
|
private |
Do not delete. Owner of pGameManager object.
|
private |
Do not delete. Input manager of the pGameManager object.