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