| 1 | module gg |
| 2 | |
| 3 | import encoding.base64 |
| 4 | import sokol.sapp |
| 5 | import os |
| 6 | |
| 7 | fn screenshot_stdout_payload(frame u64, png []u8) string { |
| 8 | return '${gg_record_stdout_prefix} frame=${frame} format=png encoding=base64 data=${base64.encode(png)}' |
| 9 | } |
| 10 | |
| 11 | fn emit_recorded_frame_to_stdout(frame u64) ! { |
| 12 | screenshot_file_path := os.join_path(os.vtmp_dir(), |
| 13 | 'vgg_record_stdout_${os.getpid()}_${frame}.png') |
| 14 | sapp.screenshot_png(screenshot_file_path)! |
| 15 | defer { |
| 16 | os.rm(screenshot_file_path) or {} |
| 17 | } |
| 18 | png := os.read_bytes(screenshot_file_path)! |
| 19 | println(screenshot_stdout_payload(frame, png)) |
| 20 | flush_stdout() |
| 21 | } |
| 22 | |
| 23 | // record_frame records the current frame to a file or stdout. |
| 24 | // record_frame acts according to settings specified in `gg.recorder_settings`. |
| 25 | @[if gg_record ?] |
| 26 | pub fn (mut ctx Context) record_frame() { |
| 27 | if ctx.frame in recorder_settings.screenshot_frames { |
| 28 | match recorder_settings.screenshot_output { |
| 29 | .stdout { |
| 30 | $if gg_record_trace ? { |
| 31 | eprintln('>>> screenshotting frame ${ctx.frame} to stdout') |
| 32 | } |
| 33 | emit_recorded_frame_to_stdout(ctx.frame) or { panic(err) } |
| 34 | } |
| 35 | .file { |
| 36 | screenshot_file_path := '${recorder_settings.screenshot_prefix}${ctx.frame}.png' |
| 37 | $if gg_record_trace ? { |
| 38 | eprintln('>>> screenshotting ${screenshot_file_path}') |
| 39 | } |
| 40 | sapp.screenshot_png(screenshot_file_path) or { panic(err) } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | if ctx.frame == recorder_settings.stop_at_frame { |
| 45 | $if gg_record_trace ? { |
| 46 | eprintln('>>> exiting at frame ${ctx.frame}') |
| 47 | } |
| 48 | flush_stdout() |
| 49 | exit(0) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn new_gg_recorder_settings() &SSRecorderSettings { |
| 54 | $if gg_record ? { |
| 55 | return new_gg_recorder_settings_from_env(os.environ(), os.executable()) |
| 56 | } $else { |
| 57 | return &SSRecorderSettings{} |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const recorder_settings = new_gg_recorder_settings() |
| 62 | |