WinRT: provided access, via SDL_GetWindowWMInfo, to SDL's WinRT CoreWindow
authorDavid Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 14:35:06 -0500
changeset 8411 aca0591bc58f
parent 8410 ba725b3a0c87
child 8412 59f1191a5cca
WinRT: provided access, via SDL_GetWindowWMInfo, to SDL's WinRT CoreWindow
VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj
VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
include/SDL_syswm.h
src/video/windowsrt/SDL_winrtvideo.cpp
src/video/windowsrt/SDL_winrtvideo.h
--- a/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj	Sat Feb 09 11:42:17 2013 -0500
+++ b/VisualC-WinPhone/SDL/SDL_VS2012-WinPhone.vcxproj	Sat Feb 09 14:35:06 2013 -0500
@@ -299,7 +299,12 @@
     <ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
     <ClCompile Include="..\..\src\joystick\SDL_joystick.c" />
     <ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c" />
-    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.cpp" />
+    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.cpp">
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
+    </ClCompile>
     <ClCompile Include="..\..\src\render\SDL_render.c" />
     <ClCompile Include="..\..\src\render\SDL_yuv_mmx.c" />
     <ClCompile Include="..\..\src\render\SDL_yuv_sw.c" />
--- a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj	Sat Feb 09 11:42:17 2013 -0500
+++ b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj	Sat Feb 09 14:35:06 2013 -0500
@@ -78,7 +78,14 @@
     <ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
     <ClCompile Include="..\..\src\joystick\SDL_joystick.c" />
     <ClCompile Include="..\..\src\loadso\windows\SDL_sysloadso.c" />
-    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.cpp" />
+    <ClCompile Include="..\..\src\render\direct3d11\SDL_render_d3d11.cpp">
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
+      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
+    </ClCompile>
     <ClCompile Include="..\..\src\render\SDL_render.c" />
     <ClCompile Include="..\..\src\render\SDL_yuv_mmx.c" />
     <ClCompile Include="..\..\src\render\SDL_yuv_sw.c" />
--- a/include/SDL_syswm.h	Sat Feb 09 11:42:17 2013 -0500
+++ b/include/SDL_syswm.h	Sat Feb 09 14:35:06 2013 -0500
@@ -103,6 +103,7 @@
 {
     SDL_SYSWM_UNKNOWN,
     SDL_SYSWM_WINDOWS,
+    SDL_SYSWM_WINDOWSRT,
     SDL_SYSWM_X11,
     SDL_SYSWM_DIRECTFB,
     SDL_SYSWM_COCOA,
@@ -171,6 +172,12 @@
             HWND window;                /**< The window handle */
         } win;
 #endif
+#if defined(SDL_VIDEO_DRIVER_WINRT)
+        struct
+        {
+            void * window;          /**< The Windows RT CoreWindow, casted from 'CoreWindow ^*' to 'void *' */
+        } winrt;
+#endif
 #if defined(SDL_VIDEO_DRIVER_X11)
         struct
         {
--- a/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 11:42:17 2013 -0500
+++ b/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 14:35:06 2013 -0500
@@ -35,6 +35,7 @@
 #include "../SDL_pixels_c.h"
 #include "../../events/SDL_events_c.h"
 #include "../../render/SDL_sysrender.h"
+#include "SDL_syswm.h"
 }
 
 #include "SDL_WinRTApp.h"
@@ -61,6 +62,7 @@
 /* Window functions */
 static int WINRT_CreateWindow(_THIS, SDL_Window * window);
 static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
+static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
 
 /* WinRT driver bootstrap functions */
 
@@ -101,6 +103,7 @@
     device->CreateWindowFramebuffer = SDL_WINRT_CreateWindowFramebuffer;
     device->UpdateWindowFramebuffer = SDL_WINRT_UpdateWindowFramebuffer;
     device->DestroyWindowFramebuffer = SDL_WINRT_DestroyWindowFramebuffer;
+    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
 
     device->free = WINRT_DeleteDevice;
 
@@ -159,14 +162,13 @@
         return -1;
     }
 
-    SDL_WindowData *data;
-    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
+    SDL_WindowData *data = new SDL_WindowData;
     if (!data) {
         SDL_OutOfMemory();
         return -1;
     }
-    SDL_zerop(data);
     data->sdlWindow = window;
+    data->coreWindow = new CoreWindow^(CoreWindow::GetForCurrentThread());
 
     /* Make sure the window is considered to be positioned at {0,0},
        and is considered fullscreen, shown, and the like.
@@ -222,6 +224,11 @@
     // resources first.
     //
     // TODO, WinRT: either make WINRT_CreateWindow not call SDL_CreateRenderer, or have it do error checking if it does call it
+
+    // HACK: make sure the SDL window references SDL_WindowData data now, in
+    // order to allow the SDL_Renderer to be created in WINRT_CreateWindow
+    window->driverdata = data;
+
     SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
     SDL_WinRTGlobalApp->m_renderer->m_sdlRenderer = renderer;
     SDL_WinRTGlobalApp->m_renderer->m_sdlRendererData = (D3D11_RenderData *) renderer->driverdata;
@@ -234,6 +241,21 @@
 void
 WINRT_DestroyWindow(_THIS, SDL_Window * window)
 {
+    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
+
+    if (data) {
+        // Delete the reference to the WinRT CoreWindow:
+        CoreWindow ^* windowPointer = ((SDL_WindowData *) window->driverdata)->coreWindow;
+        if (windowPointer) {
+            *windowPointer = nullptr;   // Clear the C++/CX reference to the CoreWindow
+            delete windowPointer;       // Delete the C++/CX reference itself
+        }
+
+        // Delete the internal window data:
+        delete data;
+        data = NULL;
+    }
+
     if (SDL_WinRTGlobalApp->HasSDLWindowData() &&
         SDL_WinRTGlobalApp->GetSDLWindowData()->sdlWindow == window)
     {
@@ -241,6 +263,23 @@
     }
 }
 
+SDL_bool
+WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
+{
+    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
+    CoreWindow ^* windowPointer = data->coreWindow;
+
+    if (info->version.major <= SDL_MAJOR_VERSION) {
+        info->subsystem = SDL_SYSWM_WINDOWSRT;
+        info->info.winrt.window = windowPointer;
+        return SDL_TRUE;
+    } else {
+        SDL_SetError("Application not compiled with SDL %d.%d\n",
+                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
+        return SDL_FALSE;
+    }
+    return SDL_FALSE;
+}
 
 #endif /* SDL_VIDEO_DRIVER_WINRT */
 
--- a/src/video/windowsrt/SDL_winrtvideo.h	Sat Feb 09 11:42:17 2013 -0500
+++ b/src/video/windowsrt/SDL_winrtvideo.h	Sat Feb 09 14:35:06 2013 -0500
@@ -23,19 +23,14 @@
 #ifndef _SDL_winrtvideo_h
 #define _SDL_winrtvideo_h
 
-#ifdef __cplusplus
 extern "C" {
-#endif
-
 #include "../SDL_sysvideo.h"
-
-#ifdef __cplusplus
 }
-#endif
 
 struct SDL_WindowData
 {
     SDL_Window *sdlWindow;
+    Windows::UI::Core::CoreWindow ^* coreWindow;
 };
 
 #endif /* _SDL_winrtvideo_h */