| 1 | import gg |
| 2 | import time |
| 3 | |
| 4 | const pwidth = 800 |
| 5 | |
| 6 | const pheight = 600 |
| 7 | |
| 8 | const pbytes = 4 |
| 9 | |
| 10 | struct AppState { |
| 11 | mut: |
| 12 | gg &gg.Context = unsafe { nil } |
| 13 | istream_idx int |
| 14 | pixels [pheight][pwidth]u32 |
| 15 | } |
| 16 | |
| 17 | @[direct_array_access] |
| 18 | fn (mut state AppState) update() { |
| 19 | mut rcolor := u64(state.gg.frame) |
| 20 | for { |
| 21 | for y in 0 .. pheight { |
| 22 | for x in 0 .. pwidth { |
| 23 | rcolor = rcolor * 1664525 + 1013904223 |
| 24 | state.pixels[y][x] = u32(rcolor & 0x0000_0000_FFFF_FFFF) | 0x1010AFFF |
| 25 | } |
| 26 | } |
| 27 | time.sleep(33 * time.millisecond) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn (mut state AppState) draw() { |
| 32 | mut istream_image := state.gg.get_cached_image_by_idx(state.istream_idx) |
| 33 | istream_image.update_pixel_data(unsafe { &u8(&state.pixels) }) |
| 34 | size := gg.window_size() |
| 35 | state.gg.draw_image(0, 0, size.width, size.height, istream_image) |
| 36 | } |
| 37 | |
| 38 | // gg callbacks: |
| 39 | |
| 40 | fn graphics_init(mut state AppState) { |
| 41 | state.istream_idx = state.gg.new_streaming_image(pwidth, pheight, pbytes, pixel_format: .rgba8) |
| 42 | } |
| 43 | |
| 44 | fn graphics_frame(mut state AppState) { |
| 45 | state.gg.begin() |
| 46 | state.draw() |
| 47 | state.gg.end() |
| 48 | } |
| 49 | |
| 50 | fn main() { |
| 51 | mut state := &AppState{} |
| 52 | state.gg = gg.new_context( |
| 53 | width: 800 |
| 54 | height: 600 |
| 55 | create_window: true |
| 56 | window_title: 'Random Static' |
| 57 | init_fn: graphics_init |
| 58 | frame_fn: graphics_frame |
| 59 | user_data: state |
| 60 | ) |
| 61 | spawn state.update() |
| 62 | state.gg.run() |
| 63 | } |
| 64 | |