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 CameraBase;
62
63// Forward decl
64struct Camera;
65
67using CameraRaw = SDL_Camera*;
68
75
76// Forward decl
77struct CameraFrame;
78
91using CameraID = SDL_CameraID;
92
104using CameraSpec = SDL_CameraSpec;
105
113using CameraPosition = SDL_CameraPosition;
114
116 SDL_CAMERA_POSITION_UNKNOWN;
117
119 SDL_CAMERA_POSITION_FRONT_FACING;
120
122 SDL_CAMERA_POSITION_BACK_FACING;
123
124#if SDL_VERSION_ATLEAST(3, 4, 0)
125
133using CameraPermissionState = SDL_CameraPermissionState;
134
136 SDL_CAMERA_PERMISSION_STATE_DENIED;
137
139 SDL_CAMERA_PERMISSION_STATE_PENDING;
140
142 SDL_CAMERA_PERMISSION_STATE_APPROVED;
143#else
144
152using CameraPermissionState = int;
153
154#endif // SDL_VERSION_ATLEAST(3, 4, 0)
155
161struct CameraBase : ResourceBaseT<CameraRaw>
162{
164
176 void Close();
177
211
224 CameraID GetID();
225
237
258 std::optional<CameraSpec> GetFormat();
259
300 CameraFrame AcquireFrame(Uint64* timestampNS = nullptr);
301
327 void ReleaseFrame(CameraFrame&& lock);
328};
329
338{
339 using CameraBase::CameraBase;
340
348 constexpr explicit Camera(CameraRaw resource) noexcept
349 : CameraBase(resource)
350 {
351 }
352
354 constexpr Camera(Camera&& other) noexcept
355 : Camera(other.release())
356 {
357 }
358
403 Camera(CameraID instance_id, OptionalRef<const CameraSpec> spec = {});
404
406 ~Camera() { SDL_CloseCamera(get()); }
407
409 constexpr Camera& operator=(Camera&& other) noexcept
410 {
411 swap(*this, other);
412 return *this;
413 }
414};
415
417class CameraFrame : public Surface
418{
419 CameraRef m_lock;
420
421public:
463 CameraFrame(CameraRef resource, Uint64* timestampNS = nullptr);
464
466 CameraFrame(const CameraFrame& other) = delete;
467
469 CameraFrame(CameraFrame&& other) noexcept
470 : Surface(std::move(other))
471 , m_lock(std::move(other.m_lock))
472 {
473 }
474
499
500 CameraFrame& operator=(const CameraFrame& other) = delete;
501
504 {
505 std::swap(m_lock, other.m_lock);
506 return *this;
507 }
508
532 void reset();
533
535 CameraRef resource() const { return m_lock; }
536};
537
559inline int GetNumCameraDrivers() { return SDL_GetNumCameraDrivers(); }
560
583inline const char* GetCameraDriver(int index)
584{
585 return SDL_GetCameraDriver(index);
586}
587
602inline const char* GetCurrentCameraDriver()
603{
604 return SDL_GetCurrentCameraDriver();
605}
606
620{
621 int count;
622 auto data = SDL_GetCameras(&count);
623 return OwnArray<CameraID>(data, count);
624}
625
659{
660 int count;
661 auto data = SDL_GetCameraSupportedFormats(instance_id, &count);
662 return OwnArray<CameraSpec*>(data, count);
663}
664
678inline const char* GetCameraName(CameraID instance_id)
679{
680 return SDL_GetCameraName(instance_id);
681}
682
701{
702 return SDL_GetCameraPosition(instance_id);
703}
704
749inline Camera OpenCamera(CameraID instance_id,
751{
752 return Camera(instance_id, spec);
753}
754
756 : Camera(SDL_OpenCamera(instance_id, spec))
757{
758}
759
793{
794 return SDL_GetCameraPermissionState(camera);
795}
796
801
816{
817 return CheckError(SDL_GetCameraID(camera));
818}
819
821
834{
835 return {CheckError(SDL_GetCameraProperties(camera))};
836}
837
842
864inline std::optional<CameraSpec> GetCameraFormat(CameraRef camera)
865{
866 if (CameraSpec spec; SDL_GetCameraFormat(camera, &spec)) return spec;
867 return std::nullopt;
868}
869
870inline std::optional<CameraSpec> CameraBase::GetFormat()
871{
872 return SDL::GetCameraFormat(get());
873}
874
916 Uint64* timestampNS = nullptr)
917{
918 return Surface::borrow(SDL_AcquireCameraFrame(camera, timestampNS));
919}
920
922{
923 return {CameraRef(*this), timestampNS};
924}
925
927 : Surface(AcquireCameraFrame(resource, timestampNS))
928 , m_lock(std::move(resource))
929{
930 if (!*this) m_lock.release();
931}
932
959inline void ReleaseCameraFrame(CameraRef camera, SurfaceRef frame)
960{
961 SDL_ReleaseCameraFrame(camera, frame);
962}
963
965{
966 SDL_assert_paranoid(lock.resource() == *this);
967 std::move(lock).reset();
968}
969
971{
972 if (!m_lock) return;
974 m_lock = {};
975}
976
989inline void CloseCamera(CameraRaw camera) { SDL_CloseCamera(camera); }
990
992
994
995} // namespace SDL
996
997#endif /* SDL3PP_CAMERA_H_ */
Camera Frame.
Definition SDL3pp_camera.h:418
CameraFrame & operator=(CameraFrame &&other) noexcept
Assignment operator.
Definition SDL3pp_camera.h:503
CameraFrame(CameraFrame &&other) noexcept
Move constructor.
Definition SDL3pp_camera.h:469
CameraFrame(const CameraFrame &other)=delete
Copy constructor.
CameraRef resource() const
Get the reference to locked resource.
Definition SDL3pp_camera.h:535
~CameraFrame()
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:498
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 release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
#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:959
CameraID GetID()
Get the instance ID of an opened camera.
Definition SDL3pp_camera.h:820
std::optional< CameraSpec > GetCameraFormat(CameraRef camera)
Get the spec that a camera is using when generating images.
Definition SDL3pp_camera.h:864
void ReleaseFrame(CameraFrame &&lock)
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:964
constexpr CameraPosition CAMERA_POSITION_FRONT_FACING
CAMERA_POSITION_FRONT_FACING.
Definition SDL3pp_camera.h:118
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_APPROVED
CAMERA_PERMISSION_STATE_APPROVED.
Definition SDL3pp_camera.h:141
Surface AcquireCameraFrame(CameraRef camera, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:915
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_PENDING
CAMERA_PERMISSION_STATE_PENDING.
Definition SDL3pp_camera.h:138
SDL_Camera * CameraRaw
Alias to raw representation for Camera.
Definition SDL3pp_camera.h:67
void CloseCamera(CameraRaw camera)
Use this function to shut down camera processing and close the camera device.
Definition SDL3pp_camera.h:989
PropertiesRef GetCameraProperties(CameraRef camera)
Get the properties associated with an opened camera.
Definition SDL3pp_camera.h:833
constexpr CameraPosition CAMERA_POSITION_UNKNOWN
CAMERA_POSITION_UNKNOWN.
Definition SDL3pp_camera.h:115
ResourceRefT< CameraBase > CameraRef
Reference for Camera.
Definition SDL3pp_camera.h:74
constexpr CameraPosition CAMERA_POSITION_BACK_FACING
CAMERA_POSITION_BACK_FACING.
Definition SDL3pp_camera.h:121
CameraPermissionState GetCameraPermissionState(CameraRef camera)
Query if camera access has been approved by the user.
Definition SDL3pp_camera.h:792
Camera OpenCamera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition SDL3pp_camera.h:749
int GetNumCameraDrivers()
Use this function to get the number of built-in camera drivers.
Definition SDL3pp_camera.h:559
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_DENIED
CAMERA_PERMISSION_STATE_DENIED.
Definition SDL3pp_camera.h:135
OwnArray< CameraID > GetCameras()
Get a list of currently connected camera devices.
Definition SDL3pp_camera.h:619
SDL_CameraPosition CameraPosition
The position of camera in relation to system device.
Definition SDL3pp_camera.h:113
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:91
CameraPermissionState GetPermissionState()
Query if camera access has been approved by the user.
Definition SDL3pp_camera.h:797
PropertiesRef GetProperties()
Get the properties associated with an opened camera.
Definition SDL3pp_camera.h:838
OwnArray< CameraSpec * > GetCameraSupportedFormats(CameraID instance_id)
Get the list of native formats/sizes a camera supports.
Definition SDL3pp_camera.h:658
void Close()
Use this function to shut down camera processing and close the camera device.
Definition SDL3pp_camera.h:991
CameraFrame AcquireFrame(Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:921
CameraID GetCameraID(CameraRef camera)
Get the instance ID of an opened camera.
Definition SDL3pp_camera.h:815
CameraFrame(CameraRef resource, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition SDL3pp_camera.h:926
const char * GetCameraDriver(int index)
Use this function to get the name of a built in camera driver.
Definition SDL3pp_camera.h:583
const char * GetCameraName(CameraID instance_id)
Get the human-readable device name for a camera.
Definition SDL3pp_camera.h:678
std::optional< CameraSpec > GetFormat()
Get the spec that a camera is using when generating images.
Definition SDL3pp_camera.h:870
void reset()
Release a frame of video acquired from a camera.
Definition SDL3pp_camera.h:970
SDL_CameraPermissionState CameraPermissionState
The current state of a request for camera access.
Definition SDL3pp_camera.h:133
const char * GetCurrentCameraDriver()
Get the name of the current camera driver.
Definition SDL3pp_camera.h:602
CameraPosition GetCameraPosition(CameraID instance_id)
Get the position of the camera in relation to the system.
Definition SDL3pp_camera.h:700
SDL_CameraSpec CameraSpec
The details of an output format for a camera device.
Definition SDL3pp_camera.h:104
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:53
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
ResourceRefT< SurfaceBase > SurfaceRef
Reference for Surface.
Definition SDL3pp_surface.h:57
Main include header for the SDL3pp library.
Base class to Camera.
Definition SDL3pp_camera.h:162
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
The opaque structure used to identify an opened SDL camera.
Definition SDL3pp_camera.h:338
constexpr Camera(CameraRaw resource) noexcept
Constructs from raw Camera.
Definition SDL3pp_camera.h:348
constexpr Camera(Camera &&other) noexcept
Move constructor.
Definition SDL3pp_camera.h:354
constexpr Camera & operator=(Camera &&other) noexcept
Assignment operator.
Definition SDL3pp_camera.h:409
~Camera()
Destructor.
Definition SDL3pp_camera.h:406
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:1677
static Surface borrow(SurfaceRaw resource)
Safely borrows the from SurfaceRaw.
Definition SDL3pp_surface.h:1844
constexpr Surface(SurfaceRaw resource) noexcept
Constructs from raw Surface.
Definition SDL3pp_surface.h:1687