Brenta Engine 1.2
Loading...
Searching...
No Matches
window.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/subsystem.hpp>
9#include <brenta/time.hpp>
10#include <brenta/key.hpp>
11
12#include <tenno/memory.hpp>
13
14#include <string>
15
16namespace brenta
17{
18
19class FrameBuffer;
20
21//
22// Window subsystem
23// ----------------
24//
25// This class is used to create a window and handle all the events
26// related to the window. This class provides methods to initialize
27// the window, get the window size, check if a key is pressed, get the
28// time since the start of the program, and more.
29//
30class Window : public Subsystem
31{
32public:
33
34 class Driver;
35 struct Config;
36 class Builder;
37
38 using WindowHandle = void*;
39 using ProcHandle = void*;
40 using Callback = void*;
41
42 static tenno::shared_ptr<FrameBuffer> framebuffer;
43
44 // Subsystem interface
45 static const std::string subsystem_name;
46 std::string name() override;
47 bool is_initialized() override;
48 std::expected<void, Subsystem::Error> initialize() override;
49 std::expected<void, Subsystem::Error> terminate() override;
50
51 // Member functions
52
53 static Window &instance();
54
55 //
56 // Getters
57 //
58
59 static int get_width();
60 static int get_height();
61 static bool should_close();
62 static bool is_key_pressed(Key key);
63 static Time get_time();
64 static ProcHandle get_proc_address();
65 static WindowHandle get_window();
66 static tenno::shared_ptr<Window::Driver> get_driver();
67
68 //
69 // Setters
70 //
71
72 static void set_mouse_capture(bool is_captured);
73 static void set_mouse_callback(Callback callback);
74 static void set_size_callback(Callback callback);
75 static void set_mouse_pos_callback(Callback callback);
76 static void set_key_callback(Callback callback);
77 static void set_dimensions(int width, int height);
78
79 //
80 // Utils
81 //
82
83 // Swap the front and back buffers
84 // Having two buffers avoids flickering.
85 static void swap_buffers();
86 // Poll all pending events
87 static void poll_events();
88 // Updated the window dimensions
89 static void update_dimensions();
90 // Set the internal dimensions to [width] and [height]
91 static void make_context_current();
92 static void close();
93
94protected:
95
96 static bool initialized;
97 static Window::Config init_config;
98 static tenno::shared_ptr<Window::Driver> backend;
99
100 // Private constructors / destructors for singleton
101 Window() = default;
102 ~Window() = default;
103
104};
105
107{
108public:
109
110 Driver() = default;
111 virtual ~Driver() = default;
112
113 virtual std::expected<void, std::string> initialize(const Window::Config &conf) = 0;
114 virtual std::expected<void, std::string> terminate() = 0;
115
116 //
117 // Getters
118 //
119
120 virtual int get_width() = 0;
121 virtual int get_height() = 0;
122 virtual bool should_close() = 0;
123 virtual bool is_key_pressed(Key key) = 0;
124 virtual Time get_time() = 0;
125 virtual Window::ProcHandle get_proc_address() = 0;
126 virtual Window::WindowHandle get_window() = 0;
127
128 //
129 // Setters
130 //
131
132 virtual void set_mouse_capture(bool is_captured) = 0;
133 virtual void set_mouse_callback(Window::Callback callback) = 0;
134 virtual void set_size_callback(Window::Callback callback) = 0;
135 virtual void set_mouse_pos_callback(Window::Callback callback) = 0;
136 virtual void set_key_callback(Window::Callback callback) = 0;
137 virtual void set_dimensions(int width, int height) = 0;
138
139 //
140 // Utils
141 //
142
143 virtual void swap_buffers() = 0;
144 virtual void poll_events() = 0;
145 virtual void update_dimensions() = 0;
146 virtual void set_context_version(int major, int minor) = 0;
147 virtual void use_core_profile() = 0;
148 virtual void set_hints_apple() = 0;
149 virtual void create_window(int width, int height,
150 const std::string& title) = 0;
151 virtual void make_context_current() = 0;
152 virtual void close() = 0;
153
154};
155
157{
158public:
159 int width = 800;
160 int height = 600;
161 std::string title = "Brenta Engine";
162 bool capture_mouse = false;
163 bool msaa = false;
164 bool vsync = false;
165 bool debug = false;
166};
167
169{
170private:
171
172 Window::Config conf = {};
173
174public:
175
176 Builder() = default;
177 ~Builder() = default;
178
179 Builder &width(int width);
180 Builder &height(int height);
181 Builder &title(const std::string &title);
182 Builder &capture_mouse();
183 Builder &msaa();
184 Builder &vsync();
185 Builder &debug(); // opengl debug errors, set this during development
186
187 Subsystem &build() override;
188};
189
190} // namespace brenta