WinRT: made SDL's inner WinRT CoreWindow be accessible to non-C++/CX code, in theory

This commit is contained in:
David Ludwig 2013-04-16 23:40:03 -04:00
parent 3a0f7dfe30
commit 52a3b9b1f5
4 changed files with 51 additions and 23 deletions

View file

@ -58,6 +58,10 @@ struct SDL_SysWMinfo;
#include <windows.h> #include <windows.h>
#endif #endif
#if defined(SDL_VIDEO_DRIVER_WINRT)
#include <Unknwn.h>
#endif
/* This is the structure for custom window manager events */ /* This is the structure for custom window manager events */
#if defined(SDL_VIDEO_DRIVER_X11) #if defined(SDL_VIDEO_DRIVER_X11)
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
@ -175,7 +179,7 @@ struct SDL_SysWMinfo
#if defined(SDL_VIDEO_DRIVER_WINRT) #if defined(SDL_VIDEO_DRIVER_WINRT)
struct struct
{ {
void * window; /**< The Windows RT CoreWindow, casted from 'CoreWindow ^*' to 'void *' */ IUnknown * window; /**< The Windows RT CoreWindow */
} winrt; } winrt;
#endif #endif
#if defined(SDL_VIDEO_DRIVER_X11) #if defined(SDL_VIDEO_DRIVER_X11)

View file

@ -23,6 +23,11 @@
#if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
#ifdef __WINRT__
#include <windows.ui.core.h>
#include <windows.foundation.h>
#endif
extern "C" { extern "C" {
#include "../../core/windows/SDL_windows.h" #include "../../core/windows/SDL_windows.h"
//#include "SDL_hints.h" //#include "SDL_hints.h"
@ -562,7 +567,7 @@ D3D11_CreateDeviceResources(SDL_Renderer * renderer)
#ifdef __WINRT__ #ifdef __WINRT__
static CoreWindow ^ static ABI::Windows::UI::Core::ICoreWindow *
D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer) D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
{ {
SDL_Window * sdlWindow = renderer->window; SDL_Window * sdlWindow = renderer->window;
@ -580,12 +585,16 @@ D3D11_GetCoreWindowFromSDLRenderer(SDL_Renderer * renderer)
return nullptr; return nullptr;
} }
CoreWindow ^* coreWindowPointer = (CoreWindow ^*) sdlWindowInfo.info.winrt.window; if ( ! sdlWindowInfo.info.winrt.window ) {
if ( ! coreWindowPointer ) {
return nullptr; return nullptr;
} }
return *coreWindowPointer; ABI::Windows::UI::Core::ICoreWindow * coreWindow = nullptr;
if (FAILED(sdlWindowInfo.info.winrt.window->QueryInterface(&coreWindow))) {
return nullptr;
}
return coreWindow;
} }
// Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels. // Method to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
@ -604,12 +613,19 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
{ {
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata; D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK; HRESULT result = S_OK;
Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer); ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
// Store the window bounds so the next time we get a SizeChanged event we can // Store the window bounds so the next time we get a SizeChanged event we can
// avoid rebuilding everything if the size is identical. // avoid rebuilding everything if the size is identical.
data->windowSizeInDIPs.x = coreWindow->Bounds.Width; ABI::Windows::Foundation::Rect coreWindowBounds;
data->windowSizeInDIPs.y = coreWindow->Bounds.Height; result = coreWindow->get_Bounds(&coreWindowBounds);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__", Get Window Bounds", result);
return result;
}
data->windowSizeInDIPs.x = coreWindowBounds.Width;
data->windowSizeInDIPs.y = coreWindowBounds.Height;
// Calculate the necessary swap chain and render target size in pixels. // Calculate the necessary swap chain and render target size in pixels.
float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x); float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
@ -685,9 +701,16 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
return result; return result;
} }
IUnknown * coreWindowAsIUnknown = nullptr;
result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", CoreWindow to IUnknown", result);
return result;
}
result = dxgiFactory->CreateSwapChainForCoreWindow( result = dxgiFactory->CreateSwapChainForCoreWindow(
data->d3dDevice.Get(), data->d3dDevice.Get(),
reinterpret_cast<IUnknown*>(coreWindow), coreWindowAsIUnknown,
&swapChainDesc, &swapChainDesc,
nullptr, // Allow on all displays. nullptr, // Allow on all displays.
&data->swapChain &data->swapChain
@ -776,10 +799,17 @@ D3D11_UpdateForWindowSizeChange(SDL_Renderer * renderer)
{ {
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata; D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK; HRESULT result = S_OK;
Windows::UI::Core::CoreWindow ^ coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer); ABI::Windows::UI::Core::ICoreWindow * coreWindow = D3D11_GetCoreWindowFromSDLRenderer(renderer);
ABI::Windows::Foundation::Rect coreWindowBounds;
if (coreWindow->Bounds.Width != data->windowSizeInDIPs.x || result = coreWindow->get_Bounds(&coreWindowBounds);
coreWindow->Bounds.Height != data->windowSizeInDIPs.y || if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", Get Window Bounds", result);
return result;
}
if (coreWindowBounds.Width != data->windowSizeInDIPs.x ||
coreWindowBounds.Height != data->windowSizeInDIPs.y ||
data->orientation != DisplayProperties::CurrentOrientation) data->orientation != DisplayProperties::CurrentOrientation)
{ {
ID3D11RenderTargetView* nullViews[] = {nullptr}; ID3D11RenderTargetView* nullViews[] = {nullptr};

View file

@ -172,7 +172,7 @@ WINRT_CreateWindow(_THIS, SDL_Window * window)
} }
window->driverdata = data; window->driverdata = data;
data->sdlWindow = window; data->sdlWindow = window;
data->coreWindow = new CoreWindow^(CoreWindow::GetForCurrentThread()); data->coreWindow = CoreWindow::GetForCurrentThread();
/* Make sure the window is considered to be positioned at {0,0}, /* Make sure the window is considered to be positioned at {0,0},
and is considered fullscreen, shown, and the like. and is considered fullscreen, shown, and the like.
@ -233,13 +233,6 @@ WINRT_DestroyWindow(_THIS, SDL_Window * window)
} }
if (data) { 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 the internal window data:
delete data; delete data;
data = NULL; data = NULL;
@ -250,11 +243,10 @@ SDL_bool
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info) WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{ {
SDL_WindowData * data = (SDL_WindowData *) window->driverdata; SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
CoreWindow ^* windowPointer = data->coreWindow;
if (info->version.major <= SDL_MAJOR_VERSION) { if (info->version.major <= SDL_MAJOR_VERSION) {
info->subsystem = SDL_SYSWM_WINDOWSRT; info->subsystem = SDL_SYSWM_WINDOWSRT;
info->info.winrt.window = windowPointer; info->info.winrt.window = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
return SDL_TRUE; return SDL_TRUE;
} else { } else {
SDL_SetError("Application not compiled with SDL %d.%d\n", SDL_SetError("Application not compiled with SDL %d.%d\n",

View file

@ -27,10 +27,12 @@ extern "C" {
#include "../SDL_sysvideo.h" #include "../SDL_sysvideo.h"
} }
#include <agile.h>
struct SDL_WindowData struct SDL_WindowData
{ {
SDL_Window *sdlWindow; SDL_Window *sdlWindow;
Windows::UI::Core::CoreWindow ^* coreWindow; Platform::Agile<Windows::UI::Core::CoreWindow> coreWindow;
}; };
#endif /* _SDL_winrtvideo_h */ #endif /* _SDL_winrtvideo_h */