1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Blue Engine by Elham Aryanpur
 *
 * Resource Sharing example using same resource in multiple objects
 *
 * The license is same as the one on the root.
*/
use blue_engine::{primitive_shapes::square, Engine, ObjectSettings, TextureData};

fn main() {
    // Start the engine
    let mut engine = Engine::new().expect("window not initialized");

    // build a texture as an example of resource to be shared
    let texture = engine
        .renderer
        .build_texture(
            "background",
            TextureData::Path("resources/BlueLogoDiscord.png".to_string()),
            blue_engine::TextureMode::Clamp,
        )
        .unwrap();

    // build your main object with the texture
    square(
        "main",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of main square");

    // add the texture to the main object as normally would
    engine
        .objects
        .get_mut("main")
        .unwrap()
        .set_texture(texture)
        .expect("Error during inserting texture to the main square");
    // set position to make it visible
    engine
        .objects
        .get_mut("main")
        .expect("Error during setting the position of the main square")
        .set_position(-1.5f32, 0f32, 0f32);

    // create another object where you want to get resources shared with
    square(
        "alt",
        ObjectSettings::default(),
        &mut engine.renderer,
        &mut engine.objects,
    )
    .expect("Error during creation of alt square");

    // here you can use `reference_texture` to reference the texture from the main object
    engine
        .objects
        .get_mut("alt")
        .expect("Error during copying texture of the main square")
        .reference_texture("main");
    // setting position again to make it visible
    engine
        .objects
        .get_mut("alt")
        .expect("Error during setting the position of the alt square")
        .set_position(1.5f32, 0f32, 0f32);

    engine
        .update_loop(move |_, _, _, _, _, _| {})
        .expect("Error during update loop");
}