ombre 0.6.7

Shadowy game and graphics library for Rust
Documentation
use super::*;

#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
pub struct TextureId(id::Id);

impl TextureId {
    /// Get the next texture id.
    pub fn next() -> Self {
        Self(id::next())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Hash, Default)]
pub enum FilterMode {
    Linear,
    #[default]
    Nearest,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub enum TextureAccess {
    /// A regular read-only texture.
    #[default]
    Static,
    /// Can be used as the output of a render pass.
    RenderTarget,
}

#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub enum TextureTarget {
    #[default]
    Texture2D,
}

/// Supported texture formats.
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Default)]
pub enum TextureFormat {
    /// 32-bit color with alpha.
    #[default]
    Rgba8,
    /// 24-bit color.
    Rgb8,
    /// 32-bit floating point depth.
    Depth,
    /// 8-bit alpha.
    Alpha,
}

impl TextureFormat {
    /// Returns the size in bytes of a texture with the given dimensions.
    pub fn size<T: From<u32> + std::ops::Mul<Output = T>>(self, dimensions: Size<T>) -> T {
        let area = dimensions.area();
        match self {
            TextureFormat::Rgb8 => T::from(3) * area,
            TextureFormat::Rgba8 => T::from(4) * area,
            TextureFormat::Depth => T::from(4) * area,
            TextureFormat::Alpha => T::from(1) * area,
        }
    }
}

/// How texture sampling should wrap.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub enum TextureWrap {
    /// Samples at coordinate `x + 1` map to `1`.
    #[default]
    Clamp,
    /// Samples at coordinate `x + 1` map to `x`.
    Repeat,
    /// Samples at coordinate `x + 1` map to `1 - x`.
    Mirror,
}

/// Describes a texture.
#[derive(Debug, Copy, Clone)]
pub struct Texture<T> {
    pub data: T,
    pub size: Size<u32>,
    pub access: TextureAccess,
    pub target: TextureTarget,
    pub format: TextureFormat,
    pub wrap: TextureWrap,
    pub min_filter: FilterMode,
    pub mag_filter: FilterMode,
}

impl<T: Default> Default for Texture<T> {
    fn default() -> Self {
        Texture {
            data: T::default(),
            access: TextureAccess::default(),
            target: TextureTarget::default(),
            format: TextureFormat::default(),
            wrap: TextureWrap::default(),
            min_filter: FilterMode::default(),
            mag_filter: FilterMode::default(),
            size: Size::default(),
        }
    }
}