task based events on iOS, but not fully multithreaded

This commit is contained in:
2018-08-08 22:13:16 +02:00
parent 7c14ec19b0
commit 49a72cb1e7

View File

@@ -12,6 +12,9 @@
#include "app.h" #include "app.h"
#import "objc_utils.h" #import "objc_utils.h"
std::deque<std::packaged_task<void()>> tasklist;
std::mutex task_mutex;
@interface GameImagePicker : UIImagePickerController @interface GameImagePicker : UIImagePickerController
{ {
@public std::promise<std::string> promise; @public std::promise<std::string> promise;
@@ -203,9 +206,12 @@ std::set<UITouch*> ignored_touch;
// if (touch.majorRadius > 25.f) // if (touch.majorRadius > 25.f)
// ignored_touch.insert(touch); // ignored_touch.insert(touch);
// NSLog(@"touch down size %f", touch.majorRadius); // NSLog(@"touch down size %f", touch.majorRadius);
[self async_lock]; // [self async_lock];
App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, touch.force, source); std::lock_guard<std::mutex> lock(task_mutex);
[self async_unlock]; 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_count = 1;
t_pos = {touchLocation.x * scale, touchLocation.y * scale}; t_pos = {touchLocation.x * scale, touchLocation.y * scale};
} }
@@ -246,23 +252,29 @@ std::set<UITouch*> ignored_touch;
p1 = glm::vec2(tl1.x * scale, tl1.y * scale); p1 = glm::vec2(tl1.x * scale, tl1.y * scale);
} }
[self async_lock]; //[self async_lock];
if (pen_down) if (pen_down)
{ {
if (t0.type == UITouchType::UITouchTypeStylus) std::lock_guard<std::mutex> lock(task_mutex);
App::I.mouse_move(p0.x, p0.y, t0.force, kEventSource::Stylus); tasklist.emplace_back([tt0=t0.type,tt1=t1.type,p0,p1,f0=t0.force,f1=t1.force] {
if (t1.type == UITouchType::UITouchTypeStylus) if (tt0 == UITouchType::UITouchTypeStylus)
App::I.mouse_move(p1.x, p1.y, t1.force, kEventSource::Stylus); 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) else if (n == 2)
{ {
if (t_count == 1) std::lock_guard<std::mutex> lock(task_mutex);
{ tasklist.emplace_back([c=t_count, p0, p1] {
App::I.mouse_cancel(0); if (c == 1)
App::I.gesture_start(p0, p1); {
} App::I.mouse_cancel(0);
else App::I.gesture_start(p0, p1);
App::I.gesture_move(p0, p1); }
else
App::I.gesture_move(p0, p1);
});
t_count = 2; t_count = 2;
} }
else if (n == 1) else if (n == 1)
@@ -279,10 +291,13 @@ std::set<UITouch*> ignored_touch;
else else
{ {
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; 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<std::mutex> 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}; t_pos = {tl0.x * scale, tl0.y * scale};
} }
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
@@ -300,12 +315,19 @@ std::set<UITouch*> ignored_touch;
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch; kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
pen_down = false; pen_down = false;
[self async_lock]; // [self async_lock];
if (t_count == 2) // if (t_count == 2)
App::I.gesture_end(); // App::I.gesture_end();
else // else
App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source); // App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source);
[self async_unlock]; // [self async_unlock];
std::lock_guard<std::mutex> 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_count = 0;
t_pos = {touchLocation.x * scale, touchLocation.y * scale}; t_pos = {touchLocation.x * scale, touchLocation.y * scale};
@@ -392,7 +414,6 @@ std::set<UITouch*> ignored_touch;
- (void)tearDownGL - (void)tearDownGL
{ {
[EAGLContext setCurrentContext:self.context]; [EAGLContext setCurrentContext:self.context];
} }
#pragma mark - GLKView and GLKViewController delegate methods #pragma mark - GLKView and GLKViewController delegate methods
@@ -408,19 +429,26 @@ std::set<UITouch*> ignored_touch;
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - time); auto dt = std::chrono::duration<float>(now - time);
[gl_lock lock]; std::deque<std::packaged_task<void()>> working_list;
if (!(App::I.redraw || App::I.animate)) if (!tasklist.empty())
{ {
[self.context presentRenderbuffer:GL_FRAMEBUFFER]; std::lock_guard<std::mutex> lock(task_mutex);
[gl_lock unlock]; working_list = std::move(tasklist);
return; }
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.clear();
App::I.update(dt.count()); App::I.update(dt.count());
[self.context presentRenderbuffer:GL_FRAMEBUFFER]; [self.context presentRenderbuffer:GL_FRAMEBUFFER];
[gl_lock unlock]; [self async_unlock];
time = now; time = now;
} }