Skip to content

Commit

Permalink
Copy draw state code from Gfx
Browse files Browse the repository at this point in the history
Closes #154

- Detect changes from previous draw state
- Added a `draw_state` module for reuse
- Added `GlGraphics::use_draw_state`
- Added `GlGraphics::clear_draw_state`
  • Loading branch information
bvssvni committed Oct 7, 2015
1 parent 95e2be5 commit e367501
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "piston2d-opengl_graphics"
version = "0.14.0"
version = "0.14.1"
authors = [
"bvssvni <[email protected]>",
"Coeuvre <[email protected]>",
Expand Down
54 changes: 34 additions & 20 deletions src/back_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ pub struct GlGraphics {
textured: Textured,
// Keeps track of the current shader program.
current_program: Option<GLuint>,
// Keeps track of the current draw state.
current_draw_state: Option<DrawState>,
}

impl<'a> GlGraphics {
Expand All @@ -223,6 +225,7 @@ impl<'a> GlGraphics {
colored: Colored::new(glsl),
textured: Textured::new(glsl),
current_program: None,
current_draw_state: None,
}
}

Expand Down Expand Up @@ -261,6 +264,33 @@ impl<'a> GlGraphics {
self.current_program = None
}

/// Sets the current draw state, by detecting changes.
pub fn use_draw_state(&mut self, draw_state: &DrawState) {
match self.current_draw_state {
None => {
draw_state::bind_scissor(draw_state.scissor);
draw_state::bind_primitive(draw_state.primitive);
draw_state::bind_multi_sample(draw_state.multi_sample);
draw_state::bind_depth(draw_state.depth);
draw_state::bind_stencil(draw_state.stencil,
draw_state.primitive.get_cull_face());
draw_state::bind_blend(draw_state.blend);
draw_state::bind_color_mask(draw_state.color_mask);
}
Some(ref old_state) => {
draw_state::bind_state(old_state, draw_state);
}
}
self.current_draw_state = Some(*draw_state);
}

/// Unsets the current draw state.
///
/// This forces the current draw state to be set on next drawing call.
pub fn clear_draw_state(&mut self) {
self.current_draw_state = None;
}

/// Draws graphics.
pub fn draw<F>(&mut self, viewport: Viewport, f: F)
where
Expand Down Expand Up @@ -321,21 +351,13 @@ impl Graphics for GlGraphics {
where F: FnMut(&mut FnMut(&[f32]))
{
{
// Set shader program.
// Set shader program and draw state.
let shader_program = self.colored.program;
self.use_program(shader_program);
self.use_draw_state(draw_state);
}
let ref mut shader = self.colored;

draw_state::bind_scissor(draw_state.scissor);
draw_state::bind_primitive(draw_state.primitive);
draw_state::bind_multi_sample(draw_state.multi_sample);
draw_state::bind_depth(draw_state.depth);
draw_state::bind_stencil(draw_state.stencil,
draw_state.primitive.get_cull_face());
draw_state::bind_blend(draw_state.blend);
draw_state::bind_color_mask(draw_state.color_mask);

unsafe {
gl::BindVertexArray(shader.vao);
gl::Uniform4f(shader.color, color[0], color[1], color[2], color[3]);
Expand Down Expand Up @@ -370,21 +392,13 @@ impl Graphics for GlGraphics {
where F: FnMut(&mut FnMut(&[f32], &[f32]))
{
{
// Set shader program.
// Set shader program and draw state.
let shader_program = self.textured.program;
self.use_program(shader_program);
self.use_draw_state(draw_state);
}
let ref mut shader = self.textured;

draw_state::bind_scissor(draw_state.scissor);
draw_state::bind_primitive(draw_state.primitive);
draw_state::bind_multi_sample(draw_state.multi_sample);
draw_state::bind_depth(draw_state.depth);
draw_state::bind_stencil(draw_state.stencil,
draw_state.primitive.get_cull_face());
draw_state::bind_blend(draw_state.blend);
draw_state::bind_color_mask(draw_state.color_mask);

let texture = texture.get_id();
unsafe {
gl::BindTexture(gl::TEXTURE_2D, texture);
Expand Down
2 changes: 1 addition & 1 deletion src/draw_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn bind_state(old_state: &DrawState, new_state: &DrawState) {
bind_multi_sample(new_state.multi_sample);
}
if old_state.scissor != new_state.scissor {
bind_scissor(old_state.scissor);
bind_scissor(new_state.scissor);
}
if old_state.depth != new_state.depth
|| old_state.stencil != new_state.stencil
Expand Down

0 comments on commit e367501

Please sign in to comment.