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
PlatformController.hpp
Go to the documentation of this file.
1
6#ifndef MATF_RG_PROJECT_PLATFORM_H
7#define MATF_RG_PROJECT_PLATFORM_H
8
10#include <memory>
11#include <vector>
15
16struct GLFWwindow;
17
18namespace engine::platform {
23 struct FrameTime {
27 float dt;
28
32 float previous;
33
37 float current;
38 };
39
45 class PlatformController final : public core::Controller {
46 friend class ControllerManager;
47
48 public:
54 const Key &key(KeyId key) const;
55
60 const MousePosition &mouse() const;
61
66 std::string_view name() const override;
67
72 const Window *window() const {
73 return &m_window;
74 }
75
80 void register_platform_event_observer(std::unique_ptr<PlatformEventObserver> observer);
81
86 const FrameTime &frame_time() const {
87 return m_frame_time;
88 }
89
94 float dt() const {
95 return m_frame_time.dt;
96 }
97
101 void set_enable_cursor(bool enabled);
102
106 void swap_buffers();
107
111 void _platform_on_mouse(double x, double y);
112
116 void _platform_on_keyboard(int key, int action);
117
121 void _platform_on_scroll(double x, double y);
122
126 void _platform_on_framebuffer_resize(int width, int height);
127
132
134
135 private:
137
141 void initialize() override;
142
146 void terminate() override;
147
148 bool loop() override;
149
150 void poll_events() override;
151
153
154 void update_key(Key &key_data) const;
155
158 std::vector<Key> m_keys;
159 std::vector<std::unique_ptr<PlatformEventObserver> > m_platform_event_observers;
160 };
161} // namespace engine
162
163#endif//MATF_RG_PROJECT_PLATFORM_H
Defines the Controller class that serves as the base class for all controllers in the engine.
Defines the Input class that serves as the interface handling user input via keyboard and mouse.
Defines the PlatformEventObserver class that serves as the interface for platform-specific event obse...
Defines the Window class that provides basic window properties.
Controllers are a hook into the App main loop execution. By overriding virtual functions of this clas...
Definition Controller.hpp:42
static TController * create_if_absent()
Definition Controller.hpp:179
Represents the state of the key in a given frame.
Definition Input.hpp:154
Registers Platform events such as mouse movement, key press, window events...
Definition PlatformController.hpp:45
void poll_events() override
Process internal and external events. Executes in the core::App::poll_events.
Definition PlatformController.cpp:94
void swap_buffers()
Swaps the current draw buffer for the main window. Should be called at the end of the frame.
Definition PlatformController.cpp:102
void _platform_on_mouse(double x, double y)
Called from the platform-specific callback. You shouldn't call this function directly.
Definition PlatformController.cpp:204
Key & key_ref(KeyId key)
Definition PlatformController.cpp:173
const Window * window() const
Get the window.
Definition PlatformController.hpp:72
void initialize() override
Initializes the platform layer and registers platform-specific event callbacks.
Definition PlatformController.cpp:38
Window m_window
Definition PlatformController.hpp:157
void update_key(Key &key_data) const
Updates the state of a key. Key states are repesented as a state machine with the following states: R...
Definition PlatformController.cpp:123
void terminate() override
Terminate the platform layer.
Definition PlatformController.cpp:78
std::vector< std::unique_ptr< PlatformEventObserver > > m_platform_event_observers
Definition PlatformController.hpp:159
void _platform_on_framebuffer_resize(int width, int height)
Called from the platform-specific callback. You shouldn't call this function directly.
Definition PlatformController.cpp:230
FrameTime m_frame_time
Definition PlatformController.hpp:156
void _platform_on_window_close(GLFWwindow *window)
Called from the platform-specific callback. You shouldn't call this function directly.
Definition PlatformController.cpp:238
std::string_view name() const override
Get the name of the Controller.
Definition PlatformController.cpp:196
float dt() const
Get elapsed time for the previous frame.
Definition PlatformController.hpp:94
void _platform_on_scroll(double x, double y)
Called from the platform-specific callback. You shouldn't call this function directly.
Definition PlatformController.cpp:223
const FrameTime & frame_time() const
Get FrameTime for the previous frame. Updated in during core::App::loop.
Definition PlatformController.hpp:86
const Key & key(KeyId key) const
Get the state of the Key in the current frame.
Definition PlatformController.cpp:187
void set_enable_cursor(bool enabled)
Enables/disabled the visibility of the cursor on screen.
Definition PlatformController.cpp:251
friend class ControllerManager
Definition PlatformController.hpp:46
const MousePosition & mouse() const
Get the state of the MousePosition in the current frame.
Definition PlatformController.cpp:192
bool loop() override
Checks whether the main loop should continue. Executes in the core::App::loop.
Definition PlatformController.cpp:86
std::vector< Key > m_keys
Definition PlatformController.hpp:158
void _platform_on_mouse_button(int button, int action)
Definition PlatformController.cpp:244
void _platform_on_keyboard(int key, int action)
Called from the platform-specific callback. You shouldn't call this function directly.
Definition PlatformController.cpp:216
void register_platform_event_observer(std::unique_ptr< PlatformEventObserver > observer)
Register a PlatformEventObserver callback for platform events. By default, the PlatformController reg...
Definition PlatformController.cpp:200
Holds window properties.
Definition Window.hpp:16
Definition Input.hpp:10
KeyId
All the Keys that the engine can register.
Definition Input.hpp:14
Stores elapsed time for frames in seconds.
Definition PlatformController.hpp:23
float dt
Elapsed seconds for the previous frame.
Definition PlatformController.hpp:27
float current
Time from the initialization of the Platform to the moment when the current frame began.
Definition PlatformController.hpp:37
float previous
Time from the initialization of the Platform to the moment when the previous frame began.
Definition PlatformController.hpp:32
Represents mouse position in a given frame relative to the top left corner of the screen.
Definition Input.hpp:237