docs/README-winrt.md
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 15 Mar 2015 19:25:10 +0100
changeset 9383 62164ad0b7d5
parent 9335 3eb0896ecb91
child 9390 afd88e715bd7
permissions -rw-r--r--
Updated name of assert type in test program.
     1 WinRT
     2 =====
     3 
     4 This port allows SDL applications to run on Microsoft's platforms that require
     5 use of "Windows Runtime", aka. "WinRT", APIs.  WinRT apps are currently
     6 full-screen only, and run in what Microsoft sometimes refers to as their
     7 "Modern" (formerly, "Metro"), environment.  For Windows 8.x, Microsoft may also
     8 refer to them as "Windows Store" apps, due to them being distributed,
     9 primarily, via a Microsoft-run online store (of the same name).
    10 
    11 Some of the operating systems that include WinRT, are:
    12 
    13 * Windows 8.x
    14 * Windows RT 8.x (aka. Windows 8.x for ARM processors)
    15 * Windows Phone 8.x
    16 
    17 
    18 Requirements
    19 ------------
    20 
    21 * Microsoft Visual C++ (aka Visual Studio), either 2013 or 2012 versions
    22   - Free, "Community" or "Express" editions may be used, so long as they
    23     include  support for either "Windows Store" or "Windows Phone" apps.
    24     "Express" versions marked as supporting "Windows Desktop" development
    25     typically do not include support for creating WinRT apps, to note.
    26     (The "Community" edition of Visual C++ 2013 does, however, support both
    27     desktop/Win32 and WinRT development).
    28   - Visual C++ 2012 can only build apps that target versions 8.0 of Windows,
    29     or  Windows Phone.  8.0-targetted apps will run on devices running 8.1
    30     editions of Windows, however they will not be able to take advantage of
    31     8.1-specific features.
    32   - Visual C++ 2013 cannot create app projects that target Windows 8.0.
    33     Visual C++ 2013 Update 4, can create app projects for Windows Phone 8.0,
    34     Windows Phone 8.1, and Windows 8.1, but not Windows 8.0.  An optional
    35     Visual Studio add-in, "Tools for Maintaining Store apps for Windows 8",
    36     allows Visual C++ 2013 to load and build Windows 8.0 projects that were
    37     created with Visual C++ 2012, so long as Visual C++ 2012 is installed
    38     on the same machine.  More details on targeting different versions of
    39     Windows can found at the following web pages:
    40       - [Develop apps by using Visual Studio 2013](http://msdn.microsoft.com/en-us/library/windows/apps/br211384.aspx)
    41       - [To add the Tools for Maintaining Store apps for Windows 8](http://msdn.microsoft.com/en-us/library/windows/apps/dn263114.aspx#AddMaintenanceTools)
    42 * A valid Microsoft account - This requirement is not imposed by SDL, but
    43   rather by Microsoft's Visual C++ toolchain.  This is required to launch or 
    44   debug apps.
    45 
    46 
    47 Status
    48 ------
    49 
    50 Here is a rough list of what works, and what doens't:
    51 
    52 * What works:
    53   * compilation via Visual C++ 2012 and 2013
    54   * compile-time platform detection for SDL programs.  The C/C++ #define,
    55     `__WINRT__`, will be set to 1 (by SDL) when compiling for WinRT.
    56   * GPU-accelerated 2D rendering, via SDL_Renderer.
    57   * software rendering, via either SDL_Surface (optionally in conjunction with
    58     SDL_GetWindowSurface() and SDL_UpdateWindowSurface()) or via the
    59     SDL_Renderer APIs
    60   * threads.  Significant chunks of Win32's threading APIs are not available in
    61     WinRT.  A new, SDL threading backend was built using C++11's threading APIs
    62     (std::thread, std::mutex, std::condition_variable, etc.), which C or C++
    63     programs alike can access via SDL's threading APIs.  Support for thread
    64     priorities is not, however, currently available, due to restrictions in
    65     WinRT's own API set.
    66   * timers (via SDL_GetTicks(), SDL_AddTimer(), SDL_GetPerformanceCounter(),
    67     SDL_GetPerformanceFrequency(), etc.)
    68   * file I/O via SDL_RWops
    69   * mouse input  (unsupported on Windows Phone)
    70   * audio, via a modified version of SDL's XAudio2 backend
    71   * .DLL file loading.  Libraries must be packaged inside applications.  Loading
    72     anything outside of the app is not supported.
    73   * system path retrieval via SDL's filesystem APIs
    74   * game controllers.  Support is provided via the SDL_Joystick and
    75     SDL_GameController APIs, and is backed by Microsoft's XInput API.
    76   * multi-touch input
    77   * app events.  SDL_APP_WILLENTER* and SDL_APP_DIDENTER* events get sent out as
    78     appropriate.
    79   * window events.  SDL_WINDOWEVENT_MINIMIZED and SDL_WINDOWEVENT_RESTORED are
    80     sent out on app suspend and resume, respectively.  SDL_WINDOWEVENT_SHOWN and
    81     SDL_WINDOWEVENT_HIDDEN are also sent, but not necessarily on app suspend or
    82     resume, as WinRT treats these two concepts differently..
    83   * using Direct3D 11.x APIs outside of SDL.  Non-XAML / Direct3D-only apps can
    84     choose to render content directly via Direct3D, using SDL to manage the
    85     internal WinRT window, as well as input and audio.  (Use
    86     SDL_GetWindowWMInfo() to get the WinRT 'CoreWindow', and pass it into
    87     IDXGIFactory2::CreateSwapChainForCoreWindow() as appropriate.)
    88 
    89 * What partially works:
    90   * keyboard input.  Most of WinRT's documented virtual keys are supported, as
    91     well as many keys with documented hardware scancodes.
    92   * OpenGL.  Experimental support for OpenGL ES 2 is available via the ANGLE
    93     project, using either MS Open Technologies' repository, at 
    94     https://github.com/msopentech/angle (both the "winrt" and "future-dev"
    95     branches are supported), or the official ANGLE repository, at
    96     https://chromium.googlesource.com/angle/angle
    97   * SDLmain.  WinRT uses a different signature for each app's main() function.
    98     SDL-based apps that use this port must compile in SDL_winrt_main_NonXAML.cpp
    99     (in `SDL\src\main\winrt\`) directly in order for their C-style main()
   100     functions to be called.
   101   * XAML interoperability.  This feature is currently experimental (there are
   102     **many** known bugs in this, at present!), preliminary, and only for
   103     Windows 8.x/RT at the moment.  Windows Phone + XAML support is still
   104     pending.
   105 
   106 * What doesn't work:
   107   * compilation with anything other than Visual C++ 2012 or 2013
   108   * programmatically-created custom cursors.  These don't appear to be supported
   109     by WinRT.  Different OS-provided cursors can, however, be created via
   110     SDL_CreateSystemCursor() (unsupported on Windows Phone)
   111   * SDL_WarpMouseInWindow() or SDL_WarpMouseGlobal().  This are not currently
   112     supported by WinRT itself.
   113   * joysticks and game controllers that aren't supported by Microsoft's XInput
   114     API.
   115   * turning off VSync when rendering on Windows Phone.  Attempts to turn VSync
   116     off on Windows Phone result either in Direct3D not drawing anything, or it
   117     forcing VSync back on.  As such, SDL_RENDERER_PRESENTVSYNC will always get
   118     turned-on on Windows Phone.  This limitation is not present in non-Phone
   119     WinRT (such as Windows 8.x), where turning off VSync appears to work.
   120   * probably anything else that's not listed as supported
   121 
   122 
   123 
   124 Upgrade Notes
   125 -------------
   126 
   127 #### SDL_GetPrefPath() usage when upgrading WinRT apps from SDL 2.0.3
   128 
   129 SDL 2.0.4 fixes two bugs found in the WinRT version of SDL_GetPrefPath().
   130 The fixes may affect older, SDL 2.0.3-based apps' save data.  Please note
   131 that these changes only apply to SDL-based WinRT apps, and not to apps for
   132 any other platform.
   133 
   134 1. SDL_GetPrefPath() would return an invalid path, one in which the path's
   135    directory had not been created.  Attempts to create files there
   136    (via fopen(), for example), would fail, unless that directory was
   137    explicitly created beforehand.
   138 
   139 2. SDL_GetPrefPath(), for non-WinPhone-based apps, would return a path inside
   140    a WinRT 'Roaming' folder, the contents of which get automatically
   141    synchronized across multiple devices.  This process can occur while an
   142    application runs, and can cause existing save-data to be overwritten
   143    at unexpected times, with data from other devices.  (Windows Phone apps
   144    written with SDL 2.0.3 did not utilize a Roaming folder, due to API
   145    restrictions in Windows Phone 8.0).
   146 
   147 
   148 SDL_GetPrefPath(), starting with SDL 2.0.4, addresses these by:
   149 
   150 1. making sure that SDL_GetPrefPath() returns a directory in which data
   151    can be written to immediately, without first needing to create directories.
   152 
   153 2. basing SDL_GetPrefPath() off of a different, non-Roaming folder, the
   154    contents of which do not automatically get synchronized across devices
   155    (and which require less work to use safely, in terms of data integrity).
   156 
   157 Apps that wish to get their Roaming folder's path can do so either by using
   158 SDL_WinRTGetFSPathUTF8(), SDL_WinRTGetFSPathUNICODE() (which returns a
   159 UCS-2/wide-char string), or directly through the WinRT class,
   160 Windows.Storage.ApplicationData.
   161 
   162 
   163 
   164 Setup, High-Level Steps
   165 -----------------------
   166 
   167 The steps for setting up a project for an SDL/WinRT app looks like the
   168 following, at a high-level:
   169 
   170 1. create a new Visual C++ project using Microsoft's template for a,
   171    "Direct3D App".
   172 2. remove most of the files from the project.
   173 3. make your app's project directly reference SDL/WinRT's own Visual C++
   174    project file, via use of Visual C++'s "References" dialog.  This will setup
   175    the linker, and will copy SDL's .dll files to your app's final output.
   176 4. adjust your app's build settings, at minimum, telling it where to find SDL's
   177    header files.
   178 5. add a file that contains a WinRT-appropriate main function.
   179 6. add SDL-specific app code.
   180 7. build and run your app.
   181 
   182 
   183 Setup, Detailed Steps
   184 ---------------------
   185 
   186 ### 1. Create a new project ###
   187 
   188 Create a new project using one of Visual C++'s templates for a plain, non-XAML,
   189 "Direct3D App" (XAML support for SDL/WinRT is not yet ready for use).  If you
   190 don't see one of these templates, in Visual C++'s 'New Project' dialog, try
   191 using the textbox titled, 'Search Installed Templates' to look for one.
   192 
   193 
   194 ### 2. Remove unneeded files from the project ###
   195 
   196 In the new project, delete any file that has one of the following extensions:
   197 
   198 - .cpp
   199 - .h
   200 - .hlsl
   201 
   202 When you are done, you should be left with a few files, each of which will be a
   203 necessary part of your app's project.  These files will consist of:
   204 
   205 - an .appxmanifest file, which contains metadata on your WinRT app.  This is
   206   similar to an Info.plist file on iOS, or an AndroidManifest.xml on Android.
   207 - a few .png files, one of which is a splash screen (displayed when your app
   208   launches), others are app icons.
   209 - a .pfx file, used for code signing purposes.
   210 
   211 
   212 ### 3. Add references to SDL's project files ###
   213 
   214 SDL/WinRT can be built in multiple variations, spanning across three different
   215 CPU architectures (x86, x64, and ARM) and two different configurations
   216 (Debug and Release).  WinRT and Visual C++ do not currently provide a means
   217 for combining multiple variations of one library into a single file.
   218 Furthermore, it does not provide an easy means for copying pre-built .dll files
   219 into your app's final output (via Post-Build steps, for example).  It does,
   220 however, provide a system whereby an app can reference the MSVC projects of
   221 libraries such that, when the app is built:
   222 
   223 1. each library gets built for the appropriate CPU architecture(s) and WinRT
   224    platform(s).
   225 2. each library's output, such as .dll files, get copied to the app's build 
   226    output.
   227 
   228 To set this up for SDL/WinRT, you'll need to run through the following steps:
   229 
   230 1. open up the Solution Explorer inside Visual C++ (under the "View" menu, then
   231    "Solution Explorer")
   232 2. right click on your app's solution.
   233 3. navigate to "Add", then to "Existing Project..."
   234 4. find SDL/WinRT's Visual C++ project file and open it.  Different project
   235    files exist for different WinRT platforms.  All of them are in SDL's
   236    source distribution, in the following directories:
   237     * `VisualC-WinRT/WinPhone80_VS2012/` - for Windows Phone 8.0 apps
   238     * `VisualC-WinRT/WinPhone81_VS2013/` - for Windows Phone 8.1 apps
   239     * `VisualC-WinRT/WinRT80_VS2012/` - for Windows 8.0 apps
   240     * `VisualC-WinRT/WinRT81_VS2013/` - for Windows 8.1 apps
   241 5. once the project has been added, right-click on your app's project and
   242    select, "References..."
   243 6. click on the button titled, "Add New Reference..."
   244 7. check the box next to SDL
   245 8. click OK to close the dialog
   246 9. SDL will now show up in the list of references.  Click OK to close that
   247    dialog.
   248 
   249 Your project is now linked to SDL's project, insofar that when the app is
   250 built, SDL will be built as well, with its build output getting included with
   251 your app.
   252 
   253 
   254 ### 4. Adjust Your App's Build Settings ###
   255 
   256 Some build settings need to be changed in your app's project.  This guide will
   257 outline the following:
   258 
   259 - making sure that the compiler knows where to find SDL's header files
   260 - **Optional for C++, but NECESSARY for compiling C code:** telling the
   261   compiler not to use Microsoft's C++ extensions for WinRT development.
   262 - **Optional:** telling the compiler not generate errors due to missing
   263   precompiled header files.
   264 
   265 To change these settings:
   266 
   267 1. right-click on the project
   268 2. choose "Properties"
   269 3. in the drop-down box next to "Configuration", choose, "All Configurations"
   270 4. in the drop-down box next to "Platform", choose, "All Platforms"
   271 5. in the left-hand list, expand the "C/C++" section
   272 6. select "General"
   273 7. edit the "Additional Include Directories" setting, and add a path to SDL's
   274    "include" directory
   275 8. **Optional: to enable compilation of C code:** change the setting for
   276    "Consume Windows Runtime Extension" from "Yes (/ZW)" to "No".  If you're 
   277    working with a completely C++ based project, this step can usually be 
   278    omitted.
   279 9. **Optional: to disable precompiled headers (which can produce 
   280    'stdafx.h'-related build errors, if setup incorrectly:** in the left-hand 
   281    list, select "Precompiled Headers", then change the setting for "Precompiled 
   282    Header" from "Use (/Yu)" to "Not Using Precompiled Headers".
   283 10. close the dialog, saving settings, by clicking the "OK" button
   284 
   285 
   286 ### 5. Add a WinRT-appropriate main function to the app. ###
   287 
   288 C/C++-based WinRT apps do contain a `main` function that the OS will invoke when 
   289 the app starts launching. The parameters of WinRT main functions are different 
   290 than those found on other platforms, Win32 included.  SDL/WinRT provides a 
   291 platform-appropriate main function that will perform these actions, setup key 
   292 portions of the app, then invoke a classic, C/C++-style main function (that take 
   293 in "argc" and "argv" parameters).  The code for this file is contained inside 
   294 SDL's source distribution, under `src/main/winrt/SDL_winrt_main_NonXAML.cpp`.  
   295 You'll need to add this file, or a copy of it, to your app's project, and make 
   296 sure it gets compiled using a Microsoft-specific set of C++ extensions called 
   297 C++/CX.
   298 
   299 **NOTE: C++/CX compilation is currently required in at least one file of your 
   300 app's project.  This is to make sure that Visual C++'s linker builds a 'Windows 
   301 Metadata' file (.winmd) for your app.  Not doing so can lead to build errors.**
   302 
   303 To include `SDL_winrt_main_NonXAML.cpp`:
   304 
   305 1. right-click on your project (again, in Visual C++'s Solution Explorer), 
   306    navigate to "Add", then choose "Existing Item...".
   307 2. open `SDL_winrt_main_NonXAML.cpp`, which is found inside SDL's source 
   308    distribution, under `src/main/winrt/`.  Make sure that the open-file dialog 
   309    closes, either by double-clicking on the file, or single-clicking on it and 
   310    then clicking Add.
   311 3. right-click on the file (as listed in your project), then click on 
   312    "Properties...".
   313 4. in the drop-down box next to "Configuration", choose, "All Configurations"
   314 5. in the drop-down box next to "Platform", choose, "All Platforms"
   315 6. in the left-hand list, click on "C/C++"
   316 7. change the setting for "Consume Windows Runtime Extension" to "Yes (/ZW)".
   317 8. click the OK button.  This will close the dialog.
   318 
   319 
   320 ### 6. Add app code and assets ###
   321 
   322 At this point, you can add in SDL-specific source code.  Be sure to include a 
   323 C-style main function (ie: `int main(int argc, char *argv[])`).  From there you 
   324 should be able to create a single `SDL_Window` (WinRT apps can only have one 
   325 window, at present), as well as an `SDL_Renderer`.  Direct3D will be used to 
   326 draw content.  Events are received via SDL's usual event functions 
   327 (`SDL_PollEvent`, etc.)  If you have a set of existing source files and assets, 
   328 you can start adding them to the project now.  If not, or if you would like to 
   329 make sure that you're setup correctly, some short and simple sample code is 
   330 provided below.
   331 
   332 
   333 #### 6.A. ... when creating a new app ####
   334 
   335 If you are creating a new app (rather than porting an existing SDL-based app), 
   336 or if you would just like a simple app to test SDL/WinRT with before trying to 
   337 get existing code working, some working SDL/WinRT code is provided below.  To 
   338 set this up:
   339 
   340 1. right click on your app's project
   341 2. select Add, then New Item.  An "Add New Item" dialog will show up.
   342 3. from the left-hand list, choose "Visual C++"
   343 4. from the middle/main list, choose "C++ File (.cpp)"
   344 5. near the bottom of the dialog, next to "Name:", type in a name for your 
   345 source file, such as, "main.cpp".
   346 6. click on the Add button.  This will close the dialog, add the new file to 
   347 your project, and open the file in Visual C++'s text editor.
   348 7. Copy and paste the following code into the new file, then save it.
   349 
   350 
   351     #include <SDL.h>
   352     
   353     int main(int argc, char **argv)
   354     {
   355         SDL_DisplayMode mode;
   356         SDL_Window * window = NULL;
   357         SDL_Renderer * renderer = NULL;
   358         SDL_Event evt;
   359     
   360         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
   361             return 1;
   362         }
   363     
   364         if (SDL_GetCurrentDisplayMode(0, &mode) != 0) {
   365             return 1;
   366         }
   367     
   368         if (SDL_CreateWindowAndRenderer(mode.w, mode.h, SDL_WINDOW_FULLSCREEN, &window, &renderer) != 0) {
   369             return 1;
   370         }
   371     
   372         while (1) {
   373             while (SDL_PollEvent(&evt)) {
   374             }
   375     
   376             SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
   377             SDL_RenderClear(renderer);
   378             SDL_RenderPresent(renderer);
   379         }
   380     }
   381 
   382 
   383 #### 6.B. Adding code and assets ####
   384 
   385 If you have existing code and assets that you'd like to add, you should be able 
   386 to add them now.  The process for adding a set of files is as such.
   387 
   388 1. right click on the app's project
   389 2. select Add, then click on "New Item..."
   390 3. open any source, header, or asset files as appropriate.  Support for C and 
   391 C++ is available.
   392 
   393 Do note that WinRT only supports a subset of the APIs that are available to 
   394 Win32-based apps.  Many portions of the Win32 API and the C runtime are not 
   395 available.
   396 
   397 A list of unsupported C APIs can be found at 
   398 <http://msdn.microsoft.com/en-us/library/windows/apps/jj606124.aspx>
   399 
   400 General information on using the C runtime in WinRT can be found at 
   401 <http://msdn.microsoft.com/en-us/LIBRARY/hh972425(v=vs.110).aspx>
   402 
   403 A list of supported Win32 APIs for Windows 8/RT apps can be found at 
   404 <http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx>.  To note, 
   405 the list of supported Win32 APIs for Windows Phone 8 development is different.  
   406 That list can be found at 
   407 <http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662956(v=vs.105).aspx>
   408 
   409 
   410 ### 7. Build and run your app ###
   411 
   412 Your app project should now be setup, and you should be ready to build your app.  
   413 To run it on the local machine, open the Debug menu and choose "Start 
   414 Debugging".  This will build your app, then run your app full-screen.  To switch 
   415 out of your app, press the Windows key.  Alternatively, you can choose to run 
   416 your app in a window.  To do this, before building and running your app, find 
   417 the drop-down menu in Visual C++'s toolbar that says, "Local Machine".  Expand 
   418 this by clicking on the arrow on the right side of the list, then click on 
   419 Simulator.  Once you do that, any time you build and run the app, the app will 
   420 launch in window, rather than full-screen.
   421 
   422 
   423 #### 7.A. Running apps on ARM-based devices ####
   424 
   425 To build and run the app on ARM-based, "Windows RT" devices, you'll need to:
   426 
   427 - install Microsoft's "Remote Debugger" on the device.  Visual C++ installs and 
   428   debugs ARM-based apps via IP networks.
   429 - change a few options on the development machine, both to make sure it builds 
   430   for ARM (rather than x86 or x64), and to make sure it knows how to find the 
   431   Windows RT device (on the network).
   432 
   433 Microsoft's Remote Debugger can be found at 
   434 <http://msdn.microsoft.com/en-us/library/vstudio/bt727f1t.aspx>.  Please note 
   435 that separate versions of this debugger exist for different versions of Visual 
   436 C++, one for debugging with MSVC 2012, another for debugging with MSVC 2013.
   437 
   438 To setup Visual C++ to launch your app on an ARM device:
   439 
   440 1. make sure the Remote Debugger is running on your ARM device, and that it's on 
   441    the same IP network as your development machine.
   442 2. from Visual C++'s toolbar, find a drop-down menu that says, "Win32".  Click 
   443    it, then change the value to "ARM".
   444 3. make sure Visual C++ knows the hostname or IP address of the ARM device.  To 
   445    do this:
   446     1. open the app project's properties
   447     2. select "Debugging"
   448     3. next to "Machine Name", enter the hostname or IP address of the ARM 
   449        device
   450     4. if, and only if, you've turned off authentication in the Remote Debugger,
   451        then change the setting for "Require Authentication" to No
   452     5. click "OK"
   453 4. build and run the app (from Visual C++).  The first time you do this, a 
   454    prompt will show up on the ARM device, asking for a Microsoft Account.  You 
   455    do, unfortunately, need to log in here, and will need to follow the 
   456    subsequent registration steps in order to launch the app.  After you do so, 
   457    if the app didn't already launch, try relaunching it again from within Visual 
   458    C++.
   459 
   460 
   461 Troubleshooting
   462 ---------------
   463 
   464 #### Build fails with message, "error LNK2038: mismatch detected for 'vccorlib_lib_should_be_specified_before_msvcrt_lib_to_linker'"
   465 
   466 Try adding the following to your linker flags.  In MSVC, this can be done by
   467 right-clicking on the app project, navigating to Configuration Properties ->
   468 Linker -> Command Line, then adding them to the Additional Options
   469 section.
   470 
   471 * For Release builds / MSVC-Configurations, add:
   472 
   473     /nodefaultlib:vccorlib /nodefaultlib:msvcrt vccorlib.lib msvcrt.lib
   474 
   475 * For Debug builds / MSVC-Configurations, add:
   476 
   477     /nodefaultlib:vccorlibd /nodefaultlib:msvcrtd vccorlibd.lib msvcrtd.lib
   478