-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathgraphics_update_texture.rs
82 lines (70 loc) · 1.81 KB
/
graphics_update_texture.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
use notan::draw::*;
use notan::prelude::*;
const COLORS: [Color; 6] = [
Color::WHITE,
Color::RED,
Color::GREEN,
Color::BLUE,
Color::ORANGE,
Color::from_rgb(0.1, 0.2, 0.3),
];
#[derive(AppState)]
struct State {
texture: Texture,
bytes: Vec<u8>,
count: usize,
color: Color,
step: usize,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(init)
.add_config(DrawConfig)
.draw(draw)
.build()
}
fn init(app: &mut App, gfx: &mut Graphics) -> State {
let (width, height) = app.window().size();
let bytes_per_pixel = 4;
let len = (width * height * bytes_per_pixel) as usize;
let bytes = vec![0; len];
let texture = gfx
.create_texture()
.from_bytes(&bytes, width, height)
.build()
.unwrap();
State {
texture,
bytes,
count: 0,
color: Color::WHITE,
step: 0,
}
}
fn draw(gfx: &mut Graphics, state: &mut State) {
// update the data that will be send to the gpu
update_bytes(state);
// Update the texture with the new data
gfx.update_texture(&mut state.texture)
.with_data(&state.bytes)
.update()
.unwrap();
// Draw the texture using the draw 2d API for convenience
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
draw.image(&state.texture);
gfx.render(&draw);
}
fn update_bytes(state: &mut State) {
for _ in 0..100 {
let index = state.count * 4;
state.bytes[index..index + 4].copy_from_slice(&state.color.rgba_u8());
state.count += 9;
let len = state.bytes.len() / 4;
if state.count >= len {
state.count -= len;
state.step = (state.step + 1) % COLORS.len();
state.color = COLORS[state.step];
}
}
}