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