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
9namespace SDL {
10
25// Forward decl
26struct Sensor;
27
29using SensorRaw = SDL_Sensor*;
30
31// Forward decl
32struct SensorRef;
33
36{
38
41 : value(value)
42 {
43 }
44
46 constexpr SensorParam(std::nullptr_t _ = nullptr)
47 : value(nullptr)
48 {
49 }
50
52 constexpr explicit operator bool() const { return !!value; }
53
55 constexpr auto operator<=>(const SensorParam& other) const = default;
56
58 constexpr operator SensorRaw() const { return value; }
59};
60
70
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
149constexpr SensorType SENSOR_COUNT = SDL_SENSOR_COUNT;
150
159{
160 SensorRaw m_resource = nullptr;
161
162public:
164 constexpr Sensor() = default;
165
173 constexpr explicit Sensor(const SensorRaw resource)
174 : m_resource(resource)
175 {
176 }
177
179 constexpr Sensor(const Sensor& other) = delete;
180
182 constexpr Sensor(Sensor&& other)
183 : Sensor(other.release())
184 {
185 }
186
187 constexpr Sensor(const SensorRef& other) = delete;
188
189 constexpr Sensor(SensorRef&& other) = delete;
190
200 Sensor(SensorID instance_id)
201 : m_resource(SDL_OpenSensor(instance_id))
202 {
203 }
204
206 ~Sensor() { SDL_CloseSensor(m_resource); }
207
210 {
211 std::swap(m_resource, other.m_resource);
212 return *this;
213 }
214
216 constexpr SensorRaw get() const { return m_resource; }
217
219 constexpr SensorRaw release()
220 {
221 auto r = m_resource;
222 m_resource = nullptr;
223 return r;
224 }
225
227 constexpr auto operator<=>(const Sensor& other) const = default;
228
230 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
231
233 constexpr explicit operator bool() const { return !!m_resource; }
234
236 constexpr operator SensorParam() const { return {m_resource}; }
237
244 void Close();
245
255
264 const char* GetName();
265
275
283 int GetNonPortableType();
284
293 SensorID GetID();
294
306 void GetData(float* data, int num_values);
307};
308
311{
320 : Sensor(resource.value)
321 {
322 }
323
325 SensorRef(const SensorRef& other)
326 : Sensor(other.get())
327 {
328 }
329
332};
333
344constexpr float STANDARD_GRAVITY = SDL_STANDARD_GRAVITY;
345
355{
356 int count = 0;
357 auto data = SDL_GetSensors(&count);
358 return OwnArray<SensorID>(data, size_t(count));
359}
360
371inline const char* GetSensorNameForID(SensorID instance_id)
372{
373 return SDL_GetSensorNameForID(instance_id);
374}
375
388{
389 return SDL_GetSensorTypeForID(instance_id);
390}
391
404{
405 return SDL_GetSensorNonPortableTypeForID(instance_id);
406}
407
417inline Sensor OpenSensor(SensorID instance_id) { return Sensor(instance_id); }
418
429{
430 return {SDL_GetSensorFromID(instance_id)};
431}
432
443{
444 return {CheckError(SDL_GetSensorProperties(sensor))};
445}
446
448{
449 return SDL::GetSensorProperties(m_resource);
450}
451
461inline const char* GetSensorName(SensorParam sensor)
462{
463 return SDL_GetSensorName(sensor);
464}
465
466inline const char* Sensor::GetName() { return SDL::GetSensorName(m_resource); }
467
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
562inline void UpdateSensors() { SDL_UpdateSensors(); }
563
565
566} // namespace SDL
567
568#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:159
constexpr Sensor(const Sensor &other)=delete
Copy constructor.
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_sensor.h:230
constexpr Sensor(Sensor &&other)
Move constructor.
Definition: SDL3pp_sensor.h:182
constexpr Sensor(const SensorRaw resource)
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:173
Sensor & operator=(Sensor other)
Assignment operator.
Definition: SDL3pp_sensor.h:209
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:216
~Sensor()
Destructor.
Definition: SDL3pp_sensor.h:206
Sensor(SensorID instance_id)
Open a sensor for use.
Definition: SDL3pp_sensor.h:200
constexpr SensorRaw release()
Retrieves underlying SensorRaw and clear this.
Definition: SDL3pp_sensor.h:219
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
PropertiesRef GetSensorProperties(SensorParam sensor)
Get the properties associated with a sensor.
Definition: SDL3pp_sensor.h:442
SensorRef GetSensorFromID(SensorID instance_id)
Return the Sensor associated with an instance ID.
Definition: SDL3pp_sensor.h:428
constexpr SensorType SENSOR_GYRO
Gyroscope.
Definition: SDL3pp_sensor.h:135
OwnArray< SensorID > GetSensors()
Get a list of currently connected sensors.
Definition: SDL3pp_sensor.h:354
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:461
void UpdateSensors()
Update the current state of the open sensors.
Definition: SDL3pp_sensor.h:562
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:417
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:387
SDL_Sensor * SensorRaw
Alias to raw representation for Sensor.
Definition: SDL3pp_sensor.h:29
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:344
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:149
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:371
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:403
const char * GetName()
Get the implementation dependent name of a sensor.
Definition: SDL3pp_sensor.h:466
PropertiesRef GetProperties()
Get the properties associated with a sensor.
Definition: SDL3pp_sensor.h:447
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:69
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:325
Main include header for the SDL3pp library.
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:569
Safely wrap Sensor for non owning parameters.
Definition: SDL3pp_sensor.h:36
constexpr SensorParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_sensor.h:46
constexpr auto operator<=>(const SensorParam &other) const =default
Comparison.
SensorRaw value
parameter's SensorRaw
Definition: SDL3pp_sensor.h:37
constexpr SensorParam(SensorRaw value)
Constructs from SensorRaw.
Definition: SDL3pp_sensor.h:40
Semi-safe reference for Sensor.
Definition: SDL3pp_sensor.h:311
SensorRef(SensorParam resource)
Constructs from SensorParam.
Definition: SDL3pp_sensor.h:319
SensorRef(const SensorRef &other)
Copy constructor.
Definition: SDL3pp_sensor.h:325
~SensorRef()
Destructor.
Definition: SDL3pp_sensor.h:331