1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gg: support VGG_STOP_AT_FRAME=120 VGG_SCREENSHOT_FOLDER=. VGG_SCREENSHOT_FRAMES=10,20,30 ./v -d gg_record run examples/gg/bezier_anim.v (#12767)

This commit is contained in:
Delyan Angelov
2021-12-08 22:38:33 +02:00
committed by GitHub
parent 85f3372a32
commit 0021fbbaa9
5 changed files with 123 additions and 26 deletions

View File

@@ -1,50 +1,54 @@
module sapp
import os
import stbi
// 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)
// screenshot takes a screenshot of the current window.
[inline]
pub fn screenshot(path string) ? {
if !path.ends_with('.ppm') {
return error(@MOD + '.' + @FN + ' currently only supports .ppm files.')
}
return screenshot_ppm(path)
}
w := width()
h := height()
// screenshot_ppm takes a screenshot of the current window as a .ppm file
[manualfree]
pub fn screenshot_ppm(path string) ? {
ss := screenshot_window()
write_rgba_to_ppm(path, ss.width, ss.height, 4, ss.pixels) ?
unsafe { ss.destroy() }
}
size := w * h * 4 //
mut pixels := []byte{len: size, init: 0}
C.v_sapp_gl_read_rgba_pixels(0, 0, w, h, pixels.data)
// TODO use separate thread for writing the data
// TODO use stbi to support more formats
// stbi.write_png(path, w, h, components, pixels.data, 3 * w)
// stbi.write_tga(path, w, h, components, pixels.data)
write_rgba_to_ppm(path, w, h, 4, pixels) ?
unsafe {
pixels.free()
}
// screenshot_png takes a screenshot of the current window as a .png file
[manualfree]
pub fn screenshot_png(path string) ? {
ss := screenshot_window()
stbi.set_flip_vertically_on_write(true)
stbi.stbi_write_png(path, ss.width, ss.height, 4, ss.pixels, ss.width * 4) ?
unsafe { ss.destroy() }
}
// write_rgba_to_ppm writes `pixels` data in RGBA format to PPM3 format.
fn write_rgba_to_ppm(path string, w int, h int, components int, pixels []byte) ? {
fn write_rgba_to_ppm(path string, w int, h int, components int, pixels &byte) ? {
mut f_out := os.create(path) ?
defer {
f_out.close()
}
f_out.writeln('P3') ?
f_out.writeln('$w $h') ?
f_out.writeln('255') ?
for i := h - 1; i >= 0; i-- {
for j := 0; j < w; j++ {
idx := i * w * components + j * components
r := int(pixels[idx])
g := int(pixels[idx + 1])
b := int(pixels[idx + 2])
f_out.write_string('$r $g $b ') ?
unsafe {
r := int(pixels[idx])
g := int(pixels[idx + 1])
b := int(pixels[idx + 2])
f_out.write_string('$r $g $b ') ?
}
}
}
f_out.close()
}