2015-02-20 12:54:42 +03:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build darwin
|
2015-05-26 17:18:04 +03:00
|
|
|
// +build arm arm64
|
2015-02-20 12:54:42 +03:00
|
|
|
|
|
|
|
#include "_cgo_export.h"
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import <GLKit/GLKit.h>
|
|
|
|
|
|
|
|
struct utsname sysInfo;
|
|
|
|
|
2015-10-08 20:49:30 +03:00
|
|
|
@interface GoAppAppController : GLKViewController<UIContentContainer, GLKViewDelegate>
|
2015-02-20 12:54:42 +03:00
|
|
|
@end
|
|
|
|
|
2015-06-09 23:38:29 +03:00
|
|
|
@interface GoAppAppDelegate : UIResponder<UIApplicationDelegate>
|
2015-02-20 12:54:42 +03:00
|
|
|
@property (strong, nonatomic) UIWindow *window;
|
2015-06-09 23:38:29 +03:00
|
|
|
@property (strong, nonatomic) GoAppAppController *controller;
|
2015-02-20 12:54:42 +03:00
|
|
|
@end
|
|
|
|
|
2015-06-09 23:38:29 +03:00
|
|
|
@implementation GoAppAppDelegate
|
2015-02-20 12:54:42 +03:00
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
2015-10-08 20:49:30 +03:00
|
|
|
lifecycleAlive();
|
2015-02-20 12:54:42 +03:00
|
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
2015-06-09 23:38:29 +03:00
|
|
|
self.controller = [[GoAppAppController alloc] initWithNibName:nil bundle:nil];
|
2015-02-20 12:54:42 +03:00
|
|
|
self.window.rootViewController = self.controller;
|
|
|
|
[self.window makeKeyAndVisible];
|
|
|
|
return YES;
|
|
|
|
}
|
2015-10-08 20:49:30 +03:00
|
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication * )application {
|
|
|
|
lifecycleFocused();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillResignActive:(UIApplication *)application {
|
|
|
|
lifecycleVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
|
|
|
lifecycleAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillTerminate:(UIApplication *)application {
|
|
|
|
lifecycleDead();
|
|
|
|
}
|
2015-02-20 12:54:42 +03:00
|
|
|
@end
|
|
|
|
|
2015-06-09 23:38:29 +03:00
|
|
|
@interface GoAppAppController ()
|
2015-02-20 12:54:42 +03:00
|
|
|
@property (strong, nonatomic) EAGLContext *context;
|
2015-10-08 20:49:30 +03:00
|
|
|
@property (strong, nonatomic) GLKView *glview;
|
2015-02-20 12:54:42 +03:00
|
|
|
@end
|
|
|
|
|
2015-06-09 23:38:29 +03:00
|
|
|
@implementation GoAppAppController
|
2015-10-08 20:49:30 +03:00
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
// TODO: replace by swapping out GLKViewController for a UIVIewController.
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
self.paused = YES;
|
|
|
|
}
|
|
|
|
|
2015-02-20 12:54:42 +03:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
[super viewDidLoad];
|
|
|
|
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
2015-10-08 20:49:30 +03:00
|
|
|
self.glview = (GLKView*)self.view;
|
|
|
|
self.glview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
|
|
|
|
self.glview.multipleTouchEnabled = true; // TODO expose setting to user.
|
|
|
|
self.glview.context = self.context;
|
|
|
|
self.glview.userInteractionEnabled = YES;
|
|
|
|
self.glview.enableSetNeedsDisplay = YES; // only invoked once
|
|
|
|
|
|
|
|
// Do not use the GLKViewController draw loop.
|
|
|
|
self.paused = YES;
|
|
|
|
self.resumeOnDidBecomeActive = NO;
|
|
|
|
self.preferredFramesPerSecond = 0;
|
2015-06-27 17:28:28 +03:00
|
|
|
|
|
|
|
int scale = 1;
|
|
|
|
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
|
|
|
|
scale = (int)[UIScreen mainScreen].scale; // either 1.0, 2.0, or 3.0.
|
|
|
|
}
|
|
|
|
setScreen(scale);
|
|
|
|
|
|
|
|
CGSize size = [UIScreen mainScreen].bounds.size;
|
2015-08-10 17:15:43 +03:00
|
|
|
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
|
|
updateConfig((int)size.width, (int)size.height, orientation);
|
2015-02-20 12:54:42 +03:00
|
|
|
}
|
2015-05-26 21:33:20 +03:00
|
|
|
|
2015-06-27 17:28:28 +03:00
|
|
|
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
2015-08-10 17:15:43 +03:00
|
|
|
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
|
2015-08-11 06:56:25 +03:00
|
|
|
// TODO(crawshaw): come up with a plan to handle animations.
|
2015-08-10 17:15:43 +03:00
|
|
|
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
|
|
|
|
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
|
|
updateConfig((int)size.width, (int)size.height, orientation);
|
|
|
|
}];
|
2015-06-27 17:28:28 +03:00
|
|
|
}
|
2015-03-17 19:39:21 +03:00
|
|
|
|
2015-10-08 20:49:30 +03:00
|
|
|
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
|
|
|
|
// Now that we have been asked to do the first draw, disable any
|
|
|
|
// future draw and hand control over to the Go paint.Event cycle.
|
|
|
|
self.glview.enableSetNeedsDisplay = NO;
|
|
|
|
startloop((GLintptr)self.context);
|
2015-02-20 12:54:42 +03:00
|
|
|
}
|
2015-05-26 21:33:20 +03:00
|
|
|
|
2015-07-17 08:14:56 +03:00
|
|
|
#define TOUCH_TYPE_BEGIN 0 // touch.TypeBegin
|
2015-07-15 10:17:08 +03:00
|
|
|
#define TOUCH_TYPE_MOVE 1 // touch.TypeMove
|
|
|
|
#define TOUCH_TYPE_END 2 // touch.TypeEnd
|
2015-05-26 21:33:20 +03:00
|
|
|
|
2015-07-01 02:57:23 +03:00
|
|
|
static void sendTouches(int change, NSSet* touches) {
|
2015-05-26 21:33:20 +03:00
|
|
|
CGFloat scale = [UIScreen mainScreen].scale;
|
|
|
|
for (UITouch* touch in touches) {
|
|
|
|
CGPoint p = [touch locationInView:touch.view];
|
2015-07-01 02:57:23 +03:00
|
|
|
sendTouch((GoUintptr)touch, (GoUintptr)change, p.x*scale, p.y*scale);
|
2015-05-26 21:33:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
|
2015-07-17 08:14:56 +03:00
|
|
|
sendTouches(TOUCH_TYPE_BEGIN, touches);
|
2015-05-26 21:33:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
|
2015-07-15 10:17:08 +03:00
|
|
|
sendTouches(TOUCH_TYPE_MOVE, touches);
|
2015-05-26 21:33:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
|
2015-07-15 10:17:08 +03:00
|
|
|
sendTouches(TOUCH_TYPE_END, touches);
|
2015-05-26 21:33:20 +03:00
|
|
|
}
|
2015-12-28 08:42:41 +03:00
|
|
|
|
|
|
|
- (void)touchesCanceled:(NSSet*)touches withEvent:(UIEvent*)event {
|
|
|
|
sendTouches(TOUCH_TYPE_END, touches);
|
|
|
|
}
|
2015-02-20 12:54:42 +03:00
|
|
|
@end
|
|
|
|
|
|
|
|
void runApp(void) {
|
|
|
|
@autoreleasepool {
|
2015-06-09 23:38:29 +03:00
|
|
|
UIApplicationMain(0, nil, nil, NSStringFromClass([GoAppAppDelegate class]));
|
2015-02-20 12:54:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:49:30 +03:00
|
|
|
void makeCurrentContext(GLintptr context) {
|
2015-02-20 12:54:42 +03:00
|
|
|
EAGLContext* ctx = (EAGLContext*)context;
|
|
|
|
if (![EAGLContext setCurrentContext:ctx]) {
|
|
|
|
// TODO(crawshaw): determine how terrible this is. Exit?
|
|
|
|
NSLog(@"failed to set current context");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:49:30 +03:00
|
|
|
void swapBuffers(GLintptr context) {
|
|
|
|
__block EAGLContext* ctx = (EAGLContext*)context;
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
[EAGLContext setCurrentContext:ctx];
|
|
|
|
[ctx presentRenderbuffer:GL_RENDERBUFFER];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-20 12:54:42 +03:00
|
|
|
uint64_t threadID() {
|
|
|
|
uint64_t id;
|
|
|
|
if (pthread_threadid_np(pthread_self(), &id)) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|