Atlas - read-and-draw.c
Home / ext / SDL / examples / camera / 01-read-and-draw Lines: 1 | Size: 4239 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 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, ×tampNS); 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 SDL_SetRenderLogicalPresentation(renderer, frame->w, frame->h, SDL_LOGICAL_PRESENTATION_LETTERBOX); 87 texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h); 88 } 89 90 if (texture) { 91 SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch); 92 } 93 94 SDL_ReleaseCameraFrame(camera, frame); 95 } 96 97 SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, SDL_ALPHA_OPAQUE); 98 SDL_RenderClear(renderer); 99 if (texture) { /* draw the latest camera frame, if available. */ 100 SDL_RenderTexture(renderer, texture, NULL, NULL); 101 } 102 SDL_RenderPresent(renderer); 103 104 return SDL_APP_CONTINUE; /* carry on with the program! */ 105} 106 107/* This function runs once at shutdown. */ 108void SDL_AppQuit(void *appstate, SDL_AppResult result) 109{ 110 SDL_CloseCamera(camera); 111 SDL_DestroyTexture(texture); 112 /* SDL will clean up the window/renderer for us. */ 113} 114 115[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.