Permalink
Browse files

Added example how to use Air Guitar SDK in a game using CADisplayLink

  • Loading branch information...
1 parent 92a1c94 commit ce495db4259588369f7e240e8577d0490ad294c8 @ronaldmannak committed Aug 14, 2012
@@ -7,14 +7,16 @@
objects = {
/* Begin PBXBuildFile section */
+ F88BAC0715DB09A500C6C617 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F88BAC0615DB09A500C6C617 /* QuartzCore.framework */; };
+ F8AE44B415DB00DA0026B822 /* AGDisplayLinkViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8AE44B315DB00DA0026B822 /* AGDisplayLinkViewController.m */; };
F8E2113D15BA690D0074CEBB /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E2113C15BA690D0074CEBB /* UIKit.framework */; };
F8E2113F15BA690D0074CEBB /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E2113E15BA690D0074CEBB /* Foundation.framework */; };
F8E2114115BA690D0074CEBB /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E2114015BA690D0074CEBB /* CoreGraphics.framework */; };
F8E2114715BA690D0074CEBB /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F8E2114515BA690D0074CEBB /* InfoPlist.strings */; };
F8E2114915BA690D0074CEBB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E2114815BA690D0074CEBB /* main.m */; };
F8E2114D15BA690D0074CEBB /* AGAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E2114C15BA690D0074CEBB /* AGAppDelegate.m */; };
F8E2115015BA690D0074CEBB /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F8E2114E15BA690D0074CEBB /* MainStoryboard.storyboard */; };
- F8E2115315BA690D0074CEBB /* AGViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E2115215BA690D0074CEBB /* AGViewController.m */; };
+ F8E2115315BA690D0074CEBB /* AGDelegateViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8E2115215BA690D0074CEBB /* AGDelegateViewController.m */; };
F8E211D215BB3AD00074CEBB /* libAirGuitarSDK.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E2119B15BA6A370074CEBB /* libAirGuitarSDK.a */; };
F8E211E015BB40020074CEBB /* ExternalAccessory.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E211DF15BB40020074CEBB /* ExternalAccessory.framework */; };
/* End PBXBuildFile section */
@@ -37,6 +39,9 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ F88BAC0615DB09A500C6C617 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
+ F8AE44B215DB00D90026B822 /* AGDisplayLinkViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AGDisplayLinkViewController.h; sourceTree = "<group>"; };
+ F8AE44B315DB00DA0026B822 /* AGDisplayLinkViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AGDisplayLinkViewController.m; sourceTree = "<group>"; };
F8E2113815BA690D0074CEBB /* AirGuitarSDKDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AirGuitarSDKDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
F8E2113C15BA690D0074CEBB /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
F8E2113E15BA690D0074CEBB /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -48,8 +53,8 @@
F8E2114B15BA690D0074CEBB /* AGAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AGAppDelegate.h; sourceTree = "<group>"; };
F8E2114C15BA690D0074CEBB /* AGAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AGAppDelegate.m; sourceTree = "<group>"; };
F8E2114F15BA690D0074CEBB /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard.storyboard; sourceTree = "<group>"; };
- F8E2115115BA690D0074CEBB /* AGViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AGViewController.h; sourceTree = "<group>"; };
- F8E2115215BA690D0074CEBB /* AGViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AGViewController.m; sourceTree = "<group>"; };
+ F8E2115115BA690D0074CEBB /* AGDelegateViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AGDelegateViewController.h; sourceTree = "<group>"; };
+ F8E2115215BA690D0074CEBB /* AGDelegateViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AGDelegateViewController.m; sourceTree = "<group>"; };
F8E2119215BA6A370074CEBB /* AirGuitarSDK.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AirGuitarSDK.xcodeproj; path = AirGuitarSDK/AirGuitarSDK.xcodeproj; sourceTree = "<group>"; };
F8E211DC15BB3B430074CEBB /* AirGuitarSDK.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AirGuitarSDK.h; path = AirGuitarSDK/AirGuitarSDK/AirGuitarSDK.h; sourceTree = "<group>"; };
F8E211DD15BB3B430074CEBB /* AGAccessory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AGAccessory.h; path = AirGuitarSDK/AirGuitarSDK/AGAccessory.h; sourceTree = "<group>"; };
@@ -62,6 +67,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ F88BAC0715DB09A500C6C617 /* QuartzCore.framework in Frameworks */,
F8E211E015BB40020074CEBB /* ExternalAccessory.framework in Frameworks */,
F8E211D215BB3AD00074CEBB /* libAirGuitarSDK.a in Frameworks */,
F8E2113D15BA690D0074CEBB /* UIKit.framework in Frameworks */,
@@ -97,6 +103,7 @@
F8E211DC15BB3B430074CEBB /* AirGuitarSDK.h */,
F8E211DD15BB3B430074CEBB /* AGAccessory.h */,
F8E211DE15BB3B430074CEBB /* AGAccessoryManager.h */,
+ F88BAC0615DB09A500C6C617 /* QuartzCore.framework */,
F8E211DF15BB40020074CEBB /* ExternalAccessory.framework */,
F8E2113C15BA690D0074CEBB /* UIKit.framework */,
F8E2113E15BA690D0074CEBB /* Foundation.framework */,
@@ -111,8 +118,10 @@
F8E2114B15BA690D0074CEBB /* AGAppDelegate.h */,
F8E2114C15BA690D0074CEBB /* AGAppDelegate.m */,
F8E2114E15BA690D0074CEBB /* MainStoryboard.storyboard */,
- F8E2115115BA690D0074CEBB /* AGViewController.h */,
- F8E2115215BA690D0074CEBB /* AGViewController.m */,
+ F8E2115115BA690D0074CEBB /* AGDelegateViewController.h */,
+ F8E2115215BA690D0074CEBB /* AGDelegateViewController.m */,
+ F8AE44B215DB00D90026B822 /* AGDisplayLinkViewController.h */,
+ F8AE44B315DB00DA0026B822 /* AGDisplayLinkViewController.m */,
F8E2114315BA690D0074CEBB /* Supporting Files */,
);
path = AirGuitarSDKDemo;
@@ -227,7 +236,8 @@
files = (
F8E2114915BA690D0074CEBB /* main.m in Sources */,
F8E2114D15BA690D0074CEBB /* AGAppDelegate.m in Sources */,
- F8E2115315BA690D0074CEBB /* AGViewController.m in Sources */,
+ F8E2115315BA690D0074CEBB /* AGDelegateViewController.m in Sources */,
+ F8AE44B415DB00DA0026B822 /* AGDisplayLinkViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
+ <true/>
+ <key>SnapshotAutomaticallyBeforeSignificantChanges</key>
+ <false/>
+</dict>
+</plist>
@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import "AirGuitarSDK.h"
-@interface AGViewController : UITableViewController <UIAccelerometerDelegate, AGAccessoryProtocol>
+@interface AGDelegateViewController : UITableViewController <UIAccelerometerDelegate, AGAccessoryProtocol>
@@ -6,9 +6,9 @@
// Copyright (c) 2012 Yobble. All rights reserved.
//
-#import "AGViewController.h"
+#import "AGDelegateViewController.h"
-@interface AGViewController ()
+@interface AGDelegateViewController ()
@property (nonatomic, strong) UIView *viewX;
@property (nonatomic, strong) UIView *viewY;
@@ -19,7 +19,7 @@ @interface AGViewController ()
@end
-@implementation AGViewController
+@implementation AGDelegateViewController
- (void)viewDidLoad {
[super viewDidLoad];
@@ -41,13 +41,14 @@ - (void)viewDidLoad {
_viewZ.backgroundColor = [UIColor greenColor];
_viewZ.alpha = 0.5;
[self.view addSubview:_viewZ];
-
-
}
- (void)viewDidUnload {
[super viewDidUnload];
-
+ _airGuitarManager = nil;
+ _viewX = nil;
+ _viewY = nil;
+ _viewZ = nil;
}
- (void)viewWillAppear:(BOOL)animated {
@@ -61,14 +62,13 @@ - (void)viewWillAppear:(BOOL)animated {
accessory.delegate = self;
}
}
-
+
// Register for Air Guitar connect and disconnect notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:@"AGAccessoryDidConnect" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:@"AGAccessoryDidDisconnect" object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:@"AGAccessoryDidDisconnect" object:nil];
// Set up the iPhone accelerometer for demo purposes. The Air Guitar acccessory works fine if you don't initiate the iPhone's accelerometer.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
-
_iPhoneAccelerometer = [UIAccelerometer sharedAccelerometer];
_iPhoneAccelerometer.updateInterval = 1.f / 60.f;
_iPhoneAccelerometer.delegate = self;
@@ -86,14 +86,9 @@ - (void)viewWillDisappear:(BOOL)animated {
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
-
-}
-
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-{
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
+
// -------------------------------
#pragma mark - Air Guitar Delegate
@@ -112,13 +107,13 @@ - (void) accessory: (AGAccessory *) accessory x: (double) x y: (double) y z: (do
double xAmplitude = fabs(x * 480.f);
double yAmplitude = fabs(y * 480.f);
double zAmplitude = fabs(z * 480.f);
- NSLog(@"%f, %f, %f", xAmplitude, yAmplitude, zAmplitude);
+ NSLog(@"Delegate: %f, %f, %f", xAmplitude, yAmplitude, zAmplitude);
_viewX.frame = CGRectMake(0.f, 480.f - xAmplitude, 106, xAmplitude);
_viewY.frame = CGRectMake(106.f, 480.f - yAmplitude, 106, yAmplitude);
_viewZ.frame = CGRectMake(212.f, 480.f - zAmplitude, 108, zAmplitude);
}
-
+
// ------------------------------------
#pragma mark - Air Guitar Notifications
@@ -0,0 +1,23 @@
+//
+// AGDisplayLinkViewController.h
+// AirGuitarSDKDemo
+//
+// Created by Ronald Mannak on 8/14/12.
+// Copyright (c) 2012 Yobble. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+/*
+
+ This is an example on how to use Air Guitar SDK in a game using CADisplayLink.
+
+ */
+
+@interface AGDisplayLinkViewController : UIViewController
+@property (strong, nonatomic) IBOutlet UIView *viewX;
+@property (strong, nonatomic) IBOutlet UIView *viewY;
+@property (strong, nonatomic) IBOutlet UIView *viewZ;
+
+
+@end
@@ -0,0 +1,118 @@
+//
+// AGDisplayLinkViewController.m
+// AirGuitarSDKDemo
+//
+// Created by Ronald Mannak on 8/14/12.
+// Copyright (c) 2012 Yobble. All rights reserved.
+//
+
+#import "AGDisplayLinkViewController.h"
+#import <QuartzCore/QuartzCore.h>
+#import "AirGuitarSDK.h"
+
+@interface AGDisplayLinkViewController ()
+
+@property (nonatomic, strong) CADisplayLink *displayLink;
+@property (nonatomic, strong) AGAccessory *accessory;
+@property (nonatomic, strong) AGAccessoryManager *airGuitarManager;
+
+
+@end
+
+@implementation AGDisplayLinkViewController
+
+- (void)viewDidLoad {
+
+ [super viewDidLoad];
+
+ // Set up Air Guitar accessory
+ _airGuitarManager = [AGAccessoryManager sharedAccessoryManager];
+
+ // Check if Air Guitar Move compatible devices are already connected
+ // In theory, multiple Air Guitar Moves could be connected. Choose the first
+
+ if ([_airGuitarManager.connectedAGAccessories count]) {
+ NSArray *accessories = [_airGuitarManager.connectedAGAccessories allValues];
+ self.accessory = [accessories objectAtIndex:0];
+ }
+}
+
+- (void)viewDidUnload {
+ [super viewDidUnload];
+ self.viewX = nil;
+ self.viewY = nil;
+ self.viewZ = nil;
+ _airGuitarManager = nil;
+}
+
+- (void)viewWillAppear:(BOOL)animated {
+
+ [self setupGameLoop];
+
+ // Register for Air Guitar connect and disconnect notifications
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:@"AGAccessoryDidConnect" object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:@"AGAccessoryDidDisconnect" object:nil];
+}
+
+- (void)viewWillDisappear:(BOOL)animated {
+ [self endGameLoop];
+ [[NSNotificationCenter defaultCenter] removeObserver:self ];
+}
+
+// ---------------------
+#pragma mark - Game Loop
+
+- (void)setupGameLoop {
+ // Set up game loop. gameLoop will be called every frame refresh (60 times per seconds)
+ if (!self.displayLink) {
+ self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];
+ [self.displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
+ }
+}
+
+- (void)endGameLoop {
+ [self.displayLink invalidate];
+ self.displayLink = nil;
+
+ // hide bars
+ _viewX.frame = CGRectZero;
+ _viewY.frame = CGRectZero;
+ _viewZ.frame = CGRectZero;
+}
+
+- (void)gameLoop:(CADisplayLink *)sender {
+
+ CMAcceleration acceleration = self.accessory.acceleration;
+
+ double xAmplitude = fabs(acceleration.x * 480.f);
+ double yAmplitude = fabs(acceleration.y * 480.f);
+ double zAmplitude = fabs(acceleration.z * 480.f);
+ NSLog(@"CADisplayLink: %f, %f, %f", xAmplitude, yAmplitude, zAmplitude);
+
+ _viewX.frame = CGRectMake(0.f, 480.f - xAmplitude, 106, xAmplitude);
+ _viewY.frame = CGRectMake(106.f, 480.f - yAmplitude, 106, yAmplitude);
+ _viewZ.frame = CGRectMake(212.f, 480.f - zAmplitude, 108, zAmplitude);
+}
+
+// ------------------------------------
+#pragma mark - Air Guitar Notifications
+
+- (void)accessoryDidConnect: (NSNotification *)notification {
+ self.accessory = (AGAccessory *) notification.object;
+ [self setupGameLoop];
+}
+
+- (void)accessoryDidDisconnect: (NSNotification *)notification {
+ [self endGameLoop];
+}
+
+
+@synthesize displayLink = _displayLink;
+
+@synthesize airGuitarManager = _airGuitarManager,
+ accessory = _accessory;
+
+@synthesize viewX = _viewX,
+ viewY = _viewY,
+ viewZ = _viewZ;
+@end
Oops, something went wrong.

0 comments on commit ce495db

Please sign in to comment.