Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed May 17, 2024
1 parent 1b078ff commit a7e0d4b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
8 changes: 5 additions & 3 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,14 +27,15 @@ pub type SmolStr = keyboard::SmolStr;
/// Describes a button of a mouse controller.
pub type MouseButton = event::MouseButton;

pub fn run_local<U>(cx: Context, view: View, upd: U) -> Result<(), LoopError>
pub fn run_local<U>(cx: Context, ws: WindowState, upd: U) -> Result<(), LoopError>
where
U: Update,
{
let lu = EventLoop::with_user_event()
.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)
Expand Down Expand Up @@ -352,6 +353,7 @@ type Target = event_loop::ActiveEventLoop;
type Failure = Option<Box<dyn error::Error>>;

#[deprecated]
#[allow(dead_code)]
fn handle<U>(cx: Context, view: View, mut upd: U) -> impl FnMut(Event<U::Event>, &Target) -> Failure
where
U: Update,
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
6 changes: 3 additions & 3 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
27 changes: 15 additions & 12 deletions examples/cube/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Error = Box<dyn std::error::Error>;

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},
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -105,24 +105,27 @@ 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| {
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
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(())
}
4 changes: 2 additions & 2 deletions examples/cube/src/main.rs
Original file line number Diff line number Diff line change
@@ -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}");
}
}

0 comments on commit a7e0d4b

Please sign in to comment.