Skip to content

Commit

Permalink
feat: fixed scissor bounds bug, added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Feb 10, 2024
1 parent 0861f95 commit 9a89185
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blue_engine"
version = "0.5.4"
version = "0.5.5"
authors = ["Elham Aryanpur <elhamaryanpur5@gmail.com>"]
edition = "2021"
description = "General-Purpose, Easy-to-use, Fast, and Portable graphics engine"
Expand Down Expand Up @@ -84,6 +84,14 @@ path = "examples/utils/instancing.rs"
name = "render_order"
path = "examples/utils/render_order.rs"

[[example]]
name = "wireframe"
path = "examples/utils/wireframe.rs"

[[example]]
name = "scissor"
path = "examples/utils/scissor.rs"

# Development ONLY
[[example]]
name = "dev"
Expand Down
28 changes: 28 additions & 0 deletions examples/utils/scissor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Blue Engine by Elham Aryanpur
*
* Triangle example using pre-defined shapes
*
* The license is same as the one on the root.
*/

use blue_engine::{primitive_shapes::triangle, Engine, ObjectSettings};

pub fn main() {
let mut engine = Engine::new().expect("win");

triangle(
"Triangle",
ObjectSettings::default(),
&mut engine.renderer,
&mut engine.objects,
)
.unwrap();

// set scissor rect
engine.renderer.scissor_rect = Some((0, 0, 450, 350));

engine
.update_loop(move |_, _, _, _, _, _| {})
.expect("Error during update loop");
}
31 changes: 31 additions & 0 deletions examples/utils/wireframe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Blue Engine by Elham Aryanpur
*
* Triangle example using pre-defined shapes
*
* The license is same as the one on the root.
*/

use blue_engine::{primitive_shapes::triangle, Engine, ObjectSettings, ShaderSettings};

pub fn main() {
let mut engine = Engine::new().expect("win");

triangle(
"Triangle",
ObjectSettings {
shader_settings: ShaderSettings {
polygon_mode: wgpu::PolygonMode::Line,
..Default::default()
},
..Default::default()
},
&mut engine.renderer,
&mut engine.objects,
)
.unwrap();

engine
.update_loop(move |_, _, _, _, _, _| {})
.expect("Error during update loop");
}
25 changes: 18 additions & 7 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl Renderer {
pub(crate) fn pre_render(
&mut self,
objects: &ObjectStorage,
window_size: winit::dpi::PhysicalSize<u32>,
camera: &Camera,
) -> Result<
Option<(
Expand All @@ -225,7 +226,12 @@ impl Renderer {
return Ok(None);
};

let frame = surface.get_current_texture()?;
let frame = if let Ok(frame) = surface.get_current_texture() {
frame
} else {
return Ok(None);
};

let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
Expand Down Expand Up @@ -260,12 +266,17 @@ impl Renderer {

if self.scissor_rect.is_some() {
let scissor_rect = self.scissor_rect.unwrap();
render_pass.set_scissor_rect(
scissor_rect.0,
scissor_rect.1,
scissor_rect.2,
scissor_rect.3,
);
// check if scissor bounds are smaller than the window
if scissor_rect.0 + scissor_rect.2 < window_size.width as u32
&& scissor_rect.1 + scissor_rect.3 < window_size.height as u32
{
render_pass.set_scissor_rect(
scissor_rect.0,
scissor_rect.1,
scissor_rect.2,
scissor_rect.3,
);
}
}

let default_data = self.default_data.as_ref().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Engine {
}
WindowEvent::RedrawRequested => {
let pre_render = renderer
.pre_render(&objects, &camera)
.pre_render(&objects, window.inner_size(), &camera)
.expect("Couldn't get pre render data");
if pre_render.is_some() {
let (mut encoder, view, frame) = pre_render.unwrap();
Expand Down

0 comments on commit 9a89185

Please sign in to comment.