update iOS project

This commit is contained in:
2019-07-12 10:47:58 +02:00
parent c4633a906f
commit ea0679dfe5
8 changed files with 112 additions and 186 deletions

View File

@@ -12,8 +12,6 @@
#include "app.h"
#import "objc_utils.h"
std::deque<std::packaged_task<void()>> tasklist;
std::mutex task_mutex;
std::mutex render_mutex;
std::condition_variable render_cv;
@@ -30,9 +28,6 @@ std::condition_variable render_cv;
NSLock* gl_lock;
}
@property (strong, nonatomic) EAGLContext *context;
- (void)setupGL;
- (void)tearDownGL;
@end
//std::map<
@@ -82,6 +77,7 @@ std::recursive_mutex lock_mutex;
- (void)async_swap
{
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self->glview display];
}
- (void)share_file:(NSString *)file_path
@@ -144,12 +140,18 @@ std::recursive_mutex lock_mutex;
- (void)insertText:(NSString *)text
{
if (const char* cstr = [text cStringUsingEncoding:NSASCIIStringEncoding])
App::I.key_char(cstr[0]);
App::I->ui_task_async([=]
{
App::I->key_char(cstr[0]);
});
NSLog(@"%@", text);
// Do something with the typed character
}
- (void)deleteBackward {
App::I.key_char('\b');
App::I->ui_task_async([=]
{
App::I->key_char('\b');
});
// Handle the delete key
}
- (BOOL)hasText {
@@ -192,13 +194,13 @@ std::recursive_mutex lock_mutex;
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
App::I.redraw = true;
App::I.animate = true;
App::I->redraw = true;
App::I->animate = true;
}
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
App::I.redraw = true;
App::I.animate = false;
App::I->redraw = true;
App::I->animate = false;
[self unregisterForKeyboardNotifications];
}
@@ -213,11 +215,12 @@ std::recursive_mutex lock_mutex;
frame.size.height -= kbSize.height;
view.frame = frame;
[self async_lock];
App::I.resize(frame.size.width * view.contentScaleFactor,
App::I->ui_task_async([=]
{
App::I->resize(frame.size.width * view.contentScaleFactor,
frame.size.height * view.contentScaleFactor);
App::I.animate = false;
[self async_unlock];
});
App::I->animate = false;
}
// Called when the UIKeyboardWillHideNotification is sent
@@ -225,21 +228,23 @@ std::recursive_mutex lock_mutex;
{
CGRect frame = [[UIScreen mainScreen] bounds];
self.view.frame = frame;
[self async_lock];
App::I.resize(frame.size.width * self.view.contentScaleFactor,
App::I->ui_task_async([=]
{
App::I->resize(frame.size.width * self.view.contentScaleFactor,
frame.size.height * self.view.contentScaleFactor);
App::I.animate = true;
[self async_unlock];
});
App::I->animate = true;
}
- (void)reset_touch
{
[self async_lock];
if (t_count == 2)
App::I.gesture_end();
else
App::I.mouse_cancel(0);
[self async_unlock];
App::I->ui_task_async([=]
{
if (t_count == 2)
App::I->gesture_end();
else
App::I->mouse_cancel(0);
});
t_count = 0;
}
@@ -255,9 +260,8 @@ std::set<UITouch*> ignored_touch;
if (source == kEventSource::Stylus)
pen_down = true;
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, 0);
App::I->ui_task_async([touchLocation, scale, f=touch.force, source] {
App::I->mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, f, source, 0);
});
t_count = 1;
t_pos = {touchLocation.x * scale, touchLocation.y * scale};
@@ -301,25 +305,23 @@ std::set<UITouch*> ignored_touch;
if (pen_down)
{
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] {
App::I->ui_task_async([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, 0);
App::I->mouse_move(p0.x, p0.y, f0, kEventSource::Stylus, 0);
if (tt1 == UITouchType::UITouchTypeStylus)
App::I.mouse_move(p1.x, p1.y, f1, kEventSource::Stylus, 0);
App::I->mouse_move(p1.x, p1.y, f1, kEventSource::Stylus, 0);
});
}
else if (n == 2)
{
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([c=t_count, p0, p1] {
App::I->ui_task_async([c=t_count, p0, p1] {
if (c == 1)
{
App::I.mouse_cancel(0);
App::I.gesture_start(p0, p1);
App::I->mouse_cancel(0);
App::I->gesture_start(p0, p1);
}
else
App::I.gesture_move(p0, p1);
App::I->gesture_move(p0, p1);
});
t_count = 2;
}
@@ -332,15 +334,14 @@ std::set<UITouch*> ignored_touch;
//force = [app sonarpen_pressure];
if (t_count == 2)
{
//App::I.gesture_end();
//App::I.mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, force);
//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;
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, 0);
App::I->ui_task_async([touchLocation, scale, source, force] {
App::I->mouse_move(touchLocation.x * scale, touchLocation.y * scale, force, source, 0);
});
}
}
@@ -361,12 +362,11 @@ std::set<UITouch*> ignored_touch;
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
pen_down = false;
std::lock_guard<std::mutex> lock(task_mutex);
tasklist.emplace_back([tc=t_count, touchLocation, scale, source] {
App::I->ui_task_async([tc=t_count, touchLocation, scale, source] {
if (tc == 2)
App::I.gesture_end();
App::I->gesture_end();
else
App::I.mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source, 0);
App::I->mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source, 0);
});
t_count = 0;
@@ -375,15 +375,16 @@ std::set<UITouch*> ignored_touch;
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self async_lock];
App::I.resize(size.width * self.view.contentScaleFactor,
App::I->ui_task_async([=]
{
App::I->resize(size.width * self.view.contentScaleFactor,
size.height * self.view.contentScaleFactor);
[self async_unlock];
});
}
- (void)viewDidAppear:(BOOL)animated
{
App::I.redraw = true;
App::I->redraw = true;
[self resignFirstResponder];
//[self registerForKeyboardNotifications];
}
@@ -391,8 +392,9 @@ std::set<UITouch*> ignored_touch;
- (void)viewDidLoad
{
[super viewDidLoad];
App::I.initLog();
App::I.ios_view = self;
App::I = new App;
App::I->initLog();
App::I->ios_view = self;
//self.preferredFramesPerSecond = 60;
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
@@ -409,122 +411,17 @@ std::set<UITouch*> ignored_touch;
view.enableSetNeedsDisplay = NO;
glview = view;
App::I.width = view.frame.size.width * view.contentScaleFactor;
App::I.height = view.frame.size.height * view.contentScaleFactor;
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.
App::I->render_thread_start();
App::I->ui_thread_start();
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
-(void)tick
{
//std::lock_guard<std::mutex> lock(render_mutex);
render_cv.notify_one();
}
-(void)render_loop
{
while (true)
{
static auto time = std::chrono::steady_clock::now();
static double elapsed = 0;
static double min_fps_timer = 0;
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - time);
time = now;
elapsed += dt.count();
min_fps_timer += dt.count();
if (min_fps_timer > 0.5)
{
App::I.redraw = true;
min_fps_timer = 0;
}
std::deque<std::packaged_task<void()>> working_list;
if (!tasklist.empty())
{
std::lock_guard<std::mutex> lock(task_mutex);
working_list = std::move(tasklist);
}
App::I.tick(dt.count());
[self async_lock];
if (!(App::I.redraw || App::I.animate || !working_list.empty()))
{
//[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self->glview display];
[self async_unlock];
std::unique_lock<std::mutex> lock(render_mutex);
render_cv.wait(lock);
continue;
}
while (!working_list.empty())
{
working_list.front()();
working_list.pop_front();
}
App::I.clear();
App::I.update(elapsed);
[self.context presentRenderbuffer:GL_FRAMEBUFFER];
[self->glview display];
[self async_unlock];
elapsed = 0;
}
}
- (void)setupGL
{
[self async_lock];
App::I.init();
[self async_unlock];
std::thread([self]{
[self render_loop];
}).detach();
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick)];
displayLink.frameInterval = 1;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)tearDownGL
{
[EAGLContext setCurrentContext:self.context];
}
@end
@implementation GameView