WinRT: unified the two, public, app-init functions
authorDavid Ludwig <dludwig@pobox.com>
Sun, 22 Sep 2013 12:26:53 -0400
changeset 8531d481485a1b85
parent 8530 b5f77f46d028
child 8532 0f4be71aa156
WinRT: unified the two, public, app-init functions

This function, SDL_WinRTRunApp, can be used to help launch either XAML or non-XAML/Direct3D-only based apps.
VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj
VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj.filters
VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj
VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj.filters
include/SDL_main.h
include/SDL_system.h
src/core/winrt/SDL_winrtapp_direct3d.cpp
src/core/winrt/SDL_winrtapp_direct3d.h
src/core/winrt/SDL_winrtapp_xaml.cpp
src/core/winrt/SDL_winrtapp_xaml.h
src/main/winrt/SDL_winrt_main_NonXAML.cpp
src/video/winrt/SDL_winrtevents.cpp
     1.1 --- a/VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj	Mon Sep 16 22:43:12 2013 -0400
     1.2 +++ b/VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj	Sun Sep 22 12:26:53 2013 -0400
     1.3 @@ -197,6 +197,7 @@
     1.4      <ClInclude Include="..\..\src\audio\SDL_wave.h" />
     1.5      <ClInclude Include="..\..\src\audio\xaudio2\SDL_xaudio2_winrthelpers.h" />
     1.6      <ClInclude Include="..\..\src\core\windows\SDL_windows.h" />
     1.7 +    <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_common.h" />
     1.8      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_direct3d.h" />
     1.9      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_xaml.h" />
    1.10      <ClInclude Include="..\..\src\events\blank_cursor.h" />
    1.11 @@ -269,6 +270,12 @@
    1.12        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    1.13      </ClCompile>
    1.14      <ClCompile Include="..\..\src\core\windows\SDL_windows.c" />
    1.15 +    <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_common.cpp">
    1.16 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.17 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    1.18 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
    1.19 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    1.20 +    </ClCompile>
    1.21      <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_direct3d.cpp">
    1.22        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    1.23        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
     2.1 --- a/VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj.filters	Mon Sep 16 22:43:12 2013 -0400
     2.2 +++ b/VisualC-WinPhone/SDL/SDL-WinPhone_VS2012.vcxproj.filters	Sun Sep 22 12:26:53 2013 -0400
     2.3 @@ -339,6 +339,9 @@
     2.4      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_xaml.h">
     2.5        <Filter>Source Files</Filter>
     2.6      </ClInclude>
     2.7 +    <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_common.h">
     2.8 +      <Filter>Source Files</Filter>
     2.9 +    </ClInclude>
    2.10    </ItemGroup>
    2.11    <ItemGroup>
    2.12      <ClCompile Include="..\..\src\atomic\SDL_atomic.c">
    2.13 @@ -605,6 +608,9 @@
    2.14      <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_xaml.cpp">
    2.15        <Filter>Source Files</Filter>
    2.16      </ClCompile>
    2.17 +    <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_common.cpp">
    2.18 +      <Filter>Source Files</Filter>
    2.19 +    </ClCompile>
    2.20    </ItemGroup>
    2.21    <ItemGroup>
    2.22      <FxCompile Include="..\..\src\render\direct3d11\SDL_D3D11_PixelShader_FixedColor.hlsl">
     3.1 --- a/VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj	Mon Sep 16 22:43:12 2013 -0400
     3.2 +++ b/VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj	Sun Sep 22 12:26:53 2013 -0400
     3.3 @@ -47,6 +47,14 @@
     3.4        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
     3.5      </ClCompile>
     3.6      <ClCompile Include="..\..\src\core\windows\SDL_windows.c" />
     3.7 +    <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_common.cpp">
     3.8 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
     3.9 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    3.10 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
    3.11 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
    3.12 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
    3.13 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
    3.14 +    </ClCompile>
    3.15      <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_direct3d.cpp">
    3.16        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
    3.17        <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
    3.18 @@ -244,6 +252,7 @@
    3.19      <ClInclude Include="..\..\src\audio\SDL_wave.h" />
    3.20      <ClInclude Include="..\..\src\audio\xaudio2\SDL_xaudio2_winrthelpers.h" />
    3.21      <ClInclude Include="..\..\src\core\windows\SDL_windows.h" />
    3.22 +    <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_common.h" />
    3.23      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_direct3d.h" />
    3.24      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_xaml.h" />
    3.25      <ClInclude Include="..\..\src\events\blank_cursor.h" />
     4.1 --- a/VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj.filters	Mon Sep 16 22:43:12 2013 -0400
     4.2 +++ b/VisualC-WinRT/SDL/SDL-WinRT_VS2012.vcxproj.filters	Sun Sep 22 12:26:53 2013 -0400
     4.3 @@ -276,6 +276,9 @@
     4.4      <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_xaml.cpp">
     4.5        <Filter>Source Files</Filter>
     4.6      </ClCompile>
     4.7 +    <ClCompile Include="..\..\src\core\winrt\SDL_winrtapp_common.cpp">
     4.8 +      <Filter>Source Files</Filter>
     4.9 +    </ClCompile>
    4.10    </ItemGroup>
    4.11    <ItemGroup>
    4.12      <ClInclude Include="..\..\include\begin_code.h">
    4.13 @@ -608,6 +611,9 @@
    4.14      <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_xaml.h">
    4.15        <Filter>Source Files</Filter>
    4.16      </ClInclude>
    4.17 +    <ClInclude Include="..\..\src\core\winrt\SDL_winrtapp_common.h">
    4.18 +      <Filter>Source Files</Filter>
    4.19 +    </ClInclude>
    4.20    </ItemGroup>
    4.21    <ItemGroup>
    4.22      <Filter Include="Header Files">
     5.1 --- a/include/SDL_main.h	Mon Sep 16 22:43:12 2013 -0400
     5.2 +++ b/include/SDL_main.h	Sun Sep 22 12:26:53 2013 -0400
     5.3 @@ -44,8 +44,10 @@
     5.4     creating an instance of IFrameworkView in the process.
     5.5  
     5.6     Please note that #include'ing SDL_main.h is not enough to get a main()
     5.7 -   function working.  The file, src/main/winrt/SDL_WinRT_main.cpp, or a copy
     5.8 -   of it, must be compiled into the app itself.
     5.9 +   function working.  In non-XAML apps, the file,
    5.10 +   src/main/winrt/SDL_WinRT_main_NonXAML.cpp, or a copy of it, must be compiled
    5.11 +   into the app itself.  In XAML apps, the function, SDL_WinRTRunApp must be
    5.12 +   called, with a pointer to the Direct3D-hosted XAML control passed in.
    5.13  */
    5.14  #define SDL_MAIN_NEEDED
    5.15  
    5.16 @@ -125,6 +127,24 @@
    5.17  #endif /* __WIN32__ */
    5.18  
    5.19  
    5.20 +#ifdef __WINRT__
    5.21 +
    5.22 +/**
    5.23 + *  \brief Initializes and launches an SDL/WinRT application.
    5.24 + *
    5.25 + *  \param mainFunction The SDL app's C-style main().
    5.26 + *  \param xamlBackgroundPanel An optional, XAML-based, background panel.
    5.27 + *     For Non-XAML apps, this value must be set to NULL.  For XAML apps,
    5.28 + *     pass in a pointer to a SwapChainBackgroundPanel, casted to an
    5.29 + *     IInspectable (via reinterpret_cast).
    5.30 + *  \ret 0 on success, -1 on failure.  On failure, use SDL_GetError to retrieve more
    5.31 + *      information on the failure.
    5.32 + */
    5.33 +extern DECLSPEC int SDLCALL SDL_WinRTRunApp(int (*mainFunction)(int, char **), void * xamlBackgroundPanel);
    5.34 +
    5.35 +#endif /* __WINRT__ */
    5.36 +
    5.37 +
    5.38  #ifdef __cplusplus
    5.39  }
    5.40  #endif
     6.1 --- a/include/SDL_system.h	Mon Sep 16 22:43:12 2013 -0400
     6.2 +++ b/include/SDL_system.h	Sun Sep 22 12:26:53 2013 -0400
     6.3 @@ -155,21 +155,6 @@
     6.4   */
     6.5  extern DECLSPEC const char * SDLCALL SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType);
     6.6  
     6.7 -#ifdef __cplusplus_winrt
     6.8 -/**
     6.9 - *  \brief Initializes a WinRT and XAML based application.
    6.10 - *
    6.11 - *  \param backgroundPanel The XAML background panel to draw onto and receive
    6.12 - *      events from.
    6.13 - *  \param mainFunction The SDL app's C-style main().
    6.14 - *  \ret 0 on success, -1 on failure.  On failure, use SDL_GetError to retrieve more
    6.15 - *      information on the failure.
    6.16 - */
    6.17 -/* TODO, WinRT: consider making SDL_WinRTInitXAMLApp accept a void pointer to IUnknown, rather than a C++/CX reference */
    6.18 -extern DECLSPEC int SDLCALL SDL_WinRTInitXAMLApp(Platform::Object^ backgroundPanel, int (*mainFunction)(int, char **));
    6.19 -
    6.20 -#endif // ifdef __cplusplus_winrt
    6.21 -
    6.22  #endif /* __WINRT__ */
    6.23  
    6.24  
     7.1 --- a/src/core/winrt/SDL_winrtapp_direct3d.cpp	Mon Sep 16 22:43:12 2013 -0400
     7.2 +++ b/src/core/winrt/SDL_winrtapp_direct3d.cpp	Sun Sep 22 12:26:53 2013 -0400
     7.3 @@ -38,6 +38,7 @@
     7.4  
     7.5  #include "../../video/winrt/SDL_winrtevents_c.h"
     7.6  #include "../../video/winrt/SDL_winrtvideo_cpp.h"
     7.7 +#include "SDL_winrtapp_common.h"
     7.8  #include "SDL_winrtapp_direct3d.h"
     7.9  
    7.10  
    7.11 @@ -48,12 +49,6 @@
    7.12  //#define LOG_ORIENTATION_EVENTS 1
    7.13  
    7.14  
    7.15 -// HACK, DLudwig: The C-style main() will get loaded via the app's
    7.16 -// WinRT-styled main(), which is part of SDLmain_for_WinRT.cpp.
    7.17 -// This seems wrong on some level, but does seem to work.
    7.18 -typedef int (*SDL_WinRT_MainFunction)(int, char **);
    7.19 -static SDL_WinRT_MainFunction SDL_WinRT_main = nullptr;
    7.20 -
    7.21  // HACK, DLudwig: record a reference to the global, WinRT 'app'/view.
    7.22  // SDL/WinRT will use this throughout its code.
    7.23  //
    7.24 @@ -83,9 +78,9 @@
    7.25      return app;
    7.26  }
    7.27  
    7.28 -__declspec(dllexport) int SDL_WinRT_RunApplication(SDL_WinRT_MainFunction mainFunction)
    7.29 +int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **))
    7.30  {
    7.31 -    SDL_WinRT_main = mainFunction;
    7.32 +    WINRT_SDLAppEntryPoint = mainFunction;
    7.33      auto direct3DApplicationSource = ref new SDLApplicationSource();
    7.34      CoreApplication::Run(direct3DApplicationSource);
    7.35      return 0;
    7.36 @@ -328,13 +323,13 @@
    7.37  void SDL_WinRTApp::Run()
    7.38  {
    7.39      SDL_SetMainReady();
    7.40 -    if (SDL_WinRT_main)
    7.41 +    if (WINRT_SDLAppEntryPoint)
    7.42      {
    7.43          // TODO, WinRT: pass the C-style main() a reasonably realistic
    7.44          // representation of command line arguments.
    7.45          int argc = 0;
    7.46          char **argv = NULL;
    7.47 -        SDL_WinRT_main(argc, argv);
    7.48 +        WINRT_SDLAppEntryPoint(argc, argv);
    7.49      }
    7.50  }
    7.51  
     8.1 --- a/src/core/winrt/SDL_winrtapp_direct3d.h	Mon Sep 16 22:43:12 2013 -0400
     8.2 +++ b/src/core/winrt/SDL_winrtapp_direct3d.h	Sun Sep 22 12:26:53 2013 -0400
     8.3 @@ -1,5 +1,9 @@
     8.4  #pragma once
     8.5  
     8.6 +#include <Windows.h>
     8.7 +
     8.8 +extern int SDL_WinRTInitNonXAMLApp(int (*mainFunction)(int, char **));
     8.9 +
    8.10  ref class SDL_WinRTApp sealed : public Windows::ApplicationModel::Core::IFrameworkView
    8.11  {
    8.12  public:
     9.1 --- a/src/core/winrt/SDL_winrtapp_xaml.cpp	Mon Sep 16 22:43:12 2013 -0400
     9.2 +++ b/src/core/winrt/SDL_winrtapp_xaml.cpp	Sun Sep 22 12:26:53 2013 -0400
     9.3 @@ -32,13 +32,13 @@
     9.4  #include "SDL.h"
     9.5  #include "../../video/winrt/SDL_winrtevents_c.h"
     9.6  #include "../../video/winrt/SDL_winrtvideo_cpp.h"
     9.7 +#include "SDL_winrtapp_common.h"
     9.8  #include "SDL_winrtapp_xaml.h"
     9.9  
    9.10  
    9.11  
    9.12  /* SDL-internal globals: */
    9.13  SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
    9.14 -int (*WINRT_XAMLAppMainFunction)(int, char **) = NULL;
    9.15  
    9.16  #if WINAPI_FAMILY == WINAPI_FAMILY_APP
    9.17  ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
    9.18 @@ -96,8 +96,8 @@
    9.19   * SDL + XAML Initialization
    9.20   */
    9.21  
    9.22 -extern "C" int
    9.23 -SDL_WinRTInitXAMLApp(Platform::Object ^backgroundPanel, int (*mainFunction)(int, char **))
    9.24 +int
    9.25 +SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable)
    9.26  {
    9.27  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    9.28      return SDL_SetError("XAML support is not yet available in Windows Phone.");
    9.29 @@ -112,10 +112,11 @@
    9.30      using namespace Windows::UI::Xaml::Media;
    9.31  
    9.32      // Make sure we have a valid XAML element (to draw onto):
    9.33 -    if ( ! backgroundPanel) {
    9.34 -        return SDL_SetError("'backgroundPanel' can't be NULL");
    9.35 +    if ( ! backgroundPanelAsIInspectable) {
    9.36 +        return SDL_SetError("'backgroundPanelAsIInspectable' can't be NULL");
    9.37      }
    9.38  
    9.39 +    Platform::Object ^ backgroundPanel = reinterpret_cast<Object ^>((IInspectable *) backgroundPanelAsIInspectable);
    9.40      SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
    9.41      if ( ! swapChainBackgroundPanel) {
    9.42          return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
    9.43 @@ -134,7 +135,7 @@
    9.44      WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));
    9.45  
    9.46      // Make sure the app is ready to call the SDL-centric main() function:
    9.47 -    WINRT_XAMLAppMainFunction = mainFunction;
    9.48 +    WINRT_SDLAppEntryPoint = mainFunction;
    9.49      SDL_SetMainReady();
    9.50  
    9.51      // Make sure video-init knows that we're initializing XAML:
    10.1 --- a/src/core/winrt/SDL_winrtapp_xaml.h	Mon Sep 16 22:43:12 2013 -0400
    10.2 +++ b/src/core/winrt/SDL_winrtapp_xaml.h	Sun Sep 22 12:26:53 2013 -0400
    10.3 @@ -27,7 +27,7 @@
    10.4  
    10.5  #ifdef __cplusplus
    10.6  extern SDL_bool WINRT_XAMLWasEnabled;
    10.7 -extern int (*WINRT_XAMLAppMainFunction)(int, char **);
    10.8 +extern int SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable);
    10.9  #endif // ifdef __cplusplus
   10.10  
   10.11  #endif // ifndef _SDL_winrtapp_xaml_h
    11.1 --- a/src/main/winrt/SDL_winrt_main_NonXAML.cpp	Mon Sep 16 22:43:12 2013 -0400
    11.2 +++ b/src/main/winrt/SDL_winrt_main_NonXAML.cpp	Sun Sep 22 12:26:53 2013 -0400
    11.3 @@ -1,4 +1,5 @@
    11.4  
    11.5 +#include <SDL_main.h>
    11.6  #include <wrl.h>
    11.7  
    11.8  /* At least one file in any SDL/WinRT app appears to require compilation
    11.9 @@ -27,13 +28,6 @@
   11.10  #endif
   11.11  #endif
   11.12  
   11.13 -
   11.14 -/* The app's C-style main will be passed into SDL.dll as a function
   11.15 -   pointer, and called at the appropriate time.
   11.16 -*/
   11.17 -extern __declspec(dllimport) int SDL_WinRT_RunApplication(int (*)(int, char **));
   11.18 -extern "C" int SDL_main(int, char **);
   11.19 -
   11.20  /* Prevent MSVC++ from warning about threading models when defining our
   11.21     custom WinMain.  The threading model will instead be set via a direct
   11.22     call to Windows::Foundation::Initialize (rather than via an attributed
   11.23 @@ -51,13 +45,12 @@
   11.24  #pragma comment(lib, "runtimeobject.lib")
   11.25  #endif
   11.26  
   11.27 -int CALLBACK  WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
   11.28 +int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
   11.29  {
   11.30      if (FAILED(Windows::Foundation::Initialize(RO_INIT_MULTITHREADED))) {
   11.31          return 1;
   11.32      }
   11.33  
   11.34 -    SDL_WinRT_RunApplication(SDL_main);
   11.35 +    SDL_WinRTRunApp(SDL_main, NULL);
   11.36      return 0;
   11.37  }
   11.38 -
    12.1 --- a/src/video/winrt/SDL_winrtevents.cpp	Mon Sep 16 22:43:12 2013 -0400
    12.2 +++ b/src/video/winrt/SDL_winrtevents.cpp	Sun Sep 22 12:26:53 2013 -0400
    12.3 @@ -33,6 +33,7 @@
    12.4   * SDL includes:
    12.5   */
    12.6  #include "SDL_winrtevents_c.h"
    12.7 +#include "../../core/winrt/SDL_winrtapp_common.h"
    12.8  #include "../../core/winrt/SDL_winrtapp_direct3d.h"
    12.9  #include "../../core/winrt/SDL_winrtapp_xaml.h"
   12.10  #include "SDL_assert.h"
   12.11 @@ -55,7 +56,7 @@
   12.12  {
   12.13      if (SDL_WinRTGlobalApp) {
   12.14          SDL_WinRTGlobalApp->PumpEvents();
   12.15 -    } else if (WINRT_XAMLAppMainFunction) {
   12.16 +    } else if (WINRT_XAMLWasEnabled) {
   12.17          WINRT_YieldXAMLThread();
   12.18      }
   12.19  }
   12.20 @@ -95,7 +96,11 @@
   12.21  static int
   12.22  WINRT_XAMLThreadMain(void * userdata)
   12.23  {
   12.24 -    return WINRT_XAMLAppMainFunction(0, NULL);
   12.25 +    // TODO, WinRT: pass the C-style main() a reasonably realistic
   12.26 +    // representation of command line arguments.
   12.27 +    int argc = 0;
   12.28 +    char **argv = NULL;
   12.29 +    return WINRT_SDLAppEntryPoint(argc, argv);
   12.30  }
   12.31  
   12.32  void