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
25
26// Forward decl
27struct Sensor;
28
30using SensorRaw = SDL_Sensor*;
31
38
48
102using SensorType = SDL_SensorType;
103
105 SDL_SENSOR_INVALID;
106
108 SDL_SENSOR_UNKNOWN;
109
110constexpr SensorType SENSOR_ACCEL = SDL_SENSOR_ACCEL;
111
112constexpr SensorType SENSOR_GYRO = SDL_SENSOR_GYRO;
113
115constexpr SensorType SENSOR_ACCEL_L = SDL_SENSOR_ACCEL_L;
116
118 SDL_SENSOR_GYRO_L;
119
121 SDL_SENSOR_ACCEL_R;
122
124 SDL_SENSOR_GYRO_R;
125
126#if SDL_VERSION_ATLEAST(3, 2, 22)
127
128constexpr SensorType SENSOR_COUNT = SDL_SENSOR_COUNT;
129
130#endif // SDL_VERSION_ATLEAST(3, 2, 22)
131
139struct Sensor : ResourceBase<SensorRaw>
140{
142
150 constexpr explicit Sensor(SensorRaw resource) noexcept
151 : ResourceBase(resource)
152 {
153 }
154
156 constexpr Sensor(const Sensor& other) = delete;
157
159 constexpr Sensor(Sensor&& other) noexcept
160 : Sensor(other.release())
161 {
162 }
163
164 constexpr Sensor(const SensorRef& other) = delete;
165
166 constexpr Sensor(SensorRef&& other) = delete;
167
177 Sensor(SensorID instance_id);
178
180 ~Sensor() { SDL_CloseSensor(get()); }
181
183 constexpr Sensor& operator=(Sensor&& other) noexcept
184 {
185 swap(*this, other);
186 return *this;
187 }
188
190 Sensor& operator=(const Sensor& other) = delete;
191
197 void Close();
198
208
217 const char* GetName();
218
227
235 int GetNonPortableType();
236
245 SensorID GetID();
246
258 void GetData(float* data, int num_values);
259};
260
271constexpr float STANDARD_GRAVITY = SDL_STANDARD_GRAVITY;
272
282{
283 int count = 0;
284 auto data = SDL_GetSensors(&count);
285 return OwnArray<SensorID>(data, size_t(count));
286}
287
298inline const char* GetSensorNameForID(SensorID instance_id)
299{
300 return SDL_GetSensorNameForID(instance_id);
301}
302
314{
315 return SDL_GetSensorTypeForID(instance_id);
316}
317
330{
331 return SDL_GetSensorNonPortableTypeForID(instance_id);
332}
333
343inline Sensor OpenSensor(SensorID instance_id) { return Sensor(instance_id); }
344
345inline Sensor::Sensor(SensorID instance_id)
346 : Sensor(SDL_OpenSensor(instance_id))
347{
348}
349
360{
361 return {SDL_GetSensorFromID(instance_id)};
362}
363
374{
375 return {CheckError(SDL_GetSensorProperties(sensor))};
376}
377
382
392inline const char* GetSensorName(SensorRef sensor)
393{
394 return SDL_GetSensorName(sensor);
395}
396
397inline const char* Sensor::GetName() { return SDL::GetSensorName(get()); }
398
408{
409 return SDL_GetSensorType(sensor);
410}
411
413
423{
424 return SDL_GetSensorNonPortableType(sensor);
425}
426
431
442{
443 return CheckError(SDL_GetSensorID(sensor));
444}
445
447
460inline void GetSensorData(SensorRef sensor, float* data, int num_values)
461{
462 CheckError(SDL_GetSensorData(sensor, data, num_values));
463}
464
465inline void Sensor::GetData(float* data, int num_values)
466{
467 SDL::GetSensorData(get(), data, num_values);
468}
469
477inline void CloseSensor(SensorRaw sensor) { SDL_CloseSensor(sensor); }
478
479inline void Sensor::Close() { CloseSensor(release()); }
480
491inline void UpdateSensors() { SDL_UpdateSensors(); }
492
494
495} // namespace SDL
496
497#endif /* SDL3PP_SENSOR_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
ResourceRef< Sensor > SensorRef
Reference for Sensor.
Definition SDL3pp_sensor.h:37
void GetSensorData(SensorRef sensor, float *data, int num_values)
Get the current state of an opened sensor.
Definition SDL3pp_sensor.h:460
SensorRef GetSensorFromID(SensorID instance_id)
Return the Sensor associated with an instance ID.
Definition SDL3pp_sensor.h:359
SensorID GetSensorID(SensorRef sensor)
Get the instance ID of a sensor.
Definition SDL3pp_sensor.h:441
constexpr SensorType SENSOR_GYRO
Gyroscope.
Definition SDL3pp_sensor.h:112
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:47
OwnArray< SensorID > GetSensors()
Get a list of currently connected sensors.
Definition SDL3pp_sensor.h:281
const char * GetSensorName(SensorRef sensor)
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:392
int GetNonPortableType()
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:427
void GetData(float *data, int num_values)
Get the current state of an opened sensor.
Definition SDL3pp_sensor.h:465
void UpdateSensors()
Update the current state of the open sensors.
Definition SDL3pp_sensor.h:491
constexpr SensorType SENSOR_ACCEL
Accelerometer.
Definition SDL3pp_sensor.h:110
constexpr SensorType SENSOR_INVALID
Returned for an invalid sensor.
Definition SDL3pp_sensor.h:104
constexpr SensorType SENSOR_ACCEL_L
Accelerometer for left Joy-Con controller and Wii nunchuk.
Definition SDL3pp_sensor.h:115
SensorType GetSensorType(SensorRef sensor)
Get the type of a sensor.
Definition SDL3pp_sensor.h:407
Sensor OpenSensor(SensorID instance_id)
Open a sensor for use.
Definition SDL3pp_sensor.h:343
SensorType GetType()
Get the type of a sensor.
Definition SDL3pp_sensor.h:412
constexpr SensorType SENSOR_UNKNOWN
Unknown sensor type.
Definition SDL3pp_sensor.h:107
SDL_Sensor * SensorRaw
Alias to raw representation for Sensor.
Definition SDL3pp_sensor.h:30
int GetSensorNonPortableType(SensorRef sensor)
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:422
SensorType GetSensorTypeForID(SensorID instance_id)
Get the type of a sensor.
Definition SDL3pp_sensor.h:313
PropertiesRef GetSensorProperties(SensorRef sensor)
Get the properties associated with a sensor.
Definition SDL3pp_sensor.h:373
SDL_SensorType SensorType
The different sensors defined by SDL.
Definition SDL3pp_sensor.h:102
void CloseSensor(SensorRaw sensor)
Close a sensor previously opened with OpenSensor().
Definition SDL3pp_sensor.h:477
constexpr float STANDARD_GRAVITY
A constant to represent standard gravity for accelerometer sensors.
Definition SDL3pp_sensor.h:271
constexpr SensorType SENSOR_ACCEL_R
Accelerometer for right Joy-Con controller.
Definition SDL3pp_sensor.h:120
constexpr SensorType SENSOR_COUNT
SENSOR_COUNT.
Definition SDL3pp_sensor.h:128
SensorID GetID()
Get the instance ID of a sensor.
Definition SDL3pp_sensor.h:446
const char * GetSensorNameForID(SensorID instance_id)
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:298
void Close()
Close a sensor previously opened with OpenSensor().
Definition SDL3pp_sensor.h:479
constexpr SensorType SENSOR_GYRO_R
Gyroscope for right Joy-Con controller.
Definition SDL3pp_sensor.h:123
int GetSensorNonPortableTypeForID(SensorID instance_id)
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:329
const char * GetName()
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:397
PropertiesRef GetProperties()
Get the properties associated with a sensor.
Definition SDL3pp_sensor.h:378
constexpr SensorType SENSOR_GYRO_L
Gyroscope for left Joy-Con controller.
Definition SDL3pp_sensor.h:117
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
Main include header for the SDL3pp library.
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156
The opaque structure used to identify an opened SDL sensor.
Definition SDL3pp_sensor.h:140
constexpr Sensor(const Sensor &other)=delete
Copy constructor.
constexpr Sensor & operator=(Sensor &&other) noexcept
Assignment operator.
Definition SDL3pp_sensor.h:183
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
Sensor & operator=(const Sensor &other)=delete
Assignment operator.
~Sensor()
Destructor.
Definition SDL3pp_sensor.h:180
constexpr Sensor(SensorRaw resource) noexcept
Constructs from raw Sensor.
Definition SDL3pp_sensor.h:150
constexpr Sensor(Sensor &&other) noexcept
Move constructor.
Definition SDL3pp_sensor.h:159