istereo2
view src/ios/viewctl.m @ 3:dc735bdeeb8a
mkicons script
added content-scale support for "retina" devices
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Sep 2015 07:40:34 +0300 |
parents | 81d35769f546 |
children | d4fed8aac9a6 |
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 ADBannerView *ad;
11 BOOL ad_visible;
12 }
15 - (void)create_ad;
16 - (void)show_ad;
17 - (void)hide_ad;
19 - (void)init_gl;
20 - (void)destroy_gl;
21 @end
23 @implementation ViewController
25 - (void)viewDidLoad
26 {
27 [super viewDidLoad];
30 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
31 if(!self->ctx) {
32 NSLog(@"Failed to create OpenGL ES 2.0 context");
33 }
35 GLKView *view = (GLKView*)self.view;
36 view.context = self->ctx;
37 view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
39 if([view respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
40 pixel_scale = [[UIScreen mainScreen] scale];
41 view.contentScaleFactor = pixel_scale;
42 printf("pixel scale: %g\n", pixel_scale);
43 } else {
44 pixel_scale = 1.0f;
45 }
47 [self create_ad];
49 [self init_gl];
50 }
52 - (void)dealloc
53 {
54 [self destroy_gl];
56 if([EAGLContext currentContext] == self->ctx) {
57 [EAGLContext setCurrentContext: nil];
58 }
59 }
61 - (void)didReceiveMemoryWarning
62 {
63 [super didReceiveMemoryWarning];
65 if([self isViewLoaded] && ([[self view] window] == nil)) {
66 self.view = nil;
68 [self destroy_gl];
70 if([EAGLContext currentContext] == self->ctx) {
71 [EAGLContext setCurrentContext: nil];
72 }
73 self->ctx = nil;
74 }
76 // Dispose of any resources that can be recreated.
77 }
79 - (BOOL)prefersStatusBarHidden
80 {
81 return YES;
82 }
84 - (void)create_ad
85 {
86 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
87 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
88 ad_visible = YES;
89 [self hide_ad];
90 [self.view addSubview: ad];
91 ad.delegate = self;
92 }
94 - (void)show_ad
95 {
96 if(!ad_visible) {
97 CGRect rect = ad.frame;
98 rect.origin.y = 0;
99 ad.frame = rect;
100 ad_visible = YES;
101 }
102 }
104 - (void)hide_ad
105 {
106 if(ad_visible) {
107 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
108 ad_visible = NO;
109 }
110 }
112 - (void)init_gl
113 {
114 [EAGLContext setCurrentContext: self->ctx];
116 if(init() == -1) {
117 NSLog(@"app initialization failed");
118 exit(0);
119 }
120 }
122 - (void)destroy_gl
123 {
124 cleanup();
125 [EAGLContext setCurrentContext: self->ctx];
126 }
129 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
130 {
131 redraw();
132 }
134 - (void)viewDidLayoutSubviews
135 {
136 CGRect rect = self.view.frame;
137 int xsz = rect.size.width * pixel_scale;
138 int ysz = rect.size.height * pixel_scale;
139 reshape(xsz, ysz);
140 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
141 }
143 // ADBannerDelegate functions
145 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
146 {
147 [self show_ad];
148 }
150 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
151 {
152 [self hide_ad];
154 NSLog(@"Failed to retrieve ad");
155 }
158 @end /* implementation ViewController */