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