Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jul 19, 2024
1 parent 000f798 commit af4c29f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 138 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,38 +116,38 @@ We don't do anything special here, we just check is <kbd>Esc</kbd> pressed and e

Second `Draw` is used directly to draw something in the final frame:
```rust
// Describe the `Draw` handler
let draw = {
// Clone the context to closure
let cx = cx.clone();

// Create a layer for drawing a mesh on it
let layer = OnceCell::default();

move |mut frame: Frame| {
use dunge::color::Rgba;

// Create a black RGBA background
let bg = Rgba::from_bytes([0, 0, 0, !0]);

// Lazily initialize a layer
let layer = layer.get_or_init(|| cx.make_layer(&shader, frame.format()));
// Create a layer for drawing a mesh on it
let layer = cx.make_layer(&shader, view.format());

frame
// Select a layer to draw on it
.layer(&layer, bg)
// The shader has no bindings, so call empty bind
.bind_empty()
// And finally draw the mesh
.draw(&mesh);
}
// Describe the `Draw` handler
let draw = move |mut frame: Frame| {
use dunge::color::Rgba;

// Create a black RGBA background
let bg = Rgba::from_bytes([0, 0, 0, !0]);

frame
// Select a layer to draw on it
.layer(&layer, bg)
// The shader has no bindings, so call empty bind
.bind_empty()
// And finally draw the mesh
.draw(&mesh);
};
```

Now you can run our application and see the window:
> **Note:** To create a layer we need to know the window format. It would be possible to guess it, but it is better to get it directly from a view object. You can get the view from a special `make` helper, which will call a closure when the handler is initialized and passes the necessary data to it.
Now you can join two steps in one hander and run the application and see the window:
```rust
let make_handler = |cx: &Context, view: &View| {
let upd = |ctrl: &Control| {/***/};
let draw = move |mut frame: Frame| {/***/};
dunge::update(upd, draw)
};

// Run the window with handlers
dunge::window().run_local(cx, dunge::update(upd, draw))?;
dunge::window().run_local(cx, dunge::make(make_handler))?;
```

<div align="center">
Expand Down
4 changes: 2 additions & 2 deletions examples/cube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
cx.make_mesh(&data)
};

let make_upd = move |cx: &Context, view: &View| {
let make_handler = move |cx: &Context, view: &View| {
let layer = cx.make_layer(&cube_shader, view.format());

let cx = cx.clone();
Expand All @@ -134,6 +134,6 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
dunge::update(upd, draw)
};

ws.run(cx, dunge::make(make_upd))?;
ws.run(cx, dunge::make(make_handler))?;
Ok(())
}
126 changes: 61 additions & 65 deletions examples/ssaa/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
type Error = Box<dyn std::error::Error>;

pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
use {
dunge::{
bind::UniqueBinding,
color::Rgba,
glam::{Vec2, Vec4},
group::BoundTexture,
prelude::*,
sl::{Groups, InVertex, Index, Out},
texture::{DrawTexture, Filter, Sampler},
uniform::Uniform,
Format,
},
std::{cell::OnceCell, f32::consts},
use dunge::{
bind::UniqueBinding,
color::Rgba,
glam::{Vec2, Vec4},
group::BoundTexture,
prelude::*,
sl::{Groups, InVertex, Index, Out},
texture::{DrawTexture, Filter, Sampler},
uniform::Uniform,
Format,
};

const SCREEN_FACTOR: u32 = 2;
Expand All @@ -22,6 +19,8 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
struct Offset<'a>(&'a Uniform<f32>);

let triangle = |Index(idx): Index, Groups(offset): Groups<Offset>| {
use std::f32::consts;

let color = const { Vec4::new(1., 0.4, 0.8, 1.) };
let third = const { consts::TAU / 3. };

Expand Down Expand Up @@ -91,11 +90,9 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
let render_buf = make_render_buf(&cx, (1, 1));
let sam = cx.make_sampler(Filter::Nearest);

let make_stp = |size| {
<[u32; 2]>::from(size).map(|v| {
let screen_inv = const { 1. / SCREEN_FACTOR as f32 };
screen_inv / v as f32
})
let make_stp = |(width, height)| {
let screen_inv = const { 1. / SCREEN_FACTOR as f32 };
[screen_inv / width as f32, screen_inv / height as f32]
};

let buf_size = render_buf.draw_texture().size();
Expand All @@ -112,31 +109,6 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
(binder.into_binding(), handler)
};

let upd = move |state: &mut State<_>, ctrl: &Control| {
for key in ctrl.pressed_keys() {
if key.code == KeyCode::Escape {
return Then::Close;
}
}

if let Some(size) = ctrl.resized() {
state.render_buf = make_render_buf(&state.cx, size);
let buf_size = state.render_buf.draw_texture().size();
stp.update(&state.cx, make_stp(buf_size));
let map = Map {
tex: BoundTexture::new(&state.render_buf),
sam: &sam,
stp: &stp,
};

dunge::then!(state.cx.update_group(&mut state.bind_map, &handler, &map));
}

r += ctrl.delta_time().as_secs_f32() * 0.5;
uniform.update(&state.cx, r);
Then::Run
};

let screen_mesh = {
let verts = const {
[[
Expand All @@ -151,11 +123,48 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
cx.make_mesh(&data)
};

let triangle_layer = cx.make_layer(&triangle_shader, Format::SrgbAlpha);
let screen_layer = OnceCell::default();
let draw = {
let cx = cx.clone();
move |state: &State<_>, mut frame: Frame| {
struct State<R> {
cx: Context,
render_buf: R,
bind_map: UniqueBinding,
}

let state = State {
cx: cx.clone(),
render_buf,
bind_map,
};

let make_handler = move |cx: &Context, view: &View| {
let triangle_layer = cx.make_layer(&triangle_shader, Format::SrgbAlpha);
let screen_layer = cx.make_layer(&screen_shader, view.format());

let upd = move |state: &mut State<_>, ctrl: &Control| {
for key in ctrl.pressed_keys() {
if key.code == KeyCode::Escape {
return Then::Close;
}
}

if let Some(size) = ctrl.resized() {
state.render_buf = make_render_buf(&state.cx, size);
let buf_size = state.render_buf.draw_texture().size();
stp.update(&state.cx, make_stp(buf_size));
let map = Map {
tex: BoundTexture::new(&state.render_buf),
sam: &sam,
stp: &stp,
};

dunge::then!(state.cx.update_group(&mut state.bind_map, &handler, &map));
}

r += ctrl.delta_time().as_secs_f32() * 0.5;
uniform.update(&state.cx, r);
Then::Run
};

let draw = move |state: &State<_>, mut frame: Frame| {
let main = |mut frame: Frame| {
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
frame
Expand All @@ -166,28 +175,15 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {

state.cx.draw_to(&state.render_buf, dunge::draw(main));

let screen_layer =
screen_layer.get_or_init(|| cx.make_layer(&screen_shader, frame.format()));

frame
.layer(screen_layer, Options::default())
.layer(&screen_layer, Options::default())
.bind(&state.bind_map)
.draw(&screen_mesh);
}
};

struct State<R> {
cx: Context,
render_buf: R,
bind_map: UniqueBinding,
}
};

let state = State {
cx: cx.clone(),
render_buf,
bind_map,
dunge::update_with_state(state, upd, draw)
};

ws.run(cx, dunge::update_with_state(state, upd, draw))?;
ws.run(cx, dunge::make(make_handler))?;
Ok(())
}
40 changes: 19 additions & 21 deletions examples/triangle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
type Error = Box<dyn std::error::Error>;

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

#[derive(Group)]
struct Offset<'a>(&'a Uniform<f32>);

let triangle = |Index(idx): Index, Groups(offset): Groups<Offset>| {
use std::f32::consts;

let color = const { Vec4::new(1., 0.4, 0.8, 1.) };
let third = const { consts::TAU / 3. };

Expand All @@ -37,9 +36,11 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
binder.into_binding()
};

let upd = {
let make_handler = move |cx: &Context, view: &View| {
let layer = cx.make_layer(&shader, view.format());

let cx = cx.clone();
move |ctrl: &Control| {
let upd = move |ctrl: &Control| {
for key in ctrl.pressed_keys() {
if key.code == KeyCode::Escape {
return Then::Close;
Expand All @@ -49,19 +50,16 @@ pub async fn run(ws: dunge::window::WindowState) -> Result<(), Error> {
r += ctrl.delta_time().as_secs_f32() * 0.5;
uniform.update(&cx, r);
Then::Run
}
};
};

let draw = {
let cx = cx.clone();
let layer = OnceCell::default();
move |mut frame: Frame| {
let draw = move |mut frame: Frame| {
let opts = Rgba::from_standard([0.1, 0.05, 0.15, 1.]);
let layer = layer.get_or_init(|| cx.make_layer(&shader, frame.format()));
frame.layer(layer, opts).bind(&bind).draw_points(3);
}
frame.layer(&layer, opts).bind(&bind).draw_points(3);
};

dunge::update(upd, draw)
};

ws.run(cx, dunge::update(upd, draw))?;
ws.run(cx, dunge::make(make_handler))?;
Ok(())
}
Loading

0 comments on commit af4c29f

Please sign in to comment.