Maelstrom: Initial GameKit login code

https://github.com/libsdl-org/Maelstrom/commit/a901ea5b30376402258c2e3156752d08ef6470e8

From a901ea5b30376402258c2e3156752d08ef6470e8 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 22 Jun 2012 16:12:19 -0400
Subject: [PATCH] Initial GameKit login code

---
 Xcode-iOS/Maelstrom.xcodeproj/project.pbxproj |  6 ++++
 Xcode-iOS/Maelstrom_GameKit.h                 |  2 ++
 Xcode-iOS/Maelstrom_GameKit.mm                | 33 +++++++++++++++++++
 game/main.cpp                                 |  8 +++++
 4 files changed, 49 insertions(+)
 create mode 100644 Xcode-iOS/Maelstrom_GameKit.h
 create mode 100644 Xcode-iOS/Maelstrom_GameKit.mm

diff --git a/Xcode-iOS/Maelstrom.xcodeproj/project.pbxproj b/Xcode-iOS/Maelstrom.xcodeproj/project.pbxproj
index c43881f5..9c4c4d28 100644
--- a/Xcode-iOS/Maelstrom.xcodeproj/project.pbxproj
+++ b/Xcode-iOS/Maelstrom.xcodeproj/project.pbxproj
@@ -18,6 +18,7 @@
 		AA319E1E145EDA1D00384943 /* Maelstrom.png in Resources */ = {isa = PBXBuildFile; fileRef = AA319E1D145EDA1D00384943 /* Maelstrom.png */; };
 		AA319E20145EDA6400384943 /* Maelstrom_small.png in Resources */ = {isa = PBXBuildFile; fileRef = AA319E1F145EDA6400384943 /* Maelstrom_small.png */; };
 		AA7557A61594EE8F00BBD41B /* GameKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA7557A51594EE8F00BBD41B /* GameKit.framework */; };
+		AA7557AB1595062C00BBD41B /* Maelstrom_GameKit.mm in Sources */ = {isa = PBXBuildFile; fileRef = AA7557AA1595062C00BBD41B /* Maelstrom_GameKit.mm */; };
 		AA769FE0146A679C001C5008 /* Maelstrom_ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = AA769FDF146A679C001C5008 /* Maelstrom_ipad.png */; };
 		AA97808315769F3400472542 /* mainstub.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA97808215769F3400472542 /* mainstub.cpp */; };
 		AA9781571576A00100472542 /* Mac_FontServ.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA97811D1576A00100472542 /* Mac_FontServ.cpp */; };
@@ -188,6 +189,8 @@
 		AA319E1D145EDA1D00384943 /* Maelstrom.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Maelstrom.png; sourceTree = "<group>"; };
 		AA319E1F145EDA6400384943 /* Maelstrom_small.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Maelstrom_small.png; sourceTree = "<group>"; };
 		AA7557A51594EE8F00BBD41B /* GameKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GameKit.framework; path = System/Library/Frameworks/GameKit.framework; sourceTree = SDKROOT; };
+		AA7557A71595019400BBD41B /* Maelstrom_GameKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Maelstrom_GameKit.h; path = "Xcode-iOS/Maelstrom_GameKit.h"; sourceTree = "<group>"; };
+		AA7557AA1595062C00BBD41B /* Maelstrom_GameKit.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = Maelstrom_GameKit.mm; path = "Xcode-iOS/Maelstrom_GameKit.mm"; sourceTree = "<group>"; };
 		AA769FDF146A679C001C5008 /* Maelstrom_ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Maelstrom_ipad.png; sourceTree = "<group>"; };
 		AA97808215769F3400472542 /* mainstub.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mainstub.cpp; sourceTree = "<group>"; };
 		AA97811A1576A00100472542 /* bitesex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bitesex.h; sourceTree = "<group>"; };
@@ -617,6 +620,8 @@
 		AAD13B87145E8A8F0040B19C /* Maelstrom */ = {
 			isa = PBXGroup;
 			children = (
+				AA7557A71595019400BBD41B /* Maelstrom_GameKit.h */,
+				AA7557AA1595062C00BBD41B /* Maelstrom_GameKit.mm */,
 				AA97808215769F3400472542 /* mainstub.cpp */,
 				AA9781741576A03700472542 /* game */,
 				AA9781191576A00100472542 /* maclib */,
@@ -793,6 +798,7 @@
 				AA9781B81576A03700472542 /* rect.cpp in Sources */,
 				AA9781B91576A03700472542 /* replay.cpp in Sources */,
 				AA9781BA1576A03700472542 /* scores.cpp in Sources */,
+				AA7557AB1595062C00BBD41B /* Maelstrom_GameKit.mm in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
diff --git a/Xcode-iOS/Maelstrom_GameKit.h b/Xcode-iOS/Maelstrom_GameKit.h
new file mode 100644
index 00000000..bbddbb01
--- /dev/null
+++ b/Xcode-iOS/Maelstrom_GameKit.h
@@ -0,0 +1,2 @@
+
+void InitGameCenter(void);
diff --git a/Xcode-iOS/Maelstrom_GameKit.mm b/Xcode-iOS/Maelstrom_GameKit.mm
new file mode 100644
index 00000000..b1d86e85
--- /dev/null
+++ b/Xcode-iOS/Maelstrom_GameKit.mm
@@ -0,0 +1,33 @@
+
+#import "GameKit/GameKit.h"
+
+#include "Maelstrom_GameKit.h"
+
+static BOOL s_hasGameCenter = NO;
+
+static BOOL isGameCenterAPIAvailable()
+{
+   // check for presence of GKLocalPlayer API
+   Class gcClass = NSClassFromString(@"GKLocalPlayer");
+   // check if the device is running iOS 4.1 or later
+   BOOL osVersionSupported = ([[[UIDevice currentDevice] systemVersion]
+        compare:@"4.1" options:NSNumericSearch] != NSOrderedAscending);
+   return (gcClass && osVersionSupported);
+}
+
+void InitGameCenter(void)
+{
+    if (isGameCenterAPIAvailable()) 
+    {
+        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
+        [localPlayer authenticateWithCompletionHandler:^(NSError *err) {
+            if (localPlayer.authenticated) {
+                // Authentication Successful
+		s_hasGameCenter = YES;
+            } else {
+                // Disable Game Center features
+		s_hasGameCenter = NO;
+            }
+        }];
+    }
+}
diff --git a/game/main.cpp b/game/main.cpp
index dbcf45b3..000212d1 100644
--- a/game/main.cpp
+++ b/game/main.cpp
@@ -46,6 +46,10 @@
 #include "../screenlib/UIElementCheckbox.h"
 #include "../screenlib/UIElementEditbox.h"
 
+#if __IPHONEOS__
+#include "../Xcode-iOS/Maelstrom_GameKit.h"
+#endif
+
 #define MAELSTROM_ORGANIZATION	"AmbrosiaSW"
 #define MAELSTROM_NAME		"Maelstrom"
 #define MAELSTROM_DATA	"Maelstrom_Data.zip"
@@ -283,6 +287,10 @@ int MaelstromMain(int argc, char *argv[])
 		exit(1);
 	}
 
+#if __IPHONEOS__
+	InitGameCenter();
+#endif
+
 	/* Seed the random number generator */
 	SeedRandom(0L);