read-and-draw.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. /* We will use this renderer to draw into this window every frame. */
  15. static SDL_Window *window = NULL;
  16. static SDL_Renderer *renderer = NULL;
  17. static SDL_Camera *camera = NULL;
  18. static SDL_Texture *texture = NULL;
  19. /* This function runs once at startup. */
  20. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  21. {
  22. SDL_CameraID *devices = NULL;
  23. int devcount = 0;
  24. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) {
  25. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  26. return SDL_APP_FAILURE;
  27. }
  28. if (!SDL_CreateWindowAndRenderer("examples/camera/read-and-draw", 640, 480, 0, &window, &renderer)) {
  29. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  30. return SDL_APP_FAILURE;
  31. }
  32. devices = SDL_GetCameras(&devcount);
  33. if (devices == NULL) {
  34. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't enumerate camera devices!", SDL_GetError(), window);
  35. return SDL_APP_FAILURE;
  36. } else if (devcount == 0) {
  37. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't find any camera devices!", "Please connect a camera and try again.", window);
  38. return SDL_APP_FAILURE;
  39. }
  40. camera = SDL_OpenCamera(devices[0], NULL); // just take the first thing we see in any format it wants.
  41. SDL_free(devices);
  42. if (camera == NULL) {
  43. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't open camera!", SDL_GetError(), window);
  44. return SDL_APP_FAILURE;
  45. }
  46. return SDL_APP_CONTINUE; /* carry on with the program! */
  47. }
  48. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  49. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  50. {
  51. if (event->type == SDL_EVENT_QUIT) {
  52. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  53. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) {
  54. SDL_Log("Camera use approved by user!");
  55. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) {
  56. SDL_Log("Camera use denied by user!");
  57. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Camera permission denied!", "User denied access to the camera!", window);
  58. return SDL_APP_FAILURE;
  59. }
  60. return SDL_APP_CONTINUE; /* carry on with the program! */
  61. }
  62. /* This function runs once per frame, and is the heart of the program. */
  63. SDL_AppResult SDL_AppIterate(void *appstate)
  64. {
  65. Uint64 timestampNS = 0;
  66. SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS);
  67. if (frame != NULL) {
  68. /* Some platforms (like Emscripten) don't know _what_ the camera offers
  69. until the user gives permission, so we build the texture and resize
  70. the window when we get a first frame from the camera. */
  71. if (!texture) {
  72. SDL_SetWindowSize(window, frame->w, frame->h); /* Resize the window to match */
  73. texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h);
  74. }
  75. if (texture) {
  76. SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch);
  77. }
  78. SDL_ReleaseCameraFrame(camera, frame);
  79. }
  80. SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, 255);
  81. SDL_RenderClear(renderer);
  82. if (texture) { /* draw the latest camera frame, if available. */
  83. SDL_RenderTexture(renderer, texture, NULL, NULL);
  84. }
  85. SDL_RenderPresent(renderer);
  86. return SDL_APP_CONTINUE; /* carry on with the program! */
  87. }
  88. /* This function runs once at shutdown. */
  89. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  90. {
  91. SDL_CloseCamera(camera);
  92. SDL_DestroyTexture(texture);
  93. /* SDL will clean up the window/renderer for us. */
  94. }