♻️ refactor

main
_ 2021-12-18 22:22:20 +00:00
parent ac85cf2bd1
commit 47e835005b
4 changed files with 81 additions and 72 deletions

View File

@ -172,65 +172,23 @@ async fn main () -> Result <()> {
let passes = vec![
// Clear everything
Pass {
iso: IsoGlState {
shader_id: None,
flags: Default::default (),
front_face: None,
stencil: None,
depth_func: None,
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (255),
},
},
Pass::default ()
.color_mask ([1, 1, 1, 1])
.depth_mask (1)
.clone (),
// Draw world
Pass {
iso: IsoGlState {
shader_id: Some (shaders [0].get_id ()),
flags: hashmap! {
gl::CULL_FACE => true,
gl::DEPTH_TEST => true,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => false,
},
front_face: Some (FrontFace::Cw),
stencil: Some (StencilState {
func: StencilFuncState {
func: StencilFunc::Always,
reference: 0,
mask: 0,
},
op: StencilOpState {
sfail: StencilOp::Keep,
dpfail: StencilOp::Keep,
dppass: StencilOp::Keep,
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (0),
},
},
// Draw UI
Pass {
iso: IsoGlState {
shader_id: Some (shaders [0].get_id ()),
flags: hashmap! {
gl::CULL_FACE => false,
gl::DEPTH_TEST => false,
gl::TEXTURE_2D => true,
gl::STENCIL_TEST => false,
},
front_face: None,
stencil: None,
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
depth_mask: Some (1),
stencil_mask: Some (0),
},
},
Pass::default ()
.shader (&shaders [0])
.flags ([
(gl::CULL_FACE, true),
(gl::DEPTH_TEST, true),
(gl::TEXTURE_2D, true),
(gl::STENCIL_TEST, false),
].into_iter ())
.front_face (FrontFace::Cw)
.color_mask ([1, 1, 1, 1])
.depth_mask (1)
.clone (),
];
let text_stream = TriangleStream {

View File

@ -613,7 +613,7 @@ impl GameGraphics {
front_face: None,
stencil: None,
depth_func: None,
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (255),
},
@ -642,7 +642,7 @@ impl GameGraphics {
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (0),
},
@ -671,7 +671,7 @@ impl GameGraphics {
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((0, 0, 0, 0)),
color_mask: Some ([0, 0, 0, 0]),
depth_mask: Some (0),
stencil_mask: Some (255),
},
@ -700,7 +700,7 @@ impl GameGraphics {
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (0),
},
@ -729,7 +729,7 @@ impl GameGraphics {
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (0),
},
@ -771,7 +771,7 @@ impl GameGraphics {
},
}),
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (0),
},
@ -802,7 +802,7 @@ impl GameGraphics {
front_face: None,
stencil: None,
depth_func: Some (DepthFunc::Less),
color_mask: Some ((1, 1, 1, 1)),
color_mask: Some ([1, 1, 1, 1]),
depth_mask: Some (1),
stencil_mask: Some (0),
},
@ -1162,8 +1162,6 @@ impl GameGraphics {
0.0, 256.0,
];
use std::convert::TryInto;
let eps = 0.0 / 256.0;
let uv: Vec <f32> = vec! [

View File

@ -1,4 +1,7 @@
use opengl_rust::prelude::*;
use opengl_rust::{
prelude::*,
shader,
};
struct GameState {
logic_frames: u64,

View File

@ -138,6 +138,7 @@ pub struct StencilState {
// These are POD where no extra data is needed
// to safely use the flags / numbers
#[derive (Clone)]
pub struct IsoGlState {
pub shader_id: Option <u32>,
@ -146,12 +147,12 @@ pub struct IsoGlState {
pub stencil: Option <StencilState>,
pub depth_func: Option <DepthFunc>,
pub color_mask: Option <(u8, u8, u8, u8)>,
pub color_mask: Option <[u8; 4]>,
pub depth_mask: Option <u8>,
pub stencil_mask: Option <u32>,
}
impl std::default::Default for IsoGlState {
impl Default for IsoGlState {
fn default () -> Self {
Self {
shader_id: None,
@ -171,7 +172,7 @@ impl std::default::Default for IsoGlState {
pub struct NonIsoGlState {
}
impl std::default::Default for NonIsoGlState {
impl Default for NonIsoGlState {
fn default () -> Self {
Self {
}
@ -192,12 +193,61 @@ impl std::default::Default for GlState {
}
}
#[derive (Clone, Default)]
pub struct Pass {
// In the context of a Pass, "None" means "Don't care"
pub iso: IsoGlState,
}
// Builder methods
impl Pass {
pub fn shader (&mut self, x: &crate::shader_closure::ShaderClosure) -> &mut Self {
self.iso.shader_id = Some (x.get_id ());
self
}
pub fn shader_id <T: Into <Option <u32>>> (&mut self, x: T) -> &mut Self {
self.iso.shader_id = x.into ();
self
}
pub fn color_mask <T: Into <Option <[u8; 4]>>> (&mut self, x: T) -> &mut Self {
self.iso.color_mask = x.into ();
self
}
pub fn depth_mask <T: Into <Option <u8>>> (&mut self, x: T) -> &mut Self {
self.iso.depth_mask = x.into ();
self
}
pub fn stencil_mask <T: Into <Option <u32>>> (&mut self, x: T) -> &mut Self {
self.iso.stencil_mask = x.into ();
self
}
pub fn flag (&mut self, k: u32, v: bool) -> &mut Self {
self.iso.flags.insert (k, v);
self
}
pub fn flags <I: Iterator <Item=(u32, bool)>> (&mut self, i: I) -> &mut Self {
for (k, v) in i {
self.flag (k, v);
}
self
}
pub fn front_face <T: Into <Option <FrontFace>>> (&mut self, x: T) -> &mut Self {
self.iso.front_face = x.into ();
self
}
}
// State-diffing stuff
impl Pass {
pub fn apply_diff (&self, old_state: &mut IsoGlState) {
let state = &self.iso;
@ -273,7 +323,7 @@ impl Pass {
}
}
if let Some ((r, g, b, a)) = &state.color_mask {
if let Some ([r, g, b, a]) = &state.color_mask {
if old_state.color_mask != state.color_mask {
glezz::color_mask (*r, *g, *b, *a);
old_state.color_mask = state.color_mask;