From 49a72cb1e7c1899a2e0926c53d7531171c754333 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 8 Aug 2018 22:13:16 +0200 Subject: [PATCH] task based events on iOS, but not fully multithreaded --- PanoPainter/GameViewController.m | 94 +++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/PanoPainter/GameViewController.m b/PanoPainter/GameViewController.m index 9c2ba66..ea7d77f 100644 --- a/PanoPainter/GameViewController.m +++ b/PanoPainter/GameViewController.m @@ -12,6 +12,9 @@ #include "app.h" #import "objc_utils.h" +std::deque> tasklist; +std::mutex task_mutex; + @interface GameImagePicker : UIImagePickerController { @public std::promise promise; @@ -203,9 +206,12 @@ std::set ignored_touch; // if (touch.majorRadius > 25.f) // ignored_touch.insert(touch); // NSLog(@"touch down size %f", touch.majorRadius); - [self async_lock]; - App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force, source); - [self async_unlock]; +// [self async_lock]; + std::lock_guard lock(task_mutex); + tasklist.emplace_back([touchLocation, scale, f=touch.force, source] { + App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, f, source); + }); +// [self async_unlock]; t_count = 1; t_pos = {touchLocation.x * scale, touchLocation.y * scale}; } @@ -246,23 +252,29 @@ std::set ignored_touch; p1 = glm::vec2(tl1.x * scale, tl1.y * scale); } - [self async_lock]; + //[self async_lock]; if (pen_down) { - if (t0.type == UITouchType::UITouchTypeStylus) - App::I.mouse_move(p0.x, p0.y, t0.force, kEventSource::Stylus); - if (t1.type == UITouchType::UITouchTypeStylus) - App::I.mouse_move(p1.x, p1.y, t1.force, kEventSource::Stylus); + std::lock_guard lock(task_mutex); + tasklist.emplace_back([tt0=t0.type,tt1=t1.type,p0,p1,f0=t0.force,f1=t1.force] { + if (tt0 == UITouchType::UITouchTypeStylus) + App::I.mouse_move(p0.x, p0.y, f0, kEventSource::Stylus); + if (tt1 == UITouchType::UITouchTypeStylus) + App::I.mouse_move(p1.x, p1.y, f1, kEventSource::Stylus); + }); } else 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); + std::lock_guard lock(task_mutex); + tasklist.emplace_back([c=t_count, p0, p1] { + if (c == 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) @@ -279,10 +291,13 @@ std::set ignored_touch; else { kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; - App::I.mouse_move(touchLocation.x * scale, touchLocation.y * scale, force, source); + std::lock_guard lock(task_mutex); + tasklist.emplace_back([touchLocation, scale, source, force] { + App::I.mouse_move(touchLocation.x * scale, touchLocation.y * scale, force, source); + }); } } - [self async_unlock]; + //[self async_unlock]; t_pos = {tl0.x * scale, tl0.y * scale}; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event @@ -300,12 +315,19 @@ std::set ignored_touch; kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; pen_down = false; - [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]; +// [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]; + std::lock_guard lock(task_mutex); + tasklist.emplace_back([tc=t_count, touchLocation, scale, source] { + if (tc == 2) + App::I.gesture_end(); + else + App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source); + }); t_count = 0; t_pos = {touchLocation.x * scale, touchLocation.y * scale}; @@ -334,7 +356,7 @@ std::set ignored_touch; self.preferredFramesPerSecond = 60; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; - + if (!self.context) { NSLog(@"Failed to create ES context"); } @@ -392,7 +414,6 @@ std::set ignored_touch; - (void)tearDownGL { [EAGLContext setCurrentContext:self.context]; - } #pragma mark - GLKView and GLKViewController delegate methods @@ -408,19 +429,26 @@ std::set ignored_touch; auto now = std::chrono::steady_clock::now(); auto dt = std::chrono::duration(now - time); - [gl_lock lock]; - if (!(App::I.redraw || App::I.animate)) + std::deque> working_list; + if (!tasklist.empty()) { - [self.context presentRenderbuffer:GL_FRAMEBUFFER]; - [gl_lock unlock]; - return; + std::lock_guard lock(task_mutex); + working_list = std::move(tasklist); + } + + if (!(App::I.redraw || App::I.animate || !working_list.empty())) + return; + + [self async_lock]; + while (!working_list.empty()) + { + working_list.front()(); + working_list.pop_front(); } - [EAGLContext setCurrentContext:self.context]; - [view bindDrawable]; App::I.clear(); App::I.update(dt.count()); [self.context presentRenderbuffer:GL_FRAMEBUFFER]; - [gl_lock unlock]; + [self async_unlock]; time = now; }