SDL  2.0
SDL_winrtapp_xaml.cpp
Go to the documentation of this file.
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: */
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 
138 
139  // Make sure the app is ready to call the SDL-centric main() function:
140  WINRT_SDLAppEntryPoint = mainFunction;
142 
143  // Make sure video-init knows that we're initializing XAML:
144  SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
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.
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 }
SDL.h
SDL_SetMainReady
#define SDL_SetMainReady
Definition: SDL_dynapi_overrides.h:242
WINRT_OnPointerPressedViaXAML
static void WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
Definition: SDL_winrtapp_xaml.cpp:57
NULL
#define NULL
Definition: begin_code.h:167
WINRT_OnPointerReleasedViaXAML
static void WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
Definition: SDL_winrtapp_xaml.cpp:69
WINRT_OnPointerWheelChangedViaXAML
static void WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
Definition: SDL_winrtapp_xaml.cpp:75
SDL_winrtapp_xaml.h
SDL_InitSubSystem
#define SDL_InitSubSystem
Definition: SDL_dynapi_overrides.h:55
add
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF base if bpp PF set rept prefetch_distance PF set OFFSET endr endif endm macro preload_leading_step2 base if bpp ifc DST PF PF else if bpp lsl PF PF lsl PF PF lsl PF PF PF else PF lsl PF add
Definition: pixman-arm-simd-asm.h:215
ref
GLenum GLint ref
Definition: SDL_opengl_glext.h:660
WINRT_GlobalSDLWindow
SDL_Window * WINRT_GlobalSDLWindow
SDL_WinRTInitXAMLApp
int SDL_WinRTInitXAMLApp(int(*mainFunction)(int, char **), void *backgroundPanelAsIInspectable)
Definition: SDL_winrtapp_xaml.cpp:102
WINRT_SDLAppEntryPoint
int(* WINRT_SDLAppEntryPoint)(int, char **)
Definition: SDL_winrtapp_common.cpp:30
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
WINRT_XAMLWasEnabled
SDL_bool WINRT_XAMLWasEnabled
Definition: SDL_winrtapp_xaml.cpp:42
WINRT_XAMLAppEventToken
static Windows::Foundation::EventRegistrationToken WINRT_XAMLAppEventToken
Definition: SDL_winrtapp_xaml.cpp:47
SDL_winrtapp_common.h
WINRT_OnPointerMovedViaXAML
static void WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
Definition: SDL_winrtapp_xaml.cpp:63
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:80
WINRT_OnRenderViaXAML
static void WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
Definition: SDL_winrtapp_xaml.cpp:89
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
WINRT_GlobalSwapChainBackgroundPanelNative
ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative
Definition: SDL_winrtapp_xaml.cpp:46
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163