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 SensorBase;
28
29// Forward decl
30struct Sensor;
31
33using SensorRaw = SDL_Sensor*;
34
41
51
105using SensorType = SDL_SensorType;
106
108 SDL_SENSOR_INVALID;
109
111 SDL_SENSOR_UNKNOWN;
112
113constexpr SensorType SENSOR_ACCEL = SDL_SENSOR_ACCEL;
114
115constexpr SensorType SENSOR_GYRO = SDL_SENSOR_GYRO;
116
118constexpr SensorType SENSOR_ACCEL_L = SDL_SENSOR_ACCEL_L;
119
121 SDL_SENSOR_GYRO_L;
122
124 SDL_SENSOR_ACCEL_R;
125
127 SDL_SENSOR_GYRO_R;
128
129#if SDL_VERSION_ATLEAST(3, 2, 22)
130
131constexpr SensorType SENSOR_COUNT = SDL_SENSOR_COUNT;
132
133#endif // SDL_VERSION_ATLEAST(3, 2, 22)
134
140struct SensorBase : ResourceBaseT<SensorRaw>
141{
143
149 void Close();
150
160
169 const char* GetName();
170
179
187 int GetNonPortableType();
188
197 SensorID GetID();
198
210 void GetData(float* data, int num_values);
211};
212
221{
222 using SensorBase::SensorBase;
223
231 constexpr explicit Sensor(SensorRaw resource) noexcept
232 : SensorBase(resource)
233 {
234 }
235
237 constexpr Sensor(Sensor&& other) noexcept
238 : Sensor(other.release())
239 {
240 }
241
251 Sensor(SensorID instance_id);
252
254 ~Sensor() { SDL_CloseSensor(get()); }
255
257 constexpr Sensor& operator=(Sensor&& other) noexcept
258 {
259 swap(*this, other);
260 return *this;
261 }
262};
263
274constexpr float STANDARD_GRAVITY = SDL_STANDARD_GRAVITY;
275
285{
286 int count = 0;
287 auto data = SDL_GetSensors(&count);
288 return OwnArray<SensorID>(data, size_t(count));
289}
290
301inline const char* GetSensorNameForID(SensorID instance_id)
302{
303 return SDL_GetSensorNameForID(instance_id);
304}
305
317{
318 return SDL_GetSensorTypeForID(instance_id);
319}
320
333{
334 return SDL_GetSensorNonPortableTypeForID(instance_id);
335}
336
346inline Sensor OpenSensor(SensorID instance_id) { return Sensor(instance_id); }
347
348inline Sensor::Sensor(SensorID instance_id)
349 : Sensor(SDL_OpenSensor(instance_id))
350{
351}
352
363{
364 return {SDL_GetSensorFromID(instance_id)};
365}
366
377{
378 return {CheckError(SDL_GetSensorProperties(sensor))};
379}
380
385
395inline const char* GetSensorName(SensorRef sensor)
396{
397 return SDL_GetSensorName(sensor);
398}
399
400inline const char* SensorBase::GetName() { return SDL::GetSensorName(get()); }
401
411{
412 return SDL_GetSensorType(sensor);
413}
414
416
426{
427 return SDL_GetSensorNonPortableType(sensor);
428}
429
434
445{
446 return CheckError(SDL_GetSensorID(sensor));
447}
448
450
463inline void GetSensorData(SensorRef sensor, float* data, int num_values)
464{
465 CheckError(SDL_GetSensorData(sensor, data, num_values));
466}
467
468inline void SensorBase::GetData(float* data, int num_values)
469{
470 SDL::GetSensorData(get(), data, num_values);
471}
472
480inline void CloseSensor(SensorRaw sensor) { SDL_CloseSensor(sensor); }
481
483
494inline void UpdateSensors() { SDL_UpdateSensors(); }
495
497
498} // namespace SDL
499
500#endif /* SDL3PP_SENSOR_H_ */
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.
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
void GetSensorData(SensorRef sensor, float *data, int num_values)
Get the current state of an opened sensor.
Definition SDL3pp_sensor.h:463
SensorRef GetSensorFromID(SensorID instance_id)
Return the Sensor associated with an instance ID.
Definition SDL3pp_sensor.h:362
SensorID GetSensorID(SensorRef sensor)
Get the instance ID of a sensor.
Definition SDL3pp_sensor.h:444
constexpr SensorType SENSOR_GYRO
Gyroscope.
Definition SDL3pp_sensor.h:115
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:50
OwnArray< SensorID > GetSensors()
Get a list of currently connected sensors.
Definition SDL3pp_sensor.h:284
const char * GetSensorName(SensorRef sensor)
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:395
SensorID GetID()
Get the instance ID of a sensor.
Definition SDL3pp_sensor.h:449
void UpdateSensors()
Update the current state of the open sensors.
Definition SDL3pp_sensor.h:494
constexpr SensorType SENSOR_ACCEL
Accelerometer.
Definition SDL3pp_sensor.h:113
constexpr SensorType SENSOR_INVALID
Returned for an invalid sensor.
Definition SDL3pp_sensor.h:107
const char * GetName()
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:400
constexpr SensorType SENSOR_ACCEL_L
Accelerometer for left Joy-Con controller and Wii nunchuk.
Definition SDL3pp_sensor.h:118
SensorType GetSensorType(SensorRef sensor)
Get the type of a sensor.
Definition SDL3pp_sensor.h:410
Sensor OpenSensor(SensorID instance_id)
Open a sensor for use.
Definition SDL3pp_sensor.h:346
SensorType GetType()
Get the type of a sensor.
Definition SDL3pp_sensor.h:415
void Close()
Close a sensor previously opened with OpenSensor().
Definition SDL3pp_sensor.h:482
int GetNonPortableType()
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:430
constexpr SensorType SENSOR_UNKNOWN
Unknown sensor type.
Definition SDL3pp_sensor.h:110
SDL_Sensor * SensorRaw
Alias to raw representation for Sensor.
Definition SDL3pp_sensor.h:33
int GetSensorNonPortableType(SensorRef sensor)
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:425
SensorType GetSensorTypeForID(SensorID instance_id)
Get the type of a sensor.
Definition SDL3pp_sensor.h:316
PropertiesRef GetSensorProperties(SensorRef sensor)
Get the properties associated with a sensor.
Definition SDL3pp_sensor.h:376
SDL_SensorType SensorType
The different sensors defined by SDL.
Definition SDL3pp_sensor.h:105
void CloseSensor(SensorRaw sensor)
Close a sensor previously opened with OpenSensor().
Definition SDL3pp_sensor.h:480
constexpr float STANDARD_GRAVITY
A constant to represent standard gravity for accelerometer sensors.
Definition SDL3pp_sensor.h:274
constexpr SensorType SENSOR_ACCEL_R
Accelerometer for right Joy-Con controller.
Definition SDL3pp_sensor.h:123
void GetData(float *data, int num_values)
Get the current state of an opened sensor.
Definition SDL3pp_sensor.h:468
constexpr SensorType SENSOR_COUNT
SENSOR_COUNT.
Definition SDL3pp_sensor.h:131
PropertiesRef GetProperties()
Get the properties associated with a sensor.
Definition SDL3pp_sensor.h:381
const char * GetSensorNameForID(SensorID instance_id)
Get the implementation dependent name of a sensor.
Definition SDL3pp_sensor.h:301
constexpr SensorType SENSOR_GYRO_R
Gyroscope for right Joy-Con controller.
Definition SDL3pp_sensor.h:126
int GetSensorNonPortableTypeForID(SensorID instance_id)
Get the platform dependent type of a sensor.
Definition SDL3pp_sensor.h:332
constexpr SensorType SENSOR_GYRO_L
Gyroscope for left Joy-Con controller.
Definition SDL3pp_sensor.h:120
ResourceRefT< SensorBase > SensorRef
Reference for Sensor.
Definition SDL3pp_sensor.h:40
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
Main include header for the SDL3pp library.
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
Base class to Sensor.
Definition SDL3pp_sensor.h:141
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
The opaque structure used to identify an opened SDL sensor.
Definition SDL3pp_sensor.h:221
constexpr Sensor & operator=(Sensor &&other) noexcept
Assignment operator.
Definition SDL3pp_sensor.h:257
~Sensor()
Destructor.
Definition SDL3pp_sensor.h:254
constexpr Sensor(SensorRaw resource) noexcept
Constructs from raw Sensor.
Definition SDL3pp_sensor.h:231
constexpr Sensor(Sensor &&other) noexcept
Move constructor.
Definition SDL3pp_sensor.h:237