From a7e0d4b0b014e964297dc5f99096f9652c20b49a Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Fri, 17 May 2024 08:43:49 +0500 Subject: [PATCH] Update example --- dunge/src/el.rs | 8 +++++--- dunge/src/lib.rs | 5 ++++- dunge/src/window.rs | 6 +++--- examples/cube/src/lib.rs | 27 +++++++++++++++------------ examples/cube/src/main.rs | 4 ++-- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/dunge/src/el.rs b/dunge/src/el.rs index f1d4fd7..1e2e619 100644 --- a/dunge/src/el.rs +++ b/dunge/src/el.rs @@ -4,7 +4,7 @@ use { state::State, time::{Fps, Time}, update::Update, - window::{self, View}, + window::{self, View, WindowState}, }, std::{cell::Cell, error, fmt, ops, time::Duration}, wgpu::SurfaceError, @@ -27,7 +27,7 @@ pub type SmolStr = keyboard::SmolStr; /// Describes a button of a mouse controller. pub type MouseButton = event::MouseButton; -pub fn run_local(cx: Context, view: View, upd: U) -> Result<(), LoopError> +pub fn run_local(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError> where U: Update, { @@ -35,6 +35,7 @@ where .build() .map_err(LoopError::EventLoop)?; + let view = View::new(ws); let mut handler = Handler::new(cx, view, upd); let out = lu.run_app(&mut handler).map_err(LoopError::EventLoop); out.or(handler.out) @@ -352,6 +353,7 @@ type Target = event_loop::ActiveEventLoop; type Failure = Option>; #[deprecated] +#[allow(dead_code)] fn handle(cx: Context, view: View, mut upd: U) -> impl FnMut(Event, &Target) -> Failure where U: Update, @@ -366,7 +368,7 @@ where const WAIT_TIME: Duration = Duration::from_millis(100); let mut ctrl = Control { - view: View::new(unimplemented!()), + view: unimplemented!(), resized: None, min_delta_time: Cell::new(Duration::from_secs_f32(1. / 60.)), delta_time: Duration::ZERO, diff --git a/dunge/src/lib.rs b/dunge/src/lib.rs index 56c780a..974a211 100644 --- a/dunge/src/lib.rs +++ b/dunge/src/lib.rs @@ -56,6 +56,9 @@ pub use crate::window::{from_element, window_state_from_element}; #[cfg(feature = "winit")] pub use crate::{ - el::{Buttons, Control, Flow, Key, KeyCode, LoopError, Mouse, MouseButton, SmolStr, Then}, + el::{ + run_local, Buttons, Control, Flow, Key, KeyCode, LoopError, Mouse, MouseButton, SmolStr, + Then, + }, update::{update, update_with_event, update_with_state, Update}, }; diff --git a/dunge/src/window.rs b/dunge/src/window.rs index db9687e..db93b38 100644 --- a/dunge/src/window.rs +++ b/dunge/src/window.rs @@ -361,11 +361,11 @@ pub struct View { } impl View { - pub fn new(state: WindowState) -> Self { + pub(crate) fn new(ws: WindowState) -> Self { Self { - init: Init::Empty(Box::new(state.attrs)), + init: Init::Empty(Box::new(ws.attrs)), id: WindowId::from(u64::MAX), - el: state.el, + el: ws.el, format: Format::default(), size: (1, 1), } diff --git a/examples/cube/src/lib.rs b/examples/cube/src/lib.rs index 12d78d8..ced7cc1 100644 --- a/examples/cube/src/lib.rs +++ b/examples/cube/src/lib.rs @@ -1,6 +1,6 @@ type Error = Box; -pub fn run(window: dunge::window::Window) -> Result<(), Error> { +pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> { use dunge::{ color::Rgba, glam::{Mat4, Quat, Vec3}, @@ -37,7 +37,7 @@ pub fn run(window: dunge::window::Window) -> Result<(), Error> { p * m }; - let cx = window.context(); + let cx = dunge::context().await?; let cube_shader = cx.make_shader(cube); let mut r = 0.; let uniform = { @@ -105,17 +105,20 @@ pub fn run(window: dunge::window::Window) -> Result<(), Error> { }; let layer = cx.make_layer(&cube_shader, Format::window()); - let upd = move |ctrl: &Control| { - for key in ctrl.pressed_keys() { - if key.code == KeyCode::Escape { - return Then::Close; + let upd = { + let cx = cx.clone(); + move |ctrl: &Control| { + for key in ctrl.pressed_keys() { + if key.code == KeyCode::Escape { + return Then::Close; + } } - } - r += ctrl.delta_time().as_secs_f32() * 0.5; - let mat = transform(r, ctrl.size()); - uniform.update(&cx, mat); - Then::Run + r += ctrl.delta_time().as_secs_f32() * 0.5; + let mat = transform(r, ctrl.size()); + uniform.update(&cx, mat); + Then::Run + } }; let draw = move |mut frame: Frame| { @@ -123,6 +126,6 @@ pub fn run(window: dunge::window::Window) -> Result<(), Error> { frame.layer(&layer, opts).bind(&bind_transform).draw(&mesh); }; - window.run(dunge::update(upd, draw))?; + dunge::run_local(cx, ws, dunge::update(upd, draw))?; Ok(()) } diff --git a/examples/cube/src/main.rs b/examples/cube/src/main.rs index 6fce64a..b410612 100644 --- a/examples/cube/src/main.rs +++ b/examples/cube/src/main.rs @@ -1,7 +1,7 @@ fn main() { env_logger::init(); - let window = helpers::block_on(dunge::window().with_title("Cube")); - if let Err(err) = window.map_err(Box::from).and_then(cube::run) { + let ws = dunge::window_state().with_title("Cube"); + if let Err(err) = helpers::block_on(cube::run(ws)) { eprintln!("error: {err}"); } }