aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-platform/src/lib.rs
blob: cfa021b1b3083c41337788c5de0baf0228a9a735 (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
#![forbid(unsafe_code)]
//! Platform ports for clocks, input, events, windows, and graphics requests.

/// Monotonic instant.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct MonotonicInstant(pub u64);

/// Monotonic clock.
pub trait MonotonicClock {
    /// Current instant.
    fn now(&self) -> MonotonicInstant;
}

/// Platform event.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PlatformEvent {
    /// Quit requested.
    Quit,
}

/// Platform error.
#[derive(Debug)]
pub enum PlatformError {
    /// Backend failed.
    Backend,
}

impl std::fmt::Display for PlatformError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

impl std::error::Error for PlatformError {}

/// Event source.
pub trait EventSource {
    /// Polls events.
    ///
    /// # Errors
    ///
    /// Returns [`PlatformError`] when the backend cannot collect events.
    fn poll(&mut self, out: &mut Vec<PlatformEvent>) -> Result<(), PlatformError>;
}

/// Physical size.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PhysicalSize {
    /// Width.
    pub width: u32,
    /// Height.
    pub height: u32,
}

/// Window port.
pub trait WindowPort {
    /// Drawable size.
    fn drawable_size(&self) -> PhysicalSize;
    /// Presents.
    ///
    /// # Errors
    ///
    /// Returns [`PlatformError`] when the backend cannot present the current
    /// frame.
    fn present(&mut self) -> Result<(), PlatformError>;
}

/// Graphics profile.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GraphicsProfile {
    /// Desktop core.
    DesktopCore,
    /// Embedded profile.
    Embedded,
}

/// Version.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Version {
    /// Major.
    pub major: u8,
    /// Minor.
    pub minor: u8,
}

/// Graphics context request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GraphicsContextRequest {
    /// Profile.
    pub profile: GraphicsProfile,
    /// Version.
    pub version: Version,
}