istereo2

diff src/ios/viewctl.m @ 16:1b7776cb800b

ios version done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 01 Oct 2015 07:54:57 +0300
parents 3bccfc7d10fe
children
line diff
     1.1 --- a/src/ios/viewctl.m	Wed Sep 30 04:41:21 2015 +0300
     1.2 +++ b/src/ios/viewctl.m	Thu Oct 01 07:54:57 2015 +0300
     1.3 @@ -5,11 +5,15 @@
     1.4  
     1.5  @interface ViewController () {
     1.6  	EAGLContext *ctx;
     1.7 -	float pixel_scale;
     1.8 +	float pixel_scale, max_pixel_scale, low_pixel_scale;
     1.9  
    1.10  	GLKView *glview;
    1.11  	ADBannerView *ad;
    1.12  	BOOL ad_visible;
    1.13 +	BOOL ad_ready;
    1.14 +
    1.15 +	int ios_ver_major, ios_ver_minor;
    1.16 +	BOOL landscape_swap;
    1.17  }
    1.18  
    1.19  
    1.20 @@ -19,14 +23,77 @@
    1.21  
    1.22  - (void)init_gl;
    1.23  - (void)destroy_gl;
    1.24 +
    1.25 +- (void)enable_retina;
    1.26 +- (void)disable_retina;
    1.27 +- (BOOL)is_retina_enabled;
    1.28 +- (BOOL)have_retina;
    1.29  @end
    1.30  
    1.31 +static ViewController *view_ctl;
    1.32 +static BOOL ad_show_requested;
    1.33 +
    1.34 +void ad_banner_show(void)
    1.35 +{
    1.36 +	if(view_ctl) {
    1.37 +		[view_ctl show_ad];
    1.38 +	}
    1.39 +	ad_show_requested = YES;
    1.40 +}
    1.41 +
    1.42 +void ad_banner_hide(void)
    1.43 +{
    1.44 +	if(view_ctl) {
    1.45 +		[view_ctl hide_ad];
    1.46 +	}
    1.47 +	ad_show_requested = NO;
    1.48 +}
    1.49 +
    1.50 +void use_retina_res(int enable)
    1.51 +{
    1.52 +	if(enable) {
    1.53 +		[view_ctl enable_retina];
    1.54 +	} else {
    1.55 +		[view_ctl disable_retina];
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +int using_retina_res(void)
    1.60 +{
    1.61 +	return [view_ctl is_retina_enabled] ? 1 : 0;
    1.62 +}
    1.63 +
    1.64 +int have_retina(void)
    1.65 +{
    1.66 +	if(view_ctl) {
    1.67 +		return [view_ctl have_retina] ? 1 : 0;
    1.68 +	}
    1.69 +	return 0;
    1.70 +}
    1.71 +
    1.72 +
    1.73  @implementation ViewController
    1.74  
    1.75  - (void)viewDidLoad
    1.76  {
    1.77      [super viewDidLoad];
    1.78  
    1.79 +	view_ctl = self;
    1.80 +
    1.81 +	NSString *str = [[UIDevice currentDevice] systemVersion];
    1.82 +	if(sscanf(str.UTF8String, "%d.%d", &ios_ver_major, &ios_ver_minor) != 2) {
    1.83 +		ios_ver_major = 8;
    1.84 +		ios_ver_minor = 0;
    1.85 +	}
    1.86 +	printf("iOS version: %d.%d\n", ios_ver_major, ios_ver_minor);
    1.87 +
    1.88 +	// in iOS versions before 8, the view size and event coordinates are swapped in landscape
    1.89 +	if((int)NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
    1.90 +		assert(ios_ver_major < 8);
    1.91 +		landscape_swap = YES;
    1.92 +	} else {
    1.93 +		landscape_swap = NO;
    1.94 +	}
    1.95  
    1.96      self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
    1.97      if(!self->ctx) {
    1.98 @@ -38,12 +105,13 @@
    1.99      glview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
   1.100  
   1.101  	if([glview respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
   1.102 -		pixel_scale = [[UIScreen mainScreen] scale];
   1.103 -		glview.contentScaleFactor = pixel_scale;
   1.104 -		printf("pixel scale: %g\n", pixel_scale);
   1.105 +		max_pixel_scale = [[UIScreen mainScreen] scale];
   1.106 +		printf("max pixel scale: %g\n", pixel_scale);
   1.107  	} else {
   1.108 -		pixel_scale = 1.0f;
   1.109 +		max_pixel_scale = 1.0f;
   1.110  	}
   1.111 +	low_pixel_scale = 1.0f;
   1.112 +	[self disable_retina];	// default to non-retina mode
   1.113  
   1.114  	[self create_ad];
   1.115  
   1.116 @@ -52,6 +120,8 @@
   1.117  
   1.118  - (void)dealloc
   1.119  {
   1.120 +	view_ctl = nil;
   1.121 +
   1.122  	[self destroy_gl];
   1.123  
   1.124      if([EAGLContext currentContext] == self->ctx) {
   1.125 @@ -96,8 +166,13 @@
   1.126  - (void)create_ad
   1.127  {
   1.128  	ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
   1.129 +	CGRect rect = ad.frame;
   1.130 +	rect.size.width = glview.bounds.size.width;
   1.131 +	ad.frame = rect;
   1.132  	[ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
   1.133 +
   1.134  	ad_visible = YES;
   1.135 +	ad_ready = NO;
   1.136  	[self hide_ad];
   1.137  	[glview addSubview: ad];
   1.138  	ad.delegate = self;
   1.139 @@ -105,7 +180,7 @@
   1.140  
   1.141  - (void)show_ad
   1.142  {
   1.143 -	if(!ad_visible) {
   1.144 +	if(!ad_visible && ad_ready) {
   1.145  		CGRect rect = ad.frame;
   1.146  		rect.origin.y = 0;
   1.147  		ad.frame = rect;
   1.148 @@ -137,6 +212,32 @@
   1.149      [EAGLContext setCurrentContext: self->ctx];
   1.150  }
   1.151  
   1.152 +- (void)enable_retina
   1.153 +{
   1.154 +	pixel_scale = max_pixel_scale;
   1.155 +	printf("enable_retina setting pixel scale: %g\n", pixel_scale);
   1.156 +	glview.contentScaleFactor = pixel_scale;
   1.157 +	[glview setNeedsLayout];
   1.158 +}
   1.159 +
   1.160 +- (void)disable_retina
   1.161 +{
   1.162 +	pixel_scale = low_pixel_scale;
   1.163 +	printf("disable_retina setting pixel scale: %g\n", pixel_scale);
   1.164 +	glview.contentScaleFactor = pixel_scale;
   1.165 +	[glview setNeedsLayout];
   1.166 +}
   1.167 +
   1.168 +- (BOOL)is_retina_enabled
   1.169 +{
   1.170 +	return pixel_scale > low_pixel_scale;
   1.171 +}
   1.172 +
   1.173 +- (BOOL)have_retina
   1.174 +{
   1.175 +	return max_pixel_scale > 1.0;
   1.176 +}
   1.177 +
   1.178  
   1.179  - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
   1.180  {
   1.181 @@ -148,6 +249,14 @@
   1.182  	CGRect rect = self.view.frame;
   1.183  	int xsz = rect.size.width * pixel_scale;
   1.184  	int ysz = rect.size.height * pixel_scale;
   1.185 +
   1.186 +	BOOL is_landscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
   1.187 +	if(is_landscape && landscape_swap) {
   1.188 +		int tmp = xsz;
   1.189 +		xsz = ysz;
   1.190 +		ysz	= tmp;
   1.191 +	}
   1.192 +
   1.193  	reshape(xsz, ysz);
   1.194  	printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
   1.195  }
   1.196 @@ -155,40 +264,57 @@
   1.197  - (void)touchesBegan: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
   1.198  {
   1.199  	UITouch *touch = [touches anyObject];
   1.200 -	CGPoint pt = [touch locationInView: nil];
   1.201 -	mouse_button(0, 1, pt.x, pt.y);
   1.202 +	CGPoint pt = [touch locationInView: glview];
   1.203 +	int x = (int)(pt.x * pixel_scale);
   1.204 +	int y = (int)(pt.y * pixel_scale);
   1.205 +
   1.206 +	mouse_button(0, 1, x, y);
   1.207  }
   1.208  
   1.209  - (void)touchesMoved: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
   1.210  {
   1.211  	UITouch *touch = [touches anyObject];
   1.212 -	CGPoint pt = [touch locationInView: nil];
   1.213 -	mouse_motion(pt.x, pt.y);
   1.214 +	CGPoint pt = [touch locationInView: glview];
   1.215 +	int x = (int)(pt.x * pixel_scale);
   1.216 +	int y = (int)(pt.y * pixel_scale);
   1.217 +
   1.218 +	mouse_motion(x, y);
   1.219  }
   1.220  
   1.221  - (void)touchesEnded: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
   1.222  {
   1.223  	UITouch *touch = [touches anyObject];
   1.224 -	CGPoint pt = [touch locationInView: nil];
   1.225 -	mouse_button(0, 0, pt.x, pt.y);
   1.226 +	CGPoint pt = [touch locationInView: glview];
   1.227 +	int x = (int)(pt.x * pixel_scale);
   1.228 +	int y = (int)(pt.y * pixel_scale);
   1.229 +
   1.230 +	mouse_button(0, 0, x, y);
   1.231 +	printf("touch release %d %d\n", x, y);
   1.232  }
   1.233  
   1.234  - (void)touchesCancelled: (NSSet<UITouch*>*)touches withEvent: (UIEvent*)event
   1.235  {
   1.236  	UITouch *touch = [touches anyObject];
   1.237 -	CGPoint pt = [touch locationInView: nil];
   1.238 -	mouse_button(0, 0, pt.x, pt.y);
   1.239 +	CGPoint pt = [touch locationInView: glview];
   1.240 +	int x = (int)(pt.x * pixel_scale);
   1.241 +	int y = (int)(pt.y * pixel_scale);
   1.242 +
   1.243 +	mouse_button(0, 0, x, y);
   1.244  }
   1.245  
   1.246  // ADBannerDelegate functions
   1.247  
   1.248  - (void)bannerViewDidLoadAd: (ADBannerView*)banner
   1.249  {
   1.250 -	[self show_ad];
   1.251 +	ad_ready = YES;
   1.252 +	if(ad_show_requested) {
   1.253 +		[self show_ad];
   1.254 +	}
   1.255  }
   1.256  
   1.257  - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
   1.258  {
   1.259 +	ad_ready = NO;
   1.260  	[self hide_ad];
   1.261  
   1.262  	NSLog(@"Failed to retrieve ad");