2021-10-13 21:22:58 +03:00
|
|
|
module sapp
|
|
|
|
|
|
|
|
import os
|
2021-12-08 23:38:33 +03:00
|
|
|
import stbi
|
2021-10-13 21:22:58 +03:00
|
|
|
|
2023-03-30 01:10:49 +03:00
|
|
|
#define SOKOL_VALIDATE_NON_FATAL 1
|
|
|
|
|
2021-10-13 21:22:58 +03:00
|
|
|
// v_sapp_gl_read_rgba_pixels reads pixles from the OpenGL buffer into `pixels`.
|
|
|
|
fn C.v_sapp_gl_read_rgba_pixels(x int, y int, width int, height int, pixels charptr)
|
|
|
|
|
2022-01-06 15:11:40 +03:00
|
|
|
// screenshot takes a screenshot of the current window and
|
|
|
|
// saves it to `path`. The format is inferred from the extension
|
|
|
|
// of the file name in `path`.
|
|
|
|
//
|
|
|
|
// Supported formats are: `.png`, `.ppm`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn screenshot(path string) ! {
|
2022-01-06 15:11:40 +03:00
|
|
|
match os.file_ext(path) {
|
|
|
|
'.png' {
|
|
|
|
return screenshot_png(path)
|
|
|
|
}
|
|
|
|
'.ppm' {
|
|
|
|
return screenshot_ppm(path)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return error(@MOD + '.' + @FN + ' currently only supports .png and .ppm files.')
|
|
|
|
}
|
2021-10-13 21:22:58 +03:00
|
|
|
}
|
2021-12-08 23:38:33 +03:00
|
|
|
}
|
2021-10-13 21:22:58 +03:00
|
|
|
|
2022-01-06 15:11:40 +03:00
|
|
|
// screenshot_ppm takes a screenshot of the current window and
|
|
|
|
// saves it to `path` as a .ppm file.
|
2021-12-08 23:38:33 +03:00
|
|
|
[manualfree]
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn screenshot_ppm(path string) ! {
|
2021-12-08 23:38:33 +03:00
|
|
|
ss := screenshot_window()
|
2022-10-16 09:28:57 +03:00
|
|
|
write_rgba_to_ppm(path, ss.width, ss.height, 4, ss.pixels)!
|
2021-12-08 23:38:33 +03:00
|
|
|
unsafe { ss.destroy() }
|
|
|
|
}
|
2021-10-13 21:22:58 +03:00
|
|
|
|
2022-01-06 15:11:40 +03:00
|
|
|
// screenshot_png takes a screenshot of the current window and
|
|
|
|
// saves it to `path` as a .png file.
|
2021-12-08 23:38:33 +03:00
|
|
|
[manualfree]
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn screenshot_png(path string) ! {
|
2021-12-08 23:38:33 +03:00
|
|
|
ss := screenshot_window()
|
|
|
|
stbi.set_flip_vertically_on_write(true)
|
2022-10-16 09:28:57 +03:00
|
|
|
stbi.stbi_write_png(path, ss.width, ss.height, 4, ss.pixels, ss.width * 4)!
|
2021-12-08 23:38:33 +03:00
|
|
|
unsafe { ss.destroy() }
|
2021-10-13 21:22:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write_rgba_to_ppm writes `pixels` data in RGBA format to PPM3 format.
|
2022-10-16 09:28:57 +03:00
|
|
|
fn write_rgba_to_ppm(path string, w int, h int, components int, pixels &u8) ! {
|
|
|
|
mut f_out := os.create(path)!
|
2021-12-08 23:38:33 +03:00
|
|
|
defer {
|
|
|
|
f_out.close()
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
f_out.writeln('P3')!
|
2022-11-15 16:53:13 +03:00
|
|
|
f_out.writeln('${w} ${h}')!
|
2022-10-16 09:28:57 +03:00
|
|
|
f_out.writeln('255')!
|
2021-10-13 21:22:58 +03:00
|
|
|
for i := h - 1; i >= 0; i-- {
|
|
|
|
for j := 0; j < w; j++ {
|
|
|
|
idx := i * w * components + j * components
|
2021-12-08 23:38:33 +03:00
|
|
|
unsafe {
|
|
|
|
r := int(pixels[idx])
|
|
|
|
g := int(pixels[idx + 1])
|
|
|
|
b := int(pixels[idx + 2])
|
2022-11-15 16:53:13 +03:00
|
|
|
f_out.write_string('${r} ${g} ${b} ')!
|
2021-12-08 23:38:33 +03:00
|
|
|
}
|
2021-10-13 21:22:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|