hello_triangle/
main.rs

1use std::error::Error;
2
3use tridify_rs::*;
4
5pub fn main() -> Result<(), Box<dyn Error>> {
6    //Create app and main window.
7    let mut app = Tridify::new();
8    let window = app.create_window()?;
9
10    //Stores WGPU context and devices. Must be used in most GPU functions.
11    let gpu_ctx = window.ctx();
12
13    //Create brush to draw the shapes.
14    let mut brush = Brush::from_source(
15        BrushDesc::default(),
16        gpu_ctx,
17        include_str!("shader.wgsl").to_string(),
18    )?;
19
20    //Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
21    let buffer = ShapeBatch::new()
22        .add_triangle([
23            vertex!(-0.5, -0.5, 0.0, Color::SILVER),
24            vertex!(0.5, -0.5, 0.0, Color::SILVER),
25            vertex!(0.0, 0.5, 0.0, Color::SILVER),
26        ])
27        .bake_buffers(gpu_ctx);
28
29    window.set_render_loop(move |gpu, _| {
30        //Create a render pass builder which we will use to define multiple render passes (In this case, only one).
31        let mut pass_builder = gpu.create_render_builder();
32
33        //Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
34        let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
35        render_pass.render_shapes(gpu, &mut brush, &buffer);
36        render_pass.finish();
37
38        //Execute all drawing commands from all render passes and render into screen.
39        pass_builder.finish_render(gpu);
40    });
41
42    //Start program logic cycle.
43    app.start(());
44}