| 1 | module main |
| 2 | |
| 3 | import gg |
| 4 | import sokol.gfx |
| 5 | |
| 6 | fn main() { |
| 7 | mut context := gg.new_context( |
| 8 | frame_fn: frame |
| 9 | width: 500 |
| 10 | height: 500 |
| 11 | ) |
| 12 | context.run() |
| 13 | } |
| 14 | |
| 15 | fn frame(mut ctx gg.Context) { |
| 16 | ctx.begin() |
| 17 | id := ctx.new_streaming_image(ctx.width, ctx.height, 4, pixel_format: .rgba8) |
| 18 | mut img := ctx.get_cached_image_by_idx(id) |
| 19 | mut bytes := []u8{len: img.width * img.height * 4, cap: img.width * img.height * 4} |
| 20 | for y in 0 .. img.height { |
| 21 | for x in 0 .. img.width { |
| 22 | unsafe { |
| 23 | bytes[(x + img.width * y) * 4] = 100 |
| 24 | bytes[(x + img.width * y) * 4 + 1] = 100 |
| 25 | bytes[(x + img.width * y) * 4 + 2] = 100 |
| 26 | bytes[(x + img.width * y) * 4 + 3] = 255 |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | unsafe { |
| 31 | img.update_pixel_data(&bytes[0]) |
| 32 | } |
| 33 | ctx.draw_image(0, 0, ctx.width, ctx.height, img) |
| 34 | ctx.remove_cached_image_by_idx(id) |
| 35 | ctx.end() |
| 36 | gfx.destroy_image(img.simg) |
| 37 | unsafe { |
| 38 | free(&bytes[0]) |
| 39 | } |
| 40 | } |
| 41 | |