istereo2

annotate src/ios/viewctl.m @ 6:3bccfc7d10fe

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