istereo2

annotate 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
rev   line source
nuclear@2 1 #include <stdlib.h>
nuclear@2 2 #import <OpenGLES/ES2/glext.h>
nuclear@0 3 #import "viewctl.h"
nuclear@2 4 #include "istereo.h"
nuclear@0 5
nuclear@0 6 @interface ViewController () {
nuclear@0 7 EAGLContext *ctx;
nuclear@3 8 float pixel_scale;
nuclear@1 9
nuclear@1 10 ADBannerView *ad;
nuclear@1 11 BOOL ad_visible;
nuclear@0 12 }
nuclear@0 13
nuclear@1 14
nuclear@1 15 - (void)create_ad;
nuclear@2 16 - (void)show_ad;
nuclear@2 17 - (void)hide_ad;
nuclear@1 18
nuclear@1 19 - (void)init_gl;
nuclear@1 20 - (void)destroy_gl;
nuclear@0 21 @end
nuclear@0 22
nuclear@0 23 @implementation ViewController
nuclear@0 24
nuclear@0 25 - (void)viewDidLoad
nuclear@0 26 {
nuclear@0 27 [super viewDidLoad];
nuclear@0 28
nuclear@3 29
nuclear@0 30 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
nuclear@0 31 if(!self->ctx) {
nuclear@0 32 NSLog(@"Failed to create OpenGL ES 2.0 context");
nuclear@0 33 }
nuclear@0 34
nuclear@0 35 GLKView *view = (GLKView*)self.view;
nuclear@0 36 view.context = self->ctx;
nuclear@0 37 view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
nuclear@0 38
nuclear@3 39 if([view respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
nuclear@3 40 pixel_scale = [[UIScreen mainScreen] scale];
nuclear@3 41 view.contentScaleFactor = pixel_scale;
nuclear@3 42 printf("pixel scale: %g\n", pixel_scale);
nuclear@3 43 } else {
nuclear@3 44 pixel_scale = 1.0f;
nuclear@3 45 }
nuclear@3 46
nuclear@1 47 [self create_ad];
nuclear@1 48
nuclear@1 49 [self init_gl];
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 - (void)dealloc
nuclear@0 53 {
nuclear@1 54 [self destroy_gl];
nuclear@0 55
nuclear@0 56 if([EAGLContext currentContext] == self->ctx) {
nuclear@0 57 [EAGLContext setCurrentContext: nil];
nuclear@0 58 }
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 - (void)didReceiveMemoryWarning
nuclear@0 62 {
nuclear@0 63 [super didReceiveMemoryWarning];
nuclear@0 64
nuclear@0 65 if([self isViewLoaded] && ([[self view] window] == nil)) {
nuclear@0 66 self.view = nil;
nuclear@0 67
nuclear@1 68 [self destroy_gl];
nuclear@0 69
nuclear@0 70 if([EAGLContext currentContext] == self->ctx) {
nuclear@0 71 [EAGLContext setCurrentContext: nil];
nuclear@0 72 }
nuclear@0 73 self->ctx = nil;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 // Dispose of any resources that can be recreated.
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 - (BOOL)prefersStatusBarHidden
nuclear@0 80 {
nuclear@0 81 return YES;
nuclear@0 82 }
nuclear@0 83
nuclear@1 84 - (void)create_ad
nuclear@1 85 {
nuclear@1 86 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
nuclear@1 87 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
nuclear@2 88 ad_visible = YES;
nuclear@2 89 [self hide_ad];
nuclear@1 90 [self.view addSubview: ad];
nuclear@1 91 ad.delegate = self;
nuclear@1 92 }
nuclear@1 93
nuclear@2 94 - (void)show_ad
nuclear@1 95 {
nuclear@1 96 if(!ad_visible) {
nuclear@1 97 CGRect rect = ad.frame;
nuclear@1 98 rect.origin.y = 0;
nuclear@1 99 ad.frame = rect;
nuclear@1 100 ad_visible = YES;
nuclear@1 101 }
nuclear@1 102 }
nuclear@1 103
nuclear@2 104 - (void)hide_ad
nuclear@2 105 {
nuclear@2 106 if(ad_visible) {
nuclear@2 107 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
nuclear@2 108 ad_visible = NO;
nuclear@2 109 }
nuclear@2 110 }
nuclear@2 111
nuclear@2 112 - (void)init_gl
nuclear@2 113 {
nuclear@2 114 [EAGLContext setCurrentContext: self->ctx];
nuclear@2 115
nuclear@2 116 if(init() == -1) {
nuclear@2 117 NSLog(@"app initialization failed");
nuclear@2 118 exit(0);
nuclear@2 119 }
nuclear@2 120 }
nuclear@2 121
nuclear@2 122 - (void)destroy_gl
nuclear@2 123 {
nuclear@2 124 cleanup();
nuclear@2 125 [EAGLContext setCurrentContext: self->ctx];
nuclear@2 126 }
nuclear@2 127
nuclear@2 128
nuclear@2 129 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
nuclear@2 130 {
nuclear@2 131 redraw();
nuclear@2 132 }
nuclear@2 133
nuclear@2 134 - (void)viewDidLayoutSubviews
nuclear@2 135 {
nuclear@2 136 CGRect rect = self.view.frame;
nuclear@3 137 int xsz = rect.size.width * pixel_scale;
nuclear@3 138 int ysz = rect.size.height * pixel_scale;
nuclear@3 139 reshape(xsz, ysz);
nuclear@3 140 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
nuclear@2 141 }
nuclear@2 142
nuclear@2 143 // ADBannerDelegate functions
nuclear@2 144
nuclear@2 145 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
nuclear@2 146 {
nuclear@2 147 [self show_ad];
nuclear@2 148 }
nuclear@2 149
nuclear@1 150 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
nuclear@1 151 {
nuclear@2 152 [self hide_ad];
nuclear@1 153
nuclear@1 154 NSLog(@"Failed to retrieve ad");
nuclear@1 155 }
nuclear@1 156
nuclear@1 157
nuclear@0 158 @end /* implementation ViewController */