Explorar o código

Remove simulated accelerometer on desktop. Use keys in longboard sample. This is part 2 of #835.

Ken Whatmough %!s(int64=12) %!d(string=hai) anos
pai
achega
12a6492597

+ 7 - 2
gameplay/src/Game.h

@@ -510,6 +510,11 @@ public:
      */
     inline bool canExit() const;
 
+    /**
+     * Whether this game has accelerometer support.
+     */
+    inline bool hasAccelerometer() const;
+
     /**
      * Gets the current accelerometer values for use as an indication of device
      * orientation. Despite its name, implementations are at liberty to combine
@@ -517,8 +522,8 @@ public:
      * This method is best used to obtain an indication of device orientation; it
      * does not necessarily distinguish between acceleration and rotation rate.
      *
-     * @param pitch The pitch angle returned (in degrees). If NULL then not returned.
-     * @param roll The roll angle returned (in degrees). If NULL then not returned.
+     * @param pitch The pitch angle returned (in degrees). Zero if hasAccelerometer() returns false.
+     * @param roll The roll angle returned (in degrees). Zero if hasAccelerometer() returns false.
      */
     inline void getAccelerometerValues(float* pitch, float* roll);
 

+ 5 - 0
gameplay/src/Game.inl

@@ -121,6 +121,11 @@ inline bool Game::canExit() const
     return Platform::canExit();
 }
 
+inline bool Game::hasAccelerometer() const
+{
+	return Platform::hasAccelerometer();
+}
+
 inline void Game::getAccelerometerValues(float* pitch, float* roll)
 {
     Platform::getAccelerometerValues(pitch, roll);

+ 7 - 2
gameplay/src/Platform.h

@@ -200,6 +200,11 @@ private:
      */
     static bool isCursorVisible();
 
+    /**
+     * Whether the platform has accelerometer support.
+     */
+    static bool hasAccelerometer();
+
     /**
      * Gets the platform accelerometer values for use as an indication of device
      * orientation. Despite its name, implementations are at liberty to combine
@@ -207,8 +212,8 @@ private:
      * This method is best used to obtain an indication of device orientation; it
      * does not necessarily distinguish between acceleration and rotation rate.
      * 
-     * @param pitch The accelerometer pitch.
-     * @param roll The accelerometer roll.
+     * @param pitch The accelerometer pitch. Zero if hasAccelerometer() returns false.
+     * @param roll The accelerometer roll. Zero if hasAccelerometer() returns false.
      */
     static void getAccelerometerValues(float* pitch, float* roll);
 

+ 5 - 0
gameplay/src/PlatformAndroid.cpp

@@ -1177,6 +1177,11 @@ bool Platform::isMultiTouch()
     return __multiTouch;
 }
 
+bool Platform::hasAccelerometer()
+{
+    return true;
+}
+
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     double tx, ty, tz;

+ 5 - 0
gameplay/src/PlatformBlackBerry.cpp

@@ -1435,6 +1435,11 @@ bool Platform::isMultiTouch()
     return __multiTouch;
 }
 
+bool Platform::hasAccelerometer()
+{
+    return true;
+}
+
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     GP_ASSERT(pitch);

+ 7 - 22
gameplay/src/PlatformLinux.cpp

@@ -80,8 +80,6 @@ struct timespec __timespec;
 static double __timeStart;
 static double __timeAbsolute;
 static bool __vsync = WINDOW_VSYNC;
-static float __pitch;
-static float __roll;
 static bool __mouseCaptured = false;
 static float __mouseCapturePointX = 0;
 static float __mouseCapturePointY = 0;
@@ -1113,10 +1111,6 @@ namespace gameplay
 
         updateWindowSize();
 
-        static const float ACCELEROMETER_X_FACTOR = 90.0f / __windowSize[0];
-        static const float ACCELEROMETER_Y_FACTOR = 90.0f / __windowSize[1];
-        static int lx = 0;
-        static int ly = 0;
         static bool shiftDown = false;
         static bool capsOn = false;
         static XEvent evt;
@@ -1300,20 +1294,6 @@ namespace gameplay
                                 {
                                     gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0, true);
                                 }
-                                else if (evt.xmotion.state & Button3Mask)
-                                {
-                                    // Update the pitch and roll by adding the scaled deltas.
-                                    __roll += (float)(x - lx) * ACCELEROMETER_X_FACTOR;
-                                    __pitch += -(float)(y - ly) * ACCELEROMETER_Y_FACTOR;
-
-                                    // Clamp the values to the valid range.
-                                    __roll = max(min(__roll, 90.0f), -90.0f);
-                                    __pitch = max(min(__pitch, 90.0f), -90.0f);
-
-                                    // Update the last X/Y values.
-                                    lx = x;
-                                    ly = y;
-                                }
                             }
                         }
                         break;
@@ -1428,13 +1408,18 @@ namespace gameplay
         false;
     }
 
+    bool Platform::hasAccelerometer()
+    {
+        return false;
+    }
+
     void Platform::getAccelerometerValues(float* pitch, float* roll)
     {
         GP_ASSERT(pitch);
         GP_ASSERT(roll);
 
-        *pitch = __pitch;
-        *roll = __roll;
+        *pitch = 0;
+        *roll = 0;
     }
 
     void Platform::getArguments(int* argc, char*** argv)

+ 7 - 29
gameplay/src/PlatformMacOSX.mm

@@ -37,16 +37,9 @@ char** __argv = 0;
 static int __width = 1280;
 static int __height = 720;
 
-static float ACCELEROMETER_FACTOR_X = 90.0f / __width;
-static float ACCELEROMETER_FACTOR_Y = 90.0f / __height;
-
 static double __timeStart;
 static double __timeAbsolute;
 static bool __vsync = WINDOW_VSYNC;
-static float __pitch;
-static float __roll;
-static int __lx;
-static int __ly;
 static bool __hasMouse = false;
 static bool __leftMouseDown = false;
 static bool __rightMouseDown = false;
@@ -1056,8 +1049,6 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
 {
     __rightMouseDown = true;
      NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
-    __lx = point.x;
-    __ly = __height - point.y;
     
     [__view->gameLock lock];
     gameplay::Platform::mouseEventInternal(Mouse::MOUSE_PRESS_RIGHT_BUTTON, point.x, __height - point.y, 0);
@@ -1077,20 +1068,6 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
 - (void) rightMouseDragged: (NSEvent*) event
 {
     NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
-    if (__rightMouseDown)
-    {
-        // Update the pitch and roll by adding the scaled deltas.
-        __roll += (float)(point.x - __lx) * ACCELEROMETER_FACTOR_X;
-        __pitch -= -(float)(point.y - (__height - __ly)) * ACCELEROMETER_FACTOR_Y;
-    
-        // Clamp the values to the valid range.
-        __roll = max(min(__roll, 90.0f), -90.0f);
-        __pitch = max(min(__pitch, 90.0f), -90.0f);
-    
-        // Update the last X/Y values.
-        __lx = point.x;
-        __ly = (__height - point.y);
-    }
     
     // In right-mouse case, whether __rightMouseDown is true or false
     // this should not matter, mouse move is still occuring
@@ -1691,10 +1668,6 @@ int Platform::enterMessagePump()
         }
     }
 
-    // Set the scale factors for the mouse movement used to simulate the accelerometer.
-    ACCELEROMETER_FACTOR_X = 90.0f / __width;
-    ACCELEROMETER_FACTOR_Y = 90.0f / __height;
-
     NSAutoreleasePool* pool = [NSAutoreleasePool new];
     NSApplication* app = [NSApplication sharedApplication];
     NSRect screenBounds = [[NSScreen mainScreen] frame];
@@ -1825,14 +1798,19 @@ bool Platform::isMultiTouch()
 {
     return true;
 }
+
+bool Platform::hasAccelerometer()
+{
+    return false;
+}
     
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     GP_ASSERT(pitch);
     GP_ASSERT(roll);
 
-    *pitch = __pitch;
-    *roll = __roll;
+    *pitch = 0;
+    *roll = 0;
 }
 
 void Platform::getArguments(int* argc, char*** argv)

+ 8 - 33
gameplay/src/PlatformWindows.cpp

@@ -27,8 +27,6 @@ static double __timeTicksPerMillis;
 static double __timeStart;
 static double __timeAbsolute;
 static bool __vsync = WINDOW_VSYNC;
-static float __roll;
-static float __pitch;
 static HINSTANCE __hinstance = 0;
 static HWND __attachToWindow = 0;
 static HWND __hwnd = 0;
@@ -341,8 +339,6 @@ LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
         return DefWindowProc(hwnd, msg, wParam, lParam);
     }
 
-    static int lx, ly;
-
     static bool shiftDown = false;
     static bool capsOn = false;
 
@@ -383,9 +379,7 @@ LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
     }
     case WM_RBUTTONDOWN:
         UpdateCapture(wParam);
-        lx = GET_X_LPARAM(lParam);
-        ly = GET_Y_LPARAM(lParam);
-        gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON, lx, ly, 0);
+        gameplay::Platform::mouseEventInternal(gameplay::Mouse::MOUSE_PRESS_RIGHT_BUTTON, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0);
         break;
 
     case WM_RBUTTONUP:
@@ -432,26 +426,6 @@ LRESULT CALLBACK __WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
                 gameplay::Platform::touchEventInternal(gameplay::Touch::TOUCH_MOVE, x, y, 0, true);
                 return 0;
             }
-            else if ((wParam & MK_RBUTTON) == MK_RBUTTON)
-            {
-                // Scale factors for the mouse movement used to simulate the accelerometer.
-                RECT clientRect;
-                GetClientRect(__hwnd, &clientRect);
-                const float xFactor = 90.0f / clientRect.right;
-                const float yFactor = 90.0f / clientRect.bottom;
-
-                // Update the pitch and roll by adding the scaled deltas.
-                __roll += (float)(x - lx) * xFactor;
-                __pitch += -(float)(y - ly) * yFactor;
-
-                // Clamp the values to the valid range.
-                __roll = max(min(__roll, 90.0f), -90.0f);
-                __pitch = max(min(__pitch, 90.0f), -90.0f);
-
-                // Update the last X/Y values.
-                lx = x;
-                ly = y;
-            }
         }
         break;
     }
@@ -978,10 +952,6 @@ int Platform::enterMessagePump()
     GP_ASSERT(__timeTicksPerMillis);
     __timeStart = queryTime.QuadPart / __timeTicksPerMillis;
 
-    // Set the initial pitch and roll values.
-    __pitch = 0.0;
-    __roll = 0.0;
-
     SwapBuffers(__hdc);
 
     if (_game->getState() != Game::RUNNING)
@@ -1131,13 +1101,18 @@ bool Platform::isMultiTouch()
     return false;
 }
 
+bool Platform::hasAccelerometer()
+{
+    return false;
+}
+
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     GP_ASSERT(pitch);
     GP_ASSERT(roll);
 
-    *pitch = __pitch;
-    *roll = __roll;
+    *pitch = 0;
+    *roll = 0;
 }
 
 void Platform::getArguments(int* argc, char*** argv)

+ 5 - 0
gameplay/src/PlatformiOS.mm

@@ -1380,6 +1380,11 @@ void Platform::sleep(long ms)
     usleep(ms * 1000);
 }
 
+bool Platform::hasAccelerometer()
+{
+    return true;
+}
+
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     [__appDelegate getAccelerometerPitch:pitch roll:roll];

+ 3 - 3
samples/browser/src/InputSample.cpp

@@ -32,11 +32,11 @@ void InputSample::initialize()
     _crosshairUpperLimit += _crosshairLowerLimit;
 
     // Create input sample controls
-    _keyboardState = false;
+    _keyboardState = false;
     _inputSampleControls = Form::create("res/common/inputs.form");
     static_cast<Button*>(_inputSampleControls->getControl("showKeyboardButton"))->addListener(this, Listener::CLICK);
     static_cast<Button*>(_inputSampleControls->getControl("captureMouseButton"))->addListener(this, Listener::CLICK);
-
+
     if (!hasMouse())
     {
         static_cast<Button*>(_inputSampleControls->getControl("captureMouseButton"))->setVisible(false);
@@ -155,7 +155,7 @@ void InputSample::render(float elapsedTime)
         accelerometerDrawRate = 0.0f;
         getAccelerometerValues(&pitch, &roll);
     }
-    if (!_keyboardState)
+    if (hasAccelerometer() && !_keyboardState)
     {
         sprintf(buffer, "Pitch: %f   Roll: %f", pitch, roll);
         _font->measureText(buffer, _font->getSize(), &width, &height);

+ 5 - 0
samples/browser/src/Sample.cpp

@@ -175,6 +175,11 @@ bool Sample::isMultiTouch() const
     return Game::getInstance()->isMultiTouch();
 }
 
+bool Sample::hasAccelerometer() const
+{
+    return Game::getInstance()->hasAccelerometer();
+}
+
 void Sample::getAccelerometerValues(float* pitch, float* roll)
 {
     Game::getInstance()->getAccelerometerValues(pitch, roll);

+ 1 - 0
samples/browser/src/Sample.h

@@ -59,6 +59,7 @@ public:
     void setMouseCaptured(bool captured);
     void setMultiTouch(bool enabled);
     bool isMultiTouch() const;
+    bool hasAccelerometer() const;
     void getAccelerometerValues(float* pitch, float* roll);
     void schedule(long timeOffset, TimeListener* timeListener, void* cookie = 0);
 	void enableScriptCamera(bool enable);

+ 87 - 1
samples/longboard/src/LongboardGame.cpp

@@ -14,6 +14,12 @@ LongboardGame game;
 #define VELOCITY_MIN        0.2f
 #define VELOCITY_MIN_MS     (VELOCITY_MIN / 1000.0f)
 
+// Keyboard input bit-flags (powers of 2)
+#define ACCELERATE (1 << 0)
+#define BRAKE (1 << 1)
+#define STEER_LEFT (1 << 2)
+#define STEER_RIGHT (1 << 3)
+
 // Accelerometer pitch control
 #define PITCH_MIN            20.0f
 #define PITCH_MAX            70.0f
@@ -25,7 +31,8 @@ LongboardGame game;
 #define ROLL_MAX             40.0f
 
 LongboardGame::LongboardGame() 
-    : _ground(NULL), _board(NULL), _wheels(NULL), _gradient(NULL), _stateBlock(NULL), _velocity(VELOCITY_MIN_MS)
+    : _ground(NULL), _board(NULL), _wheels(NULL), _gradient(NULL), _stateBlock(NULL),
+    _keyFlags(0), _simulatedPitch(0), _simulatedRoll(0), _velocity(VELOCITY_MIN_MS)
 {
 }
 
@@ -185,6 +192,39 @@ void LongboardGame::update(float elapsedTime)
     float pitch, roll;
     getAccelerometerValues(&pitch, &roll);
 
+    // On desktop platforms without accelerometers, use key inputs instead.
+    if (!Game::hasAccelerometer())
+    {
+        float pitchTarget = 0;
+        float rollTarget = 0;
+
+        pitch = _simulatedPitch;
+        roll = _simulatedRoll;
+
+        pitchTarget = -0.5f * (PITCH_MAX + PITCH_MIN);
+        if (_keyFlags & BRAKE)
+        {
+            pitchTarget = -PITCH_MAX;
+        }
+        else if (_keyFlags & ACCELERATE)
+        {
+            pitchTarget = -PITCH_MIN;
+        }
+
+        if (_keyFlags & STEER_RIGHT)
+        {
+            rollTarget += ROLL_MAX;
+        }
+
+        if (_keyFlags & STEER_LEFT)
+        {
+            rollTarget -= ROLL_MAX;
+        }
+
+        MathUtil::smooth(&_simulatedPitch, pitchTarget, elapsedTime, 600);
+        MathUtil::smooth(&_simulatedRoll, rollTarget, elapsedTime, 100);
+    }
+
     // Clamp angles
     pitch = max(min(-pitch, PITCH_MAX), PITCH_MIN);
     roll = max(min(roll, ROLL_MAX), -ROLL_MAX);
@@ -256,6 +296,52 @@ void LongboardGame::keyEvent(Keyboard::KeyEvent evt, int key)
         case Keyboard::KEY_ESCAPE:
             exit();
             break;
+        case Keyboard::KEY_A:
+        case Keyboard::KEY_CAPITAL_A:
+        case Keyboard::KEY_LEFT_ARROW:
+            _keyFlags |= STEER_LEFT;
+            break;
+        case Keyboard::KEY_D:
+        case Keyboard::KEY_CAPITAL_D:
+        case Keyboard::KEY_RIGHT_ARROW:
+            _keyFlags |= STEER_RIGHT;
+            break;
+        case Keyboard::KEY_W:
+        case Keyboard::KEY_CAPITAL_W:
+        case Keyboard::KEY_UP_ARROW:
+            _keyFlags |= ACCELERATE;
+            break;
+        case Keyboard::KEY_S:
+        case Keyboard::KEY_CAPITAL_S:
+        case Keyboard::KEY_DOWN_ARROW:
+            _keyFlags |= BRAKE;
+            break;
+        }
+    }
+    else if (evt == Keyboard::KEY_RELEASE)
+    {
+        switch (key)
+        {
+        case Keyboard::KEY_A:
+        case Keyboard::KEY_CAPITAL_A:
+        case Keyboard::KEY_LEFT_ARROW:
+            _keyFlags &= ~STEER_LEFT;
+            break;
+        case Keyboard::KEY_D:
+        case Keyboard::KEY_CAPITAL_D:
+        case Keyboard::KEY_RIGHT_ARROW:
+            _keyFlags &= ~STEER_RIGHT;
+            break;
+        case Keyboard::KEY_W:
+        case Keyboard::KEY_CAPITAL_W:
+        case Keyboard::KEY_UP_ARROW:
+            _keyFlags &= ~ACCELERATE;
+            break;
+        case Keyboard::KEY_S:
+        case Keyboard::KEY_CAPITAL_S:
+        case Keyboard::KEY_DOWN_ARROW:
+            _keyFlags &= ~BRAKE;
+            break;
         }
     }
 }

+ 3 - 1
samples/longboard/src/LongboardGame.h

@@ -107,7 +107,9 @@ private:
     Matrix _projectionMatrix;
     Matrix _viewProjectionMatrix;
 
-    // Physics
+    // Physics and keyboard controls
+    unsigned int _keyFlags;
+    float _simulatedPitch, _simulatedRoll;
     float _velocity;
     Vector3 _direction;
     Vector2 _groundUVTransform;