freya_winit/drivers/
mod.rs1mod gl;
2#[cfg(feature = "vulkan")]
3mod vulkan;
4
5use freya_engine::prelude::Surface as SkiaSurface;
6use winit::{
7 dpi::PhysicalSize,
8 event_loop::ActiveEventLoop,
9 window::{
10 Window,
11 WindowAttributes,
12 },
13};
14
15use crate::config::WindowConfig;
16
17#[allow(clippy::large_enum_variant)]
18pub enum GraphicsDriver {
19 OpenGl(gl::OpenGLDriver),
20 #[cfg(feature = "vulkan")]
21 Vulkan(vulkan::VulkanDriver),
22}
23
24impl GraphicsDriver {
25 pub fn new(
26 event_loop: &ActiveEventLoop,
27 window_attributes: WindowAttributes,
28 window_config: &WindowConfig,
29 ) -> (Self, Window) {
30 #[cfg(feature = "vulkan")]
31 {
32 let (driver, window) =
33 vulkan::VulkanDriver::new(event_loop, window_attributes, window_config);
34
35 return (Self::Vulkan(driver), window);
36 }
37
38 #[allow(unreachable_code)]
39 let (driver, window) = gl::OpenGLDriver::new(event_loop, window_attributes, window_config);
40
41 (Self::OpenGl(driver), window)
42 }
43
44 #[allow(unused)]
45 pub fn present(
46 &mut self,
47 size: PhysicalSize<u32>,
48 window: &Window,
49 render: impl FnOnce(&mut SkiaSurface),
50 ) {
51 match self {
52 Self::OpenGl(gl) => gl.present(window, render),
53 #[cfg(feature = "vulkan")]
54 Self::Vulkan(vk) => vk.present(size, window, render),
55 _ => unimplemented!("Enable `gl` or `vulkan` features."),
56 }
57 }
58
59 #[allow(unused)]
60 pub fn resize(&mut self, size: PhysicalSize<u32>) {
61 match self {
62 Self::OpenGl(gl) => gl.resize(size),
63 #[cfg(feature = "vulkan")]
64 Self::Vulkan(vk) => vk.resize(size),
65 _ => unimplemented!("Enable `gl` or `vulkan` features."),
66 }
67 }
68}