|
@ -25,55 +25,128 @@ GLViewFunctionality simple_draw(std::function<void()> f){ |
|
|
@property GLViewFunctionality functionality; |
|
|
@property GLViewFunctionality functionality; |
|
|
@end |
|
|
@end |
|
|
|
|
|
|
|
|
|
|
|
@interface MySlider : NSSlider |
|
|
|
|
|
- (instancetype) initWithFrame:(NSRect)frame; |
|
|
|
|
|
- (void)action:(MySlider*)sender; |
|
|
|
|
|
@property std::function<void(CGFloat)> callback; |
|
|
|
|
|
@end |
|
|
|
|
|
|
|
|
|
|
|
@implementation MySlider |
|
|
|
|
|
- (instancetype) initWithFrame:(NSRect)frame{ |
|
|
|
|
|
if(self = [super initWithFrame: frame]){ |
|
|
|
|
|
self.target = self; |
|
|
|
|
|
self.action = @selector(action:); |
|
|
|
|
|
} |
|
|
|
|
|
return self; |
|
|
|
|
|
} |
|
|
|
|
|
- (void)action:(MySlider*)sender{ |
|
|
|
|
|
if(self.callback){ |
|
|
|
|
|
self.callback(sender.doubleValue); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
@end |
|
|
|
|
|
|
|
|
struct NSAppWrapperImpl { |
|
|
struct NSAppWrapperImpl { |
|
|
NSAppWrapperImpl() { |
|
|
NSAppWrapperImpl() { |
|
|
app = [NSApplication sharedApplication]; |
|
|
app = [NSApplication sharedApplication]; |
|
|
app.activationPolicy = NSApplicationActivationPolicyRegular; |
|
|
app.activationPolicy = NSApplicationActivationPolicyRegular; |
|
|
appName = [[NSProcessInfo processInfo] processName]; |
|
|
appName = [[NSProcessInfo processInfo] processName]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void set_up_menu() { |
|
|
void set_up_menu() { |
|
|
NSMenu* appMenu = [NSMenu new]; |
|
|
NSMenu* appMenu = [NSMenu new]; |
|
|
[appMenu addItemWithTitle:[@"Quit " stringByAppendingString:appName] |
|
|
[appMenu addItemWithTitle:[@"Quit " stringByAppendingString:appName] |
|
|
action:@selector(terminate:) |
|
|
action:@selector(terminate:) |
|
|
keyEquivalent:@"q"]; |
|
|
keyEquivalent:@"q"]; |
|
|
|
|
|
|
|
|
NSMenuItem * menuItem = [NSMenuItem new]; |
|
|
NSMenuItem * menuItem = [NSMenuItem new]; |
|
|
menuItem.submenu = appMenu; |
|
|
menuItem.submenu = appMenu; |
|
|
|
|
|
|
|
|
app.mainMenu = [NSMenu new]; |
|
|
app.mainMenu = [NSMenu new]; |
|
|
[app.mainMenu addItem:menuItem]; |
|
|
[app.mainMenu addItem:menuItem]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void create_window(GLViewFunctionality const & f) { |
|
|
void create_window(GLViewFunctionality const & f) { |
|
|
NSWindow * window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 500, 500) |
|
|
NSWindow * window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 500, 500) |
|
|
styleMask:NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask |
|
|
styleMask:NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask |
|
|
backing:NSBackingStoreBuffered |
|
|
backing:NSBackingStoreBuffered |
|
|
defer:YES]; |
|
|
defer:YES]; |
|
|
window.title = appName; |
|
|
window.title = appName; |
|
|
window.collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary; |
|
|
window.collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary; |
|
|
[window center]; |
|
|
if(windows.empty()){ |
|
|
[window makeKeyAndOrderFront:nil]; |
|
|
[window center]; |
|
|
|
|
|
} else { |
|
|
NSRect frame = [window contentRectForFrameRect:window.frame]; |
|
|
NSWindow * previous_window = windows.back(); |
|
|
frame.origin = {0, 0}; |
|
|
NSPoint top_right = previous_window.frame.origin; |
|
|
GLView * view = [[GLView alloc] initWithFrame:frame]; |
|
|
top_right.x += previous_window.frame.size.width; |
|
|
|
|
|
top_right.y += previous_window.frame.size.height; |
|
|
view.functionality = f; |
|
|
[window setFrameTopLeftPoint: top_right]; |
|
|
|
|
|
} |
|
|
window.contentView = view; |
|
|
[window makeKeyAndOrderFront:nil]; |
|
|
|
|
|
|
|
|
windows.push_back(window); |
|
|
NSRect frame = [window contentRectForFrameRect:window.frame]; |
|
|
} |
|
|
frame.origin = {0, 0}; |
|
|
|
|
|
GLView * view = [[GLView alloc] initWithFrame:frame]; |
|
|
void run() { |
|
|
|
|
|
[app run]; |
|
|
view.functionality = f; |
|
|
} |
|
|
|
|
|
|
|
|
window.contentView = view; |
|
|
NSApplication * app; |
|
|
windows.push_back(window); |
|
|
std::vector<NSWindow *> windows; |
|
|
} |
|
|
NSString * appName; |
|
|
|
|
|
|
|
|
void create_window(ControlFunctionality const & f) { |
|
|
|
|
|
const CGFloat width = 200; |
|
|
|
|
|
NSWindow * window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, 500) |
|
|
|
|
|
styleMask:NSTitledWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask |
|
|
|
|
|
backing:NSBackingStoreBuffered |
|
|
|
|
|
defer:YES]; |
|
|
|
|
|
window.title = appName; |
|
|
|
|
|
if(windows.empty()){ |
|
|
|
|
|
[window center]; |
|
|
|
|
|
} else { |
|
|
|
|
|
NSWindow * previous_window = windows.back(); |
|
|
|
|
|
NSPoint top_right = previous_window.frame.origin; |
|
|
|
|
|
top_right.x += previous_window.frame.size.width; |
|
|
|
|
|
top_right.y += previous_window.frame.size.height; |
|
|
|
|
|
[window setFrameTopLeftPoint: top_right]; |
|
|
|
|
|
} |
|
|
|
|
|
[window makeKeyAndOrderFront:nil]; |
|
|
|
|
|
|
|
|
|
|
|
NSRect frame = [window contentRectForFrameRect:window.frame]; |
|
|
|
|
|
frame.origin = {0, 0}; |
|
|
|
|
|
NSView * view = [[NSView alloc] initWithFrame:frame]; |
|
|
|
|
|
|
|
|
|
|
|
CGFloat y = 0; |
|
|
|
|
|
CGFloat h = 20; |
|
|
|
|
|
CGFloat padding = 8; |
|
|
|
|
|
for(auto&& c : f.sliders){ |
|
|
|
|
|
NSTextField * label = [[NSTextField alloc] initWithFrame:{padding, y+padding, 20, h}]; |
|
|
|
|
|
label.editable = NO; |
|
|
|
|
|
label.bezeled = NO; |
|
|
|
|
|
label.drawsBackground = NO; |
|
|
|
|
|
label.stringValue = [NSString stringWithUTF8String: c.name.c_str()]; |
|
|
|
|
|
[view addSubview: label]; |
|
|
|
|
|
|
|
|
|
|
|
MySlider * slider = [[MySlider alloc] initWithFrame:{20+padding, y+padding, width-20-2*padding, h}]; |
|
|
|
|
|
slider.minValue = c.min; |
|
|
|
|
|
slider.maxValue = c.max; |
|
|
|
|
|
slider.doubleValue = c.start; |
|
|
|
|
|
slider.callback = c.callback; |
|
|
|
|
|
[view addSubview: slider]; |
|
|
|
|
|
y += h; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
window.contentView = view; |
|
|
|
|
|
windows.push_back(window); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void run() { |
|
|
|
|
|
[app run]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
NSApplication * app; |
|
|
|
|
|
std::vector<NSWindow *> windows; |
|
|
|
|
|
NSString * appName; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
NSAppWrapper::NSAppWrapper() |
|
|
NSAppWrapper::NSAppWrapper() |
|
@ -88,7 +161,11 @@ void NSAppWrapper::run() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void NSAppWrapper::create_window(GLViewFunctionality const & f){ |
|
|
void NSAppWrapper::create_window(GLViewFunctionality const & f){ |
|
|
impl->create_window(f); |
|
|
impl->create_window(f); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void NSAppWrapper::create_window(ControlFunctionality const & f){ |
|
|
|
|
|
impl->create_window(f); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -109,39 +186,39 @@ void NSAppWrapper::create_window(GLViewFunctionality const & f){ |
|
|
|
|
|
|
|
|
- (id) initWithFrame:(NSRect)frameRect { |
|
|
- (id) initWithFrame:(NSRect)frameRect { |
|
|
if(self = [super initWithFrame:frameRect]){ |
|
|
if(self = [super initWithFrame:frameRect]){ |
|
|
context = std::unique_ptr<GLContext>(new GLContext); |
|
|
context = std::unique_ptr<GLContext>(new GLContext); |
|
|
|
|
|
|
|
|
self.pixelFormat = [[NSOpenGLPixelFormat alloc] initWithCGLPixelFormatObj: context->pix]; |
|
|
self.pixelFormat = [[NSOpenGLPixelFormat alloc] initWithCGLPixelFormatObj: context->pix]; |
|
|
self.openGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj: context->ctx]; |
|
|
self.openGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj: context->ctx]; |
|
|
self.wantsBestResolutionOpenGLSurface = YES; // Opt-in for retina |
|
|
self.wantsBestResolutionOpenGLSurface = YES; // Opt-in for retina |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return self; |
|
|
return self; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
- (void) prepareOpenGL{ |
|
|
- (void) prepareOpenGL{ |
|
|
[super prepareOpenGL]; |
|
|
[super prepareOpenGL]; |
|
|
|
|
|
|
|
|
context->set_as_current_context(); |
|
|
context->set_as_current_context(); |
|
|
|
|
|
|
|
|
// Synchronize buffer swaps with vertical refresh rate |
|
|
// Synchronize buffer swaps with vertical refresh rate |
|
|
GLint swapInt = 1; |
|
|
GLint swapInt = 1; |
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
|
|
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
|
|
|
|
|
|
|
|
if(functionality.initialize_callback){ |
|
|
if(functionality.initialize_callback){ |
|
|
functionality.initialize_callback({*context}); |
|
|
functionality.initialize_callback({*context, nullptr, nullptr}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Create a display link capable of being used with all active displays |
|
|
// Create a display link capable of being used with all active displays |
|
|
linky = std::unique_ptr<CVDisplayLinky>(new CVDisplayLinky(*context)); |
|
|
linky = std::unique_ptr<CVDisplayLinky>(new CVDisplayLinky(*context)); |
|
|
linky->callback = [=]{ |
|
|
linky->callback = [=]{ |
|
|
// will be called in a different thread => need new autoreleasepool |
|
|
// will be called in a different thread => need new autoreleasepool |
|
|
@autoreleasepool { |
|
|
@autoreleasepool { |
|
|
[self drawView]; |
|
|
[self drawView]; |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
linky->start(); |
|
|
linky->start(); |
|
|
|
|
|
|
|
|
// Register to be notified when the window closes so we can stop the displaylink |
|
|
// Register to be notified when the window closes so we can stop the displaylink |
|
|
[[NSNotificationCenter defaultCenter] addObserver:self |
|
|
[[NSNotificationCenter defaultCenter] addObserver:self |
|
|
selector:@selector(windowWillClose:) |
|
|
selector:@selector(windowWillClose:) |
|
@ -152,37 +229,37 @@ void NSAppWrapper::create_window(GLViewFunctionality const & f){ |
|
|
- (void) windowWillClose:(NSNotification*)notification{ |
|
|
- (void) windowWillClose:(NSNotification*)notification{ |
|
|
// The default OpenGL render buffers will be destroyed. If display link |
|
|
// The default OpenGL render buffers will be destroyed. If display link |
|
|
// continues to fire without renderbuffers, OpenGL draw calls will set errors. |
|
|
// continues to fire without renderbuffers, OpenGL draw calls will set errors. |
|
|
|
|
|
|
|
|
linky->stop(); |
|
|
linky->stop(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
- (void) reshape{ |
|
|
- (void) reshape{ |
|
|
[super reshape]; |
|
|
[super reshape]; |
|
|
|
|
|
|
|
|
context->lock(); |
|
|
context->lock(); |
|
|
|
|
|
|
|
|
// [NSView convertRectToBacking] converts point sizes to pixel sizes. |
|
|
// [NSView convertRectToBacking] converts point sizes to pixel sizes. |
|
|
// viewRectPixels will be larger (2x) than viewRectPoints for retina displays. |
|
|
// viewRectPixels will be larger (2x) than viewRectPoints for retina displays. |
|
|
// viewRectPixels will be the same as viewRectPoints for non-retina displays |
|
|
// viewRectPixels will be the same as viewRectPoints for non-retina displays |
|
|
NSRect viewRectPixels = [self convertRectToBacking:self.bounds]; |
|
|
NSRect viewRectPixels = [self convertRectToBacking:self.bounds]; |
|
|
|
|
|
|
|
|
if(functionality.resize_callback){ |
|
|
if(functionality.resize_callback){ |
|
|
functionality.resize_callback({*context}, viewRectPixels.size.width, viewRectPixels.size.height); |
|
|
functionality.resize_callback({*context, nullptr, nullptr}, viewRectPixels.size.width, viewRectPixels.size.height); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
context->unlock(); |
|
|
context->unlock(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
- (void)renewGState{ |
|
|
- (void)renewGState{ |
|
|
// Called whenever graphics state updated (such as window resize) |
|
|
// Called whenever graphics state updated (such as window resize) |
|
|
|
|
|
|
|
|
// OpenGL rendering is not synchronous with other rendering on the OSX. |
|
|
// OpenGL rendering is not synchronous with other rendering on the OSX. |
|
|
// Therefore, call disableScreenUpdatesUntilFlush so the window server |
|
|
// Therefore, call disableScreenUpdatesUntilFlush so the window server |
|
|
// doesn't render non-OpenGL content in the window asynchronously from |
|
|
// doesn't render non-OpenGL content in the window asynchronously from |
|
|
// OpenGL content, which could cause flickering. (non-OpenGL content |
|
|
// OpenGL content, which could cause flickering. (non-OpenGL content |
|
|
// includes the title bar and drawing done by the app with other APIs) |
|
|
// includes the title bar and drawing done by the app with other APIs) |
|
|
[[self window] disableScreenUpdatesUntilFlush]; |
|
|
[[self window] disableScreenUpdatesUntilFlush]; |
|
|
|
|
|
|
|
|
[super renewGState]; |
|
|
[super renewGState]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -192,24 +269,27 @@ void NSAppWrapper::create_window(GLViewFunctionality const & f){ |
|
|
|
|
|
|
|
|
- (void) drawView{ |
|
|
- (void) drawView{ |
|
|
context->set_as_current_context(); |
|
|
context->set_as_current_context(); |
|
|
|
|
|
|
|
|
// We draw on a secondary thread through the display link |
|
|
// We draw on a secondary thread through the display link |
|
|
// When resizing the view, -reshape is called automatically on the main |
|
|
// When resizing the view, -reshape is called automatically on the main |
|
|
// thread. Add a mutex around to avoid the threads accessing the context |
|
|
// thread. Add a mutex around to avoid the threads accessing the context |
|
|
// simultaneously when resizing |
|
|
// simultaneously when resizing |
|
|
context->lock(); |
|
|
context->lock(); |
|
|
|
|
|
|
|
|
NSRect viewRectPixels = [self convertRectToBacking:self.bounds]; |
|
|
NSRect viewRectPixels = [self convertRectToBacking:self.bounds]; |
|
|
|
|
|
auto& ctx = context->ctx; |
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|
|
|
|
|
glViewport(0, 0, (int)viewRectPixels.size.width, (int)viewRectPixels.size.height); |
|
|
if(functionality.draw_callback){ |
|
|
|
|
|
functionality.draw_callback({*context, [viewRectPixels]{ |
|
|
if(functionality.draw_callback){ |
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|
|
functionality.draw_callback({*context}); |
|
|
glViewport(0, 0, (int)viewRectPixels.size.width, (int)viewRectPixels.size.height); |
|
|
} |
|
|
}, [&ctx]{ |
|
|
|
|
|
CGLFlushDrawable(ctx); |
|
|
CGLFlushDrawable(context->ctx); |
|
|
} |
|
|
context->unlock(); |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
context->unlock(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@end |
|
|
@end |
|
|