1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod gl;

use freya_engine::prelude::Surface as SkiaSurface;
pub use gl::*;
use glutin::surface::GlSurface;
use winit::{
    dpi::PhysicalSize,
    event_loop::ActiveEventLoop,
    window::{
        Window,
        WindowAttributes,
    },
};

use crate::LaunchConfig;

pub enum GraphicsDriver {
    OpenGl(OpenGLDriver),
}

impl GraphicsDriver {
    pub fn new<State: Clone + 'static>(
        event_loop: &ActiveEventLoop,
        window_attributes: WindowAttributes,
        config: &LaunchConfig<State>,
    ) -> (Self, Window, SkiaSurface) {
        let (driver, window, surface) = OpenGLDriver::new(event_loop, window_attributes, config);
        (Self::OpenGl(driver), window, surface)
    }

    pub fn make_current(&mut self) {
        match self {
            Self::OpenGl(gl) => gl.make_current(),
        }
    }

    pub fn flush_and_submit(&mut self) {
        match self {
            Self::OpenGl(gl) => {
                gl.gr_context.flush_and_submit();
                gl.gl_surface.swap_buffers(&gl.gl_context).unwrap();
            }
        }
    }

    pub fn resize(&mut self, size: PhysicalSize<u32>) -> (SkiaSurface, SkiaSurface) {
        match self {
            Self::OpenGl(gl) => gl.resize(size),
        }
    }
}