WinRT: made SDL's inner WinRT CoreWindow be accessible to non-C++/CX code, in theory
authorDavid Ludwig <dludwig@pobox.com>
Tue, 16 Apr 2013 23:40:03 -0400
changeset 846398dbf81ed5e9
parent 8462 8a0b2b577b83
child 8464 a2a909304cfe
WinRT: made SDL's inner WinRT CoreWindow be accessible to non-C++/CX code, in theory
include/SDL_syswm.h
src/render/direct3d11/SDL_render_d3d11.cpp
src/video/windowsrt/SDL_winrtvideo.cpp
src/video/windowsrt/SDL_winrtvideo.h
     1.1 --- a/include/SDL_syswm.h	Sun Apr 14 12:06:58 2013 -0400
     1.2 +++ b/include/SDL_syswm.h	Tue Apr 16 23:40:03 2013 -0400
     1.3 @@ -58,6 +58,10 @@
     1.4  #include <windows.h>
     1.5  #endif
     1.6  
     1.7 +#if defined(SDL_VIDEO_DRIVER_WINRT)
     1.8 +#include <Unknwn.h>
     1.9 +#endif
    1.10 +
    1.11  /* This is the structure for custom window manager events */
    1.12  #if defined(SDL_VIDEO_DRIVER_X11)
    1.13  #if defined(__APPLE__) && defined(__MACH__)
    1.14 @@ -175,7 +179,7 @@
    1.15  #if defined(SDL_VIDEO_DRIVER_WINRT)
    1.16          struct
    1.17          {
    1.18 -            void * window;          /**< The Windows RT CoreWindow, casted from 'CoreWindow ^*' to 'void *' */
    1.19 +            IUnknown * window;          /**< The Windows RT CoreWindow */
    1.20          } winrt;
    1.21  #endif
    1.22  #if defined(SDL_VIDEO_DRIVER_X11)
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Sun Apr 14 12:06:58 2013 -0400
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Tue Apr 16 23:40:03 2013 -0400
     2.3 @@ -23,6 +23,11 @@
     2.4  
     2.5  #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
     2.6  
     2.7 +#ifdef __WINRT__
     2.8 +#include <windows.ui.core.h>
     2.9 +#include <windows.foundation.h>
    2.10 +#endif
    2.11 +
    2.12  extern "C" {
    2.13  #include "../../core/windows/SDL_windows.h"
    2.14  //#include "SDL_hints.h"
    2.15 @@ -562,7 +567,7 @@
    2.16  
    2.17  #ifdef __WINRT__
    2.18  
    2.19 -static CoreWindow ^
    2.20 +static ABI::Windows::UI::Core::ICoreWindow *
    2.21  D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
    2.22  {
    2.23      SDL_Window * sdlWindow = renderer->window;
    2.24 @@ -580,12 +585,16 @@
    2.25          return nullptr;
    2.26      }
    2.27  
    2.28 -    CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window;
    2.29 -    if ( ! coreWindowPointer ) {
    2.30 +    if ( ! sdlWindowInfo.info.winrt.window ) {
    2.31          return nullptr;
    2.32      }
    2.33  
    2.34 -    return *coreWindowPointer;
    2.35 +    ABI::Windows::UI::Core::ICoreWindow * coreWindow = nullptr;
    2.36 +    if (FAILED(sdlWindowInfo.info.winrt.window->QueryInterface(&coreWindow))) {
    2.37 +        return nullptr;
    2.38 +    }
    2.39 +
    2.40 +    return coreWindow;
    2.41  }
    2.42  
    2.43  // Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
    2.44 @@ -604,12 +613,19 @@
    2.45  {
    2.46      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    2.47      HRESULT result = S_OK;
    2.48 -    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
    2.49 +    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
    2.50  
    2.51      // Store the window bounds so the next time we get a SizeChanged event we can
    2.52      // avoid rebuilding everything if the size is identical.
    2.53 -    data->windowSizeInDIPs.x = coreWindow->Bounds.Width;
    2.54 -    data->windowSizeInDIPs.y = coreWindow->Bounds.Height;
    2.55 +    ABI::Windows::Foundation::Rect coreWindowBounds;
    2.56 +    result = coreWindow->get_Bounds(&coreWindowBounds);
    2.57 +    if (FAILED(result)) {
    2.58 +        WIN_SetErrorFromHRESULT(__FUNCTION__", Get Window Bounds", result);
    2.59 +        return result;
    2.60 +    }
    2.61 +
    2.62 +    data->windowSizeInDIPs.x = coreWindowBounds.Width;
    2.63 +    data->windowSizeInDIPs.y = coreWindowBounds.Height;
    2.64  
    2.65      // Calculate the necessary swap chain and render target size in pixels.
    2.66      float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
    2.67 @@ -685,9 +701,16 @@
    2.68              return result;
    2.69          }
    2.70  
    2.71 +        IUnknown * coreWindowAsIUnknown = nullptr;
    2.72 +        result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
    2.73 +        if (FAILED(result)) {
    2.74 +            WIN_SetErrorFromHRESULT(__FUNCTION__ ", CoreWindow to IUnknown", result);
    2.75 +            return result;
    2.76 +        }
    2.77 +
    2.78          result = dxgiFactory->CreateSwapChainForCoreWindow(
    2.79              data->d3dDevice.Get(),
    2.80 -            reinterpret_cast<IUnknown*>(coreWindow),
    2.81 +            coreWindowAsIUnknown,
    2.82              &swapChainDesc,
    2.83              nullptr, // Allow on all displays.
    2.84              &data->swapChain
    2.85 @@ -776,10 +799,17 @@
    2.86  {
    2.87      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    2.88      HRESULT result = S_OK;
    2.89 -    Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
    2.90 +    ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
    2.91 +    ABI::Windows::Foundation::Rect coreWindowBounds;
    2.92  
    2.93 -    if (coreWindow->Bounds.Width  != data->windowSizeInDIPs.x ||
    2.94 -        coreWindow->Bounds.Height != data->windowSizeInDIPs.y ||
    2.95 +    result = coreWindow->get_Bounds(&coreWindowBounds);
    2.96 +    if (FAILED(result)) {
    2.97 +        WIN_SetErrorFromHRESULT(__FUNCTION__ ", Get Window Bounds", result);
    2.98 +        return result;
    2.99 +    }
   2.100 +
   2.101 +    if (coreWindowBounds.Width  != data->windowSizeInDIPs.x ||
   2.102 +        coreWindowBounds.Height != data->windowSizeInDIPs.y ||
   2.103          data->orientation != DisplayProperties::CurrentOrientation)
   2.104      {
   2.105          ID3D11RenderTargetView* nullViews[] = {nullptr};
     3.1 --- a/src/video/windowsrt/SDL_winrtvideo.cpp	Sun Apr 14 12:06:58 2013 -0400
     3.2 +++ b/src/video/windowsrt/SDL_winrtvideo.cpp	Tue Apr 16 23:40:03 2013 -0400
     3.3 @@ -172,7 +172,7 @@
     3.4      }
     3.5      window->driverdata = data;
     3.6      data->sdlWindow = window;
     3.7 -    data->coreWindow = new CoreWindow^(CoreWindow::GetForCurrentThread());
     3.8 +    data->coreWindow = CoreWindow::GetForCurrentThread();
     3.9  
    3.10      /* Make sure the window is considered to be positioned at {0,0},
    3.11         and is considered fullscreen, shown, and the like.
    3.12 @@ -233,13 +233,6 @@
    3.13      }
    3.14  
    3.15      if (data) {
    3.16 -        // Delete the reference to the WinRT CoreWindow:
    3.17 -        CoreWindow ^* windowPointer = ((SDL_WindowData *) window->driverdata)->coreWindow;
    3.18 -        if (windowPointer) {
    3.19 -            *windowPointer = nullptr;   // Clear the C++/CX reference to the CoreWindow
    3.20 -            delete windowPointer;       // Delete the C++/CX reference itself
    3.21 -        }
    3.22 -
    3.23          // Delete the internal window data:
    3.24          delete data;
    3.25          data = NULL;
    3.26 @@ -250,11 +243,10 @@
    3.27  WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
    3.28  {
    3.29      SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
    3.30 -    CoreWindow ^* windowPointer = data->coreWindow;
    3.31  
    3.32      if (info->version.major <= SDL_MAJOR_VERSION) {
    3.33          info->subsystem = SDL_SYSWM_WINDOWSRT;
    3.34 -        info->info.winrt.window = windowPointer;
    3.35 +        info->info.winrt.window = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
    3.36          return SDL_TRUE;
    3.37      } else {
    3.38          SDL_SetError("Application not compiled with SDL %d.%d\n",
     4.1 --- a/src/video/windowsrt/SDL_winrtvideo.h	Sun Apr 14 12:06:58 2013 -0400
     4.2 +++ b/src/video/windowsrt/SDL_winrtvideo.h	Tue Apr 16 23:40:03 2013 -0400
     4.3 @@ -27,10 +27,12 @@
     4.4  #include "../SDL_sysvideo.h"
     4.5  }
     4.6  
     4.7 +#include <agile.h>
     4.8 +
     4.9  struct SDL_WindowData
    4.10  {
    4.11      SDL_Window *sdlWindow;
    4.12 -    Windows::UI::Core::CoreWindow ^* coreWindow;
    4.13 +    Platform::Agile<Windows::UI::Core::CoreWindow> coreWindow;
    4.14  };
    4.15  
    4.16  #endif /* _SDL_winrtvideo_h */