docs/README-winrt.md
author David Ludwig <dludwig@pobox.com>
Fri, 19 Sep 2014 11:27:18 -0400
changeset 9149 e910a192f68f
parent 9143 b664273a455c
child 9150 5c42e467f6cd
permissions -rw-r--r--
WinRT: fixed broken emphasis formatting in README, when rendering it with Doxygen
     1 WinRT
     2 =====
     3 
     4 SDL/WinRT layer allows SDL2-based applications to run on many of Microsoft's
     5 platforms that utilize the "Windows Runtime" (aka "WinRT") APIs.  WinRT apps
     6 are currently always full-screen apps, run in what Microsoft calls their
     7 "Modern" environment (aka. "Metro"), and are distributed via Microsoft-run
     8 online stores.  Some of the operating systems that support such apps include:
     9 
    10 * Windows 8.x
    11 * Windows RT 8.x (aka. Windows 8.x for ARM processors)
    12 * Windows Phone 8.x
    13 
    14 To note, WinRT applications that run on Windows 8.x and/or Windows RT are often
    15 called "Windows Store" apps.
    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 Setup, High-Level Steps
    45 -----------------------
    46 
    47 The steps for setting up a project for an SDL/WinRT app looks like the
    48 following, at a high-level:
    49 
    50 1. create a new Visual C++ project using Microsoft's template for a,
    51    "Direct3D App".
    52 2. remove most of the files from the project.
    53 3. make your app's project directly reference SDL/WinRT's own Visual C++
    54    project file, via use of Visual C++'s "References" dialog.  This will setup
    55    the linker, and will copy SDL's .dll files to your app's final output.
    56 4. adjust your app's build settings, at minimum, telling it where to find SDL's
    57    header files.
    58 5. add a file that contains a WinRT-appropriate main function.
    59 6. add SDL-specific app code.
    60 7. build and run your app.
    61 
    62 
    63 Setup, Detailed Steps
    64 ---------------------
    65 
    66 ### 1. Create a new project ###
    67 
    68 Create a new project using one of Visual C++'s templates for a plain, non-XAML,
    69 "Direct3D App" (XAML support for SDL/WinRT is not yet ready for use).  If you
    70 don't see one of these templates, in Visual C++'s 'New Project' dialog, try
    71 using the textbox titled, 'Search Installed Templates' to look for one.
    72 
    73 
    74 ### 2. Remove unneeded files from the project ###
    75 
    76 In the new project, delete any file that has one of the following extensions:
    77 
    78 - .cpp
    79 - .h
    80 - .hlsl
    81 
    82 When you are done, you should be left with a few files, each of which will be a
    83 necessary part of your app's project.  These files will consist of:
    84 
    85 - an .appxmanifest file, which contains metadata on your WinRT app.  This is
    86   similar to an Info.plist file on iOS, or an AndroidManifest.xml on Android.
    87 - a few .png files, one of which is a splash screen (displayed when your app
    88   launches), others are app icons.
    89 - a .pfx file, used for code signing purposes.
    90 
    91 
    92 ### 3. Add references to SDL's project files ###
    93 
    94 SDL/WinRT can be built in multiple variations, spanning across three different
    95 CPU architectures (x86, x64, and ARM) and two different configurations
    96 (Debug and Release).  WinRT and Visual C++ do not currently provide a means
    97 for combining multiple variations of one library into a single file.
    98 Furthermore, it does not provide an easy means for copying pre-built .dll files
    99 into your app's final output (via Post-Build steps, for example).  It does,
   100 however, provide a system whereby an app can reference the MSVC projects of
   101 libraries such that, when the app is built:
   102 
   103 1. each library gets built for the appropriate CPU architecture(s) and WinRT
   104    platform(s).
   105 2. each library's output, such as .dll files, get copied to the app's build 
   106    output.
   107 
   108 To set this up for SDL/WinRT, you'll need to run through the following steps:
   109 
   110 1. open up the Solution Explorer inside Visual C++ (under the "View" menu, then
   111    "Solution Explorer")
   112 2. right click on your app's solution.
   113 3. navigate to "Add", then to "Existing Project..."
   114 4. find SDL/WinRT's Visual C++ project file and open it.  Different project
   115    files exist for different WinRT platforms.  All of them are in SDL's
   116    source distribution, in the following directories:
   117     * `VisualC-WinRT/WinPhone80_VS2012/` - for Windows Phone 8.0 apps
   118     * `VisualC-WinRT/WinPhone81_VS2013/` - for Windows Phone 8.1 apps
   119     * `VisualC-WinRT/WinRT80_VS2012/` - for Windows 8.0 apps
   120     * `VisualC-WinRT/WinRT81_VS2013/` - for Windows 8.1 apps
   121 5. once the project has been added, right-click on your app's project and
   122    select, "References..."
   123 6. click on the button titled, "Add New Reference..."
   124 7. check the box next to SDL
   125 8. click OK to close the dialog
   126 9. SDL will now show up in the list of references.  Click OK to close that
   127    dialog.
   128 
   129 Your project is now linked to SDL's project, insofar that when the app is
   130 built, SDL will be built as well, with its build output getting included with
   131 your app.
   132 
   133 
   134 ### 4. Adjust Your App's Build Settings ###
   135 
   136 Some build settings need to be changed in your app's project.  This guide will
   137 outline the following:
   138 
   139 - making sure that the compiler knows where to find SDL's header files
   140 - **Optional for C++, but NECESSARY for compiling C code:** telling the
   141   compiler not to use Microsoft's C++ extensions for WinRT development.
   142 - **Optional:** telling the compiler not generate errors due to missing
   143   precompiled header files.
   144 
   145 To change these settings:
   146 
   147 1. right-click on the project
   148 2. choose "Properties"
   149 3. in the drop-down box next to "Configuration", choose, "All Configurations"
   150 4. in the drop-down box next to "Platform", choose, "All Platforms"
   151 5. in the left-hand list, expand the "C/C++" section
   152 6. select "General"
   153 7. edit the "Additional Include Directories" setting, and add a path to SDL's
   154    "include" directory
   155 8. **Optional: to enable compilation of C code:** change the setting for
   156    "Consume Windows Runtime Extension" from "Yes (/ZW)" to "No".  If you're 
   157    working with a completely C++ based project, this step can usually be 
   158    omitted.
   159 9. **Optional: to disable precompiled headers (which can produce 
   160    'stdafx.h'-related build errors, if setup incorrectly:** in the left-hand 
   161    list, select "Precompiled Headers", then change the setting for "Precompiled 
   162    Header" from "Use (/Yu)" to "Not Using Precompiled Headers".
   163 10. close the dialog, saving settings, by clicking the "OK" button
   164 
   165 
   166 ### 5. Add a WinRT-appropriate main function to the app. ###
   167 
   168 C/C++-based WinRT apps do contain a `main` function that the OS will invoke when 
   169 the app starts launching. The parameters of WinRT main functions are different 
   170 than those found on other platforms, Win32 included.  SDL/WinRT provides a 
   171 platform-appropriate main function that will perform these actions, setup key 
   172 portions of the app, then invoke a classic, C/C++-style main function (that take 
   173 in "argc" and "argv" parameters).  The code for this file is contained inside 
   174 SDL's source distribution, under `src/main/winrt/SDL_winrt_main_NonXAML.cpp`.  
   175 You'll need to add this file, or a copy of it, to your app's project, and make 
   176 sure it gets compiled using a Microsoft-specific set of C++ extensions called 
   177 C++/CX.
   178 
   179 **NOTE: C++/CX compilation is currently required in at least one file of your 
   180 app's project.  This is to make sure that Visual C++'s linker builds a 'Windows 
   181 Metadata' file (.winmd) for your app.  Not doing so can lead to build errors.**
   182 
   183 To include `SDL_winrt_main_NonXAML.cpp`:
   184 
   185 1. right-click on your project (again, in Visual C++'s Solution Explorer), 
   186    navigate to "Add", then choose "Existing Item...".
   187 2. open `SDL_winrt_main_NonXAML.cpp`, which is found inside SDL's source 
   188    distribution, under `src/main/winrt/`.  Make sure that the open-file dialog 
   189    closes, either by double-clicking on the file, or single-clicking on it and 
   190    then clicking Add.
   191 3. right-click on the file (as listed in your project), then click on 
   192    "Properties...".
   193 4. in the drop-down box next to "Configuration", choose, "All Configurations"
   194 5. in the drop-down box next to "Platform", choose, "All Platforms"
   195 6. in the left-hand list, click on "C/C++"
   196 7. change the setting for "Consume Windows Runtime Extension" to "Yes (/ZW)".
   197 8. click the OK button.  This will close the dialog.
   198 
   199 
   200 ### 6. Add app code and assets ###
   201 
   202 At this point, you can add in SDL-specific source code.  Be sure to include a 
   203 C-style main function (ie: `int main(int argc, char *argv[])`).  From there you 
   204 should be able to create a single `SDL_Window` (WinRT apps can only have one 
   205 window, at present), as well as an `SDL_Renderer`.  Direct3D will be used to 
   206 draw content.  Events are received via SDL's usual event functions 
   207 (`SDL_PollEvent`, etc.)  If you have a set of existing source files and assets, 
   208 you can start adding them to the project now.  If not, or if you would like to 
   209 make sure that you're setup correctly, some short and simple sample code is 
   210 provided below.
   211 
   212 
   213 #### 6.A. ... when creating a new app ####
   214 
   215 If you are creating a new app (rather than porting an existing SDL-based app), 
   216 or if you would just like a simple app to test SDL/WinRT with before trying to 
   217 get existing code working, some working SDL/WinRT code is provided below.  To 
   218 set this up:
   219 
   220 1. right click on your app's project
   221 2. select Add, then New Item.  An "Add New Item" dialog will show up.
   222 3. from the left-hand list, choose "Visual C++"
   223 4. from the middle/main list, choose "C++ File (.cpp)"
   224 5. near the bottom of the dialog, next to "Name:", type in a name for your 
   225 source file, such as, "main.cpp".
   226 6. click on the Add button.  This will close the dialog, add the new file to 
   227 your project, and open the file in Visual C++'s text editor.
   228 7. Copy and paste the following code into the new file, then save it.
   229 
   230 
   231     #include <SDL.h>
   232     
   233     int main(int argc, char **argv)
   234     {
   235         SDL_DisplayMode mode;
   236         SDL_Window * window = NULL;
   237         SDL_Renderer * renderer = NULL;
   238         SDL_Event evt;
   239     
   240         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
   241             return 1;
   242         }
   243     
   244         if (SDL_GetCurrentDisplayMode(0, &mode) != 0) {
   245             return 1;
   246         }
   247     
   248         if (SDL_CreateWindowAndRenderer(mode.w, mode.h, SDL_WINDOW_FULLSCREEN, &window, &renderer) != 0) {
   249             return 1;
   250         }
   251     
   252         while (1) {
   253             while (SDL_PollEvent(&evt)) {
   254             }
   255     
   256             SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
   257             SDL_RenderClear(renderer);
   258             SDL_RenderPresent(renderer);
   259         }
   260     }
   261 
   262 
   263 #### 6.B. Adding code and assets ####
   264 
   265 If you have existing code and assets that you'd like to add, you should be able 
   266 to add them now.  The process for adding a set of files is as such.
   267 
   268 1. right click on the app's project
   269 2. select Add, then click on "New Item..."
   270 3. open any source, header, or asset files as appropriate.  Support for C and 
   271 C++ is available.
   272 
   273 Do note that WinRT only supports a subset of the APIs that are available to 
   274 Win32-based apps.  Many portions of the Win32 API and the C runtime are not 
   275 available.
   276 
   277 A list of unsupported C APIs can be found at 
   278 <http://msdn.microsoft.com/en-us/library/windows/apps/jj606124.aspx>
   279 
   280 General information on using the C runtime in WinRT can be found at 
   281 <http://msdn.microsoft.com/en-us/LIBRARY/hh972425(v=vs.110).aspx>
   282 
   283 A list of supported Win32 APIs for Windows 8/RT apps can be found at 
   284 <http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx>.  To note, 
   285 the list of supported Win32 APIs for Windows Phone 8 development is different.  
   286 That list can be found at 
   287 <http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662956(v=vs.105).aspx>
   288 
   289 
   290 ### 7. Build and run your app ###
   291 
   292 Your app project should now be setup, and you should be ready to build your app.  
   293 To run it on the local machine, open the Debug menu and choose "Start 
   294 Debugging".  This will build your app, then run your app full-screen.  To switch 
   295 out of your app, press the Windows key.  Alternatively, you can choose to run 
   296 your app in a window.  To do this, before building and running your app, find 
   297 the drop-down menu in Visual C++'s toolbar that says, "Local Machine".  Expand 
   298 this by clicking on the arrow on the right side of the list, then click on 
   299 Simulator.  Once you do that, any time you build and run the app, the app will 
   300 launch in window, rather than full-screen.
   301 
   302 
   303 #### 7.A. Running apps on ARM-based devices ####
   304 
   305 To build and run the app on ARM-based, "Windows RT" devices, you'll need to:
   306 
   307 - install Microsoft's "Remote Debugger" on the device.  Visual C++ installs and 
   308   debugs ARM-based apps via IP networks.
   309 - change a few options on the development machine, both to make sure it builds 
   310   for ARM (rather than x86 or x64), and to make sure it knows how to find the 
   311   Windows RT device (on the network).
   312 
   313 Microsoft's Remote Debugger can be found at 
   314 <http://msdn.microsoft.com/en-us/library/vstudio/bt727f1t.aspx>.  Please note 
   315 that separate versions of this debugger exist for different versions of Visual 
   316 C++, one for debugging with MSVC 2012, another for debugging with MSVC 2013.
   317 
   318 To setup Visual C++ to launch your app on an ARM device:
   319 
   320 1. make sure the Remote Debugger is running on your ARM device, and that it's on 
   321    the same IP network as your development machine.
   322 2. from Visual C++'s toolbar, find a drop-down menu that says, "Win32".  Click 
   323    it, then change the value to "ARM".
   324 3. make sure Visual C++ knows the hostname or IP address of the ARM device.  To 
   325    do this:
   326     1. open the app project's properties
   327     2. select "Debugging"
   328     3. next to "Machine Name", enter the hostname or IP address of the ARM 
   329        device
   330     4. if, and only if, you've turned off authentication in the Remote Debugger, then change the setting for "Require Authentication" to No
   331     5. click "OK"
   332 4. build and run the app (from Visual C++).  The first time you do this, a 
   333    prompt will show up on the ARM device, asking for a Microsoft Account.  You 
   334    do, unfortunately, need to log in here, and will need to follow the 
   335    subsequent registration steps in order to launch the app.  After you do so, 
   336    if the app didn't already launch, try relaunching it again from within Visual 
   337    C++.
   338 
   339 
   340 TODO
   341 ----
   342 
   343 - Document details of SDL satellite library support
   344 - Make [NuGet](https://www.nuget.org) packages for SDL/WinRT
   345 - Create templates for both MSVC 2012 and MSVC 2013, and have the corresponding
   346   VSIX packages either include pre-built copies of SDL, or reference binaries
   347   available via MSVC's NuGet servers
   348     - Write setup instructions that use MSVC 201x templates
   349 - Write a list of caveats found in SDL/WinRT, such as APIs that don't work due
   350   to platform restrictions, or things that need further work