v2 / examples / gg / x_y_frame.v
51 lines · 45 sloc · 1.15 KB · 0a07d689955511039b653e4442dad8d9defcac94
Raw
1import gg
2import time
3
4const pwidth = 640
5const pheight = 480
6
7struct AppState {
8mut:
9 gg &gg.Context = unsafe { nil }
10 istream_idx int
11 pixels [pheight][pwidth]u32
12}
13
14@[direct_array_access]
15fn (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
28fn graphics_init(mut state AppState) {
29 state.istream_idx = state.gg.new_streaming_image(pwidth, pheight, 4, pixel_format: .rgba8)
30}
31
32fn 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
41mut state := &AppState{}
42state.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)
50spawn state.update()
51state.gg.run()
52