Commit 1d5082d815c2b0508f3dc657d87b9bdf418d444e

David Ludwig 2013-08-28T16:51:07

WinRT: corrected SDL_MOUSE* coordinates in non-Portrait modes Thanks to Pierre-Yves Gueniffey for proper pointer geometry transform code!

diff --git a/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj b/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj
index 9ed2b0c..1db16b0 100644
--- a/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj
+++ b/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj
@@ -247,6 +247,7 @@
     <ClInclude Include="..\..\src\video\SDL_sysvideo.h" />
     <ClInclude Include="..\..\src\video\winrt\SDL_winrtevents_c.h" />
     <ClInclude Include="..\..\src\video\winrt\SDL_winrtmouse.h" />
+    <ClInclude Include="..\..\src\video\winrt\SDL_winrtvideo_cpp.h" />
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\src\atomic\SDL_atomic.c" />
diff --git a/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj.filters b/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj.filters
index 4f6a1b3..4c726d7 100644
--- a/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj.filters
+++ b/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj.filters
@@ -333,6 +333,9 @@
     <ClInclude Include="..\..\src\video\winrt\SDL_winrtmouse.h">
       <Filter>Source Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\video\winrt\SDL_winrtvideo_cpp.h">
+      <Filter>Source Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\src\atomic\SDL_atomic.c">
diff --git a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
index d662e69..9728601 100644
--- a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
+++ b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
@@ -294,6 +294,7 @@
     <ClInclude Include="..\..\src\video\SDL_sysvideo.h" />
     <ClInclude Include="..\..\src\video\winrt\SDL_winrtevents_c.h" />
     <ClInclude Include="..\..\src\video\winrt\SDL_winrtmouse.h" />
+    <ClInclude Include="..\..\src\video\winrt\SDL_winrtvideo_cpp.h" />
   </ItemGroup>
   <ItemGroup>
     <FxCompile Include="..\..\src\render\direct3d11\SDL_D3D11_PixelShader_FixedColor.hlsl">
diff --git a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
index 4d4fee5..847d900 100644
--- a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
+++ b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
@@ -599,6 +599,9 @@
     <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp.h">
       <Filter>Source Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\video\winrt\SDL_winrtvideo_cpp.h">
+      <Filter>Source Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Filter Include="Header Files">
diff --git a/src/video/winrt/SDL_winrtmouse.cpp b/src/video/winrt/SDL_winrtmouse.cpp
index 40530bf..fb6f5af 100644
--- a/src/video/winrt/SDL_winrtmouse.cpp
+++ b/src/video/winrt/SDL_winrtmouse.cpp
@@ -42,6 +42,7 @@ extern "C" {
 }
 
 #include "../../core/winrt/SDL_winrtapp.h"
+#include "SDL_winrtvideo_cpp.h"
 #include "SDL_winrtmouse.h"
 
 
@@ -163,13 +164,54 @@ WINRT_QuitMouse(_THIS)
 Windows::Foundation::Point
 WINRT_TransformCursorPosition(SDL_Window * window, Windows::Foundation::Point rawPosition)
 {
+    using namespace Windows::Graphics::Display;
+
     if (!window) {
         return rawPosition;
     }
-    CoreWindow ^ nativeWindow = CoreWindow::GetForCurrentThread();
+
+    SDL_WindowData * windowData = (SDL_WindowData *) window->driverdata;
+    if (windowData->coreWindow == nullptr) {
+        // For some reason, the window isn't associated with a CoreWindow.
+        // This might end up being the case as XAML support is extended.
+        // For now, if there's no CoreWindow attached to the SDL_Window,
+        // don't do any transforms.
+        return rawPosition;
+    }
+
+    // The CoreWindow can only be accessed on certain thread(s).
+    SDL_assert(CoreWindow::GetForCurrentThread() != nullptr);
+
+    CoreWindow ^ nativeWindow = windowData->coreWindow.Get();
     Windows::Foundation::Point outputPosition;
+
+#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
     outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
     outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+#else
+    switch (DisplayProperties::CurrentOrientation)
+    {
+        case DisplayOrientations::Portrait:
+            outputPosition.X = rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
+            outputPosition.Y = rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+            break;
+        case DisplayOrientations::PortraitFlipped:
+            outputPosition.X = (float32)window->w - rawPosition.X * (((float32)window->w) / nativeWindow->Bounds.Width);
+            outputPosition.Y = (float32)window->h - rawPosition.Y * (((float32)window->h) / nativeWindow->Bounds.Height);
+            break;
+        case DisplayOrientations::Landscape:
+            outputPosition.X = rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
+            outputPosition.Y = (float32)window->h - rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
+            break;
+        case DisplayOrientations::LandscapeFlipped:
+            outputPosition.X = (float32)window->w - rawPosition.Y * (((float32)window->w) / nativeWindow->Bounds.Height);
+            outputPosition.Y = rawPosition.X * (((float32)window->h) / nativeWindow->Bounds.Width);
+            break;
+        default:
+            break;
+    }
+#endif
+
     return outputPosition;
 }
 
diff --git a/src/video/winrt/SDL_winrtvideo.cpp b/src/video/winrt/SDL_winrtvideo.cpp
index 940798b..621b2ed 100644
--- a/src/video/winrt/SDL_winrtvideo.cpp
+++ b/src/video/winrt/SDL_winrtvideo.cpp
@@ -45,6 +45,7 @@ extern "C" {
 }
 
 #include "../../core/winrt/SDL_winrtapp.h"
+#include "SDL_winrtvideo_cpp.h"
 #include "SDL_winrtevents_c.h"
 #include "SDL_winrtmouse.h"
 #include "SDL_main.h"
@@ -67,14 +68,6 @@ static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
 static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
 
 
-/* Internal window data */
-struct SDL_WindowData
-{
-    SDL_Window *sdlWindow;
-    Platform::Agile<Windows::UI::Core::CoreWindow> coreWindow;
-};
-
-
 /* The global, WinRT, SDL Window.
    For now, SDL/WinRT only supports one window (due to platform limitations of
    WinRT.
diff --git a/src/video/winrt/SDL_winrtvideo_cpp.h b/src/video/winrt/SDL_winrtvideo_cpp.h
new file mode 100644
index 0000000..eb4d788
--- /dev/null
+++ b/src/video/winrt/SDL_winrtvideo_cpp.h
@@ -0,0 +1,41 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
+
+  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.
+*/
+
+/* Windows includes: */
+#include <windows.h>
+#ifdef __cplusplus_winrt
+#include <agile.h>
+#endif
+
+/* SDL includes: */
+#include "SDL_events.h"
+
+
+#ifdef __cplusplus_winrt
+
+/* Internal window data */
+struct SDL_WindowData
+{
+    SDL_Window *sdlWindow;
+    Platform::Agile<Windows::UI::Core::CoreWindow> coreWindow;
+};
+
+#endif // ifdef __cplusplus_winrt