// // GameViewController.m // PanoPainter // // Created by Omar Mohamed Ali Mudhir on 07/05/17. // Copyright © 2017 Omar Mohamed Ali Mudhir. All rights reserved. // #include "pch.h" #import "GameViewController.h" #import #include "app.h" @interface GameViewController () { NSLock* gl_lock; } @property (strong, nonatomic) EAGLContext *context; - (void)setupGL; - (void)tearDownGL; @end //std::map< int t_count = 0; glm::vec2 t_pos; int lock_count = 0; NSThread* lock_thread; @implementation GameViewController - (void)async_lock { if (lock_thread != [NSThread currentThread] || lock_count == 0) [gl_lock lock]; lock_thread = [NSThread currentThread]; lock_count++; [EAGLContext setCurrentContext:self.context]; GLKView* view = (GLKView*)self.view; [view bindDrawable]; } - (void)async_unlock { lock_count--; if (lock_count == 0) [gl_lock unlock]; } - (void)async_swap { [self.context presentRenderbuffer:GL_RENDERBUFFER]; } - (void)insertText:(NSString *)text { if (const char* cstr = [text cStringUsingEncoding:NSASCIIStringEncoding]) App::I.key_char(cstr[0]); NSLog(@"%@", text); // Do something with the typed character } - (void)deleteBackward { App::I.key_char('\b'); // Handle the delete key } - (BOOL)hasText { // Return whether there's any text present return YES; } - (BOOL)canBecomeFirstResponder { return YES; } // Call this method somewhere in your view controller setup code. - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil]; } - (void)keyboardWillBeShown:(NSNotification*)aNotification { App::I.redraw = true; App::I.animate = true; } - (void)keyboardWasHidden:(NSNotification*)aNotification { App::I.redraw = true; App::I.animate = false; } // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; GLKView *view = (GLKView *)self.view; CGRect frame = view.frame; frame.size.height -= kbSize.height; view.frame = frame; [self async_lock]; App::I.resize(frame.size.width * view.contentScaleFactor, frame.size.height * view.contentScaleFactor); App::I.animate = false; [self async_unlock]; } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { CGRect frame = [[UIScreen mainScreen] bounds]; self.view.frame = frame; [self async_lock]; App::I.resize(frame.size.width * self.view.contentScaleFactor, frame.size.height * self.view.contentScaleFactor); App::I.animate = true; [self async_unlock]; } - (void)reset_touch { [self async_lock]; if (t_count == 2) App::I.gesture_end(); else App::I.mouse_cancel(0); [self async_unlock]; t_count = 0; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; float scale = self.view.contentScaleFactor; kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; [self async_lock]; App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force, source); [self async_unlock]; t_count = 1; t_pos = {touchLocation.x * scale, touchLocation.y * scale}; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSUInteger n = [[event allTouches] count]; float scale = self.view.contentScaleFactor; UITouch* t0 = nullptr; UITouch* t1 = nullptr; t0 = [[[event allTouches] allObjects] objectAtIndex:0]; CGPoint tl0 = [t0 locationInView:self.view]; glm::vec2 p0 = glm::vec2(tl0.x * scale, tl0.y * scale); glm::vec2 p1; if (n > 1) { t1 = [[[event allTouches] allObjects] objectAtIndex:1]; CGPoint tl1 = [t1 locationInView:self.view]; p1 = glm::vec2(tl1.x * scale, tl1.y * scale); } [self async_lock]; if (n == 2) { if (t_count == 1) { App::I.mouse_cancel(0); App::I.gesture_start(p0, p1); } else App::I.gesture_move(p0, p1); t_count = 2; } else if (n == 1) { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; //auto p = glm::vec2(touchLocation.x * scale, touchLocation.y * scale); float force = touch.type == UITouchType::UITouchTypeStylus ? touch.force : 1.0f; if (t_count == 2) { //App::I.gesture_end(); //App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, force); } else { kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; App::I.mouse_move(touchLocation.x * scale, touchLocation.y * scale, force, source); } } [self async_unlock]; t_pos = {tl0.x * scale, tl0.y * scale}; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; float scale = self.view.contentScaleFactor; kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; [self async_lock]; if (t_count == 2) App::I.gesture_end(); else App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source); [self async_unlock]; t_count = 0; t_pos = {touchLocation.x * scale, touchLocation.y * scale}; } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [self async_lock]; App::I.resize(size.width * self.view.contentScaleFactor, size.height * self.view.contentScaleFactor); [self async_unlock]; } - (void)viewDidAppear:(BOOL)animated { App::I.redraw = true; [self resignFirstResponder]; [self registerForKeyboardNotifications]; } - (void)viewDidLoad { [super viewDidLoad]; App::I.initLog(); App::I.ios_view = self; self.preferredFramesPerSecond = 60; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; if (!self.context) { NSLog(@"Failed to create ES context"); } gl_lock = [[NSLock alloc] init]; GLKView *view = (GLKView *)self.view; view.context = self.context; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; glview = view; App::I.width = view.frame.size.width * view.contentScaleFactor; App::I.height = view.frame.size.height * view.contentScaleFactor; [self setupGL]; } - (void)dealloc { [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; if ([self isViewLoaded] && ([[self view] window] == nil)) { self.view = nil; [self tearDownGL]; if ([EAGLContext currentContext] == self.context) { [EAGLContext setCurrentContext:nil]; } self.context = nil; } // Dispose of any resources that can be recreated. } - (BOOL)prefersStatusBarHidden { return YES; } - (void)setupGL { [EAGLContext setCurrentContext:self.context]; App::I.init(); } - (void)tearDownGL { [EAGLContext setCurrentContext:self.context]; } #pragma mark - GLKView and GLKViewController delegate methods - (void)update { } - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { [gl_lock lock]; if (!(App::I.redraw || App::I.animate)) { [self.context presentRenderbuffer:GL_FRAMEBUFFER]; [gl_lock unlock]; return; } [EAGLContext setCurrentContext:self.context]; [view bindDrawable]; App::I.clear(); App::I.update(0); [self.context presentRenderbuffer:GL_FRAMEBUFFER]; [gl_lock unlock]; } @end