Skip to content

Commit df5f9b7

Browse files
committed
Simplified draw state
Closes PistonDevelopers#996 - Removed draw_state dependency - Fixed [PR 30742](rust-lang/rust#30724) (default generic parameters will become a hard error)
1 parent 065ace5 commit df5f9b7

10 files changed

+161
-271
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ name = "graphics"
2222
path = "./src/lib.rs"
2323

2424
[dependencies]
25-
draw_state = "0.3.0"
2625
interpolation = "0.1.0"
2726
piston-texture = "0.3.0"
2827
piston-viewport = "0.2.0"

src/clip_draw_state.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/context.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Transformation context
22
33
use DrawState;
4-
use default_draw_state;
54
use math::{
65
abs_transform,
76
identity,
@@ -33,7 +32,7 @@ impl Context {
3332
Context {
3433
view: identity(),
3534
transform: identity(),
36-
draw_state: *default_draw_state(),
35+
draw_state: Default::default(),
3736
viewport: None,
3837
}
3938
}
@@ -54,7 +53,7 @@ impl Context {
5453
Context {
5554
view: mat,
5655
transform: mat,
57-
draw_state: *default_draw_state(),
56+
draw_state: Default::default(),
5857
viewport: Some(viewport),
5958
}
6059
}
@@ -75,7 +74,7 @@ impl Context {
7574
Context {
7675
view: mat,
7776
transform: mat,
78-
draw_state: *default_draw_state(),
77+
draw_state: Default::default(),
7978
viewport: None,
8079
}
8180
}

src/default_draw_state.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/draw_state.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! Graphics draw state.
2+
3+
/// Graphics draw state used for blending, clipping and stencil rendering.
4+
#[derive(Copy, Clone, PartialEq, Debug, PartialOrd)]
5+
pub struct DrawState {
6+
/// Scissor mask to use. If set, no pixel outside of this
7+
/// rectangle (in screen space) will be written to as a result of rendering.
8+
pub scissor: Option<[u32; 4]>,
9+
/// Stencil test to use. If None, no stencil testing is done.
10+
pub stencil: Option<Stencil>,
11+
/// Blend function to use. If None, blending is disabled.
12+
pub blend: Option<Blend>,
13+
}
14+
15+
impl Default for DrawState {
16+
fn default() -> Self {
17+
DrawState::new_alpha()
18+
}
19+
}
20+
21+
impl DrawState {
22+
/// Uses alpha blending.
23+
pub fn new_alpha() -> DrawState {
24+
DrawState {
25+
blend: Some(Blend::Alpha),
26+
stencil: None,
27+
scissor: None,
28+
}
29+
}
30+
31+
/// Draws to stencil buffer with value 255.
32+
/// This can be used for clipping.
33+
pub fn new_clip() -> DrawState {
34+
DrawState {
35+
blend: Some(Blend::Alpha),
36+
stencil: Some(Stencil::Clip(255)),
37+
scissor: None,
38+
}
39+
}
40+
41+
/// Tests against stencil buffer with value 255.
42+
/// Draws inside the shape defined by stencil buffer.
43+
pub fn new_inside() -> DrawState {
44+
DrawState {
45+
blend: Some(Blend::Alpha),
46+
stencil: Some(Stencil::Inside(255)),
47+
scissor: None,
48+
}
49+
}
50+
51+
/// Tests against stencil buffer with value 255.
52+
/// Draws outside the shape defined by stencil buffer.
53+
pub fn new_outside() -> DrawState {
54+
DrawState {
55+
blend: Some(Blend::Alpha),
56+
stencil: Some(Stencil::Outside(255)),
57+
scissor: None,
58+
}
59+
}
60+
61+
/// Sets blending.
62+
pub fn blend(mut self, blend: Blend) -> DrawState {
63+
self.blend = Some(blend);
64+
self
65+
}
66+
67+
/// Sets scissor `[x, y, w, h]`.
68+
pub fn scissor(mut self, scissor: [u32; 4]) -> DrawState {
69+
self.scissor = Some(scissor);
70+
self
71+
}
72+
}
73+
74+
/// The blend setting to use when drawing.
75+
///
76+
/// Using presets since some backends need one pipeline state object instance
77+
/// per blending technique.
78+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
79+
pub enum Blend {
80+
/// Alpha blending (allows semi-transparent pixels).
81+
///
82+
/// ```ignore
83+
/// dest_color' = src_color * src_alpha + dest_color * (1 - src_alpha)
84+
/// dest_alpha' = src_alpha + dest_alpha
85+
/// ```
86+
Alpha,
87+
/// Additive blending.
88+
///
89+
/// ```ignore
90+
/// dest_color' = src_color + dest_color
91+
/// dest_alpha' = src_alpha + dest_alpha
92+
/// ```
93+
Add,
94+
/// Multiply color components.
95+
///
96+
/// ```ignore
97+
/// dest_color' = src_color * dest_color
98+
/// dest_alpha' = src_alpha * dest_alpha
99+
/// ```
100+
Multiply,
101+
/// Invert colors when rendering a white shape.
102+
///
103+
/// ```ignore
104+
/// dest_color' = ref_color - src_color
105+
/// dest_alpha' = dest_alpha
106+
/// ```
107+
///
108+
/// When combining two fragments, subtract the destination color from a constant color
109+
/// using the source color as weight. Has an invert effect with the constant color
110+
/// as base and source color controlling displacement from the base color.
111+
/// A white source color and a white value results in plain invert.
112+
/// The output alpha is same as destination alpha.
113+
Invert,
114+
}
115+
116+
/// Stencil buffer settings.
117+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
118+
pub enum Stencil {
119+
/// Draw to stencil buffer.
120+
Clip(u8),
121+
/// Draw pixels that have stencil value.
122+
Inside(u8),
123+
/// Draw pixels that does not have stencil value.
124+
Outside(u8),
125+
}

src/graphics_draw_state.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/inside_draw_state.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)