Commit d41fdc94d626afe659cc978c8628c278f750e67f

David Ludwig 2013-08-13T20:09:52

WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
diff --git a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
index 40a8ffd..3a770d6 100644
--- a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
+++ b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj
@@ -100,11 +100,11 @@
     <ClCompile Include="..\..\src\SDL.c" />
     <ClCompile Include="..\..\src\SDL_assert.c" />
     <ClCompile Include="..\..\src\SDL_error.c" />
-    <ClCompile Include="..\..\src\SDL_fatal.c" />
     <ClCompile Include="..\..\src\SDL_hints.c" />
     <ClCompile Include="..\..\src\SDL_log.c" />
     <ClCompile Include="..\..\src\stdlib\SDL_getenv.c" />
     <ClCompile Include="..\..\src\stdlib\SDL_iconv.c" />
+    <ClCompile Include="..\..\src\stdlib\SDL_malloc.c" />
     <ClCompile Include="..\..\src\stdlib\SDL_qsort.c" />
     <ClCompile Include="..\..\src\stdlib\SDL_stdlib.c" />
     <ClCompile Include="..\..\src\stdlib\SDL_string.c" />
@@ -239,6 +239,7 @@
     <ClInclude Include="..\..\src\events\SDL_windowevents_c.h" />
     <ClInclude Include="..\..\src\haptic\SDL_haptic_c.h" />
     <ClInclude Include="..\..\src\haptic\SDL_syshaptic.h" />
+    <ClInclude Include="..\..\src\joystick\SDL_gamecontrollerdb.h" />
     <ClInclude Include="..\..\src\joystick\SDL_joystick_c.h" />
     <ClInclude Include="..\..\src\joystick\SDL_sysjoystick.h" />
     <ClInclude Include="..\..\src\render\direct3d11\SDL_render_d3d11_cpp.h" />
diff --git a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
index 74dd2dc..9e8007c 100644
--- a/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
+++ b/VisualC-WinRT/SDL/SDL_VS2012-WinRT.vcxproj.filters
@@ -96,9 +96,6 @@
     <ClCompile Include="..\..\src\events\SDL_events.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\SDL_fatal.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="..\..\src\video\SDL_fillrect.c">
       <Filter>Source Files</Filter>
     </ClCompile>
@@ -267,6 +264,9 @@
     <ClCompile Include="..\..\src\joystick\windowsrt\SDL_xinputjoystick.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\stdlib\SDL_malloc.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\include\begin_code.h">
@@ -593,6 +593,9 @@
     <ClInclude Include="..\..\src\render\direct3d11\SDL_render_d3d11_cpp.h">
       <Filter>Source Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\joystick\SDL_gamecontrollerdb.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Filter Include="Header Files">
diff --git a/include/SDL_config_windowsrt.h b/include/SDL_config_windowsrt.h
index b6ecc00..712a914 100644
--- a/include/SDL_config_windowsrt.h
+++ b/include/SDL_config_windowsrt.h
@@ -83,6 +83,7 @@ typedef unsigned int uintptr_t;
 #define HAVE_STRING_H 1
 #define HAVE_CTYPE_H 1
 #define HAVE_MATH_H 1
+#define HAVE_FLOAT_H 1
 #define HAVE_SIGNAL_H 1
 
 /* C library functions */
@@ -123,7 +124,7 @@ typedef unsigned int uintptr_t;
 #define HAVE_ATAN 1
 #define HAVE_ATAN2 1
 #define HAVE_CEIL 1
-//#define HAVE_COPYSIGN 1   // TODO, WinRT: consider using _copysign instead
+#define HAVE__COPYSIGN 1
 #define HAVE_COS 1
 #define HAVE_COSF 1
 #define HAVE_FABS 1
@@ -131,6 +132,7 @@ typedef unsigned int uintptr_t;
 #define HAVE_LOG 1
 #define HAVE_POW 1
 //#define HAVE_SCALBN 1
+#define HAVE__SCALB 1
 #define HAVE_SIN 1
 #define HAVE_SINF 1
 #define HAVE_SQRT 1
diff --git a/include/SDL_platform.h b/include/SDL_platform.h
index 1e8e0d9..419b633 100644
--- a/include/SDL_platform.h
+++ b/include/SDL_platform.h
@@ -120,10 +120,27 @@
 #undef __SOLARIS__
 #define __SOLARIS__ 1
 #endif
-#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
-#undef __WIN32__
-#define __WIN32__   1
-#endif
+
+#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
+/* Try to find out what version of Windows we are compiling for */
+#if defined(_MSC_VER) && (_MSC_VER >= 1700)	/* _MSC_VER==1700 for MSVC 2012 */
+#include <winapifamily.h>
+#endif
+/* Default to classic, Win32 / Desktop compilation either if:
+     1. the version of Windows is explicity set to a 'Desktop' (non-Metro) app
+     2. the version of Windows cannot be determined via winapifamily.h
+   If neither is true, see if we're compiling for WinRT.
+ */
+#if ! defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+#undef __WIN32__
+#define __WIN32__   1
+/* See if we're compiling for WinRT: */
+#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
+#undef __WINRT__
+#define __WINRT__ 1
+#endif /* ! defined(WINAPI_FAMILY_PARTITION) */
+#endif /* defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) */
+
 #if defined(__PSP__)
 #undef __PSP__
 #define __PSP__ 1
diff --git a/include/SDL_stdinc.h b/include/SDL_stdinc.h
index bc39c2a..bfd6bb8 100644
--- a/include/SDL_stdinc.h
+++ b/include/SDL_stdinc.h
@@ -80,6 +80,9 @@
 # endif
 # include <math.h>
 #endif
+#ifdef HAVE_FLOAT_H
+# include <float.h>
+#endif
 #if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
 # include <iconv.h>
 #endif
diff --git a/src/SDL_log.c b/src/SDL_log.c
index a19a2c4..1bd8ae6 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -322,6 +322,8 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
         char *output;
         size_t length;
         LPTSTR tstr;
+
+#ifndef __WINRT__
         BOOL attachResult;
         DWORD attachError;
         unsigned long charsWritten; 
@@ -353,6 +355,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
                         stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
                 }
         }
+#endif /* ifndef __WINRT__ */
 
         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1;
         output = SDL_stack_alloc(char, length);
@@ -362,6 +365,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
         /* Output to debugger */
         OutputDebugString(tstr);
        
+#ifndef __WINRT__
         /* Screen output to stderr, if console was attached. */
         if (consoleAttached == 1) {
                 if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) {
@@ -371,6 +375,7 @@ SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
                     OutputDebugString(TEXT("Insufficient heap memory to write message"));
                 }
         }
+#endif /* ifndef __WINRT__ */
 
         SDL_free(tstr);
         SDL_stack_free(output);
diff --git a/src/audio/xaudio2/SDL_xaudio2.c b/src/audio/xaudio2/SDL_xaudio2.c
index 19f7cdc..2816e6c 100644
--- a/src/audio/xaudio2/SDL_xaudio2.c
+++ b/src/audio/xaudio2/SDL_xaudio2.c
@@ -91,15 +91,11 @@ extern "C" {
 }
 #endif
 
-#if defined(__WINRT__)
-#  define SDL_XAUDIO2_HAS_SDK 1
-#endif
-#if defined(__WIN32__)
 #ifdef __GNUC__
 /* The configure script already did any necessary checking */
 #  define SDL_XAUDIO2_HAS_SDK 1
 #elif defined(__WINRT__)
-/* WinRT always has access to the .the XAudio 2 SD
+/* WinRT always has access to the .the XAudio 2 SDK */
 #  define SDL_XAUDIO2_HAS_SDK
 #else
 #include <dxsdkver.h> /* XAudio2 exists as of the March 2008 DirectX SDK */
diff --git a/src/joystick/SDL_gamecontrollerdb.h b/src/joystick/SDL_gamecontrollerdb.h
index 7f6c7b2..dcdbafb 100644
--- a/src/joystick/SDL_gamecontrollerdb.h
+++ b/src/joystick/SDL_gamecontrollerdb.h
@@ -38,6 +38,8 @@ static const char *s_ControllerMappings [] =
     "4c056802000000000000504944564944,PS3 Controller,a:b14,b:b13,back:b0,dpdown:b6,dpleft:b7,dpright:b5,dpup:b4,guide:b16,leftshoulder:b10,leftstick:b1,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b11,rightstick:b2,righttrigger:b9,rightx:a2,righty:a3,start:b3,x:b15,y:b12,",
     "25090500000000000000504944564944,PS3 DualShock,a:b2,b:b1,back:b9,dpdown:h0.8,dpleft:h0.4,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b0,y:b3,",
     "xinput,X360 Controller,a:b10,b:b11,back:b5,dpdown:b1,dpleft:b2,dpright:b3,dpup:b0,guide:b14,leftshoulder:b8,leftstick:b6,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b9,rightstick:b7,righttrigger:a5,rightx:a2,righty:a3,start:b4,x:b12,y:b13,",
+#elif defined(SDL_JOYSTICK_XINPUT)
+    "xinput,X360 Controller,a:b10,b:b11,back:b5,dpdown:b1,dpleft:b2,dpright:b3,dpup:b0,guide:b14,leftshoulder:b8,leftstick:b6,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b9,rightstick:b7,righttrigger:a5,rightx:a2,righty:a3,start:b4,x:b12,y:b13,",
 #elif defined(__MACOSX__)
     "0500000047532047616d657061640000,GameStop Gamepad,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,",
     "6d0400000000000016c2000000000000,Logitech F310 Gamepad (DInput),a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,", /* Guide button doesn't seem to be sent in DInput mode. */
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 5dfc38d..5827a55 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -49,6 +49,9 @@ static const SDL_RenderDriver *render_drivers[] = {
 #if SDL_VIDEO_RENDER_D3D
     &D3D_RenderDriver,
 #endif
+#if SDL_VIDEO_RENDER_D3D11
+    &D3D11_RenderDriver,
+#endif
 #if SDL_VIDEO_RENDER_OGL
     &GL_RenderDriver,
 #endif
diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c
index 2ee62be..f5ea27d 100644
--- a/src/stdlib/SDL_stdlib.c
+++ b/src/stdlib/SDL_stdlib.c
@@ -61,6 +61,8 @@ SDL_copysign(double x, double y)
 {
 #if defined(HAVE_COPYSIGN)
     return copysign(x, y);
+#elif defined(HAVE__COPYSIGN)
+    return _copysign(x, y);
 #else
     return SDL_uclibc_copysign(x, y);
 #endif /* HAVE_COPYSIGN */
@@ -131,6 +133,8 @@ SDL_scalbn(double x, int n)
 {
 #if defined(HAVE_SCALBN)
     return scalbn(x, n);
+#elif defined(HAVE__SCALB)
+    return _scalb(x, n);
 #else
     return SDL_uclibc_scalbn(x, n);
 #endif /* HAVE_SCALBN */
diff --git a/src/thread/stdcpp/SDL_systhread.cpp b/src/thread/stdcpp/SDL_systhread.cpp
index fd4cdaf..fb1ca31 100644
--- a/src/thread/stdcpp/SDL_systhread.cpp
+++ b/src/thread/stdcpp/SDL_systhread.cpp
@@ -24,8 +24,8 @@
 
 extern "C" {
 #include "SDL_thread.h"
-#include "../SDL_systhread.h"
 #include "../SDL_thread_c.h"
+#include "../SDL_systhread.h"
 #include "SDL_log.h"
 }
 
@@ -121,4 +121,18 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
     }
 }
 
+extern "C"
+SDL_TLSData *
+SDL_SYS_GetTLSData()
+{
+    return SDL_Generic_GetTLSData();
+}
+
+extern "C"
+int
+SDL_SYS_SetTLSData(SDL_TLSData *data)
+{
+    return SDL_Generic_SetTLSData(data);
+}
+
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/timer/windows/SDL_systimer.c b/src/timer/windows/SDL_systimer.c
index ce6bd7a..f71aecf 100644
--- a/src/timer/windows/SDL_systimer.c
+++ b/src/timer/windows/SDL_systimer.c
@@ -41,6 +41,7 @@ static LARGE_INTEGER hires_start_ticks;
 static LARGE_INTEGER hires_ticks_per_second;
 #endif
 
+#ifndef __WINRT__
 static void
 timeSetPeriod(UINT uPeriod)
 {
@@ -74,6 +75,7 @@ SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValu
         timeSetPeriod(uPeriod);
     }
 }
+#endif /* ifndef __WINRT__ */
 
 void
 SDL_StartTicks(void)
@@ -90,13 +92,19 @@ SDL_StartTicks(void)
         QueryPerformanceCounter(&hires_start_ticks);
     } else {
         hires_timer_available = FALSE;
+#ifdef __WINRT__
+        start = 0;            /* the timer failed to start! */
+#else
         timeSetPeriod(1);     /* use 1 ms timer precision */
         start = timeGetTime();
+#endif /* ifdef __WINRT__ */
     }
 #endif
 
+#ifndef __WINRT__
     SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
                         SDL_TimerResolutionChanged, NULL);
+#endif
 }
 
 Uint32
@@ -119,7 +127,11 @@ SDL_GetTicks(void)
 
         return (DWORD) hires_now.QuadPart;
     } else {
+#ifdef __WINRT__
+        now = 0;
+#else
         now = timeGetTime();
+#endif // ifdef __WINRT__
     }
 #endif
 
@@ -148,6 +160,19 @@ SDL_GetPerformanceFrequency(void)
     return frequency.QuadPart;
 }
 
+#ifdef __WINRT__
+static void
+Sleep(DWORD timeout)
+{
+    static HANDLE mutex = 0;
+    if ( ! mutex )
+    {
+        mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
+    }
+    WaitForSingleObjectEx(mutex, timeout, FALSE);
+}
+#endif
+
 void
 SDL_Delay(Uint32 ms)
 {
diff --git a/src/video/windowsrt/SDL_WinRTApp.cpp b/src/video/windowsrt/SDL_WinRTApp.cpp
index a807bda..bac38e4 100644
--- a/src/video/windowsrt/SDL_WinRTApp.cpp
+++ b/src/video/windowsrt/SDL_WinRTApp.cpp
@@ -13,7 +13,7 @@ extern "C" {
 #include "SDL_stdinc.h"
 #include "SDL_render.h"
 #include "../SDL_sysvideo.h"
-#include "../../SDL_hints_c.h"
+//#include "../../SDL_hints_c.h"
 #include "../../events/scancodes_windows.h"
 #include "../../events/SDL_mouse_c.h"
 #include "../../events/SDL_keyboard_c.h"
@@ -86,7 +86,7 @@ __declspec(dllexport) int SDL_WinRT_RunApplication(SDL_WinRT_MainFunction mainFu
     return 0;
 }
 
-static void WINRT_SetDisplayOrientationsPreference(const char *name, const char *oldValue, const char *newValue)
+static void WINRT_SetDisplayOrientationsPreference(void *userdata, const char *name, const char *oldValue, const char *newValue)
 {
     SDL_assert(SDL_strcmp(name, SDL_HINT_ORIENTATIONS) == 0);
 
@@ -163,8 +163,8 @@ void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
     // otherwise the hint callback won't get registered.
     //
     // WinRT, TODO: see if an app's default orientation can be found out via WinRT API(s), then set the initial value of SDL_HINT_ORIENTATIONS accordingly.
-    SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight Portrait PortraitUpsideDown");
-    SDL_RegisterHintChangedCb(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference);
+    //SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight Portrait PortraitUpsideDown");   // DavidL: this is no longer needed (for SDL_AddHintCallback)
+    SDL_AddHintCallback(SDL_HINT_ORIENTATIONS, WINRT_SetDisplayOrientationsPreference, NULL);
 }
 
 void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
@@ -316,15 +316,19 @@ void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEven
         // Windows device.
         //
         // Commencing hack in 3... 2... 1...
-        SDL_Renderer * rendererForMainWindow = SDL_GetRenderer(m_sdlWindowData->sdlWindow);
+        //
+        // UPDATE, SDL 2.0.0 update: the 'resized' flag is now gone.  This
+        // hack might not be necessary any more.
+        //
+        //SDL_Renderer * rendererForMainWindow = SDL_GetRenderer(m_sdlWindowData->sdlWindow);
         // For now, limit the hack to when the Direct3D 11.1 is getting used:
-        const bool usingD3D11Renderer = \
-            (rendererForMainWindow != NULL) &&
-            (SDL_strcmp(rendererForMainWindow->info.name, "direct3d 11.1") == 0);
-        SDL_bool wasD3D11RendererResized = SDL_FALSE;
-        if (usingD3D11Renderer) {
-            wasD3D11RendererResized = rendererForMainWindow->resized;
-        }
+        //const bool usingD3D11Renderer = \
+        //    (rendererForMainWindow != NULL) &&
+        //    (SDL_strcmp(rendererForMainWindow->info.name, "direct3d 11.1") == 0);
+        //SDL_bool wasD3D11RendererResized = SDL_FALSE;
+        //if (usingD3D11Renderer) {
+        //    wasD3D11RendererResized = rendererForMainWindow->resized;
+        //}
 
         // Send the window-resize event to the rest of SDL, and to apps:
         const int windowWidth = (int) ceil(args->Size.Width);
@@ -335,10 +339,10 @@ void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEven
             windowWidth,
             windowHeight);
 
-        // Viewport hack, part two:
-        if (usingD3D11Renderer) {
-            rendererForMainWindow->resized = wasD3D11RendererResized;
-        }
+        //// Viewport hack, part two:
+        //if (usingD3D11Renderer) {
+        //    rendererForMainWindow->resized = wasD3D11RendererResized;
+        //}
     }
 }