Nameless Engine
Loading...
Searching...
No Matches
GameInstance.h
1#pragma once
2
3// Standard.
4#include <functional>
5#include <optional>
6#include <mutex>
7#include <filesystem>
8
9// Custom.
10#include "input/KeyboardKey.hpp"
11#include "input/MouseButton.hpp"
12#include "misc/Timer.h"
13#include "misc/Globals.h"
14
15// External.
16#include "GcPtr.h"
17
18namespace ne {
19 class Window;
20 class InputManager;
21 class GameManager;
22 class Node;
23 class CameraManager;
24
33 // Game will trigger input events.
34 friend class GameManager;
35
36 public:
37 GameInstance() = delete;
38
51
52 GameInstance(const GameInstance&) = delete;
53 GameInstance& operator=(const GameInstance&) = delete;
54
55 virtual ~GameInstance() = default;
56
62 static float getTotalApplicationTimeInSec();
63
110 void addDeferredTask(const std::function<void()>& task) const;
111
127 void addTaskToThreadPool(const std::function<void()>& task) const;
128
157 void createWorld(
158 const std::function<void(const std::optional<Error>&)>& onCreated,
159 size_t iWorldSize = Globals::getDefaultWorldSize());
160
194 const std::function<void(const std::optional<Error>&)>& onLoaded,
195 const std::filesystem::path& pathToNodeTree,
196 size_t iWorldSize = Globals::getDefaultWorldSize());
197
210 void queueGarbageCollection(bool bForce, const std::optional<std::function<void()>>& onFinished = {});
211
223 void setGarbageCollectorRunInterval(long long iGcRunIntervalInSec);
224
233
240 sgc::GcPtr<Node> getWorldRootNode() const;
241
248 float getWorldTimeInSeconds() const;
249
255 size_t getWorldSize() const;
256
263
270
276 Window* getWindow() const;
277
284
293
300
301 protected:
326 Timer* createTimer(const std::string& sTimerName);
327
333
340 virtual void onGameStarted() {}
341
350 virtual void onBeforeNewFrame(float timeSincePrevCallInSec) {}
351
361 virtual void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) {}
362
372 virtual void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) {}
373
382 virtual void onMouseMove(double xOffset, double yOffset) {};
383
389 virtual void onMouseScrollMove(int iOffset) {}
390
396 virtual void onWindowFocusChanged(bool bIsFocused) {}
397
404 virtual void onFramebufferSizeChanged(int iWidth, int iHeight) {}
405
413 virtual void onWindowClose() {}
414
439 std::pair<
440 std::recursive_mutex,
441 std::unordered_map<unsigned int, std::function<void(KeyboardModifiers, bool)>>>*
443
469 std::pair<
470 std::recursive_mutex,
471 std::unordered_map<unsigned int, std::function<void(KeyboardModifiers, float)>>>*
473
474 private:
484 void onInputActionEvent(unsigned int iActionId, KeyboardModifiers modifiers, bool bIsPressedDown);
485
495 void onInputAxisEvent(unsigned int iAxisEventId, KeyboardModifiers modifiers, float input);
496
498 std::pair<
499 std::recursive_mutex,
500 std::unordered_map<unsigned int, std::function<void(KeyboardModifiers, bool)>>>
502
504 std::pair<
505 std::recursive_mutex,
506 std::unordered_map<unsigned int, std::function<void(KeyboardModifiers, float)>>>
508
516 std::pair<std::recursive_mutex, std::vector<std::unique_ptr<Timer>>> mtxCreatedTimers;
517
520
522 Window* pGameWindow = nullptr;
523
526
531 };
532} // namespace ne
Definition: CameraManager.h:16
Definition: GameInstance.h:32
float getWorldTimeInSeconds() const
Definition: GameInstance.cpp:149
sgc::GcPtr< Node > getWorldRootNode() const
Definition: GameInstance.cpp:147
long long getGarbageCollectorRunIntervalInSec()
Definition: GameInstance.cpp:25
InputManager * getInputManager() const
Definition: GameInstance.cpp:23
void onInputAxisEvent(unsigned int iAxisEventId, KeyboardModifiers modifiers, float input)
Definition: GameInstance.cpp:87
virtual void onWindowFocusChanged(bool bIsFocused)
Definition: GameInstance.h:396
CameraManager * getCameraManager() const
Definition: GameInstance.cpp:21
void stopAndDisableCreatedTimers()
Definition: GameInstance.cpp:63
bool bAllowCreatingTimers
Definition: GameInstance.h:519
size_t getTotalSpawnedNodeCount()
Definition: GameInstance.cpp:153
virtual void onBeforeNewFrame(float timeSincePrevCallInSec)
Definition: GameInstance.h:350
void queueGarbageCollection(bool bForce, const std::optional< std::function< void()> > &onFinished={})
Definition: GameInstance.cpp:120
Window * pGameWindow
Definition: GameInstance.h:522
std::pair< std::recursive_mutex, std::vector< std::unique_ptr< Timer > > > mtxCreatedTimers
Definition: GameInstance.h:516
void onInputActionEvent(unsigned int iActionId, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameInstance.cpp:73
virtual void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameInstance.h:372
virtual void onGameStarted()
Definition: GameInstance.h:340
void executeShaderManagerSelfValidation() const
Definition: GameInstance.cpp:129
InputManager * pInputManager
Definition: GameInstance.h:530
virtual void onWindowClose()
Definition: GameInstance.h:413
static float getTotalApplicationTimeInSec()
Definition: GameInstance.cpp:17
size_t getCalledEveryFrameNodeCount()
Definition: GameInstance.cpp:155
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, bool)> > > mtxBindedActionEvents
Definition: GameInstance.h:501
void loadNodeTreeAsWorld(const std::function< void(const std::optional< Error > &)> &onLoaded, const std::filesystem::path &pathToNodeTree, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: GameInstance.cpp:113
virtual void onFramebufferSizeChanged(int iWidth, int iHeight)
Definition: GameInstance.h:404
void addDeferredTask(const std::function< void()> &task) const
Definition: GameInstance.cpp:100
Window * getWindow() const
Definition: GameInstance.cpp:19
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, bool)> > > * getActionEventBindings()
Definition: GameInstance.cpp:136
Timer * createTimer(const std::string &sTimerName)
Definition: GameInstance.cpp:29
virtual void onMouseScrollMove(int iOffset)
Definition: GameInstance.h:389
GameManager * pGameManager
Definition: GameInstance.h:525
virtual void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameInstance.h:361
virtual void onMouseMove(double xOffset, double yOffset)
Definition: GameInstance.h:382
void setGarbageCollectorRunInterval(long long iGcRunIntervalInSec)
Definition: GameInstance.cpp:125
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, float)> > > * getAxisEventBindings()
Definition: GameInstance.cpp:143
size_t getWorldSize() const
Definition: GameInstance.cpp:151
std::pair< std::recursive_mutex, std::unordered_map< unsigned int, std::function< void(KeyboardModifiers, float)> > > mtxBindedAxisEvents
Definition: GameInstance.h:507
void addTaskToThreadPool(const std::function< void()> &task) const
Definition: GameInstance.cpp:104
void createWorld(const std::function< void(const std::optional< Error > &)> &onCreated, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: GameInstance.cpp:108
Definition: GameManager.h:34
static constexpr size_t getDefaultWorldSize()
Definition: Globals.h:33
Definition: InputManager.h:77
Definition: KeyboardKey.hpp:10
Definition: Timer.h:13
Definition: Window.h:198