Skip to content

Commit a7e0d4b

Browse files
committed
Update example
1 parent 1b078ff commit a7e0d4b

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

dunge/src/el.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {
44
state::State,
55
time::{Fps, Time},
66
update::Update,
7-
window::{self, View},
7+
window::{self, View, WindowState},
88
},
99
std::{cell::Cell, error, fmt, ops, time::Duration},
1010
wgpu::SurfaceError,
@@ -27,14 +27,15 @@ pub type SmolStr = keyboard::SmolStr;
2727
/// Describes a button of a mouse controller.
2828
pub type MouseButton = event::MouseButton;
2929

30-
pub fn run_local<U>(cx: Context, view: View, upd: U) -> Result<(), LoopError>
30+
pub fn run_local<U>(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError>
3131
where
3232
U: Update,
3333
{
3434
let lu = EventLoop::with_user_event()
3535
.build()
3636
.map_err(LoopError::EventLoop)?;
3737

38+
let view = View::new(ws);
3839
let mut handler = Handler::new(cx, view, upd);
3940
let out = lu.run_app(&mut handler).map_err(LoopError::EventLoop);
4041
out.or(handler.out)
@@ -352,6 +353,7 @@ type Target = event_loop::ActiveEventLoop;
352353
type Failure = Option<Box<dyn error::Error>>;
353354

354355
#[deprecated]
356+
#[allow(dead_code)]
355357
fn handle<U>(cx: Context, view: View, mut upd: U) -> impl FnMut(Event<U::Event>, &Target) -> Failure
356358
where
357359
U: Update,
@@ -366,7 +368,7 @@ where
366368
const WAIT_TIME: Duration = Duration::from_millis(100);
367369

368370
let mut ctrl = Control {
369-
view: View::new(unimplemented!()),
371+
view: unimplemented!(),
370372
resized: None,
371373
min_delta_time: Cell::new(Duration::from_secs_f32(1. / 60.)),
372374
delta_time: Duration::ZERO,

dunge/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub use crate::window::{from_element, window_state_from_element};
5656

5757
#[cfg(feature = "winit")]
5858
pub use crate::{
59-
el::{Buttons, Control, Flow, Key, KeyCode, LoopError, Mouse, MouseButton, SmolStr, Then},
59+
el::{
60+
run_local, Buttons, Control, Flow, Key, KeyCode, LoopError, Mouse, MouseButton, SmolStr,
61+
Then,
62+
},
6063
update::{update, update_with_event, update_with_state, Update},
6164
};

dunge/src/window.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ pub struct View {
361361
}
362362

363363
impl View {
364-
pub fn new(state: WindowState) -> Self {
364+
pub(crate) fn new(ws: WindowState) -> Self {
365365
Self {
366-
init: Init::Empty(Box::new(state.attrs)),
366+
init: Init::Empty(Box::new(ws.attrs)),
367367
id: WindowId::from(u64::MAX),
368-
el: state.el,
368+
el: ws.el,
369369
format: Format::default(),
370370
size: (1, 1),
371371
}

examples/cube/src/lib.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type Error = Box<dyn std::error::Error>;
22

3-
pub fn run(window: dunge::window::Window) -> Result<(), Error> {
3+
pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
44
use dunge::{
55
color::Rgba,
66
glam::{Mat4, Quat, Vec3},
@@ -37,7 +37,7 @@ pub fn run(window: dunge::window::Window) -> Result<(), Error> {
3737
p * m
3838
};
3939

40-
let cx = window.context();
40+
let cx = dunge::context().await?;
4141
let cube_shader = cx.make_shader(cube);
4242
let mut r = 0.;
4343
let uniform = {
@@ -105,24 +105,27 @@ pub fn run(window: dunge::window::Window) -> Result<(), Error> {
105105
};
106106

107107
let layer = cx.make_layer(&cube_shader, Format::window());
108-
let upd = move |ctrl: &Control| {
109-
for key in ctrl.pressed_keys() {
110-
if key.code == KeyCode::Escape {
111-
return Then::Close;
108+
let upd = {
109+
let cx = cx.clone();
110+
move |ctrl: &Control| {
111+
for key in ctrl.pressed_keys() {
112+
if key.code == KeyCode::Escape {
113+
return Then::Close;
114+
}
112115
}
113-
}
114116

115-
r += ctrl.delta_time().as_secs_f32() * 0.5;
116-
let mat = transform(r, ctrl.size());
117-
uniform.update(&cx, mat);
118-
Then::Run
117+
r += ctrl.delta_time().as_secs_f32() * 0.5;
118+
let mat = transform(r, ctrl.size());
119+
uniform.update(&cx, mat);
120+
Then::Run
121+
}
119122
};
120123

121124
let draw = move |mut frame: Frame| {
122125
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
123126
frame.layer(&layer, opts).bind(&bind_transform).draw(&mesh);
124127
};
125128

126-
window.run(dunge::update(upd, draw))?;
129+
dunge::run_local(cx, ws, dunge::update(upd, draw))?;
127130
Ok(())
128131
}

examples/cube/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
22
env_logger::init();
3-
let window = helpers::block_on(dunge::window().with_title("Cube"));
4-
if let Err(err) = window.map_err(Box::from).and_then(cube::run) {
3+
let ws = dunge::window_state().with_title("Cube");
4+
if let Err(err) = helpers::block_on(cube::run(ws)) {
55
eprintln!("error: {err}");
66
}
77
}

0 commit comments

Comments
 (0)