istereo2

view src/ios/viewctl.m @ 5:6a39b8912752

fuck the UIKit widgets, I'll do my own widgets, with blackjack, and hookers
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 22 Sep 2015 07:13:47 +0300
parents d4fed8aac9a6
children 3bccfc7d10fe
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 // ADBannerDelegate functions
157 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
158 {
159 [self show_ad];
160 }
162 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
163 {
164 [self hide_ad];
166 NSLog(@"Failed to retrieve ad");
167 }
170 @end /* implementation ViewController */