Skip to content

Commit 0a07d68

Browse files
committed
examples: add examples/gg/x_y_frame.v (based on random.v)
1 parent 6bb88ba commit 0a07d68

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

‎examples/gg/x_y_frame.v‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import gg
2+
import time
3+
4+
const pwidth = 640
5+
const pheight = 480
6+
7+
struct AppState {
8+
mut:
9+
gg &gg.Context = unsafe { nil }
10+
istream_idx int
11+
pixels [pheight][pwidth]u32
12+
}
13+
14+
@[direct_array_access]
15+
fn (mut state AppState) update() {
16+
mut rcolor := u32(0)
17+
for {
18+
for y in 0 .. pheight {
19+
for x in 0 .. pwidth {
20+
rcolor = x * y * u32(state.gg.frame)
21+
state.pixels[y][x] = u32(rcolor) | 0xFF_00_00_00 // set the alpha channel to 255
22+
}
23+
}
24+
time.sleep(16 * time.millisecond)
25+
}
26+
}
27+
28+
fn graphics_init(mut state AppState) {
29+
state.istream_idx = state.gg.new_streaming_image(pwidth, pheight, 4, pixel_format: .rgba8)
30+
}
31+
32+
fn graphics_frame(mut state AppState) {
33+
state.gg.begin()
34+
mut istream_image := state.gg.get_cached_image_by_idx(state.istream_idx)
35+
istream_image.update_pixel_data(unsafe { &u8(&state.pixels) })
36+
size := gg.window_size()
37+
state.gg.draw_image(0, 0, size.width, size.height, istream_image)
38+
state.gg.end()
39+
}
40+
41+
mut state := &AppState{}
42+
state.gg = gg.new_context(
43+
width: pwidth
44+
height: pheight
45+
window_title: 'x*y*frame'
46+
user_data: state
47+
init_fn: graphics_init
48+
frame_fn: graphics_frame
49+
)
50+
spawn state.update()
51+
state.gg.run()

0 commit comments

Comments
 (0)