00001 #ifdef RT_IOS
00002 #ifdef __OBJC__
00003
00004 #import "Types.h"
00005 #import "IOSApplicationDelegate.h"
00006 #include "RendergroupIOSGLView.h"
00007 #include "RenderTools.h"
00008
00009 using namespace RenderTools;
00010
00011 @implementation IOSApplicationDelegate
00012
00013 static IOSApplicationDelegate * s_instance = nil;
00014
00015 IOSApplicationDelegate * getIOSApplicationDelegate( void ){
00016 return( s_instance );
00017 }
00018 RendergroupEAGLView * getIOSApplicationDelegateGLView( void ){
00019 return( s_instance.glView );
00020 }
00021
00022 UIScreen * getIOSApplicationExternalScreen( void ){
00023 return( s_instance.externalScreen );
00024 }
00025
00026 UIWindow * getIOSApplicationExternalWindow( void ){
00027 return( s_instance.externalWindow );
00028 }
00029
00030 UIImageView * getIOSApplicationExternalImageView( void ){
00031 return( [ s_instance.externalWindow.subviews objectAtIndex:0 ] );
00032 }
00033
00034
00035 @synthesize window;
00036 @synthesize glView;
00037 @synthesize externalScreen;
00038 @synthesize externalWindow;
00039
00040 - (NSString *)deviceName {
00041 return [[UIDevice currentDevice] name];
00042 }
00043
00044 - (void)applicationDidFinishLaunching:(UIApplication *)application {
00045
00046 s_instance = self;
00047
00048 [UIApplication sharedApplication].statusBarHidden = YES;
00049
00050 DeviceName device = DEVICENAME_IPHONE;
00051
00052 if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
00053 device = DEVICENAME_IPAD;
00054 }
00055 else{
00056 }
00057
00058 unsigned int version = __IPHONE_OS_VERSION_MAX_ALLOWED;
00059 unsigned int major = version / 10000;
00060 unsigned int minor = ( version - major * 10000 ) / 100;
00061
00062 RendergroupAbstractView::setMajorVersion( major );
00063 RendergroupAbstractView::setMinorVersion( minor );
00064 RendergroupAbstractView::setDeviceName( device );
00065
00067 CGRect frame = [ [ UIScreen mainScreen ] applicationFrame ];
00068 int w = frame.size.width;
00069 int h = frame.size.height;
00070
00071 AbstractPropertyContainer::addGlobalVariable( "SCREEN_WIDTH", toString( w ) );
00072 AbstractPropertyContainer::addGlobalVariable( "SCREEN_HEIGHT", toString( h ) );
00073 AbstractPropertyContainer::addGlobalVariable( "SCREEN_HALF_WIDTH", toString( w / 2 ) );
00074 AbstractPropertyContainer::addGlobalVariable( "SCREEN_HALF_HEIGHT", toString( h / 2 ) );
00075 AbstractPropertyContainer::addGlobalVariable( "SCREEN_TWICE_WIDTH", toString( w * 2 ) );
00076 AbstractPropertyContainer::addGlobalVariable( "SCREEN_TWICE_HEIGHT", toString( h * 2 ) );
00077
00078 RenderTools::IOSMainLoopInitializeCallback();
00079
00080 glView = [[RendergroupEAGLView alloc] initWithFrame:frame];
00081 glView.multipleTouchEnabled = YES;
00082
00083 RenderTools::initializeGL();
00084
00085 [ window addSubview:glView ];
00086
00087 [ window layoutSubviews ];
00088 [ window makeKeyAndVisible ];
00089
00090 UIAccelerometer * a = [UIAccelerometer sharedAccelerometer];
00091 a.delegate = self;
00092 a.updateInterval = 0.1;
00093
00095 const ViewControllerContainer & vc = ViewController::getViewControllers();
00096 for( unsigned int i = 0; i < vc.size(); i++ ){
00097 vc[ i ]->initialize();
00098 }
00099
00100 [ glView startAnimation ];
00101
00102 externalScreen = nil;
00103 externalWindow.hidden = YES;
00104
00105 if( [ [ UIScreen screens ] count ] > 1 ){
00106 NSLog( @"Found an external screen." );
00107
00108
00109 externalScreen = [ [ [ UIScreen screens ] objectAtIndex:1 ] retain ];
00110 NSLog( [ NSString stringWithFormat:@"External screen: %@", externalScreen ] );
00111
00112 externalScreenModes = [ externalScreen.availableModes retain ];
00113 NSLog( [ NSString stringWithFormat:@"Available modes: %@", externalScreenModes ] );
00114
00115
00116 UIAlertView * alert = [ [ [ UIAlertView alloc ] initWithTitle:@"External Display Size"
00117 message:@"Choose a size for the external display."
00118 delegate:self
00119 cancelButtonTitle:nil
00120 otherButtonTitles:nil ] autorelease ];
00121 for( UIScreenMode * mode in externalScreenModes ){
00122 CGSize modeScreenSize = mode.size;
00123 [ alert addButtonWithTitle:[ NSString stringWithFormat:@"%.0f x %.0f pixels", modeScreenSize.width, modeScreenSize.height ] ];
00124 }
00125 [ alert show ];
00126 }
00127 }
00128
00129 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
00130 UIScreenMode * desiredMode = [ externalScreenModes objectAtIndex:buttonIndex ];
00131 NSLog( [ NSString stringWithFormat:@"Setting mode: %@", desiredMode ] );
00132 externalScreen.currentMode = desiredMode;
00133
00134 CGRect rect = CGRectZero;
00135 rect.size = desiredMode.size;
00136 externalWindow = [ [ UIWindow alloc ] initWithFrame:rect ];
00137
00138 NSLog( @"Assigning externalWindow to externalScreen." );
00139 externalWindow.screen = externalScreen;
00140
00141 [ externalScreenModes release ];
00142 [ externalScreen release ];
00143
00144 externalWindow.frame = rect;
00145 externalWindow.clipsToBounds = YES;
00146
00147 [ externalWindow addSubview:[ [ UIImageView alloc ] initWithFrame:rect ] ];
00148
00149 NSLog( @"Displaying externalWindow on externalScreen." );
00150 externalWindow.hidden = NO;
00151 [ externalWindow makeKeyAndVisible ];
00152 }
00153
00154 - (void)accelerometer: (UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
00155
00156 if( getRendergroupIOSGLViewInstance() ){
00157 getRendergroupIOSGLViewInstance()->onTilt( Vec3( acceleration.x, acceleration.y, acceleration.z ) );
00158 }
00159 }
00160
00161 - (void)applicationWillResignActive:(UIApplication *)application {
00163 [glView stopAnimation];
00164 [glView removeFromSuperview];
00165 }
00166
00167
00168 - (void)applicationDidBecomeActive:(UIApplication *)application {
00169 [glView startAnimation];
00170 [window addSubview:glView];
00171 }
00172
00176 - (void)applicationWillTerminate:(UIApplication *)application {
00177
00178 [ glView saveFramebufferToDefault ];
00179
00181 CFPreferencesSetAppValue( CFSTR( "restoreDefaults" ), kCFBooleanFalse, kCFPreferencesCurrentApplication );
00182 CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
00183
00184 [glView stopAnimation];
00185
00186 [glView removeFromSuperview];
00187
00188 [glView quit];
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 }
00199
00200
00201 #pragma mark -
00202 #pragma mark Core Data stack
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 #pragma mark -
00248 #pragma mark Application's Documents directory
00249
00253 - (NSString *)applicationDocumentsDirectory {
00254 return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
00255 }
00256
00257 #pragma mark -
00258 #pragma mark Memory management
00259
00260 - (void)dealloc {
00261
00262
00263
00264
00265
00266 [window release];
00267 [glView release];
00268 [super dealloc];
00269 }
00270
00271 @end
00272
00273 #endif
00274 #endif
00275
00276