-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathassets_try_unwrap.rs
87 lines (73 loc) · 2.05 KB
/
assets_try_unwrap.rs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use notan::draw::*;
use notan::prelude::*;
#[derive(AppState)]
struct State {
font: Font,
loading: Option<Asset<Texture>>,
loaded: Option<Texture>,
}
impl State {
fn new(assets: &mut Assets, gfx: &mut Graphics) -> Self {
// Start loading the texture
let texture = assets
.load_asset(&asset_path("rust-logo-512x512.png"))
.unwrap();
// Load a font only for debug info
let font = gfx
.create_font(include_bytes!("assets/Ubuntu-B.ttf"))
.unwrap();
Self {
font,
loading: Some(texture),
loaded: None,
}
}
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(State::new)
.add_config(DrawConfig)
.update(update)
.draw(draw)
.build()
}
fn update(state: &mut State) {
// Unwrapping an asset reference after it has been loaded we avoid the performance penalty
// of the Arc<RwLock<T>> inside the Asset instance
let was_just_loaded = state
.loading
.as_ref()
.map(|assets| assets.is_loaded())
.unwrap_or(false);
if was_just_loaded {
// Get ownership of the Asset inside the Option
let asset = state.loading.take().unwrap();
// Unwrap the asset reference to get the inner
let texture = asset.try_unwrap().unwrap();
state.loaded = Some(texture);
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
match &state.loaded {
None => {
draw.text(&state.font, "Loading...")
.position(10.0, 10.0)
.size(25.0);
}
Some(texture) => {
draw.image(texture).position(150.0, 50.0);
}
}
gfx.render(&draw);
}
// The relative path for the example is different on browsers
fn asset_path(path: &str) -> String {
let base = if cfg!(target_arch = "wasm32") {
"./assets"
} else {
"./examples/assets"
};
format!("{base}/{path}")
}