aboutsummaryrefslogtreecommitdiff
path: root/adapters/fparkan-platform-sdl/src/lib.rs
blob: f573885b47b9a648fca0700d9d304ead0e47decd (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#![forbid(unsafe_code)]
//! SDL platform adapter boundary stubs behind safe `FParkan` ports.

use fparkan_platform::{
    EventSource, GraphicsContextRequest, GraphicsProfile, PhysicalSize, PlatformError,
    PlatformEvent, Version, WindowPort,
};

/// Adapter capabilities compiled into this package.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SdlAdapterCapabilities {
    /// Supported graphics context requests in preference order.
    pub graphics: Vec<GraphicsContextRequest>,
    /// Whether adapter-owned code is free of `unsafe`.
    pub project_owned_unsafe_free: bool,
}

impl Default for SdlAdapterCapabilities {
    fn default() -> Self {
        Self {
            graphics: vec![
                GraphicsContextRequest {
                    profile: GraphicsProfile::DesktopCore,
                    version: Version { major: 3, minor: 3 },
                },
                GraphicsContextRequest {
                    profile: GraphicsProfile::Embedded,
                    version: Version { major: 2, minor: 0 },
                },
            ],
            project_owned_unsafe_free: true,
        }
    }
}

/// Returns whether the project-owned adapter boundary avoids `unsafe`.
#[must_use]
pub fn project_owned_layer_unsafe_free() -> bool {
    SdlAdapterCapabilities::default().project_owned_unsafe_free
}

/// In-memory event source used by adapter smoke tests before a concrete SDL
/// runtime is selected.
#[derive(Clone, Debug, Default)]
pub struct SdlEventSourceStub {
    pending: Vec<PlatformEvent>,
}

impl SdlEventSourceStub {
    /// Creates an event source with deterministic pending events.
    #[must_use]
    pub fn new(pending: Vec<PlatformEvent>) -> Self {
        Self { pending }
    }
}

impl EventSource for SdlEventSourceStub {
    fn poll(&mut self, out: &mut Vec<PlatformEvent>) -> Result<(), PlatformError> {
        out.append(&mut self.pending);
        Ok(())
    }
}

/// Safe window-port stub with SDL-compatible drawable-size semantics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SdlWindowStub {
    size: PhysicalSize,
    presents: u64,
}

impl SdlWindowStub {
    /// Creates a stub window with a fixed drawable size.
    #[must_use]
    pub fn new(size: PhysicalSize) -> Self {
        Self { size, presents: 0 }
    }

    /// Number of successful present calls.
    #[must_use]
    pub fn presents(&self) -> u64 {
        self.presents
    }
}

impl WindowPort for SdlWindowStub {
    fn drawable_size(&self) -> PhysicalSize {
        self.size
    }

    fn present(&mut self) -> Result<(), PlatformError> {
        self.presents = self.presents.saturating_add(1);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn adapter_boundary_is_project_owned_unsafe_free() {
        assert!(project_owned_layer_unsafe_free());
        assert_eq!(SdlAdapterCapabilities::default().graphics.len(), 2);
    }

    #[test]
    fn event_source_and_window_ports_are_deterministic() -> Result<(), PlatformError> {
        let mut source = SdlEventSourceStub::new(vec![PlatformEvent::Quit]);
        let mut events = Vec::new();
        source.poll(&mut events)?;
        source.poll(&mut events)?;
        assert_eq!(events, vec![PlatformEvent::Quit]);

        let mut window = SdlWindowStub::new(PhysicalSize {
            width: 320,
            height: 240,
        });
        assert_eq!(window.drawable_size().width, 320);
        window.present()?;
        assert_eq!(window.presents(), 1);
        Ok(())
    }
}