istereo2
view src/ios/viewctl.m @ 14:018f997dc646
button done
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 29 Sep 2015 01:11:54 +0300 |
parents | 6a39b8912752 |
children | 1b7776cb800b |
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;
10 GLKView *glview;
11 ADBannerView *ad;
12 BOOL ad_visible;
13 }
16 - (void)create_ad;
17 - (void)show_ad;
18 - (void)hide_ad;
20 - (void)init_gl;
21 - (void)destroy_gl;
22 @end
24 @implementation ViewController
26 - (void)viewDidLoad
27 {
28 [super viewDidLoad];
31 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
32 if(!self->ctx) {
33 NSLog(@"Failed to create OpenGL ES 2.0 context");
34 }
36 glview = (GLKView*)self.view;
37 glview.context = self->ctx;
38 glview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
40 if([glview respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
41 pixel_scale = [[UIScreen mainScreen] scale];
42 glview.contentScaleFactor = pixel_scale;
43 printf("pixel scale: %g\n", pixel_scale);
44 } else {
45 pixel_scale = 1.0f;
46 }
48 [self create_ad];
50 [self init_gl];
51 }
53 - (void)dealloc
54 {
55 [self destroy_gl];
57 if([EAGLContext currentContext] == self->ctx) {
58 [EAGLContext setCurrentContext: nil];
59 }
60 }
62 - (void)didReceiveMemoryWarning
63 {
64 [super didReceiveMemoryWarning];
66 if([self isViewLoaded] && ([[self view] window] == nil)) {
67 self.view = nil;
69 [self destroy_gl];
71 if([EAGLContext currentContext] == self->ctx) {
72 [EAGLContext setCurrentContext: nil];
73 }
74 self->ctx = nil;
75 }
77 // Dispose of any resources that can be recreated.
78 }
80 - (BOOL)prefersStatusBarHidden
81 {
82 return YES;
83 }
85 - (BOOL)shouldAutorotate
86 {
87 return YES;
88 }
90 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
91 {
92 return UIInterfaceOrientationMaskLandscape;
93 //return UIInterfaceOrientationMaskAll;
94 }
96 - (void)create_ad
97 {
98 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
99 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
100 ad_visible = YES;
101 [self hide_ad];
102 [glview addSubview: ad];
103 ad.delegate = self;
104 }
106 - (void)show_ad
107 {
108 if(!ad_visible) {
109 CGRect rect = ad.frame;
110 rect.origin.y = 0;
111 ad.frame = rect;
112 ad_visible = YES;
113 }
114 }
116 - (void)hide_ad
117 {
118 if(ad_visible) {
119 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
120 ad_visible = NO;
121 }
122 }
124 - (void)init_gl
125 {
126 [EAGLContext setCurrentContext: self->ctx];
128 if(init() == -1) {
129 NSLog(@"app initialization failed");
130 exit(0);
131 }
132 }
134 - (void)destroy_gl
135 {
136 cleanup();
137 [EAGLContext setCurrentContext: self->ctx];
138 }
141 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
142 {
143 redraw();
144 }
146 - (void)viewDidLayoutSubviews
147 {
148 CGRect rect = self.view.frame;
149 int xsz = rect.size.width * pixel_scale;
150 int ysz = rect.size.height * pixel_scale;
151 reshape(xsz, ysz);
152 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
153 }
155 - (void)touchesBegan: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
156 {
157 UITouch *touch = [touches anyObject];
158 CGPoint pt = [touch locationInView: nil];
159 mouse_button(0, 1, pt.x, pt.y);
160 }
162 - (void)touchesMoved: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
163 {
164 UITouch *touch = [touches anyObject];
165 CGPoint pt = [touch locationInView: nil];
166 mouse_motion(pt.x, pt.y);
167 }
169 - (void)touchesEnded: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
170 {
171 UITouch *touch = [touches anyObject];
172 CGPoint pt = [touch locationInView: nil];
173 mouse_button(0, 0, pt.x, pt.y);
174 }
176 - (void)touchesCancelled: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
177 {
178 UITouch *touch = [touches anyObject];
179 CGPoint pt = [touch locationInView: nil];
180 mouse_button(0, 0, pt.x, pt.y);
181 }
183 // ADBannerDelegate functions
185 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
186 {
187 [self show_ad];
188 }
190 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
191 {
192 [self hide_ad];
194 NSLog(@"Failed to retrieve ad");
195 }
198 @end /* implementation ViewController */