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
60// Forward decl
61struct Camera;
62
64using CameraRaw = SDL_Camera*;
65
66// Forward decl
67struct CameraRef;
68
71{
73
76 : value(value)
77 {
78 }
79
81 constexpr CameraParam(std::nullptr_t _ = nullptr)
82 : value(nullptr)
83 {
84 }
85
87 constexpr explicit operator bool() const { return !!value; }
88
90 constexpr auto operator<=>(const CameraParam& other) const = default;
91
93 constexpr operator CameraRaw() const { return value; }
94};
95
108using CameraID = SDL_CameraID;
109
121using CameraSpec = SDL_CameraSpec;
122
130using CameraPosition = SDL_CameraPosition;
131
133 SDL_CAMERA_POSITION_UNKNOWN;
134
136 SDL_CAMERA_POSITION_FRONT_FACING;
137
139 SDL_CAMERA_POSITION_BACK_FACING;
140
149{
150 CameraRaw m_resource = nullptr;
151
152public:
154 constexpr Camera() = default;
155
163 constexpr explicit Camera(const CameraRaw resource)
164 : m_resource(resource)
165 {
166 }
167
169 constexpr Camera(const Camera& other) = delete;
170
172 constexpr Camera(Camera&& other)
173 : Camera(other.release())
174 {
175 }
176
177 constexpr Camera(const CameraRef& other) = delete;
178
179 constexpr Camera(CameraRef&& other) = delete;
180
226 : m_resource(SDL_OpenCamera(instance_id, spec))
227 {
228 }
229
231 ~Camera() { SDL_CloseCamera(m_resource); }
232
235 {
236 std::swap(m_resource, other.m_resource);
237 return *this;
238 }
239
241 constexpr CameraRaw get() const { return m_resource; }
242
244 constexpr CameraRaw release()
245 {
246 auto r = m_resource;
247 m_resource = nullptr;
248 return r;
249 }
250
252 constexpr auto operator<=>(const Camera& other) const = default;
253
255 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
256
258 constexpr explicit operator bool() const { return !!m_resource; }
259
261 constexpr operator CameraParam() const { return {m_resource}; }
262
274 void Close();
275
306 int GetPermissionState();
307
320 CameraID GetID();
321
333
354 std::optional<CameraSpec> GetFormat();
355
396 Surface AcquireFrame(Uint64* timestampNS = nullptr);
397
423 void ReleaseFrame(SurfaceParam frame);
424};
425
428{
437 : Camera(resource.value)
438 {
439 }
440
442 CameraRef(const CameraRef& other)
443 : Camera(other.get())
444 {
445 }
446
449};
450
472inline int GetNumCameraDrivers() { return SDL_GetNumCameraDrivers(); }
473
496inline const char* GetCameraDriver(int index)
497{
498 return SDL_GetCameraDriver(index);
499}
500
515inline const char* GetCurrentCameraDriver()
516{
517 return SDL_GetCurrentCameraDriver();
518}
519
533{
534 int count;
535 auto data = SDL_GetCameras(&count);
536 return OwnArray<CameraID>(data, count);
537}
538
572{
573 int count;
574 auto data = SDL_GetCameraSupportedFormats(instance_id, &count);
575 return OwnArray<CameraSpec*>(data, count);
576}
577
591inline const char* GetCameraName(CameraID instance_id)
592{
593 return SDL_GetCameraName(instance_id);
594}
595
614{
615 return SDL_GetCameraPosition(instance_id);
616}
617
662inline Camera OpenCamera(CameraID instance_id,
664{
665 return Camera(instance_id, spec);
666}
667
700{
701 return SDL_GetCameraPermissionState(camera);
702}
703
705{
706 return SDL::GetCameraPermissionState(m_resource);
707}
708
723{
724 return CheckError(SDL_GetCameraID(camera));
725}
726
727inline CameraID Camera::GetID() { return SDL::GetCameraID(m_resource); }
728
741{
742 return {CheckError(SDL_GetCameraProperties(camera))};
743}
744
746{
747 return SDL::GetCameraProperties(m_resource);
748}
749
771inline std::optional<CameraSpec> GetCameraFormat(CameraParam camera)
772{
773 if (CameraSpec spec; SDL_GetCameraFormat(camera, &spec)) return spec;
774 return std::nullopt;
775}
776
777inline std::optional<CameraSpec> Camera::GetFormat()
778{
779 return SDL::GetCameraFormat(m_resource);
780}
781
823 Uint64* timestampNS = nullptr)
824{
825 return Surface::Borrow(SDL_AcquireCameraFrame(camera, timestampNS));
826}
827
829{
830 return SDL::AcquireCameraFrame(m_resource, timestampNS);
831}
832
860{
861 SDL_ReleaseCameraFrame(camera, frame);
862}
863
865{
866 SDL::ReleaseCameraFrame(m_resource, frame);
867}
868
881inline void CloseCamera(CameraRaw camera) { SDL_CloseCamera(camera); }
882
883inline void Camera::Close() { CloseCamera(release()); }
884
886
887} // namespace SDL
888
889#endif /* SDL3PP_CAMERA_H_ */
The opaque structure used to identify an opened SDL camera.
Definition: SDL3pp_camera.h:149
constexpr Camera()=default
Default ctor.
Camera & operator=(Camera other)
Assignment operator.
Definition: SDL3pp_camera.h:234
constexpr CameraRaw get() const
Retrieves underlying CameraRaw.
Definition: SDL3pp_camera.h:241
constexpr CameraRaw release()
Retrieves underlying CameraRaw and clear this.
Definition: SDL3pp_camera.h:244
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:172
constexpr Camera(const CameraRaw resource)
Constructs from CameraParam.
Definition: SDL3pp_camera.h:163
Camera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:225
~Camera()
Destructor.
Definition: SDL3pp_camera.h:231
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_camera.h:255
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:201
static constexpr Surface Borrow(SurfaceParam resource)
Safely borrows the from SurfaceParam.
Definition: SDL3pp_surface.h:378
Surface AcquireCameraFrame(CameraParam camera, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:822
constexpr CameraPosition CAMERA_POSITION_FRONT_FACING
CAMERA_POSITION_FRONT_FACING.
Definition: SDL3pp_camera.h:135
PropertiesRef GetCameraProperties(CameraParam camera)
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:740
Surface AcquireFrame(Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:828
CameraID GetCameraID(CameraParam camera)
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:722
void CloseCamera(CameraRaw camera)
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:881
int GetCameraPermissionState(CameraParam camera)
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:699
constexpr CameraPosition CAMERA_POSITION_UNKNOWN
CAMERA_POSITION_UNKNOWN.
Definition: SDL3pp_camera.h:132
void Close()
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:883
constexpr CameraPosition CAMERA_POSITION_BACK_FACING
CAMERA_POSITION_BACK_FACING.
Definition: SDL3pp_camera.h:138
void ReleaseCameraFrame(CameraParam camera, SurfaceParam frame)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:859
Camera OpenCamera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:662
int GetNumCameraDrivers()
Use this function to get the number of built-in camera drivers.
Definition: SDL3pp_camera.h:472
OwnArray< CameraID > GetCameras()
Get a list of currently connected camera devices.
Definition: SDL3pp_camera.h:532
SDL_CameraPosition CameraPosition
The position of camera in relation to system device.
Definition: SDL3pp_camera.h:130
SDL_CameraSpec CameraSpec
The details of an output format for a camera device.
Definition: SDL3pp_camera.h:121
OwnArray< CameraSpec * > GetCameraSupportedFormats(CameraID instance_id)
Get the list of native formats/sizes a camera supports.
Definition: SDL3pp_camera.h:571
SDL_Camera * CameraRaw
Alias to raw representation for Camera.
Definition: SDL3pp_camera.h:64
const char * GetCameraDriver(int index)
Use this function to get the name of a built in camera driver.
Definition: SDL3pp_camera.h:496
PropertiesRef GetProperties()
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:745
const char * GetCameraName(CameraID instance_id)
Get the human-readable device name for a camera.
Definition: SDL3pp_camera.h:591
std::optional< CameraSpec > GetCameraFormat(CameraParam camera)
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:771
int GetPermissionState()
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:704
CameraID GetID()
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:727
std::optional< CameraSpec > GetFormat()
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:777
const char * GetCurrentCameraDriver()
Get the name of the current camera driver.
Definition: SDL3pp_camera.h:515
void ReleaseFrame(SurfaceParam frame)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:864
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:108
CameraPosition GetCameraPosition(CameraID instance_id)
Get the position of the camera in relation to the system.
Definition: SDL3pp_camera.h:613
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
Main include header for the SDL3pp library.
Safely wrap Camera for non owning parameters.
Definition: SDL3pp_camera.h:71
constexpr auto operator<=>(const CameraParam &other) const =default
Comparison.
constexpr CameraParam(CameraRaw value)
Constructs from CameraRaw.
Definition: SDL3pp_camera.h:75
constexpr CameraParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_camera.h:81
CameraRaw value
parameter's CameraRaw
Definition: SDL3pp_camera.h:72
Semi-safe reference for Camera.
Definition: SDL3pp_camera.h:428
~CameraRef()
Destructor.
Definition: SDL3pp_camera.h:448
CameraRef(const CameraRef &other)
Copy constructor.
Definition: SDL3pp_camera.h:442
CameraRef(CameraParam resource)
Constructs from CameraParam.
Definition: SDL3pp_camera.h:436
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:701
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:46