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"
#import "objc_utils.h"
std::deque<std::packaged_task<void()>> tasklist;
std::mutex task_mutex;
@interface GameImagePicker : UIImagePickerController
{
@public std::promise<std::string> promise;
@@ -203,9 +206,12 @@ std::set<UITouch*> 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<std::mutex> 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<UITouch*> 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<std::mutex> 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)
std::lock_guard<std::mutex> 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<UITouch*> ignored_touch;
else
{
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
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};
}
-(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;
pen_down = false;
[self async_lock];
if (t_count == 2)
// [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<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);
[self async_unlock];
});
t_count = 0;
t_pos = {touchLocation.x * scale, touchLocation.y * scale};
@@ -392,7 +414,6 @@ std::set<UITouch*> ignored_touch;
- (void)tearDownGL
{
[EAGLContext setCurrentContext:self.context];
}
#pragma mark - GLKView and GLKViewController delegate methods
@@ -408,19 +429,26 @@ std::set<UITouch*> ignored_touch;
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - time);
[gl_lock lock];
if (!(App::I.redraw || App::I.animate))
std::deque<std::packaged_task<void()>> working_list;
if (!tasklist.empty())
{
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[gl_lock unlock];
return;
std::lock_guard<std::mutex> 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;
}