From 8cc73c3bc518ff4b135f940a213fbd0675abfd63 Mon Sep 17 00:00:00 2001 From: nemo Date: Thu, 12 Jun 2025 10:39:16 +0200 Subject: [PATCH] exemple record in raylib --- .gitignore | 3 ++ Makefile | 9 ++++++ main.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4dcf2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +a.out +output.mp4 +preuve-a-conviction diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fa556ef --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: + gcc main.c -lraylib + +run: all + ./a.out + mpv ./output.mp4 + +clean: + rm -f ./a.out output.mp4 diff --git a/main.c b/main.c new file mode 100644 index 0000000..0956a96 --- /dev/null +++ b/main.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +#include "raylib.h" +#include "rlgl.h" + +void draw_frame(int frame) +{ + BeginDrawing(); + { + ClearBackground(BEIGE); + DrawText(TextFormat("frame: %d", frame), 0, 0, 50, RED); + DrawRectangle(frame*3, frame*2, 50, 50, BLUE); + } + EndDrawing(); +} + +int main() +{ + int pipefd[2]; + pid_t pid; + + if (pipe(pipefd) == -1) { + printf("ERR: pipe\n"); + return -1; + } + + pid = fork(); + if (pid == -1) { + printf("ERR: fork\n"); + return -1; + } + + if (pid == 0) { + close(pipefd[1]); + + dup2(pipefd[0], STDIN_FILENO); + close(pipefd[0]); + + execlp("ffmpeg", "ffmpeg", + "-y", + "-f", "rawvideo", + "-pix_fmt", "rgba", + "-s", "900x600", + "-r", "60", + "-i", "-", + "output.mp4", + NULL); + + // unreachable if ffmpeg launch + printf("ERR: execlp"); + return -1; + } else { + close(pipefd[0]); + + InitWindow(900, 600, "screen record test"); + // NOTE: lower FPS if frame bug + SetTargetFPS(60); + Vector2 scale = GetWindowScaleDPI(); + int width = GetScreenWidth(); + int height = GetScreenHeight(); + + int frame; + for (frame = 0; frame < 300; frame++) { + draw_frame(frame); + + int w = width*scale.x; + int h = height*scale.y; + + unsigned char *screen_data = rlReadScreenPixels(w, h); + write(pipefd[1], screen_data, w*h*4*sizeof(*screen_data)); + + RL_FREE(screen_data); + } + + close(pipefd[1]); + wait(NULL); + + printf("%d frame in %.02fs\n", frame, GetTime()); + } +}