raw_gl_context/
lib.rs

1use raw_window_handle::HasRawWindowHandle;
2
3use std::ffi::c_void;
4use std::marker::PhantomData;
5
6#[cfg(target_os = "windows")]
7mod win;
8#[cfg(target_os = "windows")]
9use win as platform;
10
11#[cfg(target_os = "linux")]
12mod x11;
13#[cfg(target_os = "linux")]
14use crate::x11 as platform;
15
16#[cfg(target_os = "macos")]
17mod macos;
18#[cfg(target_os = "macos")]
19use macos as platform;
20
21pub struct GlConfig {
22    pub version: (u8, u8),
23    pub profile: Profile,
24    pub red_bits: u8,
25    pub blue_bits: u8,
26    pub green_bits: u8,
27    pub alpha_bits: u8,
28    pub depth_bits: u8,
29    pub stencil_bits: u8,
30    pub samples: Option<u8>,
31    pub srgb: bool,
32    pub double_buffer: bool,
33    pub vsync: bool,
34}
35
36impl Default for GlConfig {
37    fn default() -> Self {
38        GlConfig {
39            version: (3, 2),
40            profile: Profile::Core,
41            red_bits: 8,
42            blue_bits: 8,
43            green_bits: 8,
44            alpha_bits: 8,
45            depth_bits: 24,
46            stencil_bits: 8,
47            samples: None,
48            srgb: true,
49            double_buffer: true,
50            vsync: false,
51        }
52    }
53}
54
55#[derive(PartialEq, Eq)]
56pub enum Profile {
57    Compatibility,
58    Core,
59}
60
61#[derive(Debug)]
62pub enum GlError {
63    InvalidWindowHandle,
64    VersionNotSupported,
65    CreationFailed,
66}
67
68pub struct GlContext {
69    context: platform::GlContext,
70    phantom: PhantomData<*mut ()>,
71}
72
73impl GlContext {
74    pub fn create(
75        parent: &impl HasRawWindowHandle,
76        config: GlConfig,
77    ) -> Result<GlContext, GlError> {
78        platform::GlContext::create(parent, config).map(|context| GlContext {
79            context,
80            phantom: PhantomData,
81        })
82    }
83
84    pub fn make_current(&self) {
85        self.context.make_current();
86    }
87
88    pub fn make_not_current(&self) {
89        self.context.make_not_current();
90    }
91
92    pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
93        self.context.get_proc_address(symbol)
94    }
95
96    pub fn swap_buffers(&self) {
97        self.context.swap_buffers();
98    }
99}