From 6aaedb813fa11ba0679c3051bc2eb28646b9506c Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 30 Aug 2025 16:53:58 -0700 Subject: Update to SDL3 --- .../examples/camera/01-read-and-draw/README.txt | 1 + .../camera/01-read-and-draw/onmouseover.webp | Bin 0 -> 346856 bytes .../camera/01-read-and-draw/read-and-draw.c | 113 +++++++++++++++++++++ .../examples/camera/01-read-and-draw/thumbnail.png | Bin 0 -> 239597 bytes 4 files changed, 114 insertions(+) create mode 100644 src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/README.txt create mode 100644 src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/onmouseover.webp create mode 100644 src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c create mode 100644 src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/thumbnail.png (limited to 'src/contrib/SDL-3.2.20/examples/camera') diff --git a/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/README.txt b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/README.txt new file mode 100644 index 0000000..f7ac1f5 --- /dev/null +++ b/src/contrib/SDL-3.2.20/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/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/onmouseover.webp b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/onmouseover.webp new file mode 100644 index 0000000..1d414e6 Binary files /dev/null and b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/onmouseover.webp differ diff --git a/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c new file mode 100644 index 0000000..989fd54 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c @@ -0,0 +1,113 @@ +/* + * 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 +#include + +/* 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; + + SDL_SetAppMetadata("Example Camera Read and Draw", "1.0", "com.example.camera-read-and-draw"); + + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } + + if (!SDL_CreateWindowAndRenderer("examples/camera/read-and-draw", 640, 480, 0, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } + + devices = SDL_GetCameras(&devcount); + if (devices == NULL) { + SDL_Log("Couldn't enumerate camera devices: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } else if (devcount == 0) { + SDL_Log("Couldn't find any camera devices! Please connect a camera and try again."); + 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_Log("Couldn't open camera: %s", SDL_GetError()); + 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, 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!"); + 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, ×tampNS); + + 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, SDL_ALPHA_OPAQUE); + 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_AppResult result) +{ + SDL_CloseCamera(camera); + SDL_DestroyTexture(texture); + /* SDL will clean up the window/renderer for us. */ +} + diff --git a/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/thumbnail.png b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/thumbnail.png new file mode 100644 index 0000000..98fe4e0 Binary files /dev/null and b/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/thumbnail.png differ -- cgit v1.2.3