Merge branch 'master' of github.com:LaurentGomila/SFML

This commit is contained in:
Laurent Gomila 2011-08-03 20:10:28 +02:00
commit 3bba2fab98
11 changed files with 367 additions and 104 deletions

View File

@ -71,6 +71,8 @@ else() # MACOSX
${SRCROOT}/OSX/InputImpl.hpp ${SRCROOT}/OSX/InputImpl.hpp
${SRCROOT}/OSX/HIDInputManager.hpp ${SRCROOT}/OSX/HIDInputManager.hpp
${SRCROOT}/OSX/HIDInputManager.mm ${SRCROOT}/OSX/HIDInputManager.mm
${SRCROOT}/OSX/HIDJoystickManager.hpp
${SRCROOT}/OSX/HIDJoystickManager.cpp
${SRCROOT}/OSX/JoystickImpl.cpp ${SRCROOT}/OSX/JoystickImpl.cpp
${SRCROOT}/OSX/JoystickImpl.hpp ${SRCROOT}/OSX/JoystickImpl.hpp
${SRCROOT}/OSX/SFApplication.h ${SRCROOT}/OSX/SFApplication.h

View File

@ -47,9 +47,6 @@ namespace priv
/// keyboard and mouse states. It's only purpose is /// keyboard and mouse states. It's only purpose is
/// to help sf::priv::InputImpl class. /// to help sf::priv::InputImpl class.
/// ///
/// HIDInputManager provides a function to get all connected joysticks
/// to help sf::priv::JoystickImpl.
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class HIDInputManager : NonCopyable class HIDInputManager : NonCopyable
{ {
@ -85,14 +82,6 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool IsMouseButtonPressed(Mouse::Button button); bool IsMouseButtonPressed(Mouse::Button button);
////////////////////////////////////////////////////////////
/// \brief List all connected joysticks
///
/// \return a retained CFSetRef of IOHIDDeviceRef or NULL
///
////////////////////////////////////////////////////////////
CFSetRef CopyJoystickDevices();
public : public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -106,6 +95,16 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
static long GetLocationID(IOHIDDeviceRef device); static long GetLocationID(IOHIDDeviceRef device);
////////////////////////////////////////////////////////////
/// \brief Create a mask (dictionary) for an IOHIDManager
///
/// \param page HID page
/// \param usage HID usage page
/// \return a retained CFDictionaryRef
///
////////////////////////////////////////////////////////////
static CFDictionaryRef CopyDevicesMask(UInt32 page, UInt32 usage);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Try to convert a character into a SFML key code. /// Try to convert a character into a SFML key code.
/// ///
@ -208,16 +207,6 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void FreeUp(); void FreeUp();
////////////////////////////////////////////////////////////
/// \brief Create a mask (dictionary) for an IOHIDManager
///
/// \param page HID page
/// \param usage HID usage page
/// \return a retained CFDictionaryRef
///
////////////////////////////////////////////////////////////
static CFDictionaryRef CopyDevicesMaskForManager(UInt32 page, UInt32 usage);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Filter the devices and return them. /// \brief Filter the devices and return them.
/// ///

View File

@ -127,13 +127,6 @@ bool HIDInputManager::IsMouseButtonPressed(Mouse::Button button)
} }
////////////////////////////////////////////////////////////
CFSetRef HIDInputManager::CopyJoystickDevices()
{
return CopyDevices(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick);
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
long HIDInputManager::GetLocationID(IOHIDDeviceRef device) long HIDInputManager::GetLocationID(IOHIDDeviceRef device)
{ {
@ -156,6 +149,28 @@ long HIDInputManager::GetLocationID(IOHIDDeviceRef device)
} }
////////////////////////////////////////////////////////////
CFDictionaryRef HIDInputManager::CopyDevicesMask(UInt32 page, UInt32 usage)
{
// Create the dictionary.
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
// Add the page value.
CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), value);
CFRelease(value);
// Add the usage value (which is only valid if page value exists).
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), value);
CFRelease(value);
return dict;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
HIDInputManager::HIDInputManager() HIDInputManager::HIDInputManager()
: amIValid(true) : amIValid(true)
@ -516,33 +531,11 @@ void HIDInputManager::FreeUp()
} }
////////////////////////////////////////////////////////////
CFDictionaryRef HIDInputManager::CopyDevicesMaskForManager(UInt32 page, UInt32 usage)
{
// Create the dictionary.
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
// Add the page value.
CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), value);
CFRelease(value);
// Add the usage value (which is only valid if page value exists).
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), value);
CFRelease(value);
return dict;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
CFSetRef HIDInputManager::CopyDevices(UInt32 page, UInt32 usage) CFSetRef HIDInputManager::CopyDevices(UInt32 page, UInt32 usage)
{ {
// Filter and keep only the requested devices // Filter and keep only the requested devices
CFDictionaryRef mask = CopyDevicesMaskForManager(page, usage); CFDictionaryRef mask = CopyDevicesMask(page, usage);
IOHIDManagerSetDeviceMatching(myManager, mask); IOHIDManagerSetDeviceMatching(myManager, mask);

View File

@ -0,0 +1,127 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2011 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 <SFML/Window/OSX/HIDJoystickManager.hpp>
#include <SFML/Window/OSX/HIDInputManager.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
HIDJoystickManager& HIDJoystickManager::GetInstance()
{
static HIDJoystickManager manager;
return manager;
}
////////////////////////////////////////////////////////////
unsigned int HIDJoystickManager::GetJoystickCount()
{
Update();
return myJoystickCount;
}
////////////////////////////////////////////////////////////
CFSetRef HIDJoystickManager::CopyJoysticks()
{
CFSetRef devices = IOHIDManagerCopyDevices(myHIDManager);
return devices;
}
////////////////////////////////////////////////////////////
HIDJoystickManager::HIDJoystickManager()
: myHIDManager(0)
, myJoystickCount(0)
{
myHIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
CFDictionaryRef mask = HIDInputManager::CopyDevicesMask(kHIDPage_GenericDesktop,
kHIDUsage_GD_Joystick);
IOHIDManagerSetDeviceMatching(myHIDManager, mask);
CFRelease(mask);
IOHIDManagerRegisterDeviceMatchingCallback(myHIDManager, pluggedIn, this);
IOHIDManagerRegisterDeviceRemovalCallback(myHIDManager, pluggedOut, this);
IOHIDManagerScheduleWithRunLoop(myHIDManager,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
IOHIDManagerOpen(myHIDManager, kIOHIDOptionsTypeNone);
}
////////////////////////////////////////////////////////////
HIDJoystickManager::~HIDJoystickManager()
{
IOHIDManagerUnscheduleFromRunLoop(myHIDManager,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
IOHIDManagerRegisterDeviceMatchingCallback(myHIDManager, NULL, 0);
IOHIDManagerRegisterDeviceRemovalCallback(myHIDManager, NULL, 0);
IOHIDManagerClose(myHIDManager, kIOHIDOptionsTypeNone);
}
////////////////////////////////////////////////////////////
void HIDJoystickManager::Update()
{
SInt32 status = kCFRunLoopRunHandledSource;
while (status == kCFRunLoopRunHandledSource) {
status = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}
}
////////////////////////////////////////////////////////////
void HIDJoystickManager::pluggedIn(void * context, IOReturn, void *, IOHIDDeviceRef)
{
HIDJoystickManager* manager = (HIDJoystickManager*)context;
manager->myJoystickCount++;
}
////////////////////////////////////////////////////////////
void HIDJoystickManager::pluggedOut(void * context, IOReturn, void *, IOHIDDeviceRef)
{
HIDJoystickManager* manager = (HIDJoystickManager*)context;
manager->myJoystickCount--;
}
} // namespace priv
} // namespace sf

View File

@ -0,0 +1,128 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2011 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_HIDJOYSTICKMANAGER_HPP
#define SFML_HIDJOYSTICKMANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDDevice.h>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief This class manage as a singleton instance the
/// joysticks. It's only purpose is
/// to help sf::priv::JoystickImpl class.
///
////////////////////////////////////////////////////////////
class HIDJoystickManager : NonCopyable
{
public:
////////////////////////////////////////////////////////////
/// \brief Get the unique instance of the class
///
/// \note Private use only
///
/// \return Reference to the HIDJoystickManager instance
///
////////////////////////////////////////////////////////////
static HIDJoystickManager& GetInstance();
public:
////////////////////////////////////////////////////////////
/// \brief Get the number of currently connected joystick
///
////////////////////////////////////////////////////////////
unsigned int GetJoystickCount();
////////////////////////////////////////////////////////////
/// \brief Copy the devices assosiated with this HID manager
///
/// \return a retained CFSetRef of IOHIDDeviceRef or NULL
///
////////////////////////////////////////////////////////////
CFSetRef CopyJoysticks();
private:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
HIDJoystickManager();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~HIDJoystickManager();
////////////////////////////////////////////////////////////
/// \brief Make sur all event have been processed in the run loop
///
////////////////////////////////////////////////////////////
void Update();
private:
////////////////////////////////////////////////////////////
/// \brief Private "plug-in" callback
/// \note Only 'context' parametre is used.
/// \see IOHIDDeviceCallback
///
////////////////////////////////////////////////////////////
static void pluggedIn(void * context, IOReturn, void *, IOHIDDeviceRef);
////////////////////////////////////////////////////////////
/// \brief Private "plug-out" callback
/// \note Only 'context' parametre is used.
/// \see IOHIDDeviceCallback
///
////////////////////////////////////////////////////////////
static void pluggedOut(void * context, IOReturn, void *, IOHIDDeviceRef);
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
IOHIDManagerRef myHIDManager; ///< HID Manager
unsigned int myJoystickCount;///< Number of joysticks currently connected
};
} // namespace priv
} // namespace sf
#endif

View File

@ -28,6 +28,8 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp> #include <SFML/Window/JoystickImpl.hpp>
#include <SFML/Window/OSX/HIDInputManager.hpp> #include <SFML/Window/OSX/HIDInputManager.hpp>
#include <SFML/Window/OSX/HIDJoystickManager.hpp>
namespace sf namespace sf
{ {
@ -51,8 +53,20 @@ bool JoystickImpl::IsConnected(unsigned int index)
// Otherwise, let's check if it is now connected : // Otherwise, let's check if it is now connected :
else { // i.e., myLocationIDs[index] == 0 else { // i.e., myLocationIDs[index] == 0
// if there is more connected joystick to the HID manager than
// opened joystick devices then we find the new one.
unsigned int openedCount = 0;
for (unsigned int i(0); i < sf::Joystick::Count; ++i) {
if (myLocationIDs[i] != 0) openedCount++;
}
unsigned int connectedCount = HIDJoystickManager::GetInstance().GetJoystickCount();
if (connectedCount > openedCount) {
// Get all devices // Get all devices
CFSetRef devices = HIDInputManager::GetInstance().CopyJoystickDevices(); CFSetRef devices = HIDJoystickManager::GetInstance().CopyJoysticks();
if (devices != NULL) { if (devices != NULL) {
@ -95,6 +109,7 @@ bool JoystickImpl::IsConnected(unsigned int index)
CFRelease(devices); CFRelease(devices);
} }
} }
}
return state; return state;
} }
@ -107,7 +122,7 @@ bool JoystickImpl::Open(unsigned int index)
Location deviceLoc = myLocationIDs[index]; // The device we need to load Location deviceLoc = myLocationIDs[index]; // The device we need to load
// Get all devices // Get all devices
CFSetRef devices = HIDInputManager::GetInstance().CopyJoystickDevices(); CFSetRef devices = HIDJoystickManager::GetInstance().CopyJoysticks();
if (devices == NULL) { if (devices == NULL) {
return false; return false;
} }
@ -274,7 +289,7 @@ JoystickState JoystickImpl::Update()
Location selfLoc = myLocationIDs[myIndex]; Location selfLoc = myLocationIDs[myIndex];
// Get all devices // Get all devices
CFSetRef devices = HIDInputManager::GetInstance().CopyJoystickDevices(); CFSetRef devices = HIDJoystickManager::GetInstance().CopyJoysticks();
if (devices == NULL) { if (devices == NULL) {
return disconnectedState; return disconnectedState;
} }

View File

@ -32,20 +32,6 @@
#import <SFML/Window/OSX/AutoreleasePoolWrapper.h> #import <SFML/Window/OSX/AutoreleasePoolWrapper.h>
/*
* DISCUSSION :
* ============
*
* [1] (2010_07)
* should AA-related NSOpenGLPixelFormatAttribute not be in the array
* if AA is not enable (settings.AntialiasingLevel == 0) ?
* => will not be present in attributs array if 0.
*
* [2] (2010_07)
* how many buffer should be used for AA ?
* => «1» was choosen.
*/
namespace sf namespace sf
{ {
namespace priv namespace priv
@ -172,14 +158,31 @@ void SFContext::CreateContext(SFContext* shared,
attrs.push_back(NSOpenGLPFAStencilSize); attrs.push_back(NSOpenGLPFAStencilSize);
attrs.push_back((NSOpenGLPixelFormatAttribute)settings.StencilBits); attrs.push_back((NSOpenGLPixelFormatAttribute)settings.StencilBits);
if (settings.AntialiasingLevel > 0) { // [1] if (settings.AntialiasingLevel > 0) {
/*
* Antialiasing techniques are described in the
* "OpenGL Programming Guide for Mac OS X" document.
*
* According to this document, the specification currently allows
* only one multisample buffer.
*
* The document also states that software renderers should be avoided
* because antialisaing techniques are very slow with them.
*/
// Prefer multisampling over supersampling
attrs.push_back(NSOpenGLPFAMultisample); attrs.push_back(NSOpenGLPFAMultisample);
// Only one buffer is currently available.
attrs.push_back(NSOpenGLPFASampleBuffers); attrs.push_back(NSOpenGLPFASampleBuffers);
attrs.push_back((NSOpenGLPixelFormatAttribute)1); // [2] attrs.push_back((NSOpenGLPixelFormatAttribute)1);
// Antialiasing level
attrs.push_back(NSOpenGLPFASamples); attrs.push_back(NSOpenGLPFASamples);
attrs.push_back((NSOpenGLPixelFormatAttribute)settings.AntialiasingLevel); attrs.push_back((NSOpenGLPixelFormatAttribute)settings.AntialiasingLevel);
// No software renderer
attrs.push_back(NSOpenGLPFANoRecovery);
} }
attrs.push_back((NSOpenGLPixelFormatAttribute)0); // end of array attrs.push_back((NSOpenGLPixelFormatAttribute)0); // end of array

View File

@ -521,7 +521,7 @@ while (window.IsOpened())
window.Close(); window.Close();
// Escape pressed : exit // Escape pressed : exit
if (event.Type == sf::Event::KeyPressed &amp;&amp; event.Key.Code == sf::Key::Escape) if (event.Type == sf::Event::KeyPressed &amp;&amp; event.Key.Code == sf::Keyboard::Escape)
window.Close(); window.Close();
} }
</string> </string>

View File

@ -84,6 +84,12 @@ subject to the following restrictions:
<dict> <dict>
<key>WRAPPER_EXTENSION</key> <key>WRAPPER_EXTENSION</key>
<string>app</string> <string>app</string>
<key>GCC_PRECOMPILE_PREFIX_HEADER</key>
<string>NO</string>
<key>GCC_PREFIX_HEADER</key>
<string></string>
</dict> </dict>
<key>Configurations</key> <key>Configurations</key>
<dict> <dict>

View File

@ -506,7 +506,7 @@ while (window.IsOpened())
window.Close(); window.Close();
// Escape pressed : exit // Escape pressed : exit
if (event.Type == sf::Event::KeyPressed &amp;&amp; event.Key.Code == sf::Key::Escape) if (event.Type == sf::Event::KeyPressed &amp;&amp; event.Key.Code == sf::Keyboard::Escape)
window.Close(); window.Close();
} }
</string> </string>