Skip to content

Commit

Permalink
fix: #60
Browse files Browse the repository at this point in the history
now can use `Engine::window::close_engine()`
  • Loading branch information
ElhamAryanpur committed Jun 6, 2024
1 parent 67c47cd commit 8da08c6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
7 changes: 6 additions & 1 deletion examples/dev/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use blue_engine::{
fn main() {
let mut engine = Engine::new_config(blue_engine::WindowDescriptor {
power_preference: blue_engine::PowerPreference::LowPower,
present_mode: blue_engine::wgpu::PresentMode::Immediate,
present_mode: blue_engine::wgpu::PresentMode::Mailbox,
..Default::default()
})
.expect("win");
Expand Down Expand Up @@ -106,6 +106,9 @@ fn main() {

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

if input.key_held(blue_engine::KeyCode::Escape) {
_window.close_engine();
}
if input.key_held(blue_engine::KeyCode::ArrowUp) {
sprite.set_position(
sprite.position.x,
Expand Down Expand Up @@ -154,4 +157,6 @@ fn main() {
}
})
.expect("Error during update loop");

println!("Engine have been closed")
}
29 changes: 28 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ macro_rules! impl_deref {
};
}

macro_rules! impl_deref_field {
($struct:ty,$type:ty,$field:ident) => {
impl std::ops::Deref for $struct {
type Target = $type;

fn deref(&self) -> &Self::Target {
&self.$field
}
}
impl std::ops::DerefMut for $struct {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.$field
}
}
};
}

/// Will contain all details about a vertex and will be sent to GPU
// Will be turned to C code and sent to GPU
#[repr(C)]
Expand Down Expand Up @@ -182,7 +199,7 @@ pub struct Engine {
/// The event_loop handles the events of the window and inputs, so it's used internally
pub event_loop: winit::event_loop::EventLoop<()>,
/// The window handles everything about window and inputs. This includes ability to modify window and listen to input devices for changes.
pub window: winit::window::Window,
pub window: Window,
/// The object system is a way to make it easier to work with the engine. Obviously you can work without it, but it's for those who
/// do not have the know-how, or wish to handle all the work of rendering data manually.
pub objects: ObjectStorage,
Expand Down Expand Up @@ -614,3 +631,13 @@ pub enum ExecuteOrder {
/// The main function that is the update_loop
UpdateLoopFunction,
}

/// A wrapper for winit window to make it easier to use and more ergonomic.
#[derive(Debug)]
pub struct Window {
/// The winit window itself.
pub window: crate::winit::window::Window,
/// Whether the engine should close.
pub should_close: bool,
}
impl_deref_field!(Window, crate::winit::window::Window, window);
2 changes: 0 additions & 2 deletions src/header/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,5 @@ pub use winit::keyboard::Key;
pub use winit::keyboard::KeyCode;
/// Fullscreen enum
pub use winit::window::Fullscreen;
/// Window export from winit
pub use winit::window::Window;
/// Input helper
pub use winit_input_helper::WinitInputHelper as InputHelper;
29 changes: 25 additions & 4 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use crate::{
header::{Camera, Engine, Renderer, WindowDescriptor},
ObjectStorage,
ObjectStorage, Window,
};

use winit::{
event::{DeviceEvent, Event, WindowEvent},
event_loop::EventLoop,
window::{Window, WindowBuilder},
window::WindowBuilder,
};

impl Engine {
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Engine {
let camera = Camera::new(window.inner_size(), &mut renderer)?;

Ok(Self {
window,
window: Window::new(window),
event_loop,
renderer,
objects: ObjectStorage::new(),
Expand Down Expand Up @@ -200,6 +200,10 @@ impl Engine {
}

WindowEvent::RedrawRequested => {
if window.should_close {
window_target.exit();
}

let pre_render = renderer
.pre_render(&objects, window.inner_size(), &camera)
.expect("Couldn't get pre render data");
Expand Down Expand Up @@ -261,7 +265,7 @@ impl Engine {
renderer
.instance
.create_surface_unsafe(
wgpu::SurfaceTargetUnsafe::from_window(&window)
wgpu::SurfaceTargetUnsafe::from_window(&window.window)
.expect("Couldn't create surface target"),
)
.expect("Couldn't create surface")
Expand All @@ -288,3 +292,20 @@ impl Engine {
Ok(())
}
}

impl Window {
/// create a new window
pub fn new(window: crate::winit::window::Window) -> Self {
Self {
window,
should_close: false,
}
}

/// close the engine window
#[allow(unused)]
#[allow(dead_code)]
pub fn close_engine(&mut self) {
self.should_close = true;
}
}

0 comments on commit 8da08c6

Please sign in to comment.