I'm working on a framework to easily write rendering loops and managing windows, the example below should be pretty auto-explanatory on how it should work:
//Create app and main window.
let mut app = Nucley::new();
let window = app.create_window()?;
let window_view = window.view();
//Load texture from path.
let texture = Texture::from_path(window_view, Path::new("examples/draw_cube/UV_1k.jpg"));
let sampler = Sampler::new_default(window_view);
//Binder holds textures, matrices and other uniforms.
let mut binder = Binder::new();
binder.bind(0, &texture);
binder.bind(1, &sampler);
//Create brush to draw the shapes.
let mut brush = Brush::from_path(
window.view(),
Path::new(r#"D:\Development\Rust Crates\LDrawy\examples\shared_assets\basic.wgsl"#),
)?;
// Add binder information to the brush
brush.set_binder(0, binder);
//Create a shape batch and add a triangle to it.
let mut batch = ShapeBatch::default();
batch.add_triangle([
vertex!(-0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.0, 0.5, 0.0, Color::SILVER),
]);
//Bake batches into GPU buffers.
let buffer = batch.bake_buffers(window.view())?;
//Setup the window render loop.
// TODO: Add app to closure
window.run(move |wnd| {
let mut frame = wnd.start_frame(None).expect("Issue creating frame.");
frame.render(wnd, &mut brush, &buffer);
frame.finish(wnd).expect("Error finishing frame.");
});
// Start program.
app.start(());
However, I'm getting the following issues:
error[E0597]: `texture` does not live long enough
--> examples\draw_cube\main.rs:113:20
|
113 | binder.bind(0, &texture);
| ^^^^^^^^ borrowed value does not live long enough
...
137 | / window.run(move |wnd| {
138 | | let mut frame = wnd.start_frame(None).expect("Issue creating frame.");
139 | | frame.render(wnd, &mut brush, &buffer);
140 | | frame.finish(wnd).expect("Error finishing frame.");
141 | | });
| |______- argument requires that `texture` is borrowed for `'static`
...
145 | }
| - `texture` dropped here while still borrowed
(And basically the same issue with "sampler")
What I'm not understanding here is, both "buffer" and "brush" are being properly moved to the closure but "texture" and "sampler" are not, and it complains about being dropped while still borrowed (Which they should not be dropped since the ownership should be moved to the closure).
Is there anything about lifetimes or how closures take ownership that I'm not 100% understanding here?
Thanks