matf-rg-engine 1.0.0
Base for project for the Computer Graphics course at Faculty of Mathematics, University of Belgrade
Loading...
Searching...
No Matches
Controller.hpp
Go to the documentation of this file.
1
6#ifndef MATF_RG_PROJECT_CONTROLLER_HPP
7#define MATF_RG_PROJECT_CONTROLLER_HPP
8
10#include <memory>
11#include <string_view>
12#include <vector>
13#include <typeinfo>
14
15namespace engine::core {
42 class Controller {
43 friend class App;
44
45 public:
52 template<typename TController>
53 static TController *get(std::source_location location = std::source_location::current()) {
54 static_assert(std::is_base_of_v<Controller, TController>);
56 RG_GUARANTEE(controller->is_registered(),
57 "Trying to get an unregistered controller in: {}:{}.\nPlease call register_controller<> first during App::app_setup.",
58 location.file_name(), location.line());
59 return controller;
60 }
61
66 virtual std::string_view name() const {
67 return typeid(*this).name();
68 }
69
70 virtual ~Controller() = default;
71
78 this->m_next.push_back(next);
79 }
80
87 prev->before(this);
88 }
89
94 const std::vector<Controller *> &next() const {
95 return m_next;
96 }
97
103 bool is_enabled() const {
104 return m_enabled;
105 }
106
113 void set_enable(bool value) {
115 }
116
117 private:
119 m_registered = true;
120 }
121
122 bool is_registered() const {
123 return m_registered;
124 }
125
129 virtual void initialize() {
130 }
131
136 virtual bool loop() {
137 return true;
138 }
139
143 virtual void poll_events() {
144 }
145
149 virtual void update() {
150 }
151
155 virtual void begin_draw() {
156 }
157
161 virtual void draw() {
162 }
163
167 virtual void end_draw() {
168 }
169
175 virtual void terminate() {
176 }
177
178 template<typename TController>
180 static_assert(std::is_base_of_v<Controller, TController>);
181 static std::unique_ptr<TController> controller = std::make_unique<TController>();
182 return controller.get();
183 }
184
190 std::vector<Controller *> m_next{};
191
195 bool m_registered{false};
196
200 bool m_enabled{true};
201 };
202
217 class EngineControllersBegin final : public Controller {
218 public:
219 std::string_view name() const override {
220 return "EngineControllersBegin";
221 }
222 };
223
238 class EngineControllersEnd final : public Controller {
239 public:
240 std::string_view name() const override {
241 return "EngineControllersEnd";
242 }
243 };
244} // namespace engine
245
246#endif//MATF_RG_PROJECT_CONTROLLER_HPP
Defines Error, UserError, and EngineError classes, along with macros for error handling.
#define RG_GUARANTEE(expr, msg,...)
Guarantees that an expression is true. If it is not, an engine::util::EngineError is thrown.
Definition Errors.hpp:164
Defines the base App class that serves as the application core structure and the entry point.
Definition App.hpp:45
Controllers are a hook into the App main loop execution. By overriding virtual functions of this clas...
Definition Controller.hpp:42
virtual void initialize()
Initializes the controller. Executes in the core::App::initialize.
Definition Controller.hpp:129
virtual void begin_draw()
Perform preparation for drawing. Executes in the core::App::draw, before Controller::draw.
Definition Controller.hpp:155
const std::vector< Controller * > & next() const
Definition Controller.hpp:94
void before(Controller *next)
Definition Controller.hpp:77
std::vector< Controller * > m_next
List of controllers that are dependent on this controller. If controller A is in the m_next,...
Definition Controller.hpp:190
void set_enable(bool value)
Enables or disables the controller based on value. The ControllerManager executes only the enabled co...
Definition Controller.hpp:113
void mark_as_registered()
Definition Controller.hpp:118
static TController * get(std::source_location location=std::source_location::current())
Serves as a single access point for all the Controller types throughout the code base....
Definition Controller.hpp:53
bool m_enabled
Internal field used to control weather the ControllerManager executes the controller.
Definition Controller.hpp:200
virtual std::string_view name() const
Definition Controller.hpp:66
virtual ~Controller()=default
virtual void draw()
Draw the world state. Executes in the core::App::draw.
Definition Controller.hpp:161
virtual bool loop()
Checks whether the main loop should continue. Executes in the core::App::loop.
Definition Controller.hpp:136
bool m_registered
Internal Controller field used to ensure that the controller isn't registered twice.
Definition Controller.hpp:195
virtual void poll_events()
Process internal and external events. Executes in the core::App::poll_events.
Definition Controller.hpp:143
static TController * create_if_absent()
Definition Controller.hpp:179
void after(Controller *prev)
Definition Controller.hpp:86
bool is_enabled() const
Controller will execute as long this function returns true.
Definition Controller.hpp:103
virtual void update()
Update the controller state and prepare for drawing. Executes in the core::App::update.
Definition Controller.hpp:149
virtual void end_draw()
Finalize drawing. Executes in the core::App::draw, after Controller::draw.
Definition Controller.hpp:167
bool is_registered() const
Definition Controller.hpp:122
virtual void terminate()
Terminate the controller. Executes in the core::App::terminate.
Definition Controller.hpp:175
This controller does nothing and together with EngineControllersEnd it servers as a sentinel controll...
Definition Controller.hpp:217
std::string_view name() const override
Definition Controller.hpp:219
This controller does nothing and together with EngineControllersEnd it servers as a sentinel controll...
Definition Controller.hpp:238
std::string_view name() const override
Definition Controller.hpp:240
Definition App.hpp:15