فهرست منبع

Add Raylib Example

rexim 2 سال پیش
والد
کامیت
d4be9ba8f1
3فایلهای تغییر یافته به همراه88 افزوده شده و 3 حذف شده
  1. 16 3
      README.md
  2. 9 0
      build_raylib.sh
  3. 63 0
      main_raylib.c

+ 16 - 3
README.md

@@ -2,9 +2,22 @@
 
 https://github.com/tsoding/rendering-video-in-c-with-ffmpeg/assets/165283/3a050622-07cb-4558-b2cd-149f6c19a334
 
-## Quick Start
+## Examples
+
+### Raw Example
+
+This example generates video by filling out memory with raw pixels. It uses [olive.c](https://github.com/tsoding/olive.c) library for that.
+
+```console
+$ ./build_raw.sh
+$ ./build/main_raw
+```
+
+### Raylib Example
+
+This example captures the output of [Raylib](https://github.com/raysan5/raylib) application and saves it as a video.
 
 ```console
-$ ./build.sh
-$ ./main
+$ ./build_raylib.sh
+$ ./build/main_raylib
 ```

+ 9 - 0
build_raylib.sh

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+set -xe
+
+CFLAGS="-Wall -Wextra `pkg-config --cflags raylib`"
+LIBS="`pkg-config --libs raylib` -lm"
+
+mkdir -p ./build/
+clang $CFLAGS -o ./build/main_raylib ./main_raylib.c ffmpeg_linux.c $LIBS

+ 63 - 0
main_raylib.c

@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <raylib.h>
+
+#include "ffmpeg.h"
+
+#define WIDTH 800
+#define HEIGHT 600
+#define FPS 30
+#define DURATION 10
+
+int main(void)
+{
+    int ffmpeg = ffmpeg_start_rendering(WIDTH, HEIGHT, FPS);
+
+    InitWindow(WIDTH, HEIGHT, "FFmpeg");
+    SetTargetFPS(FPS);
+    RenderTexture2D screen = LoadRenderTexture(WIDTH, HEIGHT);
+
+    float x = WIDTH/2;
+    float y = HEIGHT/2;
+    float r = HEIGHT/8;
+    float dx = 200;
+    float dy = 200;
+    float dt = 1.0f/FPS;
+    for (size_t i = 0; i < FPS*DURATION && !WindowShouldClose(); ++i) {
+        float nx = x + dx*dt;
+        if (0 < nx - r && nx + r < WIDTH) {
+            x = nx;
+        } else {
+            dx = -dx;
+        }
+
+        float ny = y + dy*dt;
+        if (0 < ny - r && ny + r < HEIGHT) {
+            y = ny;
+        } else {
+            dy = -dy;
+        }
+
+        BeginDrawing();
+            BeginTextureMode(screen);
+                ClearBackground(GetColor(0x181818FF));
+                DrawCircle(x, y, r, GetColor(0xFF0000FF));
+            EndTextureMode();
+
+            ClearBackground(GetColor(0x181818FF));
+            DrawTexture(screen.texture, 0, 0, WHITE);
+        EndDrawing();
+
+        Image image = LoadImageFromTexture(screen.texture);
+        ffmpeg_send_frame_flipped(ffmpeg, image.data, WIDTH, HEIGHT);
+        UnloadImage(image);
+    }
+
+    UnloadRenderTexture(screen);
+    CloseWindow();
+
+    ffmpeg_end_rendering(ffmpeg);
+
+    printf("Done rendering the video!\n");
+
+    return 0;
+}