swamp_render/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5pub mod anim;
6pub mod prelude;
7
8use int_math::UVec2;
9use std::fmt;
10
11pub enum VirtualScale {
12    IntScale(u16),
13    FloatScale(f32),
14}
15
16fn gcd(a: u16, b: u16) -> u16 {
17    if b == 0 { a } else { gcd(b, a % b) }
18}
19
20fn aspect_ratio(size: UVec2) -> (u16, u16) {
21    let divisor = gcd(size.x, size.y);
22    (size.x / divisor, size.y / divisor)
23}
24
25#[derive(Debug)]
26pub enum AspectRatio {
27    Ratio16By9,
28    Ratio21By9,
29    Ratio16By10,
30    Ratio4By3,
31    Other(f32),
32}
33
34impl AspectRatio {
35    fn convert(value: UVec2) -> Self {
36        let aspect = aspect_ratio(value);
37        match aspect {
38            (16, 9) => Self::Ratio16By9,
39            (21, 9) => Self::Ratio21By9,
40            (16, 10) => Self::Ratio16By10,
41            (4, 3) => Self::Ratio4By3,
42            _ => Self::Other(value.x as f32 / value.y as f32),
43        }
44    }
45}
46
47impl From<(u16, u16)> for AspectRatio {
48    fn from(value: (u16, u16)) -> Self {
49        Self::convert(value.into())
50    }
51}
52
53impl From<UVec2> for AspectRatio {
54    fn from(value: UVec2) -> Self {
55        Self::convert(value)
56    }
57}
58
59impl fmt::Display for AspectRatio {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::Ratio16By9 => write!(f, "16:9"),
63            Self::Ratio16By10 => write!(f, "16:10"),
64            Self::Ratio21By9 => write!(f, "21:9"),
65            Self::Ratio4By3 => write!(f, "4:3"),
66            Self::Other(vec) => write!(f, "aspect ratio: {vec:?}"),
67        }
68    }
69}
70
71#[derive(Debug, Copy, Clone)]
72pub struct Color {
73    r: u8,
74    g: u8,
75    b: u8,
76    a: u8,
77}
78
79impl Default for Color {
80    fn default() -> Self {
81        Self {
82            r: 255,
83            g: 255,
84            b: 255,
85            a: 255,
86        }
87    }
88}
89
90impl Color {
91    #[must_use]
92    pub fn from_f32(r: f32, g: f32, b: f32, a: f32) -> Self {
93        Self::from_octet(
94            (r * 255.0) as u8,
95            (g * 255.0) as u8,
96            (b * 255.0) as u8,
97            (a * 255.0) as u8,
98        )
99    }
100
101    #[must_use]
102    pub fn to_f32_slice(&self) -> [f32; 4] {
103        [
104            self.r as f32 / 255.0,
105            self.g as f32 / 255.0,
106            self.b as f32 / 255.0,
107            self.a as f32 / 255.0,
108        ]
109    }
110
111    #[must_use]
112    pub const fn from_octet(r: u8, g: u8, b: u8, a: u8) -> Self {
113        Self { r, g, b, a }
114    }
115
116    #[must_use]
117    pub fn to_f64(&self) -> (f64, f64, f64, f64) {
118        (
119            self.r as f64 / 255.0,
120            self.g as f64 / 255.0,
121            self.b as f64 / 255.0,
122            self.a as f64 / 255.0,
123        )
124    }
125}
126
127#[derive(Debug, Eq, PartialEq)]
128pub enum ViewportStrategy {
129    /// Tries to set the viewport to fit the virtual surface size within the physical surface size.
130    /// Depending on resolution, it can cause black borders, both vertically and horizontally.
131    /// Always keeps the aspect ratio, and is "pixel perfect"
132    FitIntegerScaling(UVec2),
133
134    /// Tries to set the viewport to fit the virtual surface size within the physical surface size.
135    /// Depending on resolution, it can cause "black borders", *either* vertically and horizontally.
136    /// Always keeps the aspect ratio, but might not be pixel perfect
137    FitFloatScaling(UVec2),
138
139    /// The viewport will be the same as the physical size.
140    MatchPhysicalSize,
141}