rich_sdl2_rust/video/gl_context/
buffer.rs

1use crate::{bind, Result, SdlError};
2
3use super::GlContext;
4
5/// A kind of the interval of swapping buffers in an OpenGL context.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum IntervalKind {
9    /// Vertical syncing but swap immediately on failed.
10    AdaptiveVerticalSync,
11    /// Always swap immediately.
12    Immediate,
13    /// Vertical syncing.
14    VerticalSync,
15}
16
17/// An extension for [`GlContext`] to add methods for control buffers.
18pub trait BufferExt {
19    /// Sets the interval mode of swapping buffers.
20    ///
21    /// # Errors
22    ///
23    /// Returns `Err` if the feature swapping them is unsupported.
24    fn set_swap_interval(&self, interval_kind: IntervalKind) -> Result<()>;
25
26    /// Swaps buffers immediately.
27    fn swap_buffer(&self);
28}
29
30impl BufferExt for GlContext<'_> {
31    fn set_swap_interval(&self, interval_kind: IntervalKind) -> Result<()> {
32        let ret = unsafe {
33            bind::SDL_GL_SetSwapInterval(match interval_kind {
34                IntervalKind::AdaptiveVerticalSync => -1,
35                IntervalKind::Immediate => 0,
36                IntervalKind::VerticalSync => 1,
37            })
38        };
39        if ret != 0 {
40            return Err(SdlError::UnsupportedFeature);
41        }
42        Ok(())
43    }
44
45    fn swap_buffer(&self) {
46        unsafe { bind::SDL_GL_SwapWindow(self.window.as_ptr()) }
47    }
48}