istereo2

view src/ios/viewctl.m @ 16:1b7776cb800b

ios version done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 01 Oct 2015 07:54:57 +0300
parents 3bccfc7d10fe
children
line source
1 #include <stdlib.h>
2 #import <OpenGLES/ES2/glext.h>
3 #import "viewctl.h"
4 #include "istereo.h"
6 @interface ViewController () {
7 EAGLContext *ctx;
8 float pixel_scale, max_pixel_scale, low_pixel_scale;
10 GLKView *glview;
11 ADBannerView *ad;
12 BOOL ad_visible;
13 BOOL ad_ready;
15 int ios_ver_major, ios_ver_minor;
16 BOOL landscape_swap;
17 }
20 - (void)create_ad;
21 - (void)show_ad;
22 - (void)hide_ad;
24 - (void)init_gl;
25 - (void)destroy_gl;
27 - (void)enable_retina;
28 - (void)disable_retina;
29 - (BOOL)is_retina_enabled;
30 - (BOOL)have_retina;
31 @end
33 static ViewController *view_ctl;
34 static BOOL ad_show_requested;
36 void ad_banner_show(void)
37 {
38 if(view_ctl) {
39 [view_ctl show_ad];
40 }
41 ad_show_requested = YES;
42 }
44 void ad_banner_hide(void)
45 {
46 if(view_ctl) {
47 [view_ctl hide_ad];
48 }
49 ad_show_requested = NO;
50 }
52 void use_retina_res(int enable)
53 {
54 if(enable) {
55 [view_ctl enable_retina];
56 } else {
57 [view_ctl disable_retina];
58 }
59 }
61 int using_retina_res(void)
62 {
63 return [view_ctl is_retina_enabled] ? 1 : 0;
64 }
66 int have_retina(void)
67 {
68 if(view_ctl) {
69 return [view_ctl have_retina] ? 1 : 0;
70 }
71 return 0;
72 }
75 @implementation ViewController
77 - (void)viewDidLoad
78 {
79 [super viewDidLoad];
81 view_ctl = self;
83 NSString *str = [[UIDevice currentDevice] systemVersion];
84 if(sscanf(str.UTF8String, "%d.%d", &ios_ver_major, &ios_ver_minor) != 2) {
85 ios_ver_major = 8;
86 ios_ver_minor = 0;
87 }
88 printf("iOS version: %d.%d\n", ios_ver_major, ios_ver_minor);
90 // in iOS versions before 8, the view size and event coordinates are swapped in landscape
91 if((int)NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
92 assert(ios_ver_major < 8);
93 landscape_swap = YES;
94 } else {
95 landscape_swap = NO;
96 }
98 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
99 if(!self->ctx) {
100 NSLog(@"Failed to create OpenGL ES 2.0 context");
101 }
103 glview = (GLKView*)self.view;
104 glview.context = self->ctx;
105 glview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
107 if([glview respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
108 max_pixel_scale = [[UIScreen mainScreen] scale];
109 printf("max pixel scale: %g\n", pixel_scale);
110 } else {
111 max_pixel_scale = 1.0f;
112 }
113 low_pixel_scale = 1.0f;
114 [self disable_retina]; // default to non-retina mode
116 [self create_ad];
118 [self init_gl];
119 }
121 - (void)dealloc
122 {
123 view_ctl = nil;
125 [self destroy_gl];
127 if([EAGLContext currentContext] == self->ctx) {
128 [EAGLContext setCurrentContext: nil];
129 }
130 }
132 - (void)didReceiveMemoryWarning
133 {
134 [super didReceiveMemoryWarning];
136 if([self isViewLoaded] && ([[self view] window] == nil)) {
137 self.view = nil;
139 [self destroy_gl];
141 if([EAGLContext currentContext] == self->ctx) {
142 [EAGLContext setCurrentContext: nil];
143 }
144 self->ctx = nil;
145 }
147 // Dispose of any resources that can be recreated.
148 }
150 - (BOOL)prefersStatusBarHidden
151 {
152 return YES;
153 }
155 - (BOOL)shouldAutorotate
156 {
157 return YES;
158 }
160 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
161 {
162 return UIInterfaceOrientationMaskLandscape;
163 //return UIInterfaceOrientationMaskAll;
164 }
166 - (void)create_ad
167 {
168 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
169 CGRect rect = ad.frame;
170 rect.size.width = glview.bounds.size.width;
171 ad.frame = rect;
172 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
174 ad_visible = YES;
175 ad_ready = NO;
176 [self hide_ad];
177 [glview addSubview: ad];
178 ad.delegate = self;
179 }
181 - (void)show_ad
182 {
183 if(!ad_visible && ad_ready) {
184 CGRect rect = ad.frame;
185 rect.origin.y = 0;
186 ad.frame = rect;
187 ad_visible = YES;
188 }
189 }
191 - (void)hide_ad
192 {
193 if(ad_visible) {
194 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
195 ad_visible = NO;
196 }
197 }
199 - (void)init_gl
200 {
201 [EAGLContext setCurrentContext: self->ctx];
203 if(init() == -1) {
204 NSLog(@"app initialization failed");
205 exit(0);
206 }
207 }
209 - (void)destroy_gl
210 {
211 cleanup();
212 [EAGLContext setCurrentContext: self->ctx];
213 }
215 - (void)enable_retina
216 {
217 pixel_scale = max_pixel_scale;
218 printf("enable_retina setting pixel scale: %g\n", pixel_scale);
219 glview.contentScaleFactor = pixel_scale;
220 [glview setNeedsLayout];
221 }
223 - (void)disable_retina
224 {
225 pixel_scale = low_pixel_scale;
226 printf("disable_retina setting pixel scale: %g\n", pixel_scale);
227 glview.contentScaleFactor = pixel_scale;
228 [glview setNeedsLayout];
229 }
231 - (BOOL)is_retina_enabled
232 {
233 return pixel_scale > low_pixel_scale;
234 }
236 - (BOOL)have_retina
237 {
238 return max_pixel_scale > 1.0;
239 }
242 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
243 {
244 redraw();
245 }
247 - (void)viewDidLayoutSubviews
248 {
249 CGRect rect = self.view.frame;
250 int xsz = rect.size.width * pixel_scale;
251 int ysz = rect.size.height * pixel_scale;
253 BOOL is_landscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
254 if(is_landscape && landscape_swap) {
255 int tmp = xsz;
256 xsz = ysz;
257 ysz = tmp;
258 }
260 reshape(xsz, ysz);
261 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
262 }
264 - (void)touchesBegan: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
265 {
266 UITouch *touch = [touches anyObject];
267 CGPoint pt = [touch locationInView: glview];
268 int x = (int)(pt.x * pixel_scale);
269 int y = (int)(pt.y * pixel_scale);
271 mouse_button(0, 1, x, y);
272 }
274 - (void)touchesMoved: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
275 {
276 UITouch *touch = [touches anyObject];
277 CGPoint pt = [touch locationInView: glview];
278 int x = (int)(pt.x * pixel_scale);
279 int y = (int)(pt.y * pixel_scale);
281 mouse_motion(x, y);
282 }
284 - (void)touchesEnded: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
285 {
286 UITouch *touch = [touches anyObject];
287 CGPoint pt = [touch locationInView: glview];
288 int x = (int)(pt.x * pixel_scale);
289 int y = (int)(pt.y * pixel_scale);
291 mouse_button(0, 0, x, y);
292 printf("touch release %d %d\n", x, y);
293 }
295 - (void)touchesCancelled: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
296 {
297 UITouch *touch = [touches anyObject];
298 CGPoint pt = [touch locationInView: glview];
299 int x = (int)(pt.x * pixel_scale);
300 int y = (int)(pt.y * pixel_scale);
302 mouse_button(0, 0, x, y);
303 }
305 // ADBannerDelegate functions
307 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
308 {
309 ad_ready = YES;
310 if(ad_show_requested) {
311 [self show_ad];
312 }
313 }
315 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
316 {
317 ad_ready = NO;
318 [self hide_ad];
320 NSLog(@"Failed to retrieve ad");
321 }
324 @end /* implementation ViewController */