sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_winrtapp_xaml.cpp (5776B)


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