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
61// Forward decl
62struct Camera;
63
65using CameraRaw = SDL_Camera*;
66
67// Forward decl
68struct CameraRef;
69
72{
74
77 : value(value)
78 {
79 }
80
82 constexpr CameraParam(std::nullptr_t _ = nullptr)
83 : value(nullptr)
84 {
85 }
86
88 constexpr explicit operator bool() const { return !!value; }
89
91 constexpr auto operator<=>(const CameraParam& other) const = default;
92
94 constexpr operator CameraRaw() const { return value; }
95};
96
109using CameraID = SDL_CameraID;
110
122using CameraSpec = SDL_CameraSpec;
123
131using CameraPosition = SDL_CameraPosition;
132
134 SDL_CAMERA_POSITION_UNKNOWN;
135
137 SDL_CAMERA_POSITION_FRONT_FACING;
138
140 SDL_CAMERA_POSITION_BACK_FACING;
141
150{
151 CameraRaw m_resource = nullptr;
152
153public:
155 constexpr Camera() = default;
156
164 constexpr explicit Camera(const CameraRaw resource)
165 : m_resource(resource)
166 {
167 }
168
170 constexpr Camera(const Camera& other) = delete;
171
173 constexpr Camera(Camera&& other)
174 : Camera(other.release())
175 {
176 }
177
178 constexpr Camera(const CameraRef& other) = delete;
179
180 constexpr Camera(CameraRef&& other) = delete;
181
228 : m_resource(SDL_OpenCamera(instance_id, spec))
229 {
230 }
231
233 ~Camera() { SDL_CloseCamera(m_resource); }
234
237 {
238 std::swap(m_resource, other.m_resource);
239 return *this;
240 }
241
243 constexpr CameraRaw get() const { return m_resource; }
244
246 constexpr CameraRaw release()
247 {
248 auto r = m_resource;
249 m_resource = nullptr;
250 return r;
251 }
252
254 constexpr auto operator<=>(const Camera& other) const = default;
255
257 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
258
260 constexpr explicit operator bool() const { return !!m_resource; }
261
263 constexpr operator CameraParam() const { return {m_resource}; }
264
277 void Close();
278
309 int GetPermissionState();
310
323 CameraID GetID();
324
336
358 std::optional<CameraSpec> GetFormat();
359
400 Surface AcquireFrame(Uint64* timestampNS = nullptr);
401
427 void ReleaseFrame(SurfaceParam frame);
428};
429
432{
441 : Camera(resource.value)
442 {
443 }
444
446 CameraRef(const CameraRef& other)
447 : Camera(other.get())
448 {
449 }
450
453};
454
476inline int GetNumCameraDrivers() { return SDL_GetNumCameraDrivers(); }
477
500inline const char* GetCameraDriver(int index)
501{
502 return SDL_GetCameraDriver(index);
503}
504
519inline const char* GetCurrentCameraDriver()
520{
521 return SDL_GetCurrentCameraDriver();
522}
523
539{
540 int count;
541 auto data = SDL_GetCameras(&count);
542 return OwnArray<CameraID>(data, count);
543}
544
582inline SDL_CameraSpec** GetCameraSupportedFormats(CameraID instance_id,
583 int* count)
584{
585 return SDL_GetCameraSupportedFormats(instance_id, count);
586}
587
601inline const char* GetCameraName(CameraID instance_id)
602{
603 return SDL_GetCameraName(instance_id);
604}
605
624{
625 return SDL_GetCameraPosition(instance_id);
626}
627
673inline Camera OpenCamera(CameraID instance_id,
675{
676 return Camera(instance_id, spec);
677}
678
711{
712 return SDL_GetCameraPermissionState(camera);
713}
714
716{
717 return SDL::GetCameraPermissionState(m_resource);
718}
719
734{
735 return CheckError(SDL_GetCameraID(camera));
736}
737
738inline CameraID Camera::GetID() { return SDL::GetCameraID(m_resource); }
739
752{
753 return {CheckError(SDL_GetCameraProperties(camera))};
754}
755
757{
758 return SDL::GetCameraProperties(m_resource);
759}
760
783inline std::optional<CameraSpec> GetCameraFormat(CameraParam camera)
784{
785 if (CameraSpec spec; SDL_GetCameraFormat(camera, &spec)) return spec;
786 return std::nullopt;
787}
788
789inline std::optional<CameraSpec> Camera::GetFormat()
790{
791 return SDL::GetCameraFormat(m_resource);
792}
793
836 Uint64* timestampNS = nullptr)
837{
838 return Surface::Borrow(SDL_AcquireCameraFrame(camera, timestampNS));
839}
840
842{
843 return SDL::AcquireCameraFrame(m_resource, timestampNS);
844}
845
873{
874 SDL_ReleaseCameraFrame(camera, frame);
875}
876
878{
879 SDL::ReleaseCameraFrame(m_resource, frame);
880}
881
895inline void CloseCamera(CameraRaw camera) { SDL_CloseCamera(camera); }
896
897inline void Camera::Close() { CloseCamera(release()); }
898
900
901} // namespace SDL
902
903#endif /* SDL3PP_CAMERA_H_ */
The opaque structure used to identify an opened SDL camera.
Definition: SDL3pp_camera.h:150
constexpr Camera()=default
Default ctor.
Camera & operator=(Camera other)
Assignment operator.
Definition: SDL3pp_camera.h:236
constexpr CameraRaw get() const
Retrieves underlying CameraRaw.
Definition: SDL3pp_camera.h:243
constexpr CameraRaw release()
Retrieves underlying CameraRaw and clear this.
Definition: SDL3pp_camera.h:246
constexpr auto operator<=>(const Camera &other) const =default
Comparison.
constexpr Camera(const Camera &other)=delete
Copy constructor.
constexpr Camera(Camera &&other)
Move constructor.
Definition: SDL3pp_camera.h:173
constexpr Camera(const CameraRaw resource)
Constructs from CameraParam.
Definition: SDL3pp_camera.h:164
Camera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:227
~Camera()
Destructor.
Definition: SDL3pp_camera.h:233
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_camera.h:257
Optional-like shim for references.
Definition: SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
A collection of pixels used in software blitting.
Definition: SDL3pp_surface.h:195
static constexpr Surface Borrow(SurfaceParam resource)
Safely borrows the from SurfaceParam.
Definition: SDL3pp_surface.h:374
Surface AcquireCameraFrame(CameraParam camera, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:835
constexpr CameraPosition CAMERA_POSITION_FRONT_FACING
CAMERA_POSITION_FRONT_FACING.
Definition: SDL3pp_camera.h:136
PropertiesRef GetCameraProperties(CameraParam camera)
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:751
Surface AcquireFrame(Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:841
CameraID GetCameraID(CameraParam camera)
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:733
void CloseCamera(CameraRaw camera)
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:895
int GetCameraPermissionState(CameraParam camera)
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:710
constexpr CameraPosition CAMERA_POSITION_UNKNOWN
CAMERA_POSITION_UNKNOWN.
Definition: SDL3pp_camera.h:133
void Close()
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:897
constexpr CameraPosition CAMERA_POSITION_BACK_FACING
CAMERA_POSITION_BACK_FACING.
Definition: SDL3pp_camera.h:139
void ReleaseCameraFrame(CameraParam camera, SurfaceParam frame)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:872
Camera OpenCamera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:673
int GetNumCameraDrivers()
Use this function to get the number of built-in camera drivers.
Definition: SDL3pp_camera.h:476
OwnArray< CameraID > GetCameras()
Get a list of currently connected camera devices.
Definition: SDL3pp_camera.h:538
SDL_CameraPosition CameraPosition
The position of camera in relation to system device.
Definition: SDL3pp_camera.h:131
SDL_CameraSpec CameraSpec
The details of an output format for a camera device.
Definition: SDL3pp_camera.h:122
SDL_Camera * CameraRaw
Alias to raw representation for Camera.
Definition: SDL3pp_camera.h:65
SDL_CameraSpec ** GetCameraSupportedFormats(CameraID instance_id, int *count)
Get the list of native formats/sizes a camera supports.
Definition: SDL3pp_camera.h:582
const char * GetCameraDriver(int index)
Use this function to get the name of a built in camera driver.
Definition: SDL3pp_camera.h:500
PropertiesRef GetProperties()
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:756
const char * GetCameraName(CameraID instance_id)
Get the human-readable device name for a camera.
Definition: SDL3pp_camera.h:601
std::optional< CameraSpec > GetCameraFormat(CameraParam camera)
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:783
int GetPermissionState()
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:715
CameraID GetID()
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:738
std::optional< CameraSpec > GetFormat()
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:789
const char * GetCurrentCameraDriver()
Get the name of the current camera driver.
Definition: SDL3pp_camera.h:519
void ReleaseFrame(SurfaceParam frame)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:877
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:109
CameraPosition GetCameraPosition(CameraID instance_id)
Get the position of the camera in relation to the system.
Definition: SDL3pp_camera.h:623
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:363
Main include header for the SDL3pp library.
Safely wrap Camera for non owning parameters.
Definition: SDL3pp_camera.h:72
constexpr auto operator<=>(const CameraParam &other) const =default
Comparison.
constexpr CameraParam(CameraRaw value)
Constructs from CameraRaw.
Definition: SDL3pp_camera.h:76
constexpr CameraParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_camera.h:82
CameraRaw value
parameter's CameraRaw
Definition: SDL3pp_camera.h:73
Semi-safe reference for Camera.
Definition: SDL3pp_camera.h:432
~CameraRef()
Destructor.
Definition: SDL3pp_camera.h:452
CameraRef(const CameraRef &other)
Copy constructor.
Definition: SDL3pp_camera.h:446
CameraRef(CameraParam resource)
Constructs from CameraParam.
Definition: SDL3pp_camera.h:440
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:569
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:46