Skip to content

Commit

Permalink
feat: added control_flow, present_mode, alpha_mode, and `desire…
Browse files Browse the repository at this point in the history
…d_maximum_frame_latency` options
  • Loading branch information
ElhamAryanpur committed Feb 17, 2024
1 parent 8426db3 commit 60513a5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 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.

2 changes: 1 addition & 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.6"
version = "0.5.7"
authors = ["Elham Aryanpur <[email protected]>"]
edition = "2021"
description = "General-Purpose, Easy-to-use, Fast, and Portable graphics engine"
Expand Down
12 changes: 12 additions & 0 deletions examples/dev/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,20 @@ fn main() {
.set_position(-0.2f32, 0f32, 0.001f32);

let speed = -0.05;

let mut last_time = std::time::Instant::now();
let mut frames = 0;
engine
.update_loop(move |renderer, _window, objects, input, camera, plugins| {
// calculate FPS
let current_time = std::time::Instant::now();
frames += 1;
if current_time - last_time >= std::time::Duration::from_secs(1) {
println!("{}ms/frame", 1000f32 / frames as f32);
frames = 0;
last_time = current_time;
}

let sprite = objects.get_mut("alt").unwrap();

if input.key_held(blue_engine::KeyCode::ArrowUp) {
Expand Down
12 changes: 12 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ pub struct WindowDescriptor {
pub backends: crate::Backends,
/// The features to be enabled on a backend
pub features: crate::wgpu::Features,
/// Controls how the events are processed
pub control_flow: crate::winit::event_loop::ControlFlow,
/// The presentation mode of renderer for things like VSync
pub present_mode: crate::wgpu::PresentMode,
/// The alpha mode which specifies how the alpha channel of the textures should be handled during compositing.
pub alpha_mode: crate::wgpu::CompositeAlphaMode,
/// The desired frame latency, check [wgpu::SurfaceConfiguration::desired_maximum_frame_latency]
pub desired_maximum_frame_latency: u32,
}
impl std::default::Default for WindowDescriptor {
/// Will quickly create a window with default settings
Expand All @@ -311,6 +319,10 @@ impl std::default::Default for WindowDescriptor {
} else {
wgpu::Features::empty()
},
control_flow: crate::winit::event_loop::ControlFlow::Poll,
present_mode: crate::wgpu::PresentMode::AutoNoVsync,
alpha_mode: crate::wgpu::CompositeAlphaMode::Auto,
desired_maximum_frame_latency: 2,
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
utils::default_resources::{DEFAULT_COLOR, DEFAULT_MATRIX_4, DEFAULT_SHADER, DEFAULT_TEXTURE},
ObjectStorage, PipelineData,
};
use wgpu::Features;
use winit::window::Window;

impl Renderer {
Expand All @@ -20,15 +19,13 @@ impl Renderer {
/// * `power_preference` - The power preference to use.
pub(crate) async fn new(
window: &Window,
power_preference: crate::PowerPreference,
backends: crate::Backends,
features: Features,
settings: crate::WindowDescriptor,
) -> color_eyre::Result<Self> {
let size = window.inner_size();

// The instance is a handle to our GPU
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
backends: settings.backends,
..Default::default()
});
#[cfg(not(feature = "android"))]
Expand All @@ -40,7 +37,7 @@ impl Renderer {

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: power_preference,
power_preference: settings.power_preference,
#[cfg(not(feature = "android"))]
compatible_surface: Some(&surface.as_ref().unwrap()),
#[cfg(feature = "android")]
Expand All @@ -54,7 +51,7 @@ impl Renderer {
.request_device(
&wgpu::DeviceDescriptor {
label: Some("Device"),
required_features: features,
required_features: settings.features,
required_limits: wgpu::Limits::default(),
},
None, // Trace path
Expand Down Expand Up @@ -82,10 +79,10 @@ impl Renderer {
#[cfg(feature = "android")]
present_mode: wgpu::PresentMode::Mailbox,
#[cfg(not(feature = "android"))]
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
present_mode: settings.present_mode,
alpha_mode: settings.alpha_mode,
view_formats: vec![tex_format],
desired_maximum_frame_latency: 2,
desired_maximum_frame_latency: settings.desired_maximum_frame_latency,
};
#[cfg(not(feature = "android"))]
surface.as_ref().unwrap().configure(&device, &config);
Expand Down
8 changes: 2 additions & 6 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Engine {
// and will contain all the callbacks and button press
// also will allow graphics API
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(settings.control_flow);

// bind the loop to window
#[cfg(not(feature = "android"))]
Expand All @@ -63,12 +64,7 @@ impl Engine {
let window = Window::new(&event_loop).unwrap();

// The renderer init on current window
let mut renderer = futures::executor::block_on(Renderer::new(
&window,
settings.power_preference,
settings.backends,
settings.features,
))?;
let mut renderer = futures::executor::block_on(Renderer::new(&window, settings))?;

let camera = Camera::new(window.inner_size(), &mut renderer)?;

Expand Down

0 comments on commit 60513a5

Please sign in to comment.