Skip to content

Commit

Permalink
Make update from view
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 18, 2024
1 parent a2df2aa commit 778f493
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
5 changes: 4 additions & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub mod prelude {
};

#[cfg(feature = "winit")]
pub use crate::el::{Control, KeyCode, Then};
pub use crate::{
el::{Control, KeyCode, Then},
window::View,
};
}

pub use {
Expand Down
55 changes: 26 additions & 29 deletions examples/cube/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
type Error = Box<dyn std::error::Error>;

pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
use {
dunge::{
color::Rgba,
glam::{Mat4, Quat, Vec3},
prelude::*,
sl::{Groups, InVertex, Out},
uniform::Uniform,
},
std::cell::OnceCell,
use dunge::{
color::Rgba,
glam::{Mat4, Quat, Vec3},
prelude::*,
sl::{Groups, InVertex, Out},
uniform::Uniform,
};

#[repr(C)]
Expand Down Expand Up @@ -112,32 +109,32 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
cx.make_mesh(&data)
};

let upd = {
let make_upd = {
let cx = cx.clone();
move |ctrl: &Control| {
for key in ctrl.pressed_keys() {
if key.code == KeyCode::Escape {
return Then::Close;
move |view: &View| {
let layer = cx.make_layer(&cube_shader, view.format());
let upd = 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 = {
let cx = cx.clone();
let layer = OnceCell::default();
move |mut frame: Frame| {
let layer = layer.get_or_init(|| cx.make_layer(&cube_shader, frame.format()));
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
frame.layer(layer, opts).bind(&bind_transform).draw(&mesh);
let draw = move |mut frame: Frame| {
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
frame.layer(&layer, opts).bind(&bind_transform).draw(&mesh);
};

dunge::update(upd, draw)
}
};

ws.run(cx, dunge::update(upd, draw))?;
ws.run(cx, dunge::from_view(make_upd))?;
Ok(())
}

0 comments on commit 778f493

Please sign in to comment.