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() = default;
169
177 constexpr explicit Sensor(const SensorRaw resource)
178 : m_resource(resource)
179 {
180 }
181
183 constexpr Sensor(const Sensor& other) = delete;
184
186 constexpr Sensor(Sensor&& other)
187 : Sensor(other.release())
188 {
189 }
190
191 constexpr Sensor(const SensorRef& other) = delete;
192
193 constexpr Sensor(SensorRef&& other) = delete;
194
204 Sensor(SensorID instance_id)
205 : m_resource(SDL_OpenSensor(instance_id))
206 {
207 }
208
210 ~Sensor() { SDL_CloseSensor(m_resource); }
211
214 {
215 std::swap(m_resource, other.m_resource);
216 return *this;
217 }
218
220 constexpr SensorRaw get() const { return m_resource; }
221
223 constexpr SensorRaw release()
224 {
225 auto r = m_resource;
226 m_resource = nullptr;
227 return r;
228 }
229
231 constexpr auto operator<=>(const Sensor& other) const = default;
232
234 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
235
237 constexpr explicit operator bool() const { return !!m_resource; }
238
240 constexpr operator SensorParam() const { return {m_resource}; }
241
247 void Close();
248
258
267 const char* GetName();
268
277
285 int GetNonPortableType();
286
295 SensorID GetID();
296
308 void GetData(float* data, int num_values);
309};
310
313{
322 : Sensor(resource.value)
323 {
324 }
325
327 SensorRef(const SensorRef& other)
328 : Sensor(other.get())
329 {
330 }
331
334};
335
346constexpr float STANDARD_GRAVITY = SDL_STANDARD_GRAVITY;
347
357{
358 int count = 0;
359 auto data = SDL_GetSensors(&count);
360 return OwnArray<SensorID>(data, size_t(count));
361}
362
373inline const char* GetSensorNameForID(SensorID instance_id)
374{
375 return SDL_GetSensorNameForID(instance_id);
376}
377
389{
390 return SDL_GetSensorTypeForID(instance_id);
391}
392
405{
406 return SDL_GetSensorNonPortableTypeForID(instance_id);
407}
408
418inline Sensor OpenSensor(SensorID instance_id) { return Sensor(instance_id); }
419
430{
431 return {SDL_GetSensorFromID(instance_id)};
432}
433
444{
445 return {CheckError(SDL_GetSensorProperties(sensor))};
446}
447
449{
450 return SDL::GetSensorProperties(m_resource);
451}
452
462inline const char* GetSensorName(SensorParam sensor)
463{
464 return SDL_GetSensorName(sensor);
465}
466
467inline const char* Sensor::GetName() { return SDL::GetSensorName(m_resource); }
468
478{
479 return SDL_GetSensorType(sensor);
480}
481
482inline SensorType Sensor::GetType() { return SDL::GetSensorType(m_resource); }
483
493{
494 return SDL_GetSensorNonPortableType(sensor);
495}
496
498{
499 return SDL::GetSensorNonPortableType(m_resource);
500}
501
512{
513 return SDL_GetSensorID(sensor);
514}
515
516inline SensorID Sensor::GetID() { return SDL::GetSensorID(m_resource); }
517
530inline void GetSensorData(SensorParam sensor, float* data, int num_values)
531{
532 CheckError(SDL_GetSensorData(sensor, data, num_values));
533}
534
535inline void Sensor::GetData(float* data, int num_values)
536{
537 SDL::GetSensorData(m_resource, data, num_values);
538}
539
547inline void CloseSensor(SensorRaw sensor) { SDL_CloseSensor(sensor); }
548
549inline void Sensor::Close() { CloseSensor(release()); }
550
561inline void UpdateSensors() { SDL_UpdateSensors(); }
562
564
565} // namespace SDL
566
567#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 Sensor(const Sensor &other)=delete
Copy constructor.
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_sensor.h:234
constexpr Sensor(Sensor &&other)
Move constructor.
Definition: SDL3pp_sensor.h:186
constexpr Sensor(const SensorRaw resource)
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:177
Sensor & operator=(Sensor other)
Assignment operator.
Definition: SDL3pp_sensor.h:213
constexpr auto operator<=>(const Sensor &other) const =default
Comparison.
constexpr Sensor()=default
Default ctor.
constexpr SensorRaw get() const
Retrieves underlying SensorRaw.
Definition: SDL3pp_sensor.h:220
~Sensor()
Destructor.
Definition: SDL3pp_sensor.h:210
Sensor(SensorID instance_id)
Open a sensor for use.
Definition: SDL3pp_sensor.h:204
constexpr SensorRaw release()
Retrieves underlying SensorRaw and clear this.
Definition: SDL3pp_sensor.h:223
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:443
SensorRef GetSensorFromID(SensorID instance_id)
Return the Sensor associated with an instance ID.
Definition: SDL3pp_sensor.h:429
constexpr SensorType SENSOR_GYRO
Gyroscope.
Definition: SDL3pp_sensor.h:135
OwnArray< SensorID > GetSensors()
Get a list of currently connected sensors.
Definition: SDL3pp_sensor.h:356
SensorID GetSensorID(SensorParam sensor)
Get the instance ID of a sensor.
Definition: SDL3pp_sensor.h:511
int GetNonPortableType()
Get the platform dependent type of a sensor.
Definition: SDL3pp_sensor.h:497
void GetData(float *data, int num_values)
Get the current state of an opened sensor.
Definition: SDL3pp_sensor.h:535
const char * GetSensorName(SensorParam sensor)
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:462
void UpdateSensors()
Update the current state of the open sensors.
Definition: SDL3pp_sensor.h:561
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:418
SensorType GetSensorType(SensorParam sensor)
Get the type of a sensor.
Definition: SDL3pp_sensor.h:477
SensorType GetType()
Get the type of a sensor.
Definition: SDL3pp_sensor.h:482
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:388
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:547
constexpr float STANDARD_GRAVITY
A constant to represent standard gravity for accelerometer sensors.
Definition: SDL3pp_sensor.h:346
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:492
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:530
SensorID GetID()
Get the instance ID of a sensor.
Definition: SDL3pp_sensor.h:516
const char * GetSensorNameForID(SensorID instance_id)
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:373
void Close()
Close a sensor previously opened with Sensor.Sensor().
Definition: SDL3pp_sensor.h:549
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:404
const char * GetName()
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:467
PropertiesRef GetProperties()
Get the properties associated with a sensor.
Definition: SDL3pp_sensor.h:448
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:701
Safely wrap Sensor for non owning parameters.
Definition: SDL3pp_sensor.h:37
constexpr SensorParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_sensor.h:47
constexpr auto operator<=>(const SensorParam &other) const =default
Comparison.
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:313
SensorRef(SensorParam resource)
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:321
SensorRef(const SensorRef &other)
Copy constructor.
Definition: SDL3pp_sensor.h:327
~SensorRef()
Destructor.
Definition: SDL3pp_sensor.h:333