| 1 | module sapp |
| 2 | |
| 3 | @[heap] |
| 4 | pub struct Screenshot { |
| 5 | width int |
| 6 | height int |
| 7 | size int |
| 8 | mut: |
| 9 | pixels &u8 = unsafe { nil } |
| 10 | } |
| 11 | |
| 12 | @[manualfree] |
| 13 | pub fn screenshot_window() &Screenshot { |
| 14 | img_width := width() |
| 15 | img_height := height() |
| 16 | img_size := img_width * img_height * 4 |
| 17 | img_pixels := unsafe { &u8(malloc(img_size)) } |
| 18 | C.v_sapp_gl_read_rgba_pixels(0, 0, img_width, img_height, img_pixels) |
| 19 | return &Screenshot{ |
| 20 | width: img_width |
| 21 | height: img_height |
| 22 | size: img_size |
| 23 | pixels: img_pixels |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // free - free *only* the Screenshot pixels. |
| 28 | @[unsafe] |
| 29 | pub fn (mut ss Screenshot) free() { |
| 30 | unsafe { |
| 31 | free(ss.pixels) |
| 32 | ss.pixels = &u8(nil) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // destroy - free the Screenshot pixels, |
| 37 | // then free the screenshot data structure itself. |
| 38 | @[unsafe] |
| 39 | pub fn (mut ss Screenshot) destroy() { |
| 40 | unsafe { ss.free() } |
| 41 | unsafe { free(ss) } |
| 42 | } |
| 43 | |