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
use crate::internal::*;

enum_from_primitive! {
    #[cfg_attr(tarpaulin, skip)]
    #[derive(Debug, PartialEq, Copy, Clone)]
    /// Defines a blend factor (F_src or F_dst) for the blending equation:
    ///
    /// `C_result = C_src * F_src + C_dst * F_dst`
    ///
    /// Where:
    /// * `C_result` - output color
    /// * `C_src` - source color
    /// * `F_src` - source blend factor
    /// * `C_dst` - destination color (the one in the framebuffer)
    /// * `F_dst` - destination blend factor
    pub enum BlendFactor
    {
        /// Color is discarded (multiplied by 0)
        Zero = GL_ZERO as isize,
        /// Color is used as is (multiplied by 1)
        One = GL_ONE as isize,
        /// Color is multiplied by source color
        SrcColor = GL_SRC_COLOR as isize,
        /// Color is multiplied by (vec(1) - source color)
        OneMinusSrcColor = GL_ONE_MINUS_SRC_COLOR as isize,
        /// Color is multiplied by destination color
        DstColor = GL_DST_COLOR as isize,
        /// Color is multiplied by (vec(1) - destination color)
        OneMinusDstColor = GL_ONE_MINUS_DST_COLOR as isize,
        /// Color is multiplied by source alpha value
        SrcAlpha = GL_SRC_ALPHA as isize,
        /// Color is multiplied by (1 - source alpha)
        OneMinusSrcAlpha = GL_ONE_MINUS_SRC_ALPHA as isize,
        /// Color is multiplied by destination alpha value
        DstAlpha = GL_DST_ALPHA as isize,
        /// Color is multiplied by (1 - destination alpha)
        OneMinusDstAlpha = GL_ONE_MINUS_DST_ALPHA as isize
    }
}

#[derive(Debug, PartialEq, Copy, Clone)]
/// Defines a pair of blend factors (F_src and F_dst) for the blending equation:
/// 
/// `C_result = C_src * F_src + C_dst * F_dst`
/// 
/// Where:
/// * `C_result` - output color
/// * `C_src` - source color
/// * `F_src` - source blend factor
/// * `C_dst` - destination color (the one in the framebuffer)
/// * `F_dst` - destination blend factor
pub struct BlendMode
{
    /// Source blend factor
    pub src: BlendFactor,
    /// Destination blend factor
    pub dst: BlendFactor
}

/// Normal blending mode (alpha-blend)
pub const NORMAL: BlendMode = BlendMode {
    src: BlendFactor::SrcAlpha,
    dst: BlendFactor::OneMinusSrcAlpha
};

/// Additive blending mode
pub const ADDITIVE: BlendMode = BlendMode {
    src: BlendFactor::One,
    dst: BlendFactor::One
};

/// Multiplicative blending mode
pub const MULTIPLY: BlendMode = BlendMode {
    src: BlendFactor::DstColor,
    dst: BlendFactor::Zero
};

impl From<BlendMode> for BLZ_BlendFunc {
    fn from(mode: BlendMode) -> BLZ_BlendFunc {
        BLZ_BlendFunc {
            source: mode.src as u32,
            destination: mode.dst as u32
        }
    }
}

/// Sets the current blending mode to be used.
/// Note - the blend mode is used when things actually get drawn on the screen,
/// e.g. `SpriteBatch::present`, `StaticBatch::present` or `Immediate::draw`.
pub fn set_blend_mode(mode: BlendMode) {
    unsafe { BLZ_SetBlendMode(mode.into()); }
}