SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_camera.h
1#ifndef SDL3PP_CAMERA_H_
2#define SDL3PP_CAMERA_H_
3
4#include <SDL3/SDL_camera.h>
5#include "SDL3pp_error.h"
6#include "SDL3pp_pixels.h"
7#include "SDL3pp_properties.h"
8#include "SDL3pp_stdinc.h"
9#include "SDL3pp_surface.h"
10
11namespace SDL {
12
59
60// Forward decl
61struct Camera;
62
64using CameraRaw = SDL_Camera*;
65
72
73// Forward decl
74struct CameraFrame;
75
88using CameraID = SDL_CameraID;
89
101using CameraSpec = SDL_CameraSpec;
102
110using CameraPosition = SDL_CameraPosition;
111
113 SDL_CAMERA_POSITION_UNKNOWN;
114
116 SDL_CAMERA_POSITION_FRONT_FACING;
117
119 SDL_CAMERA_POSITION_BACK_FACING;
120
121#if SDL_VERSION_ATLEAST(3, 4, 0)
122
130using CameraPermissionState = SDL_CameraPermissionState;
131
133 SDL_CAMERA_PERMISSION_STATE_DENIED;
134
136 SDL_CAMERA_PERMISSION_STATE_PENDING;
137
139 SDL_CAMERA_PERMISSION_STATE_APPROVED;
140#else
141
149using CameraPermissionState = int;
150
151#endif // SDL_VERSION_ATLEAST(3, 4, 0)
152
160struct Camera : ResourceBase<CameraRaw>
161{
163
171 constexpr explicit Camera(CameraRaw resource) noexcept
172 : ResourceBase(resource)
173 {
174 }
175
177 constexpr Camera(const Camera& other) = delete;
178
180 constexpr Camera(Camera&& other) noexcept
181 : Camera(other.release())
182 {
183 }
184
185 constexpr Camera(const CameraRef& other) = delete;
186
187 constexpr Camera(CameraRef&& other) = delete;
188
233 Camera(CameraID instance_id, OptionalRef<const CameraSpec> spec = {});
234
236 ~Camera() { SDL_CloseCamera(get()); }
237
239 constexpr Camera& operator=(Camera&& other) noexcept
240 {
241 swap(*this, other);
242 return *this;
243 }
244
246 Camera& operator=(const Camera& other) = delete;
247
259 void Close();
260
294
307 CameraID GetID();
308
320
341 std::optional<CameraSpec> GetFormat();
342
383 CameraFrame AcquireFrame(Uint64* timestampNS = nullptr);
384
410 void ReleaseFrame(CameraFrame&& lock);
411};
412
414class CameraFrame : public Surface
415{
416 CameraRef m_lock;
417
418public:
460 CameraFrame(CameraRef resource, Uint64* timestampNS = nullptr);
461
463 CameraFrame(const CameraFrame& other) = delete;
464
466 CameraFrame(CameraFrame&& other) noexcept
467 : Surface(std::move(other))
468 , m_lock(std::move(other.m_lock))
469 {
470 }
471
496
497 CameraFrame& operator=(const CameraFrame& other) = delete;
498
501 {
502 std::swap(m_lock, other.m_lock);
503 return *this;
504 }
505
529 void reset();
530
532 CameraRef resource() const { return m_lock; }
533};
534
556inline int GetNumCameraDrivers() { return SDL_GetNumCameraDrivers(); }
557
580inline const char* GetCameraDriver(int index)
581{
582 return SDL_GetCameraDriver(index);
583}
584
599inline const char* GetCurrentCameraDriver()
600{
601 return SDL_GetCurrentCameraDriver();
602}
603
617{
618 int count;
619 auto data = SDL_GetCameras(&count);
620 return OwnArray<CameraID>(data, count);
621}
622
656{
657 int count;
658 auto data = SDL_GetCameraSupportedFormats(instance_id, &count);
659 return OwnArray<CameraSpec*>(data, count);
660}
661
675inline const char* GetCameraName(CameraID instance_id)
676{
677 return SDL_GetCameraName(instance_id);
678}
679
698{
699 return SDL_GetCameraPosition(instance_id);
700}
701
746inline Camera OpenCamera(CameraID instance_id,
748{
749 return Camera(instance_id, spec);
750}
751
753 : Camera(SDL_OpenCamera(instance_id, spec))
754{
755}
756
790{
791 return SDL_GetCameraPermissionState(camera);
792}
793
798
813{
814 return CheckError(SDL_GetCameraID(camera));
815}
816
818
831{
832 return {CheckError(SDL_GetCameraProperties(camera))};
833}
834
839
861inline std::optional<CameraSpec> GetCameraFormat(CameraRef camera)
862{
863 if (CameraSpec spec; SDL_GetCameraFormat(camera, &spec)) return spec;
864 return std::nullopt;
865}
866
867inline std::optional<CameraSpec> Camera::GetFormat()
868{
869 return SDL::GetCameraFormat(get());
870}
871
913 Uint64* timestampNS = nullptr)
914{
915 return Surface::Borrow(SDL_AcquireCameraFrame(camera, timestampNS));
916}
917
919{
920 return {CameraRef(*this), timestampNS};
921}
922
924 : Surface(AcquireCameraFrame(resource, timestampNS))
925 , m_lock(std::move(resource))
926{
927 if (!*this) m_lock.release();
928}
929
956inline void ReleaseCameraFrame(CameraRef camera, SurfaceRef frame)
957{
958 SDL_ReleaseCameraFrame(camera, frame);
959}
960
962{
963 SDL_assert_paranoid(lock.resource() == *this);
964 std::move(lock).reset();
965}
966
968{
969 if (!m_lock) return;
971 m_lock = {};
972}
973
986inline void CloseCamera(CameraRaw camera) { SDL_CloseCamera(camera); }
987
988inline void Camera::Close() { CloseCamera(release()); }
989
991
992} // namespace SDL
993
994#endif /* SDL3PP_CAMERA_H_ */
Camera Frame.
Definition SDL3pp_camera.h:415
CameraFrame & operator=(CameraFrame &&other) noexcept
Assignment operator.
Definition SDL3pp_camera.h:500
CameraFrame(CameraFrame &&other) noexcept
Move constructor.
Definition SDL3pp_camera.h:466
CameraFrame(const CameraFrame &other)=delete
Copy constructor.
CameraRef resource() const
Get the reference to locked resource.
Definition SDL3pp_camera.h:532
~CameraFrame()
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:495
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:383
void ReleaseCameraFrame(CameraRef camera, SurfaceRef frame)
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:956
CameraPermissionState GetPermissionState()
Query if camera access has been approved by the user.
Definition SDL3pp_camera.h:794
std::optional< CameraSpec > GetCameraFormat(CameraRef camera)
Get the spec that a camera is using when generating images.
Definition SDL3pp_camera.h:861
CameraFrame AcquireFrame(Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:918
constexpr CameraPosition CAMERA_POSITION_FRONT_FACING
CAMERA_POSITION_FRONT_FACING.
Definition SDL3pp_camera.h:115
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_APPROVED
CAMERA_PERMISSION_STATE_APPROVED.
Definition SDL3pp_camera.h:138
Surface AcquireCameraFrame(CameraRef camera, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:912
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_PENDING
CAMERA_PERMISSION_STATE_PENDING.
Definition SDL3pp_camera.h:135
SDL_Camera * CameraRaw
Alias to raw representation for Camera.
Definition SDL3pp_camera.h:64
void CloseCamera(CameraRaw camera)
Use this function to shut down camera processing and close the camera device.
Definition SDL3pp_camera.h:986
PropertiesRef GetCameraProperties(CameraRef camera)
Get the properties associated with an opened camera.
Definition SDL3pp_camera.h:830
constexpr CameraPosition CAMERA_POSITION_UNKNOWN
CAMERA_POSITION_UNKNOWN.
Definition SDL3pp_camera.h:112
void Close()
Use this function to shut down camera processing and close the camera device.
Definition SDL3pp_camera.h:988
constexpr CameraPosition CAMERA_POSITION_BACK_FACING
CAMERA_POSITION_BACK_FACING.
Definition SDL3pp_camera.h:118
void ReleaseFrame(CameraFrame &&lock)
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:961
CameraPermissionState GetCameraPermissionState(CameraRef camera)
Query if camera access has been approved by the user.
Definition SDL3pp_camera.h:789
Camera OpenCamera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition SDL3pp_camera.h:746
int GetNumCameraDrivers()
Use this function to get the number of built-in camera drivers.
Definition SDL3pp_camera.h:556
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_DENIED
CAMERA_PERMISSION_STATE_DENIED.
Definition SDL3pp_camera.h:132
OwnArray< CameraID > GetCameras()
Get a list of currently connected camera devices.
Definition SDL3pp_camera.h:616
SDL_CameraPosition CameraPosition
The position of camera in relation to system device.
Definition SDL3pp_camera.h:110
SDL_CameraID CameraID
This is a unique ID for a camera device for the time it is connected to the system,...
Definition SDL3pp_camera.h:88
OwnArray< CameraSpec * > GetCameraSupportedFormats(CameraID instance_id)
Get the list of native formats/sizes a camera supports.
Definition SDL3pp_camera.h:655
ResourceRef< Camera > CameraRef
Reference for Camera.
Definition SDL3pp_camera.h:71
CameraID GetCameraID(CameraRef camera)
Get the instance ID of an opened camera.
Definition SDL3pp_camera.h:812
CameraFrame(CameraRef resource, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:923
const char * GetCameraDriver(int index)
Use this function to get the name of a built in camera driver.
Definition SDL3pp_camera.h:580
PropertiesRef GetProperties()
Get the properties associated with an opened camera.
Definition SDL3pp_camera.h:835
const char * GetCameraName(CameraID instance_id)
Get the human-readable device name for a camera.
Definition SDL3pp_camera.h:675
void reset()
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:967
CameraID GetID()
Get the instance ID of an opened camera.
Definition SDL3pp_camera.h:817
std::optional< CameraSpec > GetFormat()
Get the spec that a camera is using when generating images.
Definition SDL3pp_camera.h:867
SDL_CameraPermissionState CameraPermissionState
The current state of a request for camera access.
Definition SDL3pp_camera.h:130
const char * GetCurrentCameraDriver()
Get the name of the current camera driver.
Definition SDL3pp_camera.h:599
CameraPosition GetCameraPosition(CameraID instance_id)
Get the position of the camera in relation to the system.
Definition SDL3pp_camera.h:697
SDL_CameraSpec CameraSpec
The details of an output format for a camera device.
Definition SDL3pp_camera.h:101
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:320
ResourceRef< Surface > SurfaceRef
Reference for Surface.
Definition SDL3pp_surface.h:54
Main include header for the SDL3pp library.
The opaque structure used to identify an opened SDL camera.
Definition SDL3pp_camera.h:161
constexpr Camera(const Camera &other)=delete
Copy constructor.
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr Camera(CameraRaw resource) noexcept
Constructs from raw Camera.
Definition SDL3pp_camera.h:171
constexpr Camera(Camera &&other) noexcept
Move constructor.
Definition SDL3pp_camera.h:180
constexpr Camera & operator=(Camera &&other) noexcept
Assignment operator.
Definition SDL3pp_camera.h:239
Camera & operator=(const Camera &other)=delete
Assignment operator.
~Camera()
Destructor.
Definition SDL3pp_camera.h:236
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:172
constexpr Surface(SurfaceRaw resource) noexcept
Constructs from raw Surface.
Definition SDL3pp_surface.h:182
static Surface Borrow(SurfaceRaw resource)
Safely borrows the from SurfaceRaw.
Definition SDL3pp_surface.h:340