summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c')
-rw-r--r--src/contrib/SDL-3.2.20/examples/camera/01-read-and-draw/read-and-draw.c113
1 files changed, 113 insertions, 0 deletions
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 @@
1/*
2 * This example code reads frames from a camera and draws it to the screen.
3 *
4 * This is a very simple approach that is often Good Enough. You can get
5 * fancier with this: multiple cameras, front/back facing cameras on phones,
6 * color spaces, choosing formats and framerates...this just requests
7 * _anything_ and goes with what it is handed.
8 *
9 * This code is public domain. Feel free to use it for any purpose!
10 */
11
12#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
13#include <SDL3/SDL.h>
14#include <SDL3/SDL_main.h>
15
16/* We will use this renderer to draw into this window every frame. */
17static SDL_Window *window = NULL;
18static SDL_Renderer *renderer = NULL;
19static SDL_Camera *camera = NULL;
20static SDL_Texture *texture = NULL;
21
22
23/* This function runs once at startup. */
24SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
25{
26 SDL_CameraID *devices = NULL;
27 int devcount = 0;
28
29 SDL_SetAppMetadata("Example Camera Read and Draw", "1.0", "com.example.camera-read-and-draw");
30
31 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) {
32 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
33 return SDL_APP_FAILURE;
34 }
35
36 if (!SDL_CreateWindowAndRenderer("examples/camera/read-and-draw", 640, 480, 0, &window, &renderer)) {
37 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
38 return SDL_APP_FAILURE;
39 }
40
41 devices = SDL_GetCameras(&devcount);
42 if (devices == NULL) {
43 SDL_Log("Couldn't enumerate camera devices: %s", SDL_GetError());
44 return SDL_APP_FAILURE;
45 } else if (devcount == 0) {
46 SDL_Log("Couldn't find any camera devices! Please connect a camera and try again.");
47 return SDL_APP_FAILURE;
48 }
49
50 camera = SDL_OpenCamera(devices[0], NULL); // just take the first thing we see in any format it wants.
51 SDL_free(devices);
52 if (camera == NULL) {
53 SDL_Log("Couldn't open camera: %s", SDL_GetError());
54 return SDL_APP_FAILURE;
55 }
56
57 return SDL_APP_CONTINUE; /* carry on with the program! */
58}
59
60/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
61SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
62{
63 if (event->type == SDL_EVENT_QUIT) {
64 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
65 } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) {
66 SDL_Log("Camera use approved by user!");
67 } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) {
68 SDL_Log("Camera use denied by user!");
69 return SDL_APP_FAILURE;
70 }
71 return SDL_APP_CONTINUE; /* carry on with the program! */
72}
73
74/* This function runs once per frame, and is the heart of the program. */
75SDL_AppResult SDL_AppIterate(void *appstate)
76{
77 Uint64 timestampNS = 0;
78 SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS);
79
80 if (frame != NULL) {
81 /* Some platforms (like Emscripten) don't know _what_ the camera offers
82 until the user gives permission, so we build the texture and resize
83 the window when we get a first frame from the camera. */
84 if (!texture) {
85 SDL_SetWindowSize(window, frame->w, frame->h); /* Resize the window to match */
86 texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h);
87 }
88
89 if (texture) {
90 SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch);
91 }
92
93 SDL_ReleaseCameraFrame(camera, frame);
94 }
95
96 SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, SDL_ALPHA_OPAQUE);
97 SDL_RenderClear(renderer);
98 if (texture) { /* draw the latest camera frame, if available. */
99 SDL_RenderTexture(renderer, texture, NULL, NULL);
100 }
101 SDL_RenderPresent(renderer);
102
103 return SDL_APP_CONTINUE; /* carry on with the program! */
104}
105
106/* This function runs once at shutdown. */
107void SDL_AppQuit(void *appstate, SDL_AppResult result)
108{
109 SDL_CloseCamera(camera);
110 SDL_DestroyTexture(texture);
111 /* SDL will clean up the window/renderer for us. */
112}
113