Skip to content

Commit

Permalink
Simplified draw state
Browse files Browse the repository at this point in the history
Closes PistonDevelopers#996

- Removed draw_state dependency
- Fixed [PR 30742](rust-lang/rust#30724)
(default generic parameters will become a hard error)
  • Loading branch information
bvssvni committed Feb 3, 2016
1 parent 065ace5 commit df5f9b7
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 271 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ name = "graphics"
path = "./src/lib.rs"

[dependencies]
draw_state = "0.3.0"
interpolation = "0.1.0"
piston-texture = "0.3.0"
piston-viewport = "0.2.0"
Expand Down
53 changes: 0 additions & 53 deletions src/clip_draw_state.rs

This file was deleted.

7 changes: 3 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Transformation context
use DrawState;
use default_draw_state;
use math::{
abs_transform,
identity,
Expand Down Expand Up @@ -33,7 +32,7 @@ impl Context {
Context {
view: identity(),
transform: identity(),
draw_state: *default_draw_state(),
draw_state: Default::default(),
viewport: None,
}
}
Expand All @@ -54,7 +53,7 @@ impl Context {
Context {
view: mat,
transform: mat,
draw_state: *default_draw_state(),
draw_state: Default::default(),
viewport: Some(viewport),
}
}
Expand All @@ -75,7 +74,7 @@ impl Context {
Context {
view: mat,
transform: mat,
draw_state: *default_draw_state(),
draw_state: Default::default(),
viewport: None,
}
}
Expand Down
36 changes: 0 additions & 36 deletions src/default_draw_state.rs

This file was deleted.

125 changes: 125 additions & 0 deletions src/draw_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! Graphics draw state.
/// Graphics draw state used for blending, clipping and stencil rendering.
#[derive(Copy, Clone, PartialEq, Debug, PartialOrd)]
pub struct DrawState {
/// Scissor mask to use. If set, no pixel outside of this
/// rectangle (in screen space) will be written to as a result of rendering.
pub scissor: Option<[u32; 4]>,
/// Stencil test to use. If None, no stencil testing is done.
pub stencil: Option<Stencil>,
/// Blend function to use. If None, blending is disabled.
pub blend: Option<Blend>,
}

impl Default for DrawState {
fn default() -> Self {
DrawState::new_alpha()
}
}

impl DrawState {
/// Uses alpha blending.
pub fn new_alpha() -> DrawState {
DrawState {
blend: Some(Blend::Alpha),
stencil: None,
scissor: None,
}
}

/// Draws to stencil buffer with value 255.
/// This can be used for clipping.
pub fn new_clip() -> DrawState {
DrawState {
blend: Some(Blend::Alpha),
stencil: Some(Stencil::Clip(255)),
scissor: None,
}
}

/// Tests against stencil buffer with value 255.
/// Draws inside the shape defined by stencil buffer.
pub fn new_inside() -> DrawState {
DrawState {
blend: Some(Blend::Alpha),
stencil: Some(Stencil::Inside(255)),
scissor: None,
}
}

/// Tests against stencil buffer with value 255.
/// Draws outside the shape defined by stencil buffer.
pub fn new_outside() -> DrawState {
DrawState {
blend: Some(Blend::Alpha),
stencil: Some(Stencil::Outside(255)),
scissor: None,
}
}

/// Sets blending.
pub fn blend(mut self, blend: Blend) -> DrawState {
self.blend = Some(blend);
self
}

/// Sets scissor `[x, y, w, h]`.
pub fn scissor(mut self, scissor: [u32; 4]) -> DrawState {
self.scissor = Some(scissor);
self
}
}

/// The blend setting to use when drawing.
///
/// Using presets since some backends need one pipeline state object instance
/// per blending technique.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub enum Blend {
/// Alpha blending (allows semi-transparent pixels).
///
/// ```ignore
/// dest_color' = src_color * src_alpha + dest_color * (1 - src_alpha)
/// dest_alpha' = src_alpha + dest_alpha
/// ```
Alpha,
/// Additive blending.
///
/// ```ignore
/// dest_color' = src_color + dest_color
/// dest_alpha' = src_alpha + dest_alpha
/// ```
Add,
/// Multiply color components.
///
/// ```ignore
/// dest_color' = src_color * dest_color
/// dest_alpha' = src_alpha * dest_alpha
/// ```
Multiply,
/// Invert colors when rendering a white shape.
///
/// ```ignore
/// dest_color' = ref_color - src_color
/// dest_alpha' = dest_alpha
/// ```
///
/// When combining two fragments, subtract the destination color from a constant color
/// using the source color as weight. Has an invert effect with the constant color
/// as base and source color controlling displacement from the base color.
/// A white source color and a white value results in plain invert.
/// The output alpha is same as destination alpha.
Invert,
}

/// Stencil buffer settings.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub enum Stencil {
/// Draw to stencil buffer.
Clip(u8),
/// Draw pixels that have stencil value.
Inside(u8),
/// Draw pixels that does not have stencil value.
Outside(u8),
}
30 changes: 0 additions & 30 deletions src/graphics_draw_state.rs

This file was deleted.

53 changes: 0 additions & 53 deletions src/inside_draw_state.rs

This file was deleted.

Loading

0 comments on commit df5f9b7

Please # to comment.