Nameless Engine
Loading...
Searching...
No Matches
GameManager.h
1#pragma once
2
3// Standard.
4#include <memory>
5#include <functional>
6#include <queue>
7#include <atomic>
8#include <mutex>
9
10// Custom.
11#include "game/GameInstance.h"
12#include "input/InputManager.h"
13#include "input/KeyboardKey.hpp"
14#include "input/MouseButton.hpp"
15#include "misc/ThreadPool.h"
16#include "game/World.h"
17#include "render/RenderSettings.h"
18
19// External.
20#include "GcPtr.h"
21
22namespace ne {
23 class GameInstance;
24 class Renderer;
25 class Window;
26 class CameraManager;
27
35 // Objects of this class should only be created by a window instance.
36 friend class Window;
37
38 public:
39 GameManager(const GameManager&) = delete;
40 GameManager& operator=(const GameManager&) = delete;
41
42 virtual ~GameManager();
43
55 static GameManager* get();
56
69
85 void queueGarbageCollection(bool bForce, const std::optional<std::function<void()>>& onFinished = {});
86
133 void addDeferredTask(const std::function<void()>& task);
134
150 void addTaskToThreadPool(const std::function<void()>& task);
151
179 void createWorld(
180 const std::function<void(const std::optional<Error>&)>& onCreated,
181 size_t iWorldSize = Globals::getDefaultWorldSize());
182
213 const std::function<void(const std::optional<Error>&)>& onLoaded,
214 const std::filesystem::path& pathToNodeTree,
215 size_t iWorldSize = Globals::getDefaultWorldSize());
216
222 sgc::GcPtr<Node> getWorldRootNode();
223
230 float getWorldTimeInSeconds();
231
237 size_t getWorldSize();
238
245
252
258 Window* getWindow() const;
259
266
273
279 float getTimeSincePrevFrameInSec() const;
280
286 long long getGarbageCollectorRunIntervalInSec() const;
287
295 bool isNodeSpawned(size_t iNodeId);
296
302 bool isBeingDestroyed() const;
303
304 private:
313
321 [[nodiscard]] std::optional<Error> initialize(std::optional<RendererType> preferredRenderer);
322
332 void destroy();
333
338 template <typename MyGameInstance>
339 requires std::derived_from<MyGameInstance, GameInstance>
341 pGameInstance = std::make_unique<MyGameInstance>(pWindow, this, &inputManager);
342 }
343
349 void onGameStarted();
350
357 void onBeforeNewFrame(float timeSincePrevCallInSec);
358
366 void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown);
367
375 void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown);
376
385 void onMouseMove(double xOffset, double yOffset);
386
392 void onMouseScrollMove(int iOffset);
393
399 void onWindowFocusChanged(bool bIsFocused) const;
400
407 void onFramebufferSizeChanged(int iWidth, int iHeight) const;
408
414 void onWindowClose() const;
415
421 void onTickFinished();
422
434 void runGarbageCollection(bool bForce = false);
435
438
447 std::variant<KeyboardKey, MouseButton> key, KeyboardModifiers modifiers, bool bIsPressedDown);
448
456 void triggerAxisEvents(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown);
457
465
468
470 std::unique_ptr<GameInstance> pGameInstance;
471
473 std::pair<std::recursive_mutex, std::unique_ptr<World>> mtxWorld;
474
476 std::unique_ptr<Renderer> pRenderer;
477
479 std::unique_ptr<CameraManager> pCameraManager;
480
483
488 std::pair<std::recursive_mutex, std::queue<std::function<void()>>> mtxDeferredTasks;
489
492
494 std::chrono::steady_clock::time_point lastGcRunTime;
495
498
500 long long iGcRunIntervalInSec = 120;
501
503 std::thread::id mainThreadId;
504
507
509 bool bIsInitialized = false;
510
512 bool bIsBeingDestroyed = false;
513
515 inline static const char* sGcLeakReasons =
516 "- you are switching a world but your game instance holds some non-nullptr gc pointers,\n"
517 "- you are not using STL container wrappers for gc "
518 "pointers (for example, you need to use `sgc::GcVector<sgc::GcPtr<T>>` instead of "
519 "`std::vector<sgc::GcPtr<T>>`, and other `gc_*` containers when storing gc pointers),\n"
520 "- you are capturing `sgc::GcPtr` pointer(s) in `std::function` (this might leak in some "
521 "special),\n"
522 "- for a more detailed list of possible issues see the \"README\" file at "
523 "https://github.com/Flone-dnb/sgc";
524 };
525} // namespace ne
Definition: CameraManager.h:16
Definition: GameInstance.h:32
Definition: GameManager.h:34
void runGarbageCollection(bool bForce=false)
Definition: GameManager.cpp:238
static const char * sGcLeakReasons
Definition: GameManager.h:515
float getWorldTimeInSeconds()
Definition: GameManager.cpp:458
std::unique_ptr< GameInstance > pGameInstance
Definition: GameManager.h:470
void addTaskToThreadPool(const std::function< void()> &task)
Definition: GameManager.cpp:387
void triggerActionEvents(std::variant< KeyboardKey, MouseButton > key, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameManager.cpp:609
bool bIsBeingDestroyed
Definition: GameManager.h:512
std::unique_ptr< CameraManager > pCameraManager
Definition: GameManager.h:479
ThreadPool threadPool
Definition: GameManager.h:482
std::pair< std::recursive_mutex, std::unique_ptr< World > > mtxWorld
Definition: GameManager.h:473
Window * getWindow() const
Definition: GameManager.cpp:588
std::optional< Error > initialize(std::optional< RendererType > preferredRenderer)
Definition: GameManager.cpp:60
size_t getCalledEveryFrameNodeCount()
Definition: GameManager.cpp:488
InputManager inputManager
Definition: GameManager.h:491
void setGarbageCollectorRunInterval(long long iGcRunIntervalInSec)
Definition: GameManager.cpp:301
void executeDeferredTasks()
Definition: GameManager.cpp:349
static GameManager * get()
Definition: GameManager.cpp:299
void onMouseInput(MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameManager.cpp:509
void onMouseScrollMove(int iOffset)
Definition: GameManager.cpp:538
std::chrono::steady_clock::time_point lastGcRunTime
Definition: GameManager.h:494
Window * pWindow
Definition: GameManager.h:467
std::pair< std::recursive_mutex, std::queue< std::function< void()> > > mtxDeferredTasks
Definition: GameManager.h:488
void createWorld(const std::function< void(const std::optional< Error > &)> &onCreated, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: GameManager.cpp:396
CameraManager * getCameraManager() const
Definition: GameManager.cpp:592
bool bShouldAcceptNewDeferredTasks
Definition: GameManager.h:506
void onMouseMove(double xOffset, double yOffset)
Definition: GameManager.cpp:519
std::unique_ptr< Renderer > pRenderer
Definition: GameManager.h:476
void onGameStarted()
Definition: GameManager.cpp:216
void destroyAndCleanExistingWorld()
Definition: GameManager.cpp:813
size_t getTotalSpawnedNodeCount()
Definition: GameManager.cpp:478
void triggerAxisEvents(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameManager.cpp:707
void queueGarbageCollection(bool bForce, const std::optional< std::function< void()> > &onFinished={})
Definition: GameManager.cpp:306
void onWindowFocusChanged(bool bIsFocused) const
Definition: GameManager.cpp:557
size_t getWorldSize()
Definition: GameManager.cpp:468
float timeSincePrevFrameInSec
Definition: GameManager.h:497
void onWindowClose() const
Definition: GameManager.cpp:565
std::thread::id mainThreadId
Definition: GameManager.h:503
long long getGarbageCollectorRunIntervalInSec() const
Definition: GameManager.cpp:596
GameInstance * getGameInstance() const
Definition: GameManager.cpp:590
void destroy()
Definition: GameManager.cpp:103
sgc::GcPtr< Node > getWorldRootNode()
Definition: GameManager.cpp:448
long long iGcRunIntervalInSec
Definition: GameManager.h:500
bool bIsInitialized
Definition: GameManager.h:509
void onBeforeNewFrame(float timeSincePrevCallInSec)
Definition: GameManager.cpp:315
void addDeferredTask(const std::function< void()> &task)
Definition: GameManager.cpp:570
float getTimeSincePrevFrameInSec() const
Definition: GameManager.cpp:594
void onTickFinished()
Definition: GameManager.cpp:232
void setGameInstance()
Definition: GameManager.h:340
void onKeyboardInput(KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown)
Definition: GameManager.cpp:498
bool isNodeSpawned(size_t iNodeId)
Definition: GameManager.cpp:598
void onFramebufferSizeChanged(int iWidth, int iHeight) const
Definition: GameManager.cpp:561
void loadNodeTreeAsWorld(const std::function< void(const std::optional< Error > &)> &onLoaded, const std::filesystem::path &pathToNodeTree, size_t iWorldSize=Globals::getDefaultWorldSize())
Definition: GameManager.cpp:414
bool isBeingDestroyed() const
Definition: GameManager.cpp:607
static constexpr size_t getDefaultWorldSize()
Definition: Globals.h:33
Definition: InputManager.h:77
Definition: KeyboardKey.hpp:10
Definition: ThreadPool.h:14
Definition: Window.h:198