Created
April 7, 2013 17:22
-
-
Save simsaens/5331422 to your computer and use it in GitHub Desktop.
Example Codea Addon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.mm | |
// <#MyProjectName#> | |
// | |
// Created by <#AuthorName#> on <#CurrentDate#> | |
// Copyright (c) <#AuthorName#>. All rights reserved. | |
// | |
#import "AppDelegate.h" | |
#import "CodeaViewController.h" | |
//Import our custom Addon | |
#import "GameCenterAddon.h" | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
self.viewController = [[CodeaViewController alloc] init]; | |
//Create and add our GameCenterAddon to Codea | |
[self.viewController registerAddon:[GameCenterAddon sharedInstance]]; | |
//Replace with your project! | |
NSString* projectPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"MyProject.codea"]; | |
[self.viewController loadProjectAtPath:projectPath]; | |
self.window.rootViewController = self.viewController; | |
[self.window makeKeyAndVisible]; | |
return YES; | |
} | |
- (void)applicationWillResignActive:(UIApplication *)application | |
{ | |
} | |
- (void)applicationDidEnterBackground:(UIApplication *)application | |
{ | |
} | |
- (void)applicationWillEnterForeground:(UIApplication *)application | |
{ | |
} | |
- (void)applicationDidBecomeActive:(UIApplication *)application | |
{ | |
} | |
- (void)applicationWillTerminate:(UIApplication *)application | |
{ | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameCenterAddon.h | |
// MyProject | |
// | |
// Created by Simeon on 8/04/13. | |
// Copyright (c) 2013 MyCompany. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "GCDSingleton.h" | |
#import "CodeaAddon.h" | |
@interface GameCenterAddon : NSObject<CodeaAddon> | |
DEFINE_SHARED_INSTANCE | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameCenterAddon.m | |
// MyProject | |
// | |
// Created by Simeon on 8/04/13. | |
// Copyright (c) 2013 MyCompany. All rights reserved. | |
// | |
#import <GameKit/GameKit.h> | |
#import "GameCenterAddon.h" | |
#import "lua.h" | |
//Forward declare our Lua functions | |
static int GameCenterStart(struct lua_State *state); | |
//================================================================================================= | |
// Private GameCenterAddon Protocols | |
//================================================================================================= | |
@interface GameCenterAddon ()<GKGameCenterControllerDelegate> | |
@property (nonatomic, weak) CodeaViewController *codeaViewController; | |
@end | |
//================================================================================================= | |
// Game Center Addon | |
//================================================================================================= | |
@implementation GameCenterAddon | |
DEFINE_SHARED_INSTANCE_IMPL | |
- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L | |
{ | |
NSLog(@"GameCenterAddon Registering Functions"); | |
//Store the Codea view controller, so we can present UI from Game Center functions | |
self.codeaViewController = controller; | |
//Register the Game Center Start function, defined below | |
lua_register(L, "GameCenterStart", GameCenterStart); | |
} | |
- (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController | |
{ | |
//Dismiss the Game Center view | |
[self.codeaViewController dismissViewControllerAnimated:YES completion:^ | |
{ | |
//Unpause Codea | |
self.codeaViewController.paused = NO; | |
}]; | |
} | |
@end | |
//================================================================================================= | |
// Game Center Functions | |
//================================================================================================= | |
static int GameCenterStart(struct lua_State *state) | |
{ | |
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; | |
/* iOS 6.x */ | |
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) | |
{ | |
CodeaViewController *codea = [GameCenterAddon sharedInstance].codeaViewController; | |
if( viewController != nil ) | |
{ | |
//Pause Codea and present the Game Center view | |
codea.paused = YES; | |
[codea presentViewController:viewController animated:YES completion:NULL]; | |
} | |
else if( [GKLocalPlayer localPlayer].isAuthenticated ) | |
{ | |
//The player has been authenticated | |
//Normally you would now make your game aware that Game Center is available | |
// and show things like buttons to access leaderboards | |
//But we are just going to pop up the main Game Center view controller | |
GKGameCenterViewController *gc = [[GKGameCenterViewController alloc] init]; | |
gc.gameCenterDelegate = [GameCenterAddon sharedInstance]; | |
[codea presentViewController:gc animated:YES completion:NULL]; | |
} | |
else | |
{ | |
//Disable Game Center | |
//Unpause Codea | |
codea.paused = NO; | |
} | |
}; | |
/* iOS 5.x */ | |
// [localPlayer authenticateWithCompletionHandler:^(NSError *error) | |
// { | |
// CodeaViewController *codea = [GameCenterAddon sharedInstance].codeaViewController; | |
// codea.paused = YES; | |
// | |
// if( [GKLocalPlayer localPlayer].isAuthenticated ) | |
// { | |
// //The player has been authenticated | |
// | |
// //Normally you would now make your game aware that Game Center is available | |
// // and show things like buttons to access leaderboards | |
// | |
// //But we are just going to pop up the main Game Center view controller | |
// GKGameCenterViewController *gc = [[GKGameCenterViewController alloc] init]; | |
// gc.gameCenterDelegate = [GameCenterAddon sharedInstance]; | |
// | |
// [codea presentViewController:gc animated:YES completion:NULL]; | |
// } | |
// else | |
// { | |
// //Disable Game Center | |
// | |
// //Unpause Codea | |
// codea.paused = NO; | |
// } | |
// | |
// }]; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
From https://gist.github.com/1057420 | |
A singleton macro using Grand Central Dispatch's dispatch_once function to create the singleton the first time it is accessed. | |
* Modified so that the name of the function is always sharedInstance. | |
*/ | |
/* | |
USAGE: | |
@interface MySharedThing | |
DEFINE_SHARED_INSTANCE | |
@end | |
@implementation MySharedThing | |
DEFINE_SHARED_INSTANCE_IMPL | |
@end | |
OR, you can define the initialiser with a block (this is equivalent to using DEFINE_SHARED_INSTANCE_IMPL): | |
@implementation MySharedThing | |
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{ | |
return [[self alloc] init]; | |
}) | |
@end | |
*/ | |
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \ | |
+ (instancetype)sharedInstance\ | |
{\ | |
static dispatch_once_t pred = 0; \ | |
__strong static id _sharedObject = nil; \ | |
dispatch_once(&pred, ^{ \ | |
_sharedObject = block(); \ | |
}); \ | |
return _sharedObject; \ | |
}\ | |
#define DEFINE_SHARED_INSTANCE \ | |
+ (instancetype)sharedInstance;\ | |
#define DEFINE_SHARED_INSTANCE_IMPL \ | |
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{\ | |
return [[self alloc] init];\ | |
})\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment