le2d v0.4.8
2D game engine framework
 
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1#pragma once
4#include "le2d/build_version.hpp"
6#include "le2d/event.hpp"
10#include "le2d/unprojector.hpp"
11#include "le2d/vsync.hpp"
12#include <GLFW/glfw3.h>
13#include <klib/enum_ops.hpp>
14#include <kvf/render_device.hpp>
15#include <kvf/window.hpp>
16#include <variant>
17
18namespace le {
20enum class PlatformFlag : std::uint8_t {
21 None = 0,
26};
27constexpr auto enable_enum_bitops(PlatformFlag /*unused*/) -> bool { return true; }
28
30enum class WindowFlag : std::uint8_t {
31 None = 0,
33 Decorated = 1 << 0,
35 Resizeable = 1 << 1,
37 Visible = 1 << 2,
39 Maximized = 1 << 3,
41 FocusOnShow = 1 << 4,
43 ScaleToMonitor = 1 << 5,
45 ScaleFramebuffer = 1 << 6,
46};
47constexpr auto enable_enum_bitops(WindowFlag /*unused*/) -> bool { return true; }
48
51
53struct WindowInfo {
54 glm::ivec2 size{600};
55 klib::CString title;
57};
58
61 klib::CString title;
62};
63
65using WindowCreateInfo = std::variant<WindowInfo, FullscreenInfo>;
66
74 kvf::RenderDeviceCreateInfo render_device{};
76 vk::SampleCountFlagBits framebuffer_samples{vk::SampleCountFlagBits::e2};
78 int sfx_buffers{16};
79};
80
82class Context : public klib::Polymorphic {
83 public:
85 class Waiter;
86
87 using SpirV = std::span<std::uint32_t const>;
89
90 static constexpr auto min_render_scale_v{0.2f};
91 static constexpr auto max_render_scale_v{8.0f};
92
93 [[nodiscard]] static auto create(CreateInfo const& create_info = {}) -> std::unique_ptr<Context>;
94
95 [[nodiscard]] virtual auto get_window() const -> GLFWwindow* = 0;
96 [[nodiscard]] virtual auto get_render_device() const -> kvf::RenderDevice const& = 0;
97 [[nodiscard]] virtual auto get_resource_factory() const -> IResourceFactory const& = 0;
98 [[nodiscard]] virtual auto get_audio_mixer() const -> IAudioMixer& = 0;
99 [[nodiscard]] virtual auto get_default_shader() const -> IShader const& = 0;
100 [[nodiscard]] virtual auto get_renderer() const -> IRenderer const& = 0;
101
103 [[nodiscard]] auto window_size() const -> glm::ivec2;
105 [[nodiscard]] auto framebuffer_size() const -> glm::ivec2;
107 [[nodiscard]] auto swapchain_extent() const -> vk::Extent2D;
109 [[nodiscard]] auto main_pass_size() const -> glm::ivec2;
111 [[nodiscard]] auto display_ratio() const -> glm::vec2;
112
116 [[nodiscard]] auto unprojector(Viewport const& viewport, Transform const& view) const -> Unprojector {
117 return Unprojector{viewport, view, main_pass_size()};
118 }
119
120 [[nodiscard]] auto get_title() const -> klib::CString;
121 void set_title(klib::CString title);
122
123 [[nodiscard]] auto get_refresh_rate() const -> std::int32_t;
124
125 [[nodiscard]] auto is_fullscreen() const -> bool;
129 auto set_fullscreen(GLFWmonitor* target = nullptr) -> bool;
132 void set_windowed(glm::ivec2 size = {1280, 720});
134 void set_visible(bool visible);
137 void lock_aspect_ratio(bool lock);
138
141 [[nodiscard]] auto is_running() const -> bool;
147
150 void wait_idle();
151
153 [[nodiscard]] virtual auto get_render_scale() const -> float = 0;
156 virtual auto set_render_scale(float scale) -> bool = 0;
157
159 [[nodiscard]] virtual auto get_supported_vsync() const -> std::span<Vsync const> = 0;
161 [[nodiscard]] virtual auto get_vsync() const -> Vsync = 0;
164 virtual auto set_vsync(Vsync vsync) -> bool = 0;
165
167 [[nodiscard]] virtual auto get_samples() const -> vk::SampleCountFlagBits = 0;
169 [[nodiscard]] virtual auto get_supported_samples() const -> vk::SampleCountFlags = 0;
173 virtual auto set_samples(vk::SampleCountFlagBits samples) -> bool = 0;
174
178 virtual auto next_frame() -> vk::CommandBuffer = 0;
180 [[nodiscard]] virtual auto event_queue() const -> std::span<Event const> = 0;
184 [[nodiscard]] virtual auto begin_render(kvf::Color clear = kvf::black_v) -> IRenderer& = 0;
186 virtual void present() = 0;
187
188 [[nodiscard]] virtual auto get_frame_stats() const -> FrameStats const& = 0;
189
190 [[nodiscard]] auto create_waiter() -> Waiter;
191 [[nodiscard]] virtual auto create_render_pass(vk::SampleCountFlagBits samples) const -> std::unique_ptr<IRenderPass> = 0;
192 [[nodiscard]] virtual auto create_asset_loader(gsl::not_null<IDataLoader const*> data_loader) const -> AssetLoader = 0;
193};
194
197 public:
198 Waiter() = default;
199
200 explicit(false) Waiter(gsl::not_null<Context*> context) : m_context(context) {}
201
202 [[nodiscard]] auto is_active() const -> bool { return m_context != nullptr; }
203
204 [[nodiscard]] auto get_context() const -> Context* { return m_context.get(); }
205
206 private:
207 struct Deleter {
208 void operator()(Context* ptr) const {
209 if (!ptr) { return; }
210 ptr->wait_idle();
211 }
212 };
213
214 std::unique_ptr<Context, Deleter> m_context{};
215};
216} // namespace le
Set of AssetTypeLoaders mapped to their corrseponding AssetTypes.
Definition asset_loader.hpp:12
Calls Context::wait_idle() in its destructor.
Definition context.hpp:196
auto get_context() const -> Context *
Definition context.hpp:204
auto is_active() const -> bool
Definition context.hpp:202
Central API for most of the engine / framework.
Definition context.hpp:82
virtual auto set_vsync(Vsync vsync) -> bool=0
auto is_running() const -> bool
Check if Window is (and should remain) open.
std::span< std::uint32_t const > SpirV
Definition context.hpp:87
void cancel_window_close()
Reset the Window close flag.
void set_window_close()
Set the Window close flag.
virtual auto get_supported_vsync() const -> std::span< Vsync const >=0
virtual auto set_samples(vk::SampleCountFlagBits samples) -> bool=0
Set desired MSAA samples.
virtual auto begin_render(kvf::Color clear=kvf::black_v) -> IRenderer &=0
Begin rendering the primary RenderPass.
virtual auto get_resource_factory() const -> IResourceFactory const &=0
auto create_waiter() -> Waiter
virtual auto get_audio_mixer() const -> IAudioMixer &=0
virtual auto get_render_device() const -> kvf::RenderDevice const &=0
auto swapchain_extent() const -> vk::Extent2D
auto set_fullscreen(GLFWmonitor *target=nullptr) -> bool
Show window and set fullscreen.
auto display_ratio() const -> glm::vec2
auto get_refresh_rate() const -> std::int32_t
virtual auto get_default_shader() const -> IShader const &=0
virtual auto get_window() const -> GLFWwindow *=0
static auto create(CreateInfo const &create_info={}) -> std::unique_ptr< Context >
virtual auto get_frame_stats() const -> FrameStats const &=0
virtual auto get_samples() const -> vk::SampleCountFlagBits=0
virtual void present()=0
Submit recorded commands and present RenderTarget of primary RenderPass.
static constexpr auto min_render_scale_v
Definition context.hpp:90
virtual auto get_supported_samples() const -> vk::SampleCountFlags=0
virtual auto create_render_pass(vk::SampleCountFlagBits samples) const -> std::unique_ptr< IRenderPass >=0
void set_visible(bool visible)
Show/hide window.
virtual auto get_vsync() const -> Vsync=0
void set_title(klib::CString title)
auto framebuffer_size() const -> glm::ivec2
virtual auto next_frame() -> vk::CommandBuffer=0
Begin the next frame.
auto get_title() const -> klib::CString
virtual auto set_render_scale(float scale) -> bool=0
virtual auto get_renderer() const -> IRenderer const &=0
void lock_aspect_ratio(bool lock)
Lock or unlock the window aspect ratio.
void set_windowed(glm::ivec2 size={1280, 720})
Show window and set windowed with given size.
virtual auto get_render_scale() const -> float=0
virtual auto create_asset_loader(gsl::not_null< IDataLoader const * > data_loader) const -> AssetLoader=0
auto window_size() const -> glm::ivec2
auto main_pass_size() const -> glm::ivec2
auto is_fullscreen() const -> bool
auto unprojector(Viewport const &viewport, Transform const &view) const -> Unprojector
Definition context.hpp:116
virtual auto event_queue() const -> std::span< Event const >=0
static constexpr auto max_render_scale_v
Definition context.hpp:91
void wait_idle()
Wait for the graphics and audio devices to become idle.
Opaque audio mixer interface.
Definition audio_mixer.hpp:12
Interface for loading bytes from a data source (usually the filesystem).
Definition data_loader.hpp:14
Opaque interface for 2D render pass, owns a multi-sampled color RenderTarget.
Definition render_pass.hpp:8
Definition renderer.hpp:15
Factory for IResource derived types.
Definition resource_factory.hpp:10
Opaque interface for a Shader program.
Definition shader.hpp:7
Definition unprojector.hpp:7
Definition texture.hpp:9
Definition animation.hpp:8
constexpr auto default_window_flags_v
Default Window creation flags.
Definition context.hpp:50
WindowFlag
Window creation flags (GLFW window hints).
Definition context.hpp:30
@ FocusOnShow
Window is given input focus when shown.
@ ScaleFramebuffer
Framebuffer is resized based on content scale changes.
@ Decorated
Window is decorated (has title bar, close button, etc).
@ Maximized
Window is maximized on creation.
@ Resizeable
Window is resizable.
@ ScaleToMonitor
Content area is resized based on content scale changes.
@ Visible
Window is visible on creation.
constexpr auto enable_enum_bitops(PlatformFlag) -> bool
Definition context.hpp:27
Vsync
Definition vsync.hpp:8
std::variant< viewport::Dynamic, viewport::Letterbox > Viewport
Definition viewport.hpp:19
PlatformFlag
Platform flags (GLFW init hints).
Definition context.hpp:20
@ NoLibdecor
Disable libdecor (only relevant on Wayland).
@ ForceX11
Force X11 backend instead of Wayland (only relevant on Linux).
event::Event Event
Definition event.hpp:67
std::variant< WindowInfo, FullscreenInfo > WindowCreateInfo
Window creation parameters.
Definition context.hpp:65
Context creation parameters.
Definition context.hpp:68
vk::SampleCountFlagBits framebuffer_samples
Multi sampled anti-aliasing.
Definition context.hpp:76
kvf::RenderDeviceCreateInfo render_device
Render Device creation parameters.
Definition context.hpp:74
WindowCreateInfo window
Window creation parameters.
Definition context.hpp:72
PlatformFlag platform_flags
Platform flags.
Definition context.hpp:70
int sfx_buffers
Number of SFX buffers (concurrently playable).
Definition context.hpp:78
Definition frame_stats.hpp:6
Fullscreen window parameters.
Definition context.hpp:60
klib::CString title
Definition context.hpp:61
2D transformation.
Definition transform.hpp:8
Windowed window parameters.
Definition context.hpp:53
glm::ivec2 size
Definition context.hpp:54
WindowFlag flags
Definition context.hpp:56
klib::CString title
Definition context.hpp:55