docs/README-winrt.md
author David Ludwig <dludwig@pobox.com>
Sat, 01 Nov 2014 11:41:18 -0400
changeset 9210 1486f4f3c67c
parent 9209 e78fb0f4f9f9
child 9215 6894461723ab
permissions -rw-r--r--
WinRT: moved documentation + setup related TODO items out of README-winrt.md

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