Atlas - read-and-draw.c

Home / ext / SDL / examples / camera / 01-read-and-draw Lines: 1 | Size: 4266 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
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, SDL_WINDOW_RESIZABLE, &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 SDL_free(devices); 48 return SDL_APP_FAILURE; 49 } 50 51 camera = SDL_OpenCamera(devices[0], NULL); // just take the first thing we see in any format it wants. 52 SDL_free(devices); 53 if (camera == NULL) { 54 SDL_Log("Couldn't open camera: %s", SDL_GetError()); 55 return SDL_APP_FAILURE; 56 } 57 58 return SDL_APP_CONTINUE; /* carry on with the program! */ 59} 60 61/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 62SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 63{ 64 if (event->type == SDL_EVENT_QUIT) { 65 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 66 } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) { 67 SDL_Log("Camera use approved by user!"); 68 } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) { 69 SDL_Log("Camera use denied by user!"); 70 return SDL_APP_FAILURE; 71 } 72 return SDL_APP_CONTINUE; /* carry on with the program! */ 73} 74 75/* This function runs once per frame, and is the heart of the program. */ 76SDL_AppResult SDL_AppIterate(void *appstate) 77{ 78 Uint64 timestampNS = 0; 79 SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS); 80 81 if (frame != NULL) { 82 /* Some platforms (like Emscripten) don't know _what_ the camera offers 83 until the user gives permission, so we build the texture and resize 84 the window when we get a first frame from the camera. */ 85 if (!texture) { 86 SDL_SetWindowSize(window, frame->w, frame->h); /* Resize the window to match */ 87 SDL_SetRenderLogicalPresentation(renderer, frame->w, frame->h, SDL_LOGICAL_PRESENTATION_LETTERBOX); 88 texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h); 89 } 90 91 if (texture) { 92 SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch); 93 } 94 95 SDL_ReleaseCameraFrame(camera, frame); 96 } 97 98 SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, SDL_ALPHA_OPAQUE); 99 SDL_RenderClear(renderer); 100 if (texture) { /* draw the latest camera frame, if available. */ 101 SDL_RenderTexture(renderer, texture, NULL, NULL); 102 } 103 SDL_RenderPresent(renderer); 104 105 return SDL_APP_CONTINUE; /* carry on with the program! */ 106} 107 108/* This function runs once at shutdown. */ 109void SDL_AppQuit(void *appstate, SDL_AppResult result) 110{ 111 SDL_CloseCamera(camera); 112 SDL_DestroyTexture(texture); 113 /* SDL will clean up the window/renderer for us. */ 114} 115 116
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.