Skip to content

Commit

Permalink
WinRT: experimental and preliminary support for XAML-based overlays o…
Browse files Browse the repository at this point in the history
…n Windows 8/RT

The XAML support here is still rudimentary.  Bugs do exist.  You've been warned.

XAML support in Windows Phone 8 is not yet available (in SDL/WinRT).
  • Loading branch information
DavidLudwig committed Aug 28, 2013
1 parent 86ea4c4 commit 2cafee9
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 48 deletions.
8 changes: 8 additions & 0 deletions VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
Expand Up @@ -70,6 +70,14 @@
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
</ClCompile>
<ClCompile Include="..\..\src\core\winrt\SDL_winrtxaml.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\cpuinfo\SDL_cpuinfo.c" />
<ClCompile Include="..\..\src\events\SDL_clipboardevents.c" />
<ClCompile Include="..\..\src\events\SDL_dropevents.c" />
Expand Down
3 changes: 3 additions & 0 deletions VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
Expand Up @@ -270,6 +270,9 @@
<ClCompile Include="..\..\src\joystick\winrt\SDL_xinputjoystick.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\winrt\SDL_winrtxaml.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\begin_code.h">
Expand Down
14 changes: 14 additions & 0 deletions include/SDL_system.h
Expand Up @@ -155,6 +155,20 @@ extern DECLSPEC const wchar_t * SDLCALL SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path
*/
extern DECLSPEC const char * SDLCALL SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType);

#ifdef __cplusplus_winrt
/**
* \brief Initializes a WinRT and XAML based application.
*
* \param backgroundPanel The XAML background panel to draw onto and receive
* events from.
* \param mainFunction The SDL app's C-style main().
* \ret 0 on success, -1 on failure. On failure, use SDL_GetError to retrieve more
* information on the failure.
*/
/* TODO, WinRT: consider making SDL_WinRTInitXAMLApp accept a void pointer to IUnknown, rather than a C++/CX reference */
extern DECLSPEC int SDLCALL SDL_WinRTInitXAMLApp(Platform::Object^ backgroundPanel, int (*mainFunction)(int, char **));

#endif // ifdef __cplusplus_winrt

#endif /* __WINRT__ */

Expand Down
8 changes: 4 additions & 4 deletions src/core/winrt/SDL_winrtapp.cpp
Expand Up @@ -361,17 +361,17 @@ void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)

void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
{
WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args);
WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
}

void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
{
WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args);
WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
}

void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
{
WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args);
WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
}

void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
Expand All @@ -381,7 +381,7 @@ void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)

void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
{
WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args);
WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->CurrentPoint);
}

void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
Expand Down
162 changes: 162 additions & 0 deletions src/core/winrt/SDL_winrtxaml.cpp
@@ -0,0 +1,162 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 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 <agile.h>
#include <Windows.h>

#if WINAPI_FAMILY == WINAPI_FAMILY_APP
#include <windows.ui.xaml.media.dxinterop.h>
#endif


/* SDL includes */
#include "SDL.h"
//#include "SDL_error.h"
//#include "SDL_log.h"
//#include "SDL_main.h"
//#include "SDL_system.h"
#include "../../video/winrt/SDL_winrtevents_c.h"


/* External globals: */
extern SDL_Window * WINRT_GlobalSDLWindow;


/* Internal globals: */
SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
int (*WINRT_XAMLAppMainFunction)(int, char **) = NULL;

#if WINAPI_FAMILY == WINAPI_FAMILY_APP
ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
static Windows::Foundation::EventRegistrationToken WINRT_XAMLAppEventToken;
#endif


/*
* Input event handlers (XAML)
*/
#if WINAPI_FAMILY == WINAPI_FAMILY_APP

static void
WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
{
WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
}

static void
WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
{
WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
}

static void
WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
{
WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
}

static void
WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
{
WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
}

#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP


/*
* XAML-to-SDL Rendering Callback
*/
#if WINAPI_FAMILY == WINAPI_FAMILY_APP

static void
WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
{
WINRT_CycleXAMLThread();
}

#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP


/*
* SDL + XAML Initialization
*/

extern "C" int
SDL_WinRTInitXAMLApp(Platform::Object ^backgroundPanel, int (*mainFunction)(int, char **))
{
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
return SDL_SetError("XAML support is not yet available in Windows Phone.");
#else
// Declare C++/CX namespaces:
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;

// Make sure we have a valid XAML element (to draw onto):
if ( ! backgroundPanel) {
return SDL_SetError("'backgroundPanel' can't be NULL");
}

SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
if ( ! swapChainBackgroundPanel) {
return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
}

// Setup event handlers:
swapChainBackgroundPanel->PointerPressed += ref new PointerEventHandler(WINRT_OnPointerPressedViaXAML);
swapChainBackgroundPanel->PointerReleased += ref new PointerEventHandler(WINRT_OnPointerReleasedViaXAML);
swapChainBackgroundPanel->PointerWheelChanged += ref new PointerEventHandler(WINRT_OnPointerWheelChangedViaXAML);
swapChainBackgroundPanel->PointerMoved += ref new PointerEventHandler(WINRT_OnPointerMovedViaXAML);

// Setup for rendering:
IInspectable *panelInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(swapChainBackgroundPanel);
panelInspectable->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void **)&WINRT_GlobalSwapChainBackgroundPanelNative);

WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));

// Make sure the app is ready to call the SDL-centric main() function:
WINRT_XAMLAppMainFunction = mainFunction;
SDL_SetMainReady();

// Make sure video-init knows that we're initializing XAML:
SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
WINRT_XAMLWasEnabled = SDL_TRUE;

// Make sure video modes are detected now, while we still have access to the WinRT
// CoreWindow. WinRT will not allow the app's CoreWindow to be accessed via the
// SDL/WinRT thread.
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
// SDL_InitSubSystem will, on error, set the SDL error. Let that propogate to
// the caller to here:
WINRT_XAMLWasEnabled = oldXAMLWasEnabledValue;
return -1;
}

// All done, for now.
return 0;
#endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP / else
}
104 changes: 79 additions & 25 deletions src/render/direct3d11/SDL_render_d3d11.cpp
Expand Up @@ -26,6 +26,11 @@
#ifdef __WINRT__
#include <windows.ui.core.h>
#include <windows.foundation.h>

#if WINAPI_FAMILY == WINAPI_FAMILY_APP
#include <windows.ui.xaml.media.dxinterop.h>
#endif

#endif

extern "C" {
Expand Down Expand Up @@ -608,6 +613,13 @@ D3D11_ConvertDipsToPixels(float dips)
}
#endif


#if WINAPI_FAMILY == WINAPI_FAMILY_APP
// TODO, WinRT, XAML: get the ISwapChainBackgroundPanelNative from something other than a global var
extern ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative;
#endif


// Initialize all resources that change when the window's size changes.
// TODO, WinRT: get D3D11_CreateWindowSizeDependentResources working on Win32
HRESULT
Expand All @@ -619,15 +631,28 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)

// Store the window bounds so the next time we get a SizeChanged event we can
// avoid rebuilding everything if the size is identical.
ABI::Windows::Foundation::Rect coreWindowBounds;
result = coreWindow->get_Bounds(&coreWindowBounds);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__", Get Window Bounds", result);
return result;
ABI::Windows::Foundation::Rect nativeWindowBounds;
if (coreWindow) {
result = coreWindow->get_Bounds(&nativeWindowBounds);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__", Get Window Bounds", result);
return result;
}
} else {
// TODO, WinRT, XAML: clean up window-bounds code in D3D11_CreateWindowSizeDependentResources
SDL_DisplayMode displayMode;
if (SDL_GetDesktopDisplayMode(0, &displayMode) < 0) {
SDL_SetError(__FUNCTION__", Get Window Bounds (XAML): Unable to retrieve the native window's size");
return E_FAIL;
}

nativeWindowBounds.Width = (FLOAT) displayMode.w;
nativeWindowBounds.Height = (FLOAT) displayMode.h;
}

data->windowSizeInDIPs.x = coreWindowBounds.Width;
data->windowSizeInDIPs.y = coreWindowBounds.Height;
// TODO, WinRT, XAML: see if window/control sizes are in DIPs, or something else. If something else, then adjust renderer size tracking accordingly.
data->windowSizeInDIPs.x = nativeWindowBounds.Width;
data->windowSizeInDIPs.y = nativeWindowBounds.Height;

// Calculate the necessary swap chain and render target size in pixels.
float windowWidth = D3D11_ConvertDipsToPixels(data->windowSizeInDIPs.x);
Expand Down Expand Up @@ -660,6 +685,8 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
}
else
{
const bool usingXAML = (coreWindow == nullptr);

// Otherwise, create a new one using the same adapter as the existing Direct3D device.
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
swapChainDesc.Width = static_cast<UINT>(data->renderTargetSize.x); // Match the size of the window.
Expand All @@ -674,7 +701,11 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
swapChainDesc.Scaling = DXGI_SCALING_STRETCH; // On phone, only stretch and aspect-ratio stretch scaling are allowed.
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; // On phone, no swap effects are supported.
#else
swapChainDesc.Scaling = DXGI_SCALING_NONE;
if (usingXAML) {
swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
} else {
swapChainDesc.Scaling = DXGI_SCALING_NONE;
}
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
#endif
swapChainDesc.Flags = 0;
Expand Down Expand Up @@ -703,25 +734,48 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
return result;
}

IUnknown * coreWindowAsIUnknown = nullptr;
result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", CoreWindow to IUnknown", result);
return result;
}
if (usingXAML) {
result = dxgiFactory->CreateSwapChainForComposition(
data->d3dDevice.Get(),
&swapChainDesc,
nullptr,
&data->swapChain);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", CreateSwapChainForComposition", result);
return result;
}

result = dxgiFactory->CreateSwapChainForCoreWindow(
data->d3dDevice.Get(),
coreWindowAsIUnknown,
&swapChainDesc,
nullptr, // Allow on all displays.
&data->swapChain
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
result = WINRT_GlobalSwapChainBackgroundPanelNative->SetSwapChain(data->swapChain.Get());
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", ISwapChainBackgroundPanelNative::SetSwapChain", result);
return result;
}
#else
SDL_SetError(__FUNCTION__ ", XAML support is not yet available for Windows Phone");
return E_FAIL;
#endif
} else {
IUnknown * coreWindowAsIUnknown = nullptr;
result = coreWindow->QueryInterface(&coreWindowAsIUnknown);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", CoreWindow to IUnknown", result);
return result;
}

result = dxgiFactory->CreateSwapChainForCoreWindow(
data->d3dDevice.Get(),
coreWindowAsIUnknown,
&swapChainDesc,
nullptr, // Allow on all displays.
&data->swapChain
);
if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__, result);
return result;
}
}

// Ensure that DXGI does not queue more than one frame at a time. This both reduces latency and
// ensures that the application will only render after each VSync, minimizing power consumption.
result = dxgiDevice->SetMaximumFrameLatency(1);
Expand Down

0 comments on commit 2cafee9

Please sign in to comment.