SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_sensor.h
1#ifndef SDL3PP_SENSOR_H_
2#define SDL3PP_SENSOR_H_
3
4#include <SDL3/SDL_sensor.h>
5#include "SDL3pp_error.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8#include "SDL3pp_version.h"
9
10namespace SDL {
11
26// Forward decl
27struct Sensor;
28
30using SensorRaw = SDL_Sensor*;
31
32// Forward decl
33struct SensorRef;
34
37{
39
42 : value(value)
43 {
44 }
45
47 constexpr SensorParam(std::nullptr_t = nullptr)
48 : value(nullptr)
49 {
50 }
51
53 constexpr explicit operator bool() const { return !!value; }
54
56 constexpr auto operator<=>(const SensorParam& other) const = default;
57
59 constexpr operator SensorRaw() const { return value; }
60};
61
71
125using SensorType = SDL_SensorType;
126
128 SDL_SENSOR_INVALID;
129
131 SDL_SENSOR_UNKNOWN;
132
133constexpr SensorType SENSOR_ACCEL = SDL_SENSOR_ACCEL;
134
135constexpr SensorType SENSOR_GYRO = SDL_SENSOR_GYRO;
136
138constexpr SensorType SENSOR_ACCEL_L = SDL_SENSOR_ACCEL_L;
139
141 SDL_SENSOR_GYRO_L;
142
144 SDL_SENSOR_ACCEL_R;
145
147 SDL_SENSOR_GYRO_R;
148
149#if SDL_VERSION_ATLEAST(3, 2, 22)
150
151constexpr SensorType SENSOR_COUNT = SDL_SENSOR_COUNT;
152
153#endif // SDL_VERSION_ATLEAST(3, 2, 22)
154
163{
164 SensorRaw m_resource = nullptr;
165
166public:
168 constexpr Sensor(std::nullptr_t = nullptr) noexcept
169 : m_resource(0)
170 {
171 }
172
180 constexpr explicit Sensor(const SensorRaw resource) noexcept
181 : m_resource(resource)
182 {
183 }
184
185protected:
187 constexpr Sensor(const Sensor& other) noexcept = default;
188
189public:
191 constexpr Sensor(Sensor&& other) noexcept
192 : Sensor(other.release())
193 {
194 }
195
196 constexpr Sensor(const SensorRef& other) = delete;
197
198 constexpr Sensor(SensorRef&& other) = delete;
199
209 Sensor(SensorID instance_id)
210 : m_resource(SDL_OpenSensor(instance_id))
211 {
212 }
213
215 ~Sensor() { SDL_CloseSensor(m_resource); }
216
218 constexpr Sensor& operator=(Sensor&& other) noexcept
219 {
220 std::swap(m_resource, other.m_resource);
221 return *this;
222 }
223
224protected:
226 constexpr Sensor& operator=(const Sensor& other) noexcept = default;
227
228public:
230 constexpr SensorRaw get() const noexcept { return m_resource; }
231
233 constexpr SensorRaw release() noexcept
234 {
235 auto r = m_resource;
236 m_resource = nullptr;
237 return r;
238 }
239
241 constexpr auto operator<=>(const Sensor& other) const noexcept = default;
242
244 constexpr explicit operator bool() const noexcept { return !!m_resource; }
245
247 constexpr operator SensorParam() const noexcept { return {m_resource}; }
248
254 void Close();
255
265
274 const char* GetName();
275
284
292 int GetNonPortableType();
293
302 SensorID GetID();
303
315 void GetData(float* data, int num_values);
316};
317
320{
321 using Sensor::Sensor;
322
330 SensorRef(SensorParam resource) noexcept
331 : Sensor(resource.value)
332 {
333 }
334
342 SensorRef(SensorRaw resource) noexcept
343 : Sensor(resource)
344 {
345 }
346
348 constexpr SensorRef(const SensorRef& other) noexcept = default;
349
352};
353
364constexpr float STANDARD_GRAVITY = SDL_STANDARD_GRAVITY;
365
375{
376 int count = 0;
377 auto data = SDL_GetSensors(&count);
378 return OwnArray<SensorID>(data, size_t(count));
379}
380
391inline const char* GetSensorNameForID(SensorID instance_id)
392{
393 return SDL_GetSensorNameForID(instance_id);
394}
395
407{
408 return SDL_GetSensorTypeForID(instance_id);
409}
410
423{
424 return SDL_GetSensorNonPortableTypeForID(instance_id);
425}
426
436inline Sensor OpenSensor(SensorID instance_id) { return Sensor(instance_id); }
437
448{
449 return {SDL_GetSensorFromID(instance_id)};
450}
451
462{
463 return {CheckError(SDL_GetSensorProperties(sensor))};
464}
465
467{
468 return SDL::GetSensorProperties(m_resource);
469}
470
480inline const char* GetSensorName(SensorParam sensor)
481{
482 return SDL_GetSensorName(sensor);
483}
484
485inline const char* Sensor::GetName() { return SDL::GetSensorName(m_resource); }
486
496{
497 return SDL_GetSensorType(sensor);
498}
499
500inline SensorType Sensor::GetType() { return SDL::GetSensorType(m_resource); }
501
511{
512 return SDL_GetSensorNonPortableType(sensor);
513}
514
516{
517 return SDL::GetSensorNonPortableType(m_resource);
518}
519
530{
531 return SDL_GetSensorID(sensor);
532}
533
534inline SensorID Sensor::GetID() { return SDL::GetSensorID(m_resource); }
535
548inline void GetSensorData(SensorParam sensor, float* data, int num_values)
549{
550 CheckError(SDL_GetSensorData(sensor, data, num_values));
551}
552
553inline void Sensor::GetData(float* data, int num_values)
554{
555 SDL::GetSensorData(m_resource, data, num_values);
556}
557
565inline void CloseSensor(SensorRaw sensor) { SDL_CloseSensor(sensor); }
566
567inline void Sensor::Close() { CloseSensor(release()); }
568
579inline void UpdateSensors() { SDL_UpdateSensors(); }
580
582
583} // namespace SDL
584
585#endif /* SDL3PP_SENSOR_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
The opaque structure used to identify an opened SDL sensor.
Definition: SDL3pp_sensor.h:163
constexpr SensorRaw get() const noexcept
Retrieves underlying SensorRaw.
Definition: SDL3pp_sensor.h:230
constexpr SensorRaw release() noexcept
Retrieves underlying SensorRaw and clear this.
Definition: SDL3pp_sensor.h:233
constexpr Sensor & operator=(Sensor &&other) noexcept
Assignment operator.
Definition: SDL3pp_sensor.h:218
constexpr Sensor & operator=(const Sensor &other) noexcept=default
Assignment operator.
constexpr Sensor(const Sensor &other) noexcept=default
Copy constructor.
constexpr auto operator<=>(const Sensor &other) const noexcept=default
Comparison.
constexpr Sensor(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_sensor.h:168
~Sensor()
Destructor.
Definition: SDL3pp_sensor.h:215
constexpr Sensor(const SensorRaw resource) noexcept
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:180
Sensor(SensorID instance_id)
Open a sensor for use.
Definition: SDL3pp_sensor.h:209
constexpr Sensor(Sensor &&other) noexcept
Move constructor.
Definition: SDL3pp_sensor.h:191
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
PropertiesRef GetSensorProperties(SensorParam sensor)
Get the properties associated with a sensor.
Definition: SDL3pp_sensor.h:461
SensorRef GetSensorFromID(SensorID instance_id)
Return the Sensor associated with an instance ID.
Definition: SDL3pp_sensor.h:447
constexpr SensorType SENSOR_GYRO
Gyroscope.
Definition: SDL3pp_sensor.h:135
OwnArray< SensorID > GetSensors()
Get a list of currently connected sensors.
Definition: SDL3pp_sensor.h:374
SensorID GetSensorID(SensorParam sensor)
Get the instance ID of a sensor.
Definition: SDL3pp_sensor.h:529
int GetNonPortableType()
Get the platform dependent type of a sensor.
Definition: SDL3pp_sensor.h:515
void GetData(float *data, int num_values)
Get the current state of an opened sensor.
Definition: SDL3pp_sensor.h:553
const char * GetSensorName(SensorParam sensor)
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:480
void UpdateSensors()
Update the current state of the open sensors.
Definition: SDL3pp_sensor.h:579
constexpr SensorType SENSOR_ACCEL
Accelerometer.
Definition: SDL3pp_sensor.h:133
constexpr SensorType SENSOR_INVALID
Returned for an invalid sensor.
Definition: SDL3pp_sensor.h:127
SDL_SensorType SensorType
The different sensors defined by SDL.
Definition: SDL3pp_sensor.h:125
constexpr SensorType SENSOR_ACCEL_L
Accelerometer for left Joy-Con controller and Wii nunchuk.
Definition: SDL3pp_sensor.h:138
Sensor OpenSensor(SensorID instance_id)
Open a sensor for use.
Definition: SDL3pp_sensor.h:436
SensorType GetSensorType(SensorParam sensor)
Get the type of a sensor.
Definition: SDL3pp_sensor.h:495
SensorType GetType()
Get the type of a sensor.
Definition: SDL3pp_sensor.h:500
constexpr SensorType SENSOR_UNKNOWN
Unknown sensor type.
Definition: SDL3pp_sensor.h:130
SensorType GetSensorTypeForID(SensorID instance_id)
Get the type of a sensor.
Definition: SDL3pp_sensor.h:406
SDL_Sensor * SensorRaw
Alias to raw representation for Sensor.
Definition: SDL3pp_sensor.h:30
void CloseSensor(SensorRaw sensor)
Close a sensor previously opened with Sensor.Sensor().
Definition: SDL3pp_sensor.h:565
constexpr float STANDARD_GRAVITY
A constant to represent standard gravity for accelerometer sensors.
Definition: SDL3pp_sensor.h:364
constexpr SensorType SENSOR_ACCEL_R
Accelerometer for right Joy-Con controller.
Definition: SDL3pp_sensor.h:143
int GetSensorNonPortableType(SensorParam sensor)
Get the platform dependent type of a sensor.
Definition: SDL3pp_sensor.h:510
constexpr SensorType SENSOR_COUNT
SENSOR_COUNT.
Definition: SDL3pp_sensor.h:151
void GetSensorData(SensorParam sensor, float *data, int num_values)
Get the current state of an opened sensor.
Definition: SDL3pp_sensor.h:548
SensorID GetID()
Get the instance ID of a sensor.
Definition: SDL3pp_sensor.h:534
const char * GetSensorNameForID(SensorID instance_id)
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:391
void Close()
Close a sensor previously opened with Sensor.Sensor().
Definition: SDL3pp_sensor.h:567
constexpr SensorType SENSOR_GYRO_R
Gyroscope for right Joy-Con controller.
Definition: SDL3pp_sensor.h:146
int GetSensorNonPortableTypeForID(SensorID instance_id)
Get the platform dependent type of a sensor.
Definition: SDL3pp_sensor.h:422
const char * GetName()
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:485
PropertiesRef GetProperties()
Get the properties associated with a sensor.
Definition: SDL3pp_sensor.h:466
constexpr SensorType SENSOR_GYRO_L
Gyroscope for left Joy-Con controller.
Definition: SDL3pp_sensor.h:140
Uint32 SensorID
This is a unique ID for a sensor for the time it is connected to the system, and is never reused for ...
Definition: SDL3pp_sensor.h:70
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
Main include header for the SDL3pp library.
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716
Safely wrap Sensor for non owning parameters.
Definition: SDL3pp_sensor.h:37
constexpr auto operator<=>(const SensorParam &other) const =default
Comparison.
constexpr SensorParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_sensor.h:47
SensorRaw value
parameter's SensorRaw
Definition: SDL3pp_sensor.h:38
constexpr SensorParam(SensorRaw value)
Constructs from SensorRaw.
Definition: SDL3pp_sensor.h:41
Semi-safe reference for Sensor.
Definition: SDL3pp_sensor.h:320
SensorRef(SensorParam resource) noexcept
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:330
constexpr SensorRef(const SensorRef &other) noexcept=default
Copy constructor.
SensorRef(SensorRaw resource) noexcept
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:342
~SensorRef()
Destructor.
Definition: SDL3pp_sensor.h:351