From 7daaaa649e3816e23c41f3305691bce689e3ac0a Mon Sep 17 00:00:00 2001 From: Jonathan De Wachter Date: Mon, 6 Jan 2014 05:37:23 +0100 Subject: [PATCH] Implemented sensor API --- include/SFML/Window.hpp | 1 + include/SFML/Window/Event.hpp | 15 ++ include/SFML/Window/Sensor.hpp | 196 +++++++++++++++++++++++++ src/SFML/Window/Android/SensorImpl.cpp | 94 ++++++++++++ src/SFML/Window/Android/SensorImpl.hpp | 108 ++++++++++++++ src/SFML/Window/CMakeLists.txt | 17 ++- src/SFML/Window/OSX/SensorImpl.hpp | 109 ++++++++++++++ src/SFML/Window/OSX/SensorImpl.mm | 93 ++++++++++++ src/SFML/Window/Sensor.cpp | 83 +++++++++++ src/SFML/Window/SensorImpl.hpp | 100 +++++++++++++ src/SFML/Window/SensorManager.cpp | 150 +++++++++++++++++++ src/SFML/Window/SensorManager.hpp | 146 ++++++++++++++++++ src/SFML/Window/Unix/SensorImpl.cpp | 92 ++++++++++++ src/SFML/Window/Unix/SensorImpl.hpp | 108 ++++++++++++++ src/SFML/Window/Win32/SensorImpl.cpp | 92 ++++++++++++ src/SFML/Window/Win32/SensorImpl.hpp | 108 ++++++++++++++ src/SFML/Window/WindowImpl.cpp | 58 +++++++- src/SFML/Window/WindowImpl.hpp | 9 ++ src/SFML/Window/iOS/SensorImpl.hpp | 108 ++++++++++++++ src/SFML/Window/iOS/SensorImpl.mm | 92 ++++++++++++ 20 files changed, 1775 insertions(+), 4 deletions(-) create mode 100644 include/SFML/Window/Sensor.hpp create mode 100644 src/SFML/Window/Android/SensorImpl.cpp create mode 100644 src/SFML/Window/Android/SensorImpl.hpp create mode 100644 src/SFML/Window/OSX/SensorImpl.hpp create mode 100644 src/SFML/Window/OSX/SensorImpl.mm create mode 100644 src/SFML/Window/Sensor.cpp create mode 100644 src/SFML/Window/SensorImpl.hpp create mode 100644 src/SFML/Window/SensorManager.cpp create mode 100644 src/SFML/Window/SensorManager.hpp create mode 100644 src/SFML/Window/Unix/SensorImpl.cpp create mode 100644 src/SFML/Window/Unix/SensorImpl.hpp create mode 100644 src/SFML/Window/Win32/SensorImpl.cpp create mode 100644 src/SFML/Window/Win32/SensorImpl.hpp create mode 100644 src/SFML/Window/iOS/SensorImpl.hpp create mode 100644 src/SFML/Window/iOS/SensorImpl.mm diff --git a/include/SFML/Window.hpp b/include/SFML/Window.hpp index e38fb76bb..49f45f83b 100644 --- a/include/SFML/Window.hpp +++ b/include/SFML/Window.hpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/include/SFML/Window/Event.hpp b/include/SFML/Window/Event.hpp index 1e368ad17..59433bf95 100644 --- a/include/SFML/Window/Event.hpp +++ b/include/SFML/Window/Event.hpp @@ -32,6 +32,7 @@ #include #include #include +#include namespace sf @@ -151,6 +152,16 @@ public : int x; ///< X position of the touch, relative to the left of the owner window int y; ///< Y position of the touch, relative to the top of the owner window }; + + //////////////////////////////////////////////////////////// + /// \brief Sensor events parameters (SensorEnabled, SensorData, SensorDisabled) + /// + //////////////////////////////////////////////////////////// + struct SensorEvent + { + Sensor::Type type; + Sensor::Data data; + }; //////////////////////////////////////////////////////////// /// \brief Enumeration of the different types of events @@ -179,6 +190,9 @@ public : TouchBegan, ///< A touch event began (data in event.touch) TouchMoved, ///< A touch moved (data in event.touch) TouchEnded, ///< A touch event ended (data in event.touch) + SensorEnabled, ///< A sensor was enabled (data in event.sensor) + SensorData, ///< A new sensor data arrived (data in event.sensor) + SensorDisabled, ///< A sensor was disabled (data in event.sensor) Count ///< Keep last -- the total number of event types }; @@ -200,6 +214,7 @@ public : JoystickButtonEvent joystickButton; ///< Joystick button event parameters (Event::JoystickButtonPressed, Event::JoystickButtonReleased) JoystickConnectEvent joystickConnect; ///< Joystick (dis)connect event parameters (Event::JoystickConnected, Event::JoystickDisconnected) TouchEvent touch; ///< Touch events parameters (Event::TouchBegan, Event::TouchMoved, Event::TouchEnded) + SensorEvent sensor; ///< Sensor events parameters (Event::SensorEnabled, Event::SensorData, Event::SensorDisabled) }; }; diff --git a/include/SFML/Window/Sensor.hpp b/include/SFML/Window/Sensor.hpp new file mode 100644 index 000000000..70e22486f --- /dev/null +++ b/include/SFML/Window/Sensor.hpp @@ -0,0 +1,196 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSOR_HPP +#define SFML_SENSOR_HPP + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include +#include +#include + + +namespace sf +{ +class Window; + +//////////////////////////////////////////////////////////// +/// \brief Give access to the real-time state of the sensor +/// +//////////////////////////////////////////////////////////// +class SFML_WINDOW_API Sensor +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Sensor type + /// + //////////////////////////////////////////////////////////// + enum Type + { + Accelerometer, ///< The acceleration sensor measures the acceleration force + Gyroscope, ///< The gyroscope sensor measures the rate of rotation + Magnetometer, ///< The magnetic sensor measures the ambient magnetic field + Temperature, ///< The temperature sensor measures the ambient room temperature + Proximity, ///< The proximity sensor measures how close an object is + Gravity, ///< The gravity sensor measures the force of gravity + Light, ///< The light sensor measures the amount of ambient light or illumination + LinearAcceleration, ///< The linear acceleration sensor that measures acceleration + Orientation, ///< The orientation sensor that measures the degrees of orientation + Pressure, ///< The pressure sensor give air pressure readings + Altimeter, ///< The altimeter measures the altitude by using air pressure measurements + Compass, ///< The compass sensor give compass heading readings + + Count ///< Keep last -- the total number of sensor type + }; + + //////////////////////////////////////////////////////////// + /// \brief Sensor data + /// + //////////////////////////////////////////////////////////// + union Data + { + struct + { + float x; ///< X-axis data + float y; ///< Y-axis data + float z; ///< Z-axis data + } + acceleration, ///< Acceleration in m/s^2 (Sensor::Accelerometer, Sensor::Gravity, Sensor::LinearAcceleration) + magnetic, ///< Magnetic in uT (Sensor::Magnetometer) + vector, ///< Vector representing angle in degrees (Sensor::Orientation, Sensor::Compass) + gyroscope; ///< Gyroscope data in rad/s (Sensor::Gyroscope) + float temperature; ///< Temperature in degrees Celsius (Sensor::Temperature) + float distance; ///< Distance in meter (Sensor::Proximity, Sensor::Altimeter) + float light; ///< Illumination in lx (Sensor::Light) + float pressure; ///< Pressure in hPa (Sensor::Pressure) + }; + + //////////////////////////////////////////////////////////// + /// \brief Check if a sensor is available on the underlying platform + /// + /// \param sensor Sensor to check + /// + /// \return True if the sensor is available, false otherwise + /// + //////////////////////////////////////////////////////////// + static bool isAvailable(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Get the resolution of a sensor + /// + /// The resolution represents the sensitivity of a sensor. The + /// resolution of a sensor is the smallest change it can detect in + /// the quantity that it is measuring + /// + /// \param sensor Sensor to check + /// + /// \return The sensor's resolution in the sensor's unit. + /// + //////////////////////////////////////////////////////////// + static float getResolution(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Get the maximum range of a sensor + /// + /// The resolution represents the sensitivity of a sensor. The + /// value returned is in the sensor's unit. + /// + /// \param sensor Sensor to check + /// + /// \return The sensor's resolution in the sensor's unit. + /// + //////////////////////////////////////////////////////////// + static Vector2f getMaximumRange(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Get the minimum delay allowed between two events + /// + /// When setting the refresh rate of a sensor, you'll need to + /// make sure the value is a superior to the minimum delay + /// allowable + /// + /// \param sensor Sensor to check + /// + /// \return The minimum delay allowed between two events + /// + //////////////////////////////////////////////////////////// + Time getMinimumDelay(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Check if a sensor is enabled + /// + /// \param sensor Sensor to check + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + static bool isEnable(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable a sensor + /// + /// A sensor is disabled by default otherwise it would consume too + /// much battery power. Once a sensor enabled, it will start sending + /// sensor events of that type (if available). + /// + /// \param sensor Sensor to enable + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + static void setEnable(Type sensor, bool enable = true); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of a sensor + /// + /// This is the delay you want between each measure report + /// + /// \param sensor Sensor to modify + /// \param rate Delay between each event + /// + //////////////////////////////////////////////////////////// + static void setRefreshRate(Type sensor, const Time& rate); + + //////////////////////////////////////////////////////////// + /// \brief Get the current sensor data + /// + /// If the sensor support real-time retrieval, you'll get + /// the current sensor data otherwise this is likely the + /// latest data of this sensor type. + /// + /// \param sensor Sensor to check + /// + /// \return The current sensor data + /// + //////////////////////////////////////////////////////////// + static Data getData(Type sensor); +}; + +} // namespace sf + + +#endif // SFML_SENSOR_HPP diff --git a/src/SFML/Window/Android/SensorImpl.cpp b/src/SFML/Window/Android/SensorImpl.cpp new file mode 100644 index 000000000..c67e6716b --- /dev/null +++ b/src/SFML/Window/Android/SensorImpl.cpp @@ -0,0 +1,94 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +void SensorImpl::initialize() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +void SensorImpl::cleanup() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +SensorCaps& SensorImpl::initialize(unsigned int type) +{ + // To implement + SensorCaps capabilities; + capabilities.available = false; + + return capabilities; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::terminate() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +SensorState& SensorImpl::update() +{ + // To implement + static SensorState state; + return state; +} + +//////////////////////////////////////////////////////////// +bool SensorImpl::isEnable() +{ + // To implement + return false; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setEnable(bool enable) +{ + // To implement +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setRefreshRate(const Time& rate) +{ + // To implement +} + +} // namespace priv + +} // namespace sf diff --git a/src/SFML/Window/Android/SensorImpl.hpp b/src/SFML/Window/Android/SensorImpl.hpp new file mode 100644 index 000000000..fe4178c59 --- /dev/null +++ b/src/SFML/Window/Android/SensorImpl.hpp @@ -0,0 +1,108 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2013 Jonathan De Wachter (dewachter.jonathan@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPLANDROID_HPP +#define SFML_SENSORIMPLANDROID_HPP + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Android implementation of sensors +/// +//////////////////////////////////////////////////////////// +class SensorImpl +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Perform the global initialization of the sensor module + /// + //////////////////////////////////////////////////////////// + static void initialize(); + + //////////////////////////////////////////////////////////// + /// \brief Perform the global cleanup of the sensor module + /// + //////////////////////////////////////////////////////////// + static void cleanup(); + + //////////////////////////////////////////////////////////// + /// \brief Initialize the sensor + /// + /// \param type Index assigned to the sensor + /// + /// \return The sensor capabilities + /// + //////////////////////////////////////////////////////////// + SensorCaps& initialize(unsigned int type); + + //////////////////////////////////////////////////////////// + /// \brief Close the sensor + /// + //////////////////////////////////////////////////////////// + void terminate(); + + //////////////////////////////////////////////////////////// + /// \brief Update the sensor and get its new state + /// + /// \return Sensor state + /// + //////////////////////////////////////////////////////////// + SensorState& update(); + + //////////////////////////////////////////////////////////// + /// \brief Check if the sensor is enabled + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable the sensor + /// + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + void setEnable(bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of the sensor + /// + /// \param rate Delay between each refresh + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(const Time& rate); + +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORIMPLANDROID_HPP diff --git a/src/SFML/Window/CMakeLists.txt b/src/SFML/Window/CMakeLists.txt index ed99a289c..ba16826b5 100644 --- a/src/SFML/Window/CMakeLists.txt +++ b/src/SFML/Window/CMakeLists.txt @@ -24,7 +24,12 @@ set(SRC ${INCROOT}/Mouse.hpp ${SRCROOT}/Mouse.cpp ${INCROOT}/Touch.hpp - ${SRCROOT}/Touch.cpp + ${SRCROOT}/Touch.cpp + ${INCROOT}/Sensor.hpp + ${SRCROOT}/Sensor.cpp + ${SRCROOT}/SensorImpl.hpp + ${SRCROOT}/SensorManager.cpp + ${SRCROOT}/SensorManager.hpp ${SRCROOT}/VideoMode.cpp ${INCROOT}/VideoMode.hpp ${SRCROOT}/VideoModeImpl.hpp @@ -64,6 +69,8 @@ elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD) ${SRCROOT}/Unix/Display.hpp ${SRCROOT}/Unix/InputImpl.cpp ${SRCROOT}/Unix/InputImpl.hpp + ${SRCROOT}/Unix/SensorImpl.cpp + ${SRCROOT}/Unix/SensorImpl.hpp ${SRCROOT}/Unix/VideoModeImpl.cpp ${SRCROOT}/Unix/WindowImplX11.cpp ${SRCROOT}/Unix/WindowImplX11.hpp @@ -103,6 +110,8 @@ elseif(SFML_OS_MACOSX) ${SRCROOT}/OSX/HIDJoystickManager.cpp ${SRCROOT}/OSX/JoystickImpl.cpp ${SRCROOT}/OSX/JoystickImpl.hpp + ${SRCROOT}/OSX/SensorImpl.cpp + ${SRCROOT}/OSX/SensorImpl.hpp ${SRCROOT}/OSX/SFApplication.h ${SRCROOT}/OSX/SFApplication.m ${SRCROOT}/OSX/SFApplicationDelegate.h @@ -135,6 +144,8 @@ elseif(SFML_OS_IOS) ${SRCROOT}/iOS/InputImpl.hpp ${SRCROOT}/iOS/JoystickImpl.mm ${SRCROOT}/iOS/JoystickImpl.hpp + ${SRCROOT}/iOS/SensorImpl.mm + ${SRCROOT}/iOS/SensorImpl.hpp ${SRCROOT}/iOS/VideoModeImpl.mm ${SRCROOT}/iOS/WindowImplUIKit.hpp ${SRCROOT}/iOS/WindowImplUIKit.mm @@ -157,7 +168,9 @@ elseif(SFML_OS_ANDROID) ${SRCROOT}/Android/InputImpl.hpp ${SRCROOT}/Android/InputImpl.cpp ${SRCROOT}/Android/JoystickImpl.hpp - ${SRCROOT}/Android/JoystickImpl.cpp) + ${SRCROOT}/Android/JoystickImpl.cpp + ${SRCROOT}/Android/SensorImpl.hpp + ${SRCROOT}/Android/SensorImpl.cpp) source_group("android" FILES ${PLATFORM_SRC}) endif() diff --git a/src/SFML/Window/OSX/SensorImpl.hpp b/src/SFML/Window/OSX/SensorImpl.hpp new file mode 100644 index 000000000..c939410ef --- /dev/null +++ b/src/SFML/Window/OSX/SensorImpl.hpp @@ -0,0 +1,109 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPLOSX_HPP +#define SFML_SENSORIMPLOSX_HPP + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Mac OS X implementation of sensors +/// +//////////////////////////////////////////////////////////// +class SensorImpl +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Perform the global initialization of the sensor module + /// + //////////////////////////////////////////////////////////// + static void initialize(); + + //////////////////////////////////////////////////////////// + /// \brief Perform the global cleanup of the sensor module + /// + //////////////////////////////////////////////////////////// + static void cleanup(); + + //////////////////////////////////////////////////////////// + /// \brief Initialize the sensor + /// + /// \param type Index assigned to the sensor + /// + /// \return The sensor capabilities + /// + //////////////////////////////////////////////////////////// + SensorCaps& initialize(unsigned int type); + + //////////////////////////////////////////////////////////// + /// \brief Close the sensor + /// + //////////////////////////////////////////////////////////// + void terminate(); + + //////////////////////////////////////////////////////////// + /// \brief Update the sensor and get its new state + /// + /// \return Sensor state + /// + //////////////////////////////////////////////////////////// + SensorState& update(); + + //////////////////////////////////////////////////////////// + /// \brief Check if the sensor is enabled + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable the sensor + /// + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + void setEnable(bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of the sensor + /// + /// \param rate Delay between each refresh + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(const Time& rate); + +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORIMPLOSX_HPP diff --git a/src/SFML/Window/OSX/SensorImpl.mm b/src/SFML/Window/OSX/SensorImpl.mm new file mode 100644 index 000000000..37f7433c7 --- /dev/null +++ b/src/SFML/Window/OSX/SensorImpl.mm @@ -0,0 +1,93 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +void SensorImpl::initialize() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::cleanup() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorCaps& SensorImpl::initialize(unsigned int type) +{ + // Not applicable + SensorCaps capabilities; + capabilities.available = false; + + return capabilities; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::terminate() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorState& SensorImpl::update() +{ + // Not applicable + static SensorState state; + return state; +} + +//////////////////////////////////////////////////////////// +bool SensorImpl::isEnable() +{ + // Not applicable + return false; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setEnable(bool enable) +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setRefreshRate(const Time& rate) +{ + // Not applicable +} + +} // namespace priv + +} // namespace sf diff --git a/src/SFML/Window/Sensor.cpp b/src/SFML/Window/Sensor.cpp new file mode 100644 index 000000000..82dd2ba60 --- /dev/null +++ b/src/SFML/Window/Sensor.cpp @@ -0,0 +1,83 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include + + +namespace sf +{ + +//////////////////////////////////////////////////////////// +bool Sensor::isAvailable(Type sensor) +{ + return priv::SensorManager::getInstance().getCapabilities(sensor).available; +} + +//////////////////////////////////////////////////////////// +float Sensor::getResolution(Type sensor) +{ + return priv::SensorManager::getInstance().getCapabilities(sensor).resolution; +} + +//////////////////////////////////////////////////////////// +Vector2f Sensor::getMaximumRange(Type sensor) +{ + return priv::SensorManager::getInstance().getCapabilities(sensor).maximumRange; +} + +//////////////////////////////////////////////////////////// +Time Sensor::getMinimumDelay(Type sensor) +{ + return priv::SensorManager::getInstance().getCapabilities(sensor).minimumDelay; +} + +//////////////////////////////////////////////////////////// +bool Sensor::isEnable(Type sensor) +{ + return priv::SensorManager::getInstance().isEnable(sensor); +} + +//////////////////////////////////////////////////////////// +void Sensor::setEnable(Type sensor, bool enable) +{ + return priv::SensorManager::getInstance().setEnable(sensor, enable); +} + +//////////////////////////////////////////////////////////// +void Sensor::setRefreshRate(Type sensor, const Time& rate) +{ + priv::SensorManager::getInstance().setRefreshRate(sensor, rate); +} + +//////////////////////////////////////////////////////////// +Sensor::Data Sensor::getData(Type sensor) +{ + return priv::SensorManager::getInstance().getState(sensor).pendingData->back(); +} + +} // namespace sf diff --git a/src/SFML/Window/SensorImpl.hpp b/src/SFML/Window/SensorImpl.hpp new file mode 100644 index 000000000..ca3a702f0 --- /dev/null +++ b/src/SFML/Window/SensorImpl.hpp @@ -0,0 +1,100 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPL_HPP +#define SFML_SENSORIMPL_HPP + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include +#include +#include +#include +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Structure holding a sensor's capabilities +/// +//////////////////////////////////////////////////////////// +struct SensorCaps +{ + SensorCaps() + { + available = false; + resolution = 0; + maximumRange = Vector2f(0, 0); + minimumDelay = Time::Zero; + } + + bool available; ///< Is the sensor available on the underlying platform + float resolution; ///< How sensible the sensor is in the sensor's unit + Vector2f maximumRange; ///< Maximum range of the sensor in the sensor's unit + Time minimumDelay; ///< Minimum delay allowed between two events +}; + + +//////////////////////////////////////////////////////////// +/// \brief Structure holding a sensor's state +/// +//////////////////////////////////////////////////////////// +struct SensorState +{ + SensorState() + { + pendingData = NULL; + enabled = false; + refreshRate = Time::Zero; + } + + std::queue* pendingData; ///< Pending sensor data + bool enabled; ///< Is the sensor currently enabled? + Time refreshRate; ///< Delay between two events +}; + +} // namespace priv + +} // namespace sf + + +#if defined(SFML_SYSTEM_WINDOWS) + #include +#elif defined(SFML_SYSTEM_LINUX) + #include +#elif defined(SFML_SYSTEM_MACOS) + #include +#elif defined(SFML_SYSTEM_IOS) + #include +#elif defined(SFML_SYSTEM_ANDROID) + #include +#endif + + +#endif // SFML_SENSORIMPL_HPP diff --git a/src/SFML/Window/SensorManager.cpp b/src/SFML/Window/SensorManager.cpp new file mode 100644 index 000000000..c17abf8f2 --- /dev/null +++ b/src/SFML/Window/SensorManager.cpp @@ -0,0 +1,150 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +SensorManager& SensorManager::getInstance() +{ + static SensorManager instance; + return instance; +} + + +//////////////////////////////////////////////////////////// +const SensorCaps& SensorManager::getCapabilities(unsigned int sensor) const +{ + return m_sensors[sensor].capabilities; +} + + +//////////////////////////////////////////////////////////// +const SensorState& SensorManager::getState(unsigned int sensor) const +{ + return m_sensors[sensor].state; +} + + +//////////////////////////////////////////////////////////// +void SensorManager::update() +{ + for (int i = 0; i < Sensor::Count; ++i) + { + Item& item = m_sensors[i]; + + // Skip unavailable sensors + if (!item.capabilities.available) + continue; + + // Get the current state of the sensor + item.state = item.sensor.update(); + } +} + +//////////////////////////////////////////////////////////// +bool SensorManager::isEnable(unsigned int sensor) +{ + if (!m_sensors[sensor].capabilities.available) + { + err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl; + return false; + } + + return m_sensors[sensor].sensor.isEnable(); +} + +//////////////////////////////////////////////////////////// +void SensorManager::setEnable(unsigned int sensor, bool enable) +{ + if (!m_sensors[sensor].capabilities.available) + { + err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl; + return; + } + + m_sensors[sensor].sensor.setEnable(enable); +} + +//////////////////////////////////////////////////////////// +void SensorManager::setRefreshRate(unsigned int sensor, const Time& rate) +{ + if (!m_sensors[sensor].capabilities.available) + { + err() << "This sensor isn't available on your system (call Sensor::isAvailable to check it)" << std::endl; + return; + } + + m_sensors[sensor].sensor.setRefreshRate(rate); +} + +//////////////////////////////////////////////////////////// +SensorManager::SensorManager() +{ + // Global sensor initialization + SensorImpl::initialize(); + + // Per sensor initialization + for (int i = 0; i < Sensor::Count; ++i) + { + // Initialize the sensor and get its capabilities + m_sensors[i].capabilities = m_sensors[i].sensor.initialize(i); + + // Available sensors are disabled by default + if (m_sensors[i].capabilities.available) + m_sensors[i].sensor.setEnable(false); + } +} + +//////////////////////////////////////////////////////////// +SensorManager::~SensorManager() +{ + // Per sensor cleanup + for (int i = 0; i < Sensor::Count; ++i) + { + if (m_sensors[i].capabilities.available) + { + // Disable the sensor + m_sensors[i].sensor.setEnable(false); + + // Terminate the sensor + m_sensors[i].sensor.terminate(); + } + } + + // Global sensor cleanup + SensorImpl::cleanup(); +} + +} // namespace priv + +} // namespace sf diff --git a/src/SFML/Window/SensorManager.hpp b/src/SFML/Window/SensorManager.hpp new file mode 100644 index 000000000..8617a3c1b --- /dev/null +++ b/src/SFML/Window/SensorManager.hpp @@ -0,0 +1,146 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORMANAGER_HPP +#define SFML_SENSORMANAGER_HPP + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include +#include +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Global sensor manager +/// +//////////////////////////////////////////////////////////// +class SensorManager : NonCopyable +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Get the global unique instance of the manager + /// + /// \return Unique instance of the sensor manager + /// + //////////////////////////////////////////////////////////// + static SensorManager& getInstance(); + + //////////////////////////////////////////////////////////// + /// \brief Get the capabilities of a sensor + /// + /// \param sensor Index of the sensor + /// + /// \return Capabilities of the sensor + /// + //////////////////////////////////////////////////////////// + const SensorCaps& getCapabilities(unsigned int sensor) const; + + //////////////////////////////////////////////////////////// + /// \brief Get the current state of a sensor + /// + /// \param sensor Index of the sensor + /// + /// \return Current state of the sensor + /// + //////////////////////////////////////////////////////////// + const SensorState& getState(unsigned int sensor) const; + + //////////////////////////////////////////////////////////// + /// \brief Update the state of all the sensors + /// + //////////////////////////////////////////////////////////// + void update(); + + //////////////////////////////////////////////////////////// + /// \brief Get the current status of a sensor + /// + /// \param sensor Index of the sensor + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(unsigned int sensor); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable a sensor + /// + /// \param sensor Index of the sensor + /// \param enable Whether it should be enabled or not + /// + //////////////////////////////////////////////////////////// + void setEnable(unsigned int sensor, bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of a sensor + /// + /// \param sensor Index of the sensor + /// \param rate Delay between each event + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(unsigned int sensor, const Time& rate); + +private: + + //////////////////////////////////////////////////////////// + /// \brief Default constructor + /// + //////////////////////////////////////////////////////////// + SensorManager(); + + //////////////////////////////////////////////////////////// + /// \brief Destructor + /// + //////////////////////////////////////////////////////////// + ~SensorManager(); + + //////////////////////////////////////////////////////////// + /// \brief Sensor information and state + /// + //////////////////////////////////////////////////////////// + struct Item + { + SensorImpl sensor; ///< Sensor implementation + SensorState state; ///< The current sensor state + SensorCaps capabilities; ///< The sensor capabilities + }; + + //////////////////////////////////////////////////////////// + // Member data + //////////////////////////////////////////////////////////// + Item m_sensors[Sensor::Count]; ///< Sensors information and state +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORMANAGER_HPP diff --git a/src/SFML/Window/Unix/SensorImpl.cpp b/src/SFML/Window/Unix/SensorImpl.cpp new file mode 100644 index 000000000..dcc53e39e --- /dev/null +++ b/src/SFML/Window/Unix/SensorImpl.cpp @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +void SensorImpl::initialize() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::cleanup() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorCaps& SensorImpl::initialize(unsigned int type) +{ + // Not applicable + SensorCaps capabilities; + capabilities.available = false; + + return capabilities; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::terminate() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorState& SensorImpl::update() +{ + // Not applicable + static SensorState state; + return state; +} + +//////////////////////////////////////////////////////////// +bool SensorImpl::isEnable() +{ + // Not applicable + return false; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setEnable(bool enable) +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setRefreshRate(const Time& rate) +{ + // Not applicable +} + +} // namespace priv + +} // namespace sf diff --git a/src/SFML/Window/Unix/SensorImpl.hpp b/src/SFML/Window/Unix/SensorImpl.hpp new file mode 100644 index 000000000..8925681a0 --- /dev/null +++ b/src/SFML/Window/Unix/SensorImpl.hpp @@ -0,0 +1,108 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPLLINUX_HPP +#define SFML_SENSORIMPLLINUX_HPP + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Linux implementation of sensors +/// +//////////////////////////////////////////////////////////// +class SensorImpl +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Perform the global initialization of the sensor module + /// + //////////////////////////////////////////////////////////// + static void initialize(); + + //////////////////////////////////////////////////////////// + /// \brief Perform the global cleanup of the sensor module + /// + //////////////////////////////////////////////////////////// + static void cleanup(); + + //////////////////////////////////////////////////////////// + /// \brief Initialize the sensor + /// + /// \param type Index assigned to the sensor + /// + /// \return The sensor capabilities + /// + //////////////////////////////////////////////////////////// + SensorCaps& initialize(unsigned int type); + + //////////////////////////////////////////////////////////// + /// \brief Close the sensor + /// + //////////////////////////////////////////////////////////// + void terminate(); + + //////////////////////////////////////////////////////////// + /// \brief Update the sensor and get its new state + /// + /// \return Sensor state + /// + //////////////////////////////////////////////////////////// + SensorState& update(); + + //////////////////////////////////////////////////////////// + /// \brief Check if the sensor is enabled + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable the sensor + /// + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + void setEnable(bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of the sensor + /// + /// \param rate Delay between each refresh + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(const Time& rate); + +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORIMPLLINUX_HPP diff --git a/src/SFML/Window/Win32/SensorImpl.cpp b/src/SFML/Window/Win32/SensorImpl.cpp new file mode 100644 index 000000000..dcc53e39e --- /dev/null +++ b/src/SFML/Window/Win32/SensorImpl.cpp @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +void SensorImpl::initialize() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::cleanup() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorCaps& SensorImpl::initialize(unsigned int type) +{ + // Not applicable + SensorCaps capabilities; + capabilities.available = false; + + return capabilities; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::terminate() +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +SensorState& SensorImpl::update() +{ + // Not applicable + static SensorState state; + return state; +} + +//////////////////////////////////////////////////////////// +bool SensorImpl::isEnable() +{ + // Not applicable + return false; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setEnable(bool enable) +{ + // Not applicable +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setRefreshRate(const Time& rate) +{ + // Not applicable +} + +} // namespace priv + +} // namespace sf diff --git a/src/SFML/Window/Win32/SensorImpl.hpp b/src/SFML/Window/Win32/SensorImpl.hpp new file mode 100644 index 000000000..62ace3d04 --- /dev/null +++ b/src/SFML/Window/Win32/SensorImpl.hpp @@ -0,0 +1,108 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPLWIN32_HPP +#define SFML_SENSORIMPLWIN32_HPP + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief Windows implementation of sensors +/// +//////////////////////////////////////////////////////////// +class SensorImpl +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Perform the global initialization of the sensor module + /// + //////////////////////////////////////////////////////////// + static void initialize(); + + //////////////////////////////////////////////////////////// + /// \brief Perform the global cleanup of the sensor module + /// + //////////////////////////////////////////////////////////// + static void cleanup(); + + //////////////////////////////////////////////////////////// + /// \brief Initialize the sensor + /// + /// \param type Index assigned to the sensor + /// + /// \return The sensor capabilities + /// + //////////////////////////////////////////////////////////// + SensorCaps& initialize(unsigned int type); + + //////////////////////////////////////////////////////////// + /// \brief Close the sensor + /// + //////////////////////////////////////////////////////////// + void terminate(); + + //////////////////////////////////////////////////////////// + /// \brief Update the sensor and get its new state + /// + /// \return Sensor state + /// + //////////////////////////////////////////////////////////// + SensorState& update(); + + //////////////////////////////////////////////////////////// + /// \brief Check if the sensor is enabled + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable the sensor + /// + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + void setEnable(bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of the sensor + /// + /// \param rate Delay between each refresh + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(const Time& rate); + +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORIMPLWIN32_HPP diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index 3eb536b55..ca82a3b5a 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,11 @@ m_joyThreshold(0.1f) JoystickManager::getInstance().update(); for (unsigned int i = 0; i < Joystick::Count; ++i) m_joyStates[i] = JoystickManager::getInstance().getState(i); + + // Get the initial sensor states + SensorManager::getInstance().update(); + for (unsigned int i = 0; i < Sensor::Count; ++i) + m_senStates[i] = SensorManager::getInstance().getState(i); } @@ -108,10 +114,11 @@ bool WindowImpl::popEvent(Event& event, bool block) { // If the event queue is empty, let's first check if new events are available from the OS if (m_events.empty()) - { + { // Get events from the system processJoystickEvents(); - processEvents(); + processSensorEvents(); + processEvents(); // In blocking mode, we must process events until one is triggered if (block) @@ -123,6 +130,7 @@ bool WindowImpl::popEvent(Event& event, bool block) { sleep(milliseconds(10)); processJoystickEvents(); + processSensorEvents(); processEvents(); } } @@ -213,6 +221,52 @@ void WindowImpl::processJoystickEvents() } +//////////////////////////////////////////////////////////// +void WindowImpl::processSensorEvents() +{ + // First update sensor states + SensorManager::getInstance().update(); + + for (unsigned int i = 0; i < Sensor::Count; ++i) + { + // Copy the previous state of the sensor and get the new one + SensorState previousState = m_senStates[i]; + m_senStates[i] = SensorManager::getInstance().getState(i); + + // Check whether it's still enabled + bool enabled = m_senStates[i].enabled; + if (previousState.enabled ^ enabled) + { + Event event; + event.type = enabled ? Event::SensorEnabled : Event::SensorDisabled; + event.sensor.type = static_cast(i); + pushEvent(event); + + // This sensor has been disabled, we don't want these pending data + if (!enabled) + { + while (!m_senStates[i].pendingData->empty()) + m_senStates[i].pendingData->pop(); + } + } + + if (enabled) + { + // Send pending sensor data events + while (!m_senStates[i].pendingData->empty()) + { + Event event; + event.type = Event::SensorData; + event.sensor.type = static_cast(i); + event.sensor.data = m_senStates[i].pendingData->front(); + pushEvent(event); + + m_senStates[i].pendingData->pop(); + } + } + } +} + } // namespace priv } // namespace sf diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp index 5b6f7d500..040028d7f 100644 --- a/src/SFML/Window/WindowImpl.hpp +++ b/src/SFML/Window/WindowImpl.hpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include #include @@ -225,12 +227,19 @@ private : /// //////////////////////////////////////////////////////////// void processJoystickEvents(); + + //////////////////////////////////////////////////////////// + /// \brief Read the sensors state and generate the appropriate events + /// + //////////////////////////////////////////////////////////// + void processSensorEvents(); //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// std::queue m_events; ///< Queue of available events JoystickState m_joyStates[Joystick::Count]; ///< Previous state of the joysticks + SensorState m_senStates[Sensor::Count]; ///< Previous state of the sensors float m_joyThreshold; ///< Joystick threshold (minimum motion for MOVE event to be generated) }; diff --git a/src/SFML/Window/iOS/SensorImpl.hpp b/src/SFML/Window/iOS/SensorImpl.hpp new file mode 100644 index 000000000..bf0c11423 --- /dev/null +++ b/src/SFML/Window/iOS/SensorImpl.hpp @@ -0,0 +1,108 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +#ifndef SFML_SENSORIMPLIOS_HPP +#define SFML_SENSORIMPLIOS_HPP + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +/// \brief iOS implementation of sensors +/// +//////////////////////////////////////////////////////////// +class SensorImpl +{ +public : + + //////////////////////////////////////////////////////////// + /// \brief Perform the global initialization of the sensor module + /// + //////////////////////////////////////////////////////////// + static void initialize(); + + //////////////////////////////////////////////////////////// + /// \brief Perform the global cleanup of the sensor module + /// + //////////////////////////////////////////////////////////// + static void cleanup(); + + //////////////////////////////////////////////////////////// + /// \brief Initialize the sensor + /// + /// \param type Index assigned to the sensor + /// + /// \return The sensor capabilities + /// + //////////////////////////////////////////////////////////// + SensorCaps& initialize(unsigned int type); + + //////////////////////////////////////////////////////////// + /// \brief Close the sensor + /// + //////////////////////////////////////////////////////////// + void terminate(); + + //////////////////////////////////////////////////////////// + /// \brief Update the sensor and get its new state + /// + /// \return Sensor state + /// + //////////////////////////////////////////////////////////// + SensorState& update(); + + //////////////////////////////////////////////////////////// + /// \brief Check if the sensor is enabled + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + bool isEnable(); + + //////////////////////////////////////////////////////////// + /// \brief Enable or disable the sensor + /// + /// \param enable True to enable, false to disable + /// + //////////////////////////////////////////////////////////// + void setEnable(bool enable); + + //////////////////////////////////////////////////////////// + /// \brief Set the refresh rate of the sensor + /// + /// \param rate Delay between each refresh + /// + //////////////////////////////////////////////////////////// + void setRefreshRate(const Time& rate); + +}; + +} // namespace priv + +} // namespace sf + + +#endif // SFML_SENSORIMPLIOS_HPP diff --git a/src/SFML/Window/iOS/SensorImpl.mm b/src/SFML/Window/iOS/SensorImpl.mm new file mode 100644 index 000000000..d8ba51da5 --- /dev/null +++ b/src/SFML/Window/iOS/SensorImpl.mm @@ -0,0 +1,92 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com) +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#include + + +namespace sf +{ +namespace priv +{ +//////////////////////////////////////////////////////////// +void SensorImpl::initialize() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +void SensorImpl::cleanup() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +SensorCaps& SensorImpl::initialize(unsigned int type) +{ + // To implement + SensorCaps capabilities; + capabilities.available = false; + + return capabilities; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::terminate() +{ + // To implement +} + +//////////////////////////////////////////////////////////// +SensorState& SensorImpl::update() +{ + // To implement + static SensorState state; + return state; +} + +//////////////////////////////////////////////////////////// +bool SensorImpl::isEnable() +{ + // To implement + return false; +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setEnable(bool enable) +{ + // To implement +} + +//////////////////////////////////////////////////////////// +void SensorImpl::setRefreshRate(const Time& rate) +{ + // To implement +} + +} // namespace priv + +} // namespace sf