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