src/core/winrt/SDL_winrtapp_xaml.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 12 Mar 2014 11:57:15 -0400
changeset 8611 a762dfc67e1a
parent 8600 092802455aed
child 8612 24d36bb9c7af
permissions -rw-r--r--
Fixed various build and runtime errors when using WinRT with VS2012.
dludwig@8505
     1
/*
dludwig@8505
     2
  Simple DirectMedia Layer
dludwig@8505
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
dludwig@8505
     4
dludwig@8505
     5
  This software is provided 'as-is', without any express or implied
dludwig@8505
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8505
     7
  arising from the use of this software.
dludwig@8505
     8
dludwig@8505
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8505
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8505
    11
  freely, subject to the following restrictions:
dludwig@8505
    12
dludwig@8505
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8505
    14
     claim that you wrote the original software. If you use this software
dludwig@8505
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8505
    16
     appreciated but is not required.
dludwig@8505
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8505
    18
     misrepresented as being the original software.
dludwig@8505
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8505
    20
*/
dludwig@8505
    21
dludwig@8505
    22
/* Windows includes */
dludwig@8505
    23
#include <agile.h>
dludwig@8505
    24
#include <Windows.h>
dludwig@8505
    25
dludwig@8505
    26
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    27
#include <windows.ui.xaml.media.dxinterop.h>
dludwig@8505
    28
#endif
dludwig@8505
    29
dludwig@8505
    30
dludwig@8505
    31
/* SDL includes */
dludwig@8600
    32
#include "../../SDL_internal.h"
dludwig@8505
    33
#include "SDL.h"
dludwig@8505
    34
#include "../../video/winrt/SDL_winrtevents_c.h"
dludwig@8515
    35
#include "../../video/winrt/SDL_winrtvideo_cpp.h"
dludwig@8531
    36
#include "SDL_winrtapp_common.h"
dludwig@8522
    37
#include "SDL_winrtapp_xaml.h"
dludwig@8505
    38
dludwig@8505
    39
dludwig@8505
    40
dludwig@8515
    41
/* SDL-internal globals: */
dludwig@8505
    42
SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
dludwig@8505
    43
dludwig@8505
    44
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8611
    45
extern "C" ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
dludwig@8505
    46
static Windows::Foundation::EventRegistrationToken	WINRT_XAMLAppEventToken;
dludwig@8505
    47
#endif
dludwig@8505
    48
dludwig@8505
    49
dludwig@8505
    50
/*
dludwig@8505
    51
 * Input event handlers (XAML)
dludwig@8505
    52
 */
dludwig@8505
    53
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    54
dludwig@8522
    55
static void
dludwig@8522
    56
WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    57
{
dludwig@8522
    58
    WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    59
}
dludwig@8505
    60
dludwig@8522
    61
static void
dludwig@8522
    62
WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    63
{
dludwig@8522
    64
    WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8521
    65
}
dludwig@8521
    66
dludwig@8522
    67
static void
dludwig@8522
    68
WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    69
{
dludwig@8522
    70
    WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    71
}
dludwig@8505
    72
dludwig@8522
    73
static void
dludwig@8522
    74
WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    75
{
dludwig@8522
    76
    WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    77
}
dludwig@8505
    78
dludwig@8505
    79
#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    80
dludwig@8505
    81
dludwig@8505
    82
/*
dludwig@8505
    83
 * XAML-to-SDL Rendering Callback
dludwig@8505
    84
 */
dludwig@8505
    85
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    86
dludwig@8505
    87
static void
dludwig@8505
    88
WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
dludwig@8505
    89
{
dludwig@8505
    90
    WINRT_CycleXAMLThread();
dludwig@8505
    91
}
dludwig@8505
    92
dludwig@8505
    93
#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    94
dludwig@8505
    95
dludwig@8505
    96
/*
dludwig@8505
    97
 * SDL + XAML Initialization
dludwig@8505
    98
 */
dludwig@8505
    99
dludwig@8531
   100
int
dludwig@8531
   101
SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable)
dludwig@8505
   102
{
dludwig@8505
   103
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8505
   104
    return SDL_SetError("XAML support is not yet available in Windows Phone.");
dludwig@8505
   105
#else
dludwig@8505
   106
    // Declare C++/CX namespaces:
dludwig@8505
   107
    using namespace Platform;
dludwig@8505
   108
    using namespace Windows::Foundation;
dludwig@8505
   109
    using namespace Windows::UI::Core;
dludwig@8505
   110
    using namespace Windows::UI::Xaml;
dludwig@8505
   111
    using namespace Windows::UI::Xaml::Controls;
dludwig@8505
   112
    using namespace Windows::UI::Xaml::Input;
dludwig@8505
   113
    using namespace Windows::UI::Xaml::Media;
dludwig@8505
   114
dludwig@8505
   115
    // Make sure we have a valid XAML element (to draw onto):
dludwig@8531
   116
    if ( ! backgroundPanelAsIInspectable) {
dludwig@8531
   117
        return SDL_SetError("'backgroundPanelAsIInspectable' can't be NULL");
dludwig@8505
   118
    }
dludwig@8505
   119
dludwig@8531
   120
    Platform::Object ^ backgroundPanel = reinterpret_cast<Object ^>((IInspectable *) backgroundPanelAsIInspectable);
dludwig@8505
   121
    SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
dludwig@8505
   122
    if ( ! swapChainBackgroundPanel) {
dludwig@8505
   123
        return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
dludwig@8505
   124
    }
dludwig@8505
   125
dludwig@8505
   126
    // Setup event handlers:
dludwig@8505
   127
    swapChainBackgroundPanel->PointerPressed += ref new PointerEventHandler(WINRT_OnPointerPressedViaXAML);
dludwig@8505
   128
    swapChainBackgroundPanel->PointerReleased += ref new PointerEventHandler(WINRT_OnPointerReleasedViaXAML);
dludwig@8505
   129
    swapChainBackgroundPanel->PointerWheelChanged += ref new PointerEventHandler(WINRT_OnPointerWheelChangedViaXAML);
dludwig@8505
   130
    swapChainBackgroundPanel->PointerMoved += ref new PointerEventHandler(WINRT_OnPointerMovedViaXAML);
dludwig@8505
   131
dludwig@8505
   132
    // Setup for rendering:
dludwig@8522
   133
    IInspectable *panelInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(swapChainBackgroundPanel);
dludwig@8505
   134
    panelInspectable->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void **)&WINRT_GlobalSwapChainBackgroundPanelNative);
dludwig@8505
   135
dludwig@8505
   136
    WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));
dludwig@8505
   137
dludwig@8505
   138
    // Make sure the app is ready to call the SDL-centric main() function:
dludwig@8531
   139
    WINRT_SDLAppEntryPoint = mainFunction;
dludwig@8505
   140
    SDL_SetMainReady();
dludwig@8505
   141
dludwig@8505
   142
    // Make sure video-init knows that we're initializing XAML:
dludwig@8505
   143
    SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
dludwig@8505
   144
    WINRT_XAMLWasEnabled = SDL_TRUE;
dludwig@8505
   145
dludwig@8505
   146
    // Make sure video modes are detected now, while we still have access to the WinRT
dludwig@8505
   147
    // CoreWindow.  WinRT will not allow the app's CoreWindow to be accessed via the
dludwig@8505
   148
    // SDL/WinRT thread.
dludwig@8505
   149
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
dludwig@8505
   150
        // SDL_InitSubSystem will, on error, set the SDL error.  Let that propogate to
dludwig@8505
   151
        // the caller to here:
dludwig@8505
   152
        WINRT_XAMLWasEnabled = oldXAMLWasEnabledValue;
dludwig@8505
   153
        return -1;
dludwig@8505
   154
    }
dludwig@8505
   155
dludwig@8505
   156
    // All done, for now.
dludwig@8505
   157
    return 0;
dludwig@8505
   158
#endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP  /  else
dludwig@8505
   159
}