CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Engine.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
16
33
35
36namespace CeresEngine {
37
39 public: // Services
41 // CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
43
46 template<typename T> struct Service {
47 private:
49 UPtr<T> mService = nullptr;
50
51 public:
53 explicit Service() noexcept = default;
54
55 Service(const Service&) noexcept = delete;
56 Service& operator=(const Service&) noexcept = delete;
57
58 Service(Service&&) noexcept = delete;
59 Service& operator=(Service&&) noexcept = delete;
60
61 ~Service() noexcept { CE_ASSERT(mService == nullptr && "Service being destroyed while still initialized."); }
62
64 [[nodiscard]] bool has() const noexcept { return mService != nullptr; }
65
67 [[nodiscard]] T& get() const noexcept {
68 CE_ASSERT(mService != nullptr && "Trying to get an uninitialized service!");
69 return *mService;
70 }
71
73 [[nodiscard]] T* getPointer() const noexcept { return mService.get(); }
74
81 template<typename TT = T, typename... Args> TT& create(Args&&... args) {
82 if constexpr(std::is_constructible_v<TT, Engine&, Args...>) {
83 assign(ce_unique_new<TT>(*this, std::forward<Args>(args)...));
84 } else if constexpr(std::is_constructible_v<TT, Engine*, Args...>) {
85 assign(ce_unique_new<TT>(this, std::forward<Args>(args)...));
86 } else {
87 assign(ce_unique_new<TT>(std::forward<Args>(args)...));
88 }
89 return static_cast<TT&>(get());
90 }
91
94 void destroy() noexcept { assign(nullptr); }
95
98 void assign(UPtr<T>&& service) noexcept { mService = std::move(service); }
99
102 template<typename TT> [[nodiscard]] bool is() const noexcept { return dynamic_cast<const TT*>(mService.get()) != nullptr; }
103
105 [[nodiscard]] explicit operator bool() const noexcept { return has(); }
106
108 [[nodiscard]] CE_EXPLICIT(false) operator T&() const { return get(); } // NOLINT
109
111 [[nodiscard]] CE_EXPLICIT(false) operator T*() const { return getPointer(); } // NOLINT
112
114 [[nodiscard]] T& operator*() const { return get(); }
115
117 [[nodiscard]] T* operator->() const {
118 CE_ASSERT(mService != nullptr && "Trying to get an uninitialized service!");
119 return getPointer();
120 }
121
125 Service& operator=(UPtr<T>&& service) noexcept {
126 assign(std::forward<decltype(service)>(service));
127 return *this;
128 }
129 };
130
133
135 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
136 Service<ResourceManager> resourceManager;
137
139 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
140 Service<RenderAPI> renderAPI;
141
143 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
144 Service<AudioAPI> audioAPI;
145
147 Service<InputProvider> inputProvider;
148
150 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
151 Service<InputManager> inputManager;
152
154 Service<SceneManager> sceneManager;
155
157 Service<Renderer> renderer;
158
160 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
161 Service<Platform> platform;
162
164 CE_SCRIPT_EXPORT(pass = "ref", readonly = true)
165 Service<URLSession> urlSession;
166
167 public:
169 CE_SCRIPT_EXPORT(readonly = true)
170 double lastSimulationTime = 0.0;
171
173 CE_SCRIPT_EXPORT(readonly = true)
174 double lastRenderTime = 0.0;
175
176 UInt64 visibleObjects = 0;
177
178 public:
182
183 Engine(const Engine& engine) = delete;
184 Engine& operator=(const Engine& engine) = delete;
185
187 virtual ~Engine();
188
189 public:
191 [[nodiscard]] CE_SCRIPT_EXPORT(property = "getter", name = "shared")
192 static Engine& shared() noexcept;
193 };
194
196 Engine& gEngine() noexcept;
197
198} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
#define CE_EXPLICIT(EXPR)
Definition Macros.hpp:413
Definition AudioAPI.hpp:18
Definition Engine.hpp:38
Service< RunLoop > mainRunLoop
The engine's main thread run loop.
Definition Engine.hpp:132
UPtr< ExecutionContext > backgroundQueue
A work queue that gets drained by several background threads.
Definition Engine.hpp:42
Definition InputManager.hpp:29
Definition InputProvider.hpp:21
Definition Platform.hpp:20
Definition RenderAPI.hpp:81
The CeresEngine renderer.
Definition Renderer.hpp:35
The ResourceManager is the main class responsible for managing and handling all resources in the engi...
Definition ResourceManager.hpp:47
A manager that references and manages mutliple scenes.
Definition Scene.hpp:230
An object that coordinates a group of related, network data transfer tasks.
Definition URLSession.hpp:399
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
std::uint64_t UInt64
Definition DataTypes.hpp:26
A wrapper class that stores and manages an engine Service.
Definition Engine.hpp:46
bool is() const noexcept
Definition Engine.hpp:102
T & operator*() const
Definition Engine.hpp:114
Service & operator=(UPtr< T > &&service) noexcept
Assigns a new service.
Definition Engine.hpp:125
void destroy() noexcept
Destroys a service.
Definition Engine.hpp:94
TT & create(Args &&... args)
Creates a new service.
Definition Engine.hpp:81
void assign(UPtr< T > &&service) noexcept
Assigns a new service.
Definition Engine.hpp:98
T & get() const noexcept
Definition Engine.hpp:67
Service() noexcept=default
Creates a new Service wrapper.
bool has() const noexcept
Definition Engine.hpp:64
T * getPointer() const noexcept
Definition Engine.hpp:73
T * operator->() const
Definition Engine.hpp:117