sandbox
Loading...
Searching...
No Matches
devices_module.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_DEVICES_DEVICES_MODULE_HPP_
3#define LIBSBX_DEVICES_DEVICES_MODULE_HPP_
4
5#include <cinttypes>
6
7#include <memory>
8#include <vector>
9#include <unordered_map>
10
11#include <fmt/format.h>
12
13#include <GLFW/glfw3.h>
14
15#include <libsbx/core/module.hpp>
16#include <libsbx/core/engine.hpp>
17
18#include <libsbx/units/time.hpp>
19
20#include <libsbx/memory/aligned_storage.hpp>
21
22#include <libsbx/devices/window.hpp>
23#include <libsbx/devices/input.hpp>
24
25namespace sbx::devices {
26
27class devices_module final : public core::module<devices_module> {
28
29 inline static const auto is_registered = register_module(stage::pre);
30
31 struct context {
32
33 context() {
34 if (!glfwInit()) {
35 throw std::runtime_error{"Could not initialize glfw"};
36 }
37
38 if (!glfwVulkanSupported()) {
39 throw std::runtime_error{"Glfw does not support vulkan"};
40 }
41 }
42
43 ~context() {
44 glfwTerminate();
45 }
46
47 }; // struct context
48
49public:
50
52 : _context{},
53 _window{window_create_info{"Demo", 1280, 720}} {
54 const auto initial_size = math::vector2{_window.width(), _window.height()};
55
56 input::set_active_viewport(math::vector2{0.0f, 0.0f}, initial_size);
57 input::set_scene_input_active(true);
58
59 _window.on_framebuffer_resized() += [](const auto& event) {
60 input::set_active_viewport(math::vector2{0.0f, 0.0f}, math::vector2{static_cast<std::float_t>(event.width), static_cast<std::float_t>(event.height)});
61 };
62 }
63
64 ~devices_module() override {
65
66 }
67
68 auto update() -> void override {
69 input::_transition_pressed_keys();
70 input::_transition_pressed_mouse_buttons();
71 input::_transition_scroll_delta();
72
73 glfwPollEvents();
74 }
75
76 auto window() -> devices::window& {
77 return _window;
78 }
79
80 auto required_instance_extensions() const -> std::vector<const char*> {
81 auto extension_count = std::uint32_t{0};
82 auto** extensions = glfwGetRequiredInstanceExtensions(&extension_count);
83
84 return std::vector<const char*>{extensions, extensions + extension_count};
85 }
86
87private:
88
89 context _context;
90 devices::window _window;
91
92}; // class devices_module
93
94} // namespace sbx::devices
95
96#endif // LIBSBX_DEVICES_DEVICES_MODULE_HPP_
Definition: module.hpp:92
Definition: devices_module.hpp:27
Definition: window.hpp:33
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: window.hpp:27