118 lines
2.9 KiB
Objective-C
118 lines
2.9 KiB
Objective-C
#import "ViewController.h"
|
|
#import "egl_bridge_ios.h"
|
|
#include "utils.h"
|
|
|
|
#include "EGL/egl.h"
|
|
|
|
#include "GLES2/gl2.h"
|
|
#include "GLES2/gl2ext.h"
|
|
|
|
@interface ViewController () {
|
|
}
|
|
|
|
@property (strong, nonatomic) MGLContext *context;
|
|
|
|
- (void)setupGL;
|
|
|
|
@end
|
|
|
|
@implementation ViewController
|
|
|
|
- (void)viewDidLoad
|
|
{
|
|
[super viewDidLoad];
|
|
|
|
viewController = self;
|
|
|
|
MGLKView *view = glView = (MGLKView *)self.view;
|
|
view.drawableDepthFormat = MGLDrawableDepthFormat24;
|
|
view.enableSetNeedsDisplay = YES;
|
|
|
|
self.context = [[MGLContext alloc] initWithAPI:kMGLRenderingAPIOpenGLES3];
|
|
|
|
if (!self.context) {
|
|
self.context = [[MGLContext alloc] initWithAPI:kMGLRenderingAPIOpenGLES2];
|
|
}
|
|
|
|
if (!self.context) {
|
|
NSLog(@"Failed to create ES context");
|
|
}
|
|
|
|
view.context = self.context;
|
|
#ifndef USE_EGL
|
|
glContext = self.context;
|
|
#endif
|
|
|
|
[MGLContext setCurrentContext:self.context];
|
|
|
|
[self setupGL];
|
|
}
|
|
|
|
/*
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
{
|
|
[super viewWillAppear:animated];
|
|
}
|
|
*/
|
|
|
|
- (void)dealloc
|
|
{
|
|
if ([MGLContext currentContext] == self.context) {
|
|
[MGLContext setCurrentContext:nil];
|
|
}
|
|
}
|
|
|
|
- (void)setupGL
|
|
{
|
|
[MGLContext setCurrentContext:self.context];
|
|
|
|
CGRect screenBounds = [[UIScreen mainScreen] bounds];
|
|
CGFloat screenScale = [[UIScreen mainScreen] scale];
|
|
|
|
int width_c = (int) roundf(screenBounds.size.width * screenScale);
|
|
int height_c = (int) roundf(screenBounds.size.height * screenScale);
|
|
// glViewport(0, 0, width_c, height_c);
|
|
callback_AppDelegate_didFinishLaunching(width_c, height_c);
|
|
}
|
|
|
|
- (void)mglkView:(MGLKView *)view drawInRect:(CGRect)rect {
|
|
// glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
|
|
// glClear(GL_COLOR_BUFFER_BIT);
|
|
// [self setNeedsDisplay]
|
|
// NSLog(@"swapbuffer");
|
|
|
|
[super pause];
|
|
}
|
|
|
|
- (void)sendTouchEvent:(NSSet *)touches withEvent:(int)event
|
|
{
|
|
UITouch* touchEvent = [touches anyObject];
|
|
CGPoint locationInView = [touchEvent locationInView:self.view];
|
|
// CGPoint normalizedPoint = getNormalizedPoint(self.view, locationInView);
|
|
callback_ViewController_onTouch(event, locationInView.x, locationInView.y /* normalizedPoint.x, normalizedPoint.y */);
|
|
}
|
|
|
|
// Equals to Android ACTION_DOWN
|
|
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
|
{
|
|
[super touchesBegan: touches withEvent: event];
|
|
[self sendTouchEvent: touches withEvent: ACTION_DOWN];
|
|
}
|
|
|
|
// Equals to Android ACTION_MOVE
|
|
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
|
|
{
|
|
[super touchesMoved: touches withEvent: event];
|
|
[self sendTouchEvent: touches withEvent: ACTION_MOVE];
|
|
}
|
|
|
|
// Equals to Android ACTION_UP
|
|
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
|
|
{
|
|
[super touchesEnded: touches withEvent: event];
|
|
[self sendTouchEvent: touches withEvent: ACTION_UP];
|
|
}
|
|
|
|
// #pragma mark - GLKView and GLKViewController delegate methods
|
|
@end
|