SDL: examples: Added a simple camera example.

From 85ca5167354a333599cf64a79987f8a8be6dbd21 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 19 Aug 2024 14:48:25 -0400
Subject: [PATCH] examples: Added a simple camera example.

---
 examples/CMakeLists.txt                       |   1 +
 examples/camera/01-read-and-draw/README.txt   |   1 +
 .../camera/01-read-and-draw/read-and-draw.c   | 112 ++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 examples/camera/01-read-and-draw/README.txt
 create mode 100644 examples/camera/01-read-and-draw/read-and-draw.c

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index eea6a2887c724..21770603444f3 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -186,6 +186,7 @@ add_sdl_example_executable(renderer-primitives SOURCES renderer/02-primitives/re
 add_sdl_example_executable(audio-simple-playback SOURCES audio/01-simple-playback/simple-playback.c)
 add_sdl_example_executable(audio-simple-playback-callback SOURCES audio/02-simple-playback-callback/simple-playback-callback.c)
 add_sdl_example_executable(audio-load-wav SOURCES audio/03-load-wav/load-wav.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.wav)
+add_sdl_example_executable(camera-read-and-draw SOURCES camera/01-read-and-draw/read-and-draw.c)
 add_sdl_example_executable(pen-drawing-lines SOURCES pen/01-drawing-lines/drawing-lines.c)
 add_sdl_example_executable(game-snake SOURCES game/01-snake/main.c game/01-snake/snake.c)
 
diff --git a/examples/camera/01-read-and-draw/README.txt b/examples/camera/01-read-and-draw/README.txt
new file mode 100644
index 0000000000000..f7ac1f51c03eb
--- /dev/null
+++ b/examples/camera/01-read-and-draw/README.txt
@@ -0,0 +1 @@
+This reads from a webcam and draws frames of video to the screen.
diff --git a/examples/camera/01-read-and-draw/read-and-draw.c b/examples/camera/01-read-and-draw/read-and-draw.c
new file mode 100644
index 0000000000000..91997e3f2d150
--- /dev/null
+++ b/examples/camera/01-read-and-draw/read-and-draw.c
@@ -0,0 +1,112 @@
+/*
+ * This example code reads frames from a camera and draws it to the screen.
+ *
+ * This is a very simple approach that is often Good Enough. You can get
+ * fancier with this: multiple cameras, front/back facing cameras on phones,
+ * color spaces, choosing formats and framerates...this just requests
+ * _anything_ and goes with what it is handed.
+ *
+ * This code is public domain. Feel free to use it for any purpose!
+ */
+
+#define SDL_MAIN_USE_CALLBACKS 1  /* use the callbacks instead of main() */
+#include <SDL3/SDL.h>
+#include <SDL3/SDL_main.h>
+
+/* We will use this renderer to draw into this window every frame. */
+static SDL_Window *window = NULL;
+static SDL_Renderer *renderer = NULL;
+static SDL_Camera *camera = NULL;
+static SDL_Texture *texture = NULL;
+
+
+/* This function runs once at startup. */
+SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
+{
+    SDL_CameraID *devices = NULL;
+    int devcount = 0;
+
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA) == -1) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
+        return SDL_APP_FAILURE;
+    }
+
+    if (SDL_CreateWindowAndRenderer("examples/camera/read-and-draw", 640, 480, 0, &window, &renderer) == -1) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
+        return SDL_APP_FAILURE;
+    }
+
+    devices = SDL_GetCameras(&devcount);
+    if (devices == NULL) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't enumerate camera devices!", SDL_GetError(), window);
+        return SDL_APP_FAILURE;
+    } else if (devcount == 0) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't find any camera devices!", "Please connect a camera and try again.", window);
+        return SDL_APP_FAILURE;
+    }
+
+    camera = SDL_OpenCamera(devices[0], NULL);  // just take the first thing we see in any format it wants.
+    SDL_free(devices);
+    if (camera == NULL) {
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't open camera!", SDL_GetError(), window);
+        return SDL_APP_FAILURE;
+    }
+
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
+SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
+{
+    if (event->type == SDL_EVENT_QUIT) {
+        return SDL_APP_SUCCESS;  /* end the program, reporting success to the OS. */
+    } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) {
+        SDL_Log("Camera use approved by user!");
+    } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) {
+        SDL_Log("Camera use denied by user!");
+        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Camera permission denied!", "User denied access to the camera!", window);
+        return SDL_APP_FAILURE;
+    }
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs once per frame, and is the heart of the program. */
+SDL_AppResult SDL_AppIterate(void *appstate)
+{
+    Uint64 timestampNS = 0;
+    SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS);
+
+    if (frame != NULL) {
+        /* Some platforms (like Emscripten) don't know _what_ the camera offers
+           until the user gives permission, so we build the texture and resize
+           the window when we get a first frame from the camera. */
+        if (!texture) {
+            SDL_SetWindowSize(window, frame->w, frame->h);  /* Resize the window to match */
+            texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h);
+        }
+
+        if (texture) {
+            SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch);
+        }
+
+        SDL_ReleaseCameraFrame(camera, frame);
+    }
+
+    SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, 255);
+    SDL_RenderClear(renderer);
+    if (texture) {  /* draw the latest camera frame, if available. */
+        SDL_RenderTexture(renderer, texture, NULL, NULL);
+    }
+    SDL_RenderPresent(renderer);
+
+    return SDL_APP_CONTINUE;  /* carry on with the program! */
+}
+
+/* This function runs once at shutdown. */
+void SDL_AppQuit(void *appstate)
+{
+    SDL_CloseCamera(camera);
+    SDL_DestroyTexture(texture);
+    /* SDL will clean up the window/renderer for us. */
+}
+