1*fce0c873SBarry Smith// 2*fce0c873SBarry Smith// iphoneViewController.m 3*fce0c873SBarry Smith// iphone 4*fce0c873SBarry Smith// 5*fce0c873SBarry Smith// Created by Barry Smith on 5/12/10. 6*fce0c873SBarry Smith// Copyright __MyCompanyName__ 2010. All rights reserved. 7*fce0c873SBarry Smith// 8*fce0c873SBarry Smith 9*fce0c873SBarry Smith#import "iphoneViewController.h" 10*fce0c873SBarry Smith#import <PETSc/petsc.h> 11*fce0c873SBarry Smith 12*fce0c873SBarry Smith@implementation iphoneViewController 13*fce0c873SBarry Smith@synthesize glkView; 14*fce0c873SBarry Smith@synthesize textField; 15*fce0c873SBarry Smith@synthesize textView; 16*fce0c873SBarry Smith 17*fce0c873SBarry Smith 18*fce0c873SBarry Smith/* 19*fce0c873SBarry Smith// The designated initializer. Override to perform setup that is required before the view is loaded. 20*fce0c873SBarry Smith- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 21*fce0c873SBarry Smith if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 22*fce0c873SBarry Smith // Custom initialization 23*fce0c873SBarry Smith } 24*fce0c873SBarry Smith return self; 25*fce0c873SBarry Smith} 26*fce0c873SBarry Smith*/ 27*fce0c873SBarry Smith 28*fce0c873SBarry Smith 29*fce0c873SBarry Smith/* 30*fce0c873SBarry Smith // Implement loadView to create a view hierarchy programmatically, without using a nib. 31*fce0c873SBarry Smith- (void)loadView { 32*fce0c873SBarry Smith} 33*fce0c873SBarry Smith*/ 34*fce0c873SBarry Smith 35*fce0c873SBarry Smith 36*fce0c873SBarry Smith// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 37*fce0c873SBarry Smith- (void)viewDidLoad { 38*fce0c873SBarry Smith [super viewDidLoad]; 39*fce0c873SBarry Smith EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 40*fce0c873SBarry Smith 41*fce0c873SBarry Smith if (!context) { 42*fce0c873SBarry Smith NSLog(@"Failed to create ES context"); 43*fce0c873SBarry Smith } 44*fce0c873SBarry Smith NSLog(@"Created ES context"); 45*fce0c873SBarry Smith 46*fce0c873SBarry Smith glkView.context = context; 47*fce0c873SBarry Smith [EAGLContext setCurrentContext:context]; 48*fce0c873SBarry Smith} 49*fce0c873SBarry Smith 50*fce0c873SBarry Smith 51*fce0c873SBarry Smith 52*fce0c873SBarry Smith/* 53*fce0c873SBarry Smith// Override to allow orientations other than the default portrait orientation. 54*fce0c873SBarry Smith- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 55*fce0c873SBarry Smith // Return YES for supported orientations 56*fce0c873SBarry Smith return (interfaceOrientation == UIInterfaceOrientationPortrait); 57*fce0c873SBarry Smith} 58*fce0c873SBarry Smith*/ 59*fce0c873SBarry Smith 60*fce0c873SBarry Smith- (void)didReceiveMemoryWarning { 61*fce0c873SBarry Smith // Releases the view if it doesn't have a superview. 62*fce0c873SBarry Smith [super didReceiveMemoryWarning]; 63*fce0c873SBarry Smith 64*fce0c873SBarry Smith // Release any cached data, images, etc that aren't in use. 65*fce0c873SBarry Smith} 66*fce0c873SBarry Smith 67*fce0c873SBarry Smith- (void)viewDidUnload { 68*fce0c873SBarry Smith [glkView release]; 69*fce0c873SBarry Smith glkView = nil; 70*fce0c873SBarry Smith [glkView release]; 71*fce0c873SBarry Smith glkView = nil; 72*fce0c873SBarry Smith [self setGlkView:nil]; 73*fce0c873SBarry Smith // Release any retained subviews of the main view. 74*fce0c873SBarry Smith // e.g. self.myOutlet = nil; 75*fce0c873SBarry Smith} 76*fce0c873SBarry Smith 77*fce0c873SBarry Smith 78*fce0c873SBarry Smith- (void)dealloc { 79*fce0c873SBarry Smith [textField release]; 80*fce0c873SBarry Smith [glkView release]; 81*fce0c873SBarry Smith [glkView release]; 82*fce0c873SBarry Smith [glkView release]; 83*fce0c873SBarry Smith [super dealloc]; 84*fce0c873SBarry Smith} 85*fce0c873SBarry Smith 86*fce0c873SBarry Smith/* 87*fce0c873SBarry Smith Not sure if this does anything 88*fce0c873SBarry Smith*/ 89*fce0c873SBarry Smithvoid SwapBuffers() 90*fce0c873SBarry Smith{ 91*fce0c873SBarry Smith EAGLContext* context = [EAGLContext currentContext]; 92*fce0c873SBarry Smith 93*fce0c873SBarry Smith [context presentRenderbuffer:GL_RENDERBUFFER]; 94*fce0c873SBarry Smith} 95*fce0c873SBarry Smith 96*fce0c873SBarry SmithUITextView *globalTextView; 97*fce0c873SBarry Smith 98*fce0c873SBarry Smith 99*fce0c873SBarry Smith/* 100*fce0c873SBarry Smith This is called by PETSc for all print calls. 101*fce0c873SBarry Smith 102*fce0c873SBarry Smith Simply addeds to the NSString in globalTextView and it gets displayed in the UITextView in the display 103*fce0c873SBarry Smith*/ 104*fce0c873SBarry SmithPetscErrorCode PetscVFPrintfiPhone(FILE *fd,const char *format,va_list Argp) 105*fce0c873SBarry Smith{ 106*fce0c873SBarry Smith size_t len; 107*fce0c873SBarry Smith char str[1024]; 108*fce0c873SBarry Smith 109*fce0c873SBarry Smith PetscVSNPrintf(str,1024,format,&len,Argp); 110*fce0c873SBarry Smith globalTextView.text = [NSString stringWithFormat:@"%@%s", globalTextView.text,str]; 111*fce0c873SBarry Smith return 0; 112*fce0c873SBarry Smith} 113*fce0c873SBarry Smith 114*fce0c873SBarry Smith#define main ex19 115*fce0c873SBarry Smith#define help help19 116*fce0c873SBarry Smith#define Field Field19 117*fce0c873SBarry Smith#define PETSC_APPLE_FRAMEWORK 118*fce0c873SBarry Smith#include "../../../../../../src/snes/examples/tutorials/ex19.c" 119*fce0c873SBarry Smith#undef main 120*fce0c873SBarry Smith#undef help 121*fce0c873SBarry Smith#undef Field 122*fce0c873SBarry Smith#define main ex48 123*fce0c873SBarry Smith#define help help48 124*fce0c873SBarry Smith#define Field Field48 125*fce0c873SBarry Smith#include "../../../../../../src/snes/examples/tutorials/ex48.c" 126*fce0c873SBarry Smith#undef main 127*fce0c873SBarry Smith#undef help 128*fce0c873SBarry Smith#undef Field 129*fce0c873SBarry Smith#define main ex4 130*fce0c873SBarry Smith#define help help4 131*fce0c873SBarry Smith#include "../../../../../../src/sys/classes/draw/examples/tests/ex4.c" 132*fce0c873SBarry Smith#undef main 133*fce0c873SBarry Smith#undef help 134*fce0c873SBarry Smith#undef Field 135*fce0c873SBarry Smith#define main ex3 136*fce0c873SBarry Smith#define help help3 137*fce0c873SBarry Smith#include "../../../../../../src/sys/classes/draw/examples/tests/ex3.c" 138*fce0c873SBarry Smith 139*fce0c873SBarry Smithextern PetscErrorCode PetscDrawOpenGLESRegisterGLKView(GLKView *); 140*fce0c873SBarry Smith 141*fce0c873SBarry Smith/* 142*fce0c873SBarry Smith This is called each time one hits return in the TextField. 143*fce0c873SBarry Smith 144*fce0c873SBarry Smith Converts the string to a collection of arguments that are then passed on to PETSc 145*fce0c873SBarry Smith*/ 146*fce0c873SBarry Smith- (BOOL) textFieldShouldReturn: (UITextField*) theTextField { 147*fce0c873SBarry Smith [theTextField resignFirstResponder]; /* makes the keyboard disappear */ 148*fce0c873SBarry Smith textView.text = @""; /* clears the UITextView */ 149*fce0c873SBarry Smith 150*fce0c873SBarry Smith globalTextView = textView; /* we make this class member a global so can use in PetscVFPrintfiPhone() */ 151*fce0c873SBarry Smith 152*fce0c873SBarry Smith PetscDrawOpenGLESRegisterGLKView(glkView); /* Let PETSc know about this window it may use */ 153*fce0c873SBarry Smith textView.font = [UIFont fontWithName:@"Courier" size:8.0]; /* make the font size in the UITextView a more reasonable size and use fixed width*/ 154*fce0c873SBarry Smith 155*fce0c873SBarry Smith 156*fce0c873SBarry Smith 157*fce0c873SBarry Smith 158*fce0c873SBarry Smith const char *str = [textField.text UTF8String]; 159*fce0c873SBarry Smith char **args; 160*fce0c873SBarry Smith int argc; 161*fce0c873SBarry Smith PetscBool flg1,flg2,flg3,flg4; 162*fce0c873SBarry Smith 163*fce0c873SBarry Smith PetscErrorCode ierr = PetscStrToArray(str,' ',&argc,&args); 164*fce0c873SBarry Smith ierr = PetscStrncmp(str, "./ex19", 6, &flg1); 165*fce0c873SBarry Smith ierr = PetscStrncmp(str, "./ex48", 6, &flg2); 166*fce0c873SBarry Smith ierr = PetscStrncmp(str, "./ex4", 5, &flg3); 167*fce0c873SBarry Smith ierr = PetscStrncmp(str, "./ex3", 5, &flg4); 168*fce0c873SBarry Smith if (flg1) { 169*fce0c873SBarry Smith ex19(argc,args); 170*fce0c873SBarry Smith } else if (flg2) { 171*fce0c873SBarry Smith ex48(argc,args); 172*fce0c873SBarry Smith } else if (flg3) { 173*fce0c873SBarry Smith ex4(argc,args); 174*fce0c873SBarry Smith } else if (flg4) { 175*fce0c873SBarry Smith ex3(argc,args); 176*fce0c873SBarry Smith } else { 177*fce0c873SBarry Smith textView.text =@"Must start with ./ex3, ./ex4, ./ex19 or ./ex48 "; 178*fce0c873SBarry Smith ierr = PetscStrToArrayDestroy(argc,args); 179*fce0c873SBarry Smith return YES; 180*fce0c873SBarry Smith } 181*fce0c873SBarry Smith ierr = PetscStrToArrayDestroy(argc,args); 182*fce0c873SBarry Smith 183*fce0c873SBarry Smith 184*fce0c873SBarry Smith return YES; 185*fce0c873SBarry Smith} 186*fce0c873SBarry Smith 187*fce0c873SBarry Smith 188*fce0c873SBarry Smith 189*fce0c873SBarry Smith@end 190