This port allows SDL applications to run on Microsoft's platforms that require use of "Windows Runtime", aka. "WinRT", APIs. Microsoft may, in some cases, refer to them as either "Windows Store", or for Windows 10, "UWP" apps.
In the past, SDL has supported Windows RT 8.x, Windows Phone, etc, but in modern times this port is focused on UWP apps, which run on Windows 10, and modern Xbox consoles.
Here is a rough list of what works, and what doesn't:
What works:
__WINRT__
, will be set to 1 (by SDL) when compiling for WinRT.What partially works:
SDL\src\main\winrt\
) directly in order for their C-style main()
functions to be called.What doesn't work:
SDL 2.0.4 fixes two bugs found in the WinRT version of SDL_GetPrefPath(). The fixes may affect older, SDL 2.0.3-based apps' save data. Please note that these changes only apply to SDL-based WinRT apps, and not to apps for any other platform.
SDL_GetPrefPath() would return an invalid path, one in which the path's directory had not been created. Attempts to create files there (via fopen(), for example), would fail, unless that directory was explicitly created beforehand.
SDL_GetPrefPath(), for non-WinPhone-based apps, would return a path inside a WinRT 'Roaming' folder, the contents of which get automatically synchronized across multiple devices. This process can occur while an application runs, and can cause existing save-data to be overwritten at unexpected times, with data from other devices. (Windows Phone apps written with SDL 2.0.3 did not utilize a Roaming folder, due to API restrictions in Windows Phone 8.0).
SDL_GetPrefPath(), starting with SDL 2.0.4, addresses these by:
making sure that SDL_GetPrefPath() returns a directory in which data can be written to immediately, without first needing to create directories.
basing SDL_GetPrefPath() off of a different, non-Roaming folder, the contents of which do not automatically get synchronized across devices (and which require less work to use safely, in terms of data integrity).
Apps that wish to get their Roaming folder's path can do so either by using SDL_WinRTGetFSPathUTF8(), SDL_WinRTGetFSPathUNICODE() (which returns a UCS-2/wide-char string), or directly through the WinRT class, Windows.Storage.ApplicationData.
The steps for setting up a project for an SDL/WinRT app looks like the following, at a high-level:
Create a new project using one of Visual C++'s templates for a plain, non-XAML, "Direct3D App" (XAML support for SDL/WinRT is not yet ready for use). If you don't see one of these templates, in Visual C++'s 'New Project' dialog, try using the textbox titled, 'Search Installed Templates' to look for one.
In the new project, delete any file that has one of the following extensions:
When you are done, you should be left with a few files, each of which will be a necessary part of your app's project. These files will consist of:
SDL/WinRT can be built in multiple variations, spanning across three different CPU architectures (x86, x64, and ARM) and two different configurations (Debug and Release). WinRT and Visual C++ do not currently provide a means for combining multiple variations of one library into a single file. Furthermore, it does not provide an easy means for copying pre-built .dll files into your app's final output (via Post-Build steps, for example). It does, however, provide a system whereby an app can reference the MSVC projects of libraries such that, when the app is built:
To set this up for SDL/WinRT, you'll need to run through the following steps:
VisualC-WinRT
directory.Your project is now linked to SDL's project, insofar that when the app is built, SDL will be built as well, with its build output getting included with your app.
Some build settings need to be changed in your app's project. This guide will outline the following:
To change these settings:
A few files should be included directly in your app's MSVC project, specifically:
To include these files for C/C++ projects:
SDL_winrt_main_NonXAML.cpp
SDL2-WinRTResources.rc
SDL2-WinRTResource_BlankCursor.cur
SDL_winrt_main_NonXAML.cpp
(as listed in your
project), then click on "Properties...".NOTE: C++/CX compilation is currently required in at least one file of your app's project. This is to make sure that Visual C++'s linker builds a 'Windows Metadata' file (.winmd) for your app. Not doing so can lead to build errors.
For non-C++ projects, you will need to call SDL_WinRTRunApp from your language's
main function, and generate SDL2-WinRTResources.res manually by using rc
via
the Developer Command Prompt and including it as a within the
first block in your Visual Studio project file.
At this point, you can add in SDL-specific source code. Be sure to include a
C-style main function (ie: int main(int argc, char *argv[])
). From there you
should be able to create a single SDL_Window
(WinRT apps can only have one
window, at present), as well as an SDL_Renderer
. Direct3D will be used to
draw content. Events are received via SDL's usual event functions
(SDL_PollEvent
, etc.) If you have a set of existing source files and assets,
you can start adding them to the project now. If not, or if you would like to
make sure that you're setup correctly, some short and simple sample code is
provided below.
If you are creating a new app (rather than porting an existing SDL-based app), or if you would just like a simple app to test SDL/WinRT with before trying to get existing code working, some working SDL/WinRT code is provided below. To set this up:
#include <SDL.h>
int main(int argc, char **argv)
{
SDL_DisplayMode mode;
SDL_Window * window = NULL;
SDL_Renderer * renderer = NULL;
SDL_Event evt;
SDL_bool keep_going = SDL_TRUE;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return 1;
} else if (SDL_GetCurrentDisplayMode(0, &mode) != 0) {
return 1;
} else if (SDL_CreateWindowAndRenderer(mode.w, mode.h, SDL_WINDOW_FULLSCREEN, &window, &renderer) != 0) {
return 1;
}
while (keep_going) {
while (SDL_PollEvent(&evt)) {
if ((evt.type == SDL_KEYDOWN) && (evt.key.keysym.sym == SDLK_ESCAPE)) {
keep_going = SDL_FALSE;
}
}
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
If you have existing code and assets that you'd like to add, you should be able to add them now. The process for adding a set of files is as such.
Do note that WinRT only supports a subset of the APIs that are available to Win32-based apps. Many portions of the Win32 API and the C runtime are not available.
A list of unsupported C APIs can be found at http://msdn.microsoft.com/en-us/library/windows/apps/jj606124.aspx
General information on using the C runtime in WinRT can be found at https://msdn.microsoft.com/en-us/library/hh972425.aspx
A list of supported Win32 APIs for WinRT apps can be found at http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx. To note, the list of supported Win32 APIs for Windows Phone 8.0 is different. That list can be found at http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662956(v=vs.105).aspx
Your app project should now be setup, and you should be ready to build your app. To run it on the local machine, open the Debug menu and choose "Start Debugging". This will build your app, then run your app full-screen. To switch out of your app, press the Windows key. Alternatively, you can choose to run your app in a window. To do this, before building and running your app, find the drop-down menu in Visual C++'s toolbar that says, "Local Machine". Expand this by clicking on the arrow on the right side of the list, then click on Simulator. Once you do that, any time you build and run the app, the app will launch in window, rather than full-screen.
These instructions do not include Windows Phone, despite Windows Phone typically running on ARM processors. They are specifically for devices that use the "Windows RT" operating system, which was a modified version of Windows 8.x that ran primarily on ARM-based tablet computers.
To build and run the app on ARM-based, "Windows RT" devices, you'll need to:
Microsoft's Remote Debugger can be found at https://msdn.microsoft.com/en-us/library/hh441469.aspx. Please note that separate versions of this debugger exist for different versions of Visual C++, one each for MSVC 2015, 2013, and 2012.
To setup Visual C++ to launch your app on an ARM device:
Try adding the following to your linker flags. In MSVC, this can be done by right-clicking on the app project, navigating to Configuration Properties -> Linker -> Command Line, then adding them to the Additional Options section.
For Release builds / MSVC-Configurations, add:
/nodefaultlib:vccorlib /nodefaultlib:msvcrt vccorlib.lib msvcrt.lib
For Debug builds / MSVC-Configurations, add:
/nodefaultlib:vccorlibd /nodefaultlib:msvcrtd vccorlibd.lib msvcrtd.lib
This may be caused by a bug in Windows itself, whereby hiding the mouse cursor can cause mouse-position reporting to fail.
SDL provides a workaround for this, but it requires that an app links to a
set of Win32-style cursor image-resource files. A copy of suitable resource
files can be found in src/main/winrt/
. Adding them to an app's Visual C++
project file should be sufficient to get the app to use them.
This can be caused for any one of a few reasons, which Visual Studio can report, but won't always do so in an up-front manner.
To help determine why this error comes up:
If Visual Studio reports (via its Output window) that the project:
"could not be loaded because it's missing install components. To fix this launch Visual Studio setup with the following selections: Microsoft.VisualStudio.ComponentGroup.UWP.VC"
... then you will need to re-launch Visual Studio's installer, and make sure that the workflow for "Universal Windows Platform development" is checked, and that its optional component, "C++ Universal Windows Platform tools" is also checked. While you are there, if you are planning on targeting UWP / Windows 10, also make sure that you check the optional component, "Windows 10 SDK (10.0.10240.0)". After making sure these items are checked as-appropriate, install them.
Once you install these components, try re-launching Visual Studio, and re-opening the SDL project file. If you still get the error dialog, try using the Output window, again, seeing what Visual Studio says about it.
Windows only permits certain game controllers and joysticks to work within WinRT / UWP apps. Even if a game controller or joystick works in a Win32 app, that device is not guaranteed to work inside a WinRT / UWP app.
According to Microsoft, "Xbox compatible controllers" should work inside UWP apps, potentially with more working in the future. This includes, but may not be limited to, Microsoft-made Xbox controllers and USB adapters. (Source: https://social.msdn.microsoft.com/Forums/en-US/9064838b-e8c3-4c18-8a83-19bf0dfe150d/xinput-fails-to-detect-game-controllers?forum=wpdevelop)