Просмотр исходного кода

Adding "preferPhysical" parameter to Game::getGamepad().
Using this to simplify the tests that use gamepads.
Fixing some issues related to adding and removing gamepads.
Allowing for multiple virtual gamepads within a game's configuration.

Adam Blake 13 лет назад
Родитель
Сommit
bd425196ae

+ 1 - 1
gameplay/gameplay.vcxproj

@@ -922,7 +922,7 @@
       </PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=0;WIN32;_DEBUG;_LIB;GAMEPLAY_MEM_LEAK_DETECTION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>USE_XINPUT;_ITERATOR_DEBUG_LEVEL=0;WIN32;_DEBUG;_LIB;GAMEPLAY_MEM_LEAK_DETECTION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(ProjectDir)src;..\external-deps\lua\include;..\external-deps\bullet\include;..\external-deps\openal\include\AL;..\external-deps\alut\include\AL;..\external-deps\oggvorbis\include;..\external-deps\glew\include;..\external-deps\libpng\include;..\external-deps\zlib\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
       <RuntimeTypeInfo>true</RuntimeTypeInfo>
       <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>

+ 18 - 7
gameplay/src/Game.cpp

@@ -186,7 +186,7 @@ void Game::shutdown()
         unsigned int gamepadCount = Gamepad::getGamepadCount();
         for (unsigned int i = 0; i < gamepadCount; i++)
         {
-            Gamepad* gamepad = Gamepad::getGamepad(i);
+            Gamepad* gamepad = Gamepad::getGamepad(i, false);
             SAFE_DELETE(gamepad);
         }
 
@@ -576,13 +576,24 @@ void Game::loadGamepads()
     {
         // Check if there are any virtual gamepads included in the .config file.
         // If there are, create and initialize them.
-        Properties* gamepadProperties = _properties->getNamespace("gamepads", true);
-        if (gamepadProperties && gamepadProperties->exists("form"))
+        Properties* inner = _properties->getNextNamespace();
+        while (inner != NULL)
         {
-            const char* gamepadFormPath = gamepadProperties->getString("form");
-            GP_ASSERT(gamepadFormPath);
-            Gamepad* gamepad = Gamepad::add(gamepadFormPath);
-            GP_ASSERT(gamepad);
+            std::string spaceName(inner->getNamespace());
+            // This namespace was accidentally named "gamepads" originally but we'll keep this check
+            // for backwards compatibility.
+            if (spaceName == "gamepads" || spaceName == "gamepad")
+            {
+                if (inner->exists("form"))
+                {
+                    const char* gamepadFormPath = inner->getString("form");
+                    GP_ASSERT(gamepadFormPath);
+                    Gamepad* gamepad = Gamepad::add(gamepadFormPath);
+                    GP_ASSERT(gamepad);
+                }
+            }
+
+            inner = _properties->getNextNamespace();
         }
     }
 }

+ 3 - 3
gameplay/src/Game.h

@@ -450,14 +450,14 @@ public:
     /**
      * Gets the gamepad at the specified index. 
      *
-     * The gamepad index can change when connnected and disconnected so you
-     * cannot relie on this other than iterating through them all to display
+     * The gamepad index can change when connected and disconnected so you
+     * cannot rely on this other than iterating through them all to display
      * them or poll them.
      *
      * @param index The index of the gamepad to retrieve.
      * @return The gamepad at the specified index.
      */
-    inline Gamepad* getGamepad(unsigned int index) const;
+    inline Gamepad* getGamepad(unsigned int index, bool preferPhysical = true) const;
 
     /**
      * Sets multi-touch is to be enabled/disabled. Default is disabled.

+ 2 - 2
gameplay/src/Game.inl

@@ -121,9 +121,9 @@ inline unsigned int Game::getGamepadCount() const
     return Gamepad::getGamepadCount();
 }
 
-inline Gamepad* Game::getGamepad(unsigned int index) const
+inline Gamepad* Game::getGamepad(unsigned int index, bool preferPhysical) const
 {
-    return Gamepad::getGamepad(index);
+    return Gamepad::getGamepad(index, preferPhysical);
 }
 
 inline void Game::displayKeyboard(bool display)

+ 32 - 5
gameplay/src/Gamepad.cpp

@@ -9,7 +9,7 @@ namespace gameplay
 static std::vector<Gamepad*> __gamepads;
 
 Gamepad::Gamepad(const char* formPath)
-    : _handle(0), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL), _buttons(0)
+    : _handle(INT_MAX), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL), _buttons(0)
 {
     GP_ASSERT(formPath);
     _form = Form::create(formPath);
@@ -74,9 +74,9 @@ void Gamepad::remove(GamepadHandle handle)
         Gamepad* gamepad = *it;
         if (gamepad->_handle == handle)
         {
+            it = __gamepads.erase(it);
             Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, gamepad);
             SAFE_DELETE(gamepad);
-            it = __gamepads.erase(it);
         }
         else
         {
@@ -93,9 +93,9 @@ void Gamepad::remove(Gamepad* gamepad)
         Gamepad* g = *it;
         if (g == gamepad)
         {
+            it = __gamepads.erase(it);
             Game::getInstance()->gamepadEvent(DISCONNECTED_EVENT, g);
             SAFE_DELETE(gamepad);
-            it = __gamepads.erase(it);
         }
         else
         {
@@ -138,9 +138,36 @@ unsigned int Gamepad::getGamepadCount()
     return __gamepads.size();
 }
 
-Gamepad* Gamepad::getGamepad(unsigned int index)
+Gamepad* Gamepad::getGamepad(unsigned int index, bool preferPhysical)
 {
-    return __gamepads[index];
+    unsigned int count = __gamepads.size();
+    if (index >= count)
+        return NULL;
+
+    if (!preferPhysical)
+        return __gamepads[index];
+
+    // Virtual gamepads are guaranteed to come before physical gamepads in the vector.
+    Gamepad* backupVirtual = NULL;
+    if (index < count && __gamepads[index]->isVirtual())
+    {
+        backupVirtual = __gamepads[index];
+    }
+
+    for (unsigned int i = 0; i < count; ++i)
+    {
+        if (!__gamepads[i]->isVirtual())
+        {
+            // __gamepads[i] is the first physical gamepad 
+            // and should be returned from getGamepad(0, true).
+            if (index + i < count)
+            {
+                return __gamepads[index + i];
+            }
+        }
+    }
+
+    return backupVirtual;
 }
 
 Gamepad::ButtonMapping Gamepad::getButtonMappingFromString(const char* string)

+ 1 - 1
gameplay/src/Gamepad.h

@@ -209,7 +209,7 @@ private:
 
     static unsigned int getGamepadCount();
 
-    static Gamepad* getGamepad(unsigned int index);
+    static Gamepad* getGamepad(unsigned int index, bool preferPhysical = true);
 
     static ButtonMapping getButtonMappingFromString(const char* string);
 

+ 1 - 1
gameplay/src/PlatformWindows.cpp

@@ -1235,8 +1235,8 @@ void Platform::pollGamepadState(Gamepad* gamepad)
     else
     {
         // Gamepad was disconnected.
-        Platform::gamepadEventDisconnectedInternal(gamepad->_handle);
         __connectedXInput[gamepad->_handle] = false;
+        Platform::gamepadEventDisconnectedInternal(gamepad->_handle);
     }
 }
 #else