From 36117b371635a4d980f77c394d7138b8387399eb Mon Sep 17 00:00:00 2001 From: treeform Date: Fri, 15 Oct 2021 17:03:46 -0700 Subject: [PATCH 1/3] Add macos. --- examples/basic.nim | 5 + src/windy.nim | 39 ++- src/windy/platforms/macos/macos.m | 406 +++++++++++++++++++++++++ src/windy/platforms/macos/platform.nim | 63 ++++ tests/test.nim | 1 + 5 files changed, 503 insertions(+), 11 deletions(-) create mode 100644 src/windy/platforms/macos/macos.m create mode 100644 src/windy/platforms/macos/platform.nim diff --git a/examples/basic.nim b/examples/basic.nim index 9d31cf7..cb7fcd0 100644 --- a/examples/basic.nim +++ b/examples/basic.nim @@ -19,11 +19,16 @@ echo "GL_SHADING_LANGUAGE_VERSION: ", cast[cstring](glGetString(GL_SHADING_LANGU let bxy = newBoxy() proc display() = + + window.lock() + bxy.beginFrame(windowSize) bxy.drawRect(rect(vec2(0, 0), windowSize.vec2), color(1, 1, 1, 1)) bxy.drawRect(rect(vec2(100, 100), vec2(200, 200)), color(1, 0, 1, 1)) bxy.endFrame() + window.swapBuffers() + window.unlock() while true: pollEvents() diff --git a/src/windy.nim b/src/windy.nim index c61a21c..953f41e 100644 --- a/src/windy.nim +++ b/src/windy.nim @@ -4,6 +4,8 @@ export common when defined(windows): import windy/platforms/win32/platform +elif defined(macosx): + import windy/platforms/macos/platform elif defined(linux): import windy/platforms/x11/platform @@ -27,17 +29,20 @@ proc newWindow*( ): Window {.raises: [WindyError]} = # resizeable, fullscreen, transparent, decorated, floating result = Window() - result.platform = newPlatformWindow( - title, - w, - h, - vsync, - openglMajorVersion, - openglMinorVersion, - msaa, - depthBits, - stencilBits - ) + try: + result.platform = newPlatformWindow( + title, + w, + h, + vsync, + openglMajorVersion, + openglMinorVersion, + msaa, + depthBits, + stencilBits + ) + except: + raise newException(WindyError, "Creating native window failed: " & getCurrentExceptionMsg()) proc makeContextCurrent*(window: Window) {.raises: [WindyError]} = window.platform.makeContextCurrent() @@ -45,6 +50,12 @@ proc makeContextCurrent*(window: Window) {.raises: [WindyError]} = proc swapBuffers*(window: Window) {.raises: [WindyError]} = window.platform.swapBuffers() +proc show*(window: PlatformWindow) = + discard + +proc hide*(window: PlatformWindow) = + discard + proc pollEvents*() = platformPollEvents() @@ -56,3 +67,9 @@ proc `visible=`*(window: Window, visible: bool) = window.platform.show() else: window.platform.hide() + +proc lock*(window: Window) = + window.platform.lock() + +proc unlock*(window: Window) = + window.platform.unlock() diff --git a/src/windy/platforms/macos/macos.m b/src/windy/platforms/macos/macos.m new file mode 100644 index 0000000..ae9a589 --- /dev/null +++ b/src/windy/platforms/macos/macos.m @@ -0,0 +1,406 @@ +// gcc Cocoa.m -o OSXWindow -framework Cocoa -framework Quartz -framework OpenGL + +#import +#import +#import +#include + + +@class View; +static CVReturn GlobalDisplayLinkCallback(CVDisplayLinkRef, const CVTimeStamp*, const CVTimeStamp*, CVOptionFlags, CVOptionFlags*, void*); + +@interface View : NSOpenGLView { +@public + CVDisplayLinkRef displayLink; + bool running; + NSRect windowRect; + NSRecursiveLock* appLock; +} +@end + +@implementation View +- (id) initWithFrame: (NSRect) frame { + + printf("initWithFrame\n"); + + running = true; + + // No multisampling + int samples = 0; + + // Keep multisampling attributes at the start of the attribute lists + // since code below assumes they are array elements 0 through 4. + NSOpenGLPixelFormatAttribute windowedAttrs[] = + { + NSOpenGLPFAMultisample, + NSOpenGLPFASampleBuffers, samples ? 1 : 0, + NSOpenGLPFASamples, samples, + NSOpenGLPFAAccelerated, + NSOpenGLPFADoubleBuffer, + NSOpenGLPFAColorSize, 32, + NSOpenGLPFADepthSize, 24, + NSOpenGLPFAAlphaSize, 8, + NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core, + 0 + }; + + // Try to choose a supported pixel format + NSOpenGLPixelFormat* pf = [ + [NSOpenGLPixelFormat alloc] + initWithAttributes:windowedAttrs + ]; + + if (!pf) { + bool valid = false; + while (!pf && samples > 0) { + samples /= 2; + windowedAttrs[2] = samples ? 1 : 0; + windowedAttrs[4] = samples; + pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:windowedAttrs]; + if (pf) { + valid = true; + break; + } + } + + if (!valid) { + NSLog(@"OpenGL pixel format not supported."); + return nil; + } + } + + self = [super initWithFrame:frame pixelFormat:[pf autorelease]]; + appLock = [[NSRecursiveLock alloc] init]; + + return self; +} + +- (void) prepareOpenGL { + + printf("prepareOpenGL\n"); + + [super prepareOpenGL]; + + [[self window] setLevel: NSNormalWindowLevel]; + [[self window] makeKeyAndOrderFront: self]; + + // Make all the OpenGL calls to setup rendering and build the necessary rendering objects + [[self openGLContext] makeCurrentContext]; + // Synchronize buffer swaps with vertical refresh rate + GLint swapInt = 1; // Vsynch on! + [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; + + // Create a display link capable of being used with all active displays + CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); + + // Set the renderer output callback function + CVDisplayLinkSetOutputCallback(displayLink, &GlobalDisplayLinkCallback, self); + + CGLContextObj cglContext = (CGLContextObj)[[self openGLContext] CGLContextObj]; + CGLPixelFormatObj cglPixelFormat = (CGLPixelFormatObj)[[self pixelFormat] CGLPixelFormatObj]; + CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); + + GLint dim[2] = {windowRect.size.width, windowRect.size.height}; + CGLSetParameter(cglContext, kCGLCPSurfaceBackingSize, dim); + CGLEnable(cglContext, kCGLCESurfaceBackingSize); + + // Activate the display link + CVDisplayLinkStart(displayLink); +} + +// Tell the window to accept input events +- (BOOL)acceptsFirstResponder { + return YES; +} + +- (void)mouseMoved:(NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + // NSLog(@"mouseMoved: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void) mouseDragged: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + // NSLog(@"mouseDragged: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void)scrollWheel: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Mouse wheel at: %lf, %lf. Delta: %lf", point.x, point.y, [event deltaY]); + [appLock unlock]; +} + +- (void) mouseDown: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Left mouse down: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void) mouseUp: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Left mouse up: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void) rightMouseDown: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Right mouse down: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void) rightMouseUp: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Right mouse up: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void)otherMouseDown: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Middle mouse down: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void)otherMouseUp: (NSEvent*) event { + [appLock lock]; + NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; + NSLog(@"Middle mouse up: %lf, %lf", point.x, point.y); + [appLock unlock]; +} + +- (void) mouseEntered: (NSEvent*)event { + [appLock lock]; + NSLog(@"Mouse entered"); + [appLock unlock]; +} + +- (void) mouseExited: (NSEvent*)event { + [appLock lock]; + NSLog(@"Mouse left"); + [appLock unlock]; +} + +- (void) keyDown: (NSEvent*) event { + [appLock lock]; + if ([event isARepeat] == NO) { + NSLog(@"Key down: %d", [event keyCode]); + } + [appLock unlock]; +} + +- (void) keyUp: (NSEvent*) event { + [appLock lock]; + NSLog(@"Key up: %d", [event keyCode]); + [appLock unlock]; +} + +// Update +- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime { + [appLock lock]; + + [[self openGLContext] makeCurrentContext]; + CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); + + // NSLog(@"Update"); + + CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]); + CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); + + [appLock unlock]; + + return kCVReturnSuccess; +} + +// Resize +- (void)windowDidResize:(NSNotification*)notification { + NSSize size = [ [ self.window contentView ] frame ].size; + [appLock lock]; + [[self openGLContext] makeCurrentContext]; + CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); + NSLog(@"Window resize: %lf, %lf", size.width, size.height); + // Temp + windowRect.size.width = size.width; + windowRect.size.height = size.height; + glViewport(0, 0, windowRect.size.width, windowRect.size.height); + // End temp + CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); + [appLock unlock]; +} + +- (void)resumeDisplayRenderer { + [appLock lock]; + CVDisplayLinkStop(displayLink); + [appLock unlock]; +} + +- (void)haltDisplayRenderer { + [appLock lock]; + CVDisplayLinkStop(displayLink); + [appLock unlock]; +} + +// Terminate window when the red X is pressed +-(void)windowWillClose:(NSNotification *)notification { + if (running) { + running = false; + + [appLock lock]; + NSLog(@"Cleanup"); + + CVDisplayLinkStop(displayLink); + CVDisplayLinkRelease(displayLink); + + [appLock unlock]; + } + + [NSApp terminate:self]; +} + +// Cleanup +- (void) dealloc { + [appLock release]; + [super dealloc]; +} +@end + +static CVReturn GlobalDisplayLinkCallback( + CVDisplayLinkRef displayLink, + const CVTimeStamp* now, + const CVTimeStamp* outputTime, + CVOptionFlags flagsIn, + CVOptionFlags* flagsOut, + void* displayLinkContext +) { + CVReturn result = [(View*)displayLinkContext getFrameForTime:outputTime]; + return result; +} + +void innerInit() { + [NSApplication sharedApplication]; +} + +void innerPollEvents() { + @autoreleasepool { + while(true) + { + NSEvent* event = [NSApp + nextEventMatchingMask:NSEventMaskAny + untilDate:[NSDate distantPast] + inMode:NSDefaultRunLoopMode + dequeue:YES + ]; + if (event == nil) + break; + [NSApp sendEvent:event]; + } + } +} + +void innerMakeContextCurrent(View* view) { + [[view openGLContext] makeCurrentContext]; +} + +void innerSwapBuffers(View* view) { + CGLFlushDrawable((CGLContextObj)[[view openGLContext] CGLContextObj]); +} + +void innerLock(View* view) { + [view->appLock lock]; +} + +void innerUnlock(View* view) { + [view->appLock unlock]; +} + +void innerNewPlatformWindow( + char* utf8Title, + int w, + int h, + NSWindow **windowRet, + View **viewRet +) { + // Create a window: + + // Style flags + NSUInteger windowStyle = + NSTitledWindowMask | + NSClosableWindowMask | + NSResizableWindowMask | + NSMiniaturizableWindowMask; + + // Window bounds (x, y, width, height) + NSRect screenRect = [[NSScreen mainScreen] frame]; + NSRect viewRect = NSMakeRect(0, 0, w, h); + NSRect windowRect = NSMakeRect( + NSMidX(screenRect) - NSMidX(viewRect), + NSMidY(screenRect) - NSMidY(viewRect), + viewRect.size.width, + viewRect.size.height + ); + + NSWindow* window = [[NSWindow alloc] + initWithContentRect:windowRect + styleMask:windowStyle + backing:NSBackingStoreBuffered + defer:NO + ]; + [window autorelease]; + + // Window controller + NSWindowController * windowController = + [[NSWindowController alloc] initWithWindow:window]; + [windowController autorelease]; + + // Since Snow Leopard, programs without application bundles and Info.plist files don't get a menubar + // and can't be brought to the front unless the presentation option is changed + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; + + // Next, we need to create the menu bar. You don't need to give the first item in the menubar a name + // (it will get the application's name automatically) + id menubar = [[NSMenu new] autorelease]; + id appMenuItem = [[NSMenuItem new] autorelease]; + [menubar addItem:appMenuItem]; + [NSApp setMainMenu:menubar]; + + // Then we add the quit item to the menu. Fortunately the action is simple since terminate: is + // already implemented in NSApplication and the NSApplication is always in the responder chain. + id appMenu = [[NSMenu new] autorelease]; + id appName = [[NSProcessInfo processInfo] processName]; + id quitTitle = [@"Quit " stringByAppendingString:appName]; + id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle + action:@selector(terminate:) keyEquivalent:@"q"] autorelease]; + [appMenu addItem:quitMenuItem]; + [appMenuItem setSubmenu:appMenu]; + + // Create app delegate to handle system events + View* view = [[[View alloc] initWithFrame:windowRect] autorelease]; + view->windowRect = windowRect; + [window setAcceptsMouseMovedEvents:YES]; + [window setContentView:view]; + [window setDelegate:view]; + + // Add fullscreen button + [window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary]; + + // Show window and run event loop + [NSApp activateIgnoringOtherApps:YES]; + [window makeKeyAndOrderFront: nil]; + + NSString *title = [NSString stringWithUTF8String:utf8Title]; + [window setTitle:title]; + + // Window need to process some events to initilize openGL + innerPollEvents(); + + windowRet[0] = window; + viewRet[0] = view; + +} diff --git a/src/windy/platforms/macos/platform.nim b/src/windy/platforms/macos/platform.nim new file mode 100644 index 0000000..e84ed38 --- /dev/null +++ b/src/windy/platforms/macos/platform.nim @@ -0,0 +1,63 @@ +{. + passL: "-framework Cocoa -framework OpenGL -framework Metal -framework QuartzCore", + compile: "macos.m", +.} + +import ../../common + +type + PlatformWindow* = ref object + windowPtr: pointer + viewPtr: pointer + +proc innerInit() {.importc.} +proc innerNewPlatformWindow( + titie: cstring, + w:cint, + h:cint, + windowPtr: ptr[pointer], + viewPtr: ptr[pointer] +) {.importc.} +proc innerPollEvents() {.importc.} +proc innerMakeContextCurrent(viewPtr: pointer) {.importc.} +proc innerSwapBuffers(viewPtr: pointer) {.importc.} +proc innerLock(viewPtr: pointer) {.importc.} +proc innerUnlock(viewPtr: pointer) {.importc.} + +proc platformInit*() = + innerInit() + +proc newPlatformWindow*( + title: string, + w: int, + h: int, + vsync: bool, + openglMajorVersion: int, + openglMinorVersion: int, + msaa: MSAA, + depthBits: int, + stencilBits: int +): PlatformWindow = + result = PlatformWindow() + innerNewPlatformWindow( + title, + w.cint, + h.cint, + result.windowPtr.addr, + result.viewPtr.addr + ) + +proc makeContextCurrent*(window: PlatformWindow) = + innerMakeContextCurrent(window.viewPtr) + +proc swapBuffers*(window: PlatformWindow) = + innerSwapBuffers(window.viewPtr) + +proc platformPollEvents*() = + innerPollEvents() + +proc lock*(window: PlatformWindow) = + innerLock(window.viewPtr) + +proc unlock*(window: PlatformWindow) = + innerUnlock(window.viewPtr) diff --git a/tests/test.nim b/tests/test.nim index e69de29..7a5fffe 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -0,0 +1 @@ +import windy From 4de1ebf928f37f6a15155ed32cdb7ee32da612f7 Mon Sep 17 00:00:00 2001 From: treeform Date: Fri, 15 Oct 2021 18:47:46 -0700 Subject: [PATCH 2/3] Remove locks. --- examples/basic.nim | 4 -- src/windy.nim | 6 -- src/windy/platforms/macos/macos.m | 85 -------------------------- src/windy/platforms/macos/platform.nim | 8 --- 4 files changed, 103 deletions(-) diff --git a/examples/basic.nim b/examples/basic.nim index cb7fcd0..d079329 100644 --- a/examples/basic.nim +++ b/examples/basic.nim @@ -19,16 +19,12 @@ echo "GL_SHADING_LANGUAGE_VERSION: ", cast[cstring](glGetString(GL_SHADING_LANGU let bxy = newBoxy() proc display() = - - window.lock() - bxy.beginFrame(windowSize) bxy.drawRect(rect(vec2(0, 0), windowSize.vec2), color(1, 1, 1, 1)) bxy.drawRect(rect(vec2(100, 100), vec2(200, 200)), color(1, 0, 1, 1)) bxy.endFrame() window.swapBuffers() - window.unlock() while true: pollEvents() diff --git a/src/windy.nim b/src/windy.nim index 953f41e..ed2f553 100644 --- a/src/windy.nim +++ b/src/windy.nim @@ -67,9 +67,3 @@ proc `visible=`*(window: Window, visible: bool) = window.platform.show() else: window.platform.hide() - -proc lock*(window: Window) = - window.platform.lock() - -proc unlock*(window: Window) = - window.platform.unlock() diff --git a/src/windy/platforms/macos/macos.m b/src/windy/platforms/macos/macos.m index ae9a589..c02cf47 100644 --- a/src/windy/platforms/macos/macos.m +++ b/src/windy/platforms/macos/macos.m @@ -7,22 +7,18 @@ @class View; -static CVReturn GlobalDisplayLinkCallback(CVDisplayLinkRef, const CVTimeStamp*, const CVTimeStamp*, CVOptionFlags, CVOptionFlags*, void*); @interface View : NSOpenGLView { @public CVDisplayLinkRef displayLink; bool running; NSRect windowRect; - NSRecursiveLock* appLock; } @end @implementation View - (id) initWithFrame: (NSRect) frame { - printf("initWithFrame\n"); - running = true; // No multisampling @@ -70,20 +66,13 @@ - (id) initWithFrame: (NSRect) frame { } self = [super initWithFrame:frame pixelFormat:[pf autorelease]]; - appLock = [[NSRecursiveLock alloc] init]; return self; } - (void) prepareOpenGL { - - printf("prepareOpenGL\n"); - [super prepareOpenGL]; - [[self window] setLevel: NSNormalWindowLevel]; - [[self window] makeKeyAndOrderFront: self]; - // Make all the OpenGL calls to setup rendering and build the necessary rendering objects [[self openGLContext] makeCurrentContext]; // Synchronize buffer swaps with vertical refresh rate @@ -93,9 +82,6 @@ - (void) prepareOpenGL { // Create a display link capable of being used with all active displays CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); - // Set the renderer output callback function - CVDisplayLinkSetOutputCallback(displayLink, &GlobalDisplayLinkCallback, self); - CGLContextObj cglContext = (CGLContextObj)[[self openGLContext] CGLContextObj]; CGLPixelFormatObj cglPixelFormat = (CGLPixelFormatObj)[[self pixelFormat] CGLPixelFormatObj]; CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat); @@ -114,115 +100,72 @@ - (BOOL)acceptsFirstResponder { } - (void)mouseMoved:(NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; // NSLog(@"mouseMoved: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void) mouseDragged: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; // NSLog(@"mouseDragged: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void)scrollWheel: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Mouse wheel at: %lf, %lf. Delta: %lf", point.x, point.y, [event deltaY]); - [appLock unlock]; } - (void) mouseDown: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Left mouse down: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void) mouseUp: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Left mouse up: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void) rightMouseDown: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Right mouse down: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void) rightMouseUp: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Right mouse up: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void)otherMouseDown: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Middle mouse down: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void)otherMouseUp: (NSEvent*) event { - [appLock lock]; NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil]; NSLog(@"Middle mouse up: %lf, %lf", point.x, point.y); - [appLock unlock]; } - (void) mouseEntered: (NSEvent*)event { - [appLock lock]; NSLog(@"Mouse entered"); - [appLock unlock]; } - (void) mouseExited: (NSEvent*)event { - [appLock lock]; NSLog(@"Mouse left"); - [appLock unlock]; } - (void) keyDown: (NSEvent*) event { - [appLock lock]; if ([event isARepeat] == NO) { NSLog(@"Key down: %d", [event keyCode]); } - [appLock unlock]; } - (void) keyUp: (NSEvent*) event { - [appLock lock]; NSLog(@"Key up: %d", [event keyCode]); - [appLock unlock]; } -// Update -- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime { - [appLock lock]; - - [[self openGLContext] makeCurrentContext]; - CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); - - // NSLog(@"Update"); - - CGLFlushDrawable((CGLContextObj)[[self openGLContext] CGLContextObj]); - CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); - - [appLock unlock]; - - return kCVReturnSuccess; -} // Resize - (void)windowDidResize:(NSNotification*)notification { NSSize size = [ [ self.window contentView ] frame ].size; - [appLock lock]; [[self openGLContext] makeCurrentContext]; CGLLockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); NSLog(@"Window resize: %lf, %lf", size.width, size.height); @@ -232,19 +175,14 @@ - (void)windowDidResize:(NSNotification*)notification { glViewport(0, 0, windowRect.size.width, windowRect.size.height); // End temp CGLUnlockContext((CGLContextObj)[[self openGLContext] CGLContextObj]); - [appLock unlock]; } - (void)resumeDisplayRenderer { - [appLock lock]; CVDisplayLinkStop(displayLink); - [appLock unlock]; } - (void)haltDisplayRenderer { - [appLock lock]; CVDisplayLinkStop(displayLink); - [appLock unlock]; } // Terminate window when the red X is pressed @@ -252,13 +190,11 @@ -(void)windowWillClose:(NSNotification *)notification { if (running) { running = false; - [appLock lock]; NSLog(@"Cleanup"); CVDisplayLinkStop(displayLink); CVDisplayLinkRelease(displayLink); - [appLock unlock]; } [NSApp terminate:self]; @@ -266,23 +202,10 @@ -(void)windowWillClose:(NSNotification *)notification { // Cleanup - (void) dealloc { - [appLock release]; [super dealloc]; } @end -static CVReturn GlobalDisplayLinkCallback( - CVDisplayLinkRef displayLink, - const CVTimeStamp* now, - const CVTimeStamp* outputTime, - CVOptionFlags flagsIn, - CVOptionFlags* flagsOut, - void* displayLinkContext -) { - CVReturn result = [(View*)displayLinkContext getFrameForTime:outputTime]; - return result; -} - void innerInit() { [NSApplication sharedApplication]; } @@ -312,13 +235,6 @@ void innerSwapBuffers(View* view) { CGLFlushDrawable((CGLContextObj)[[view openGLContext] CGLContextObj]); } -void innerLock(View* view) { - [view->appLock lock]; -} - -void innerUnlock(View* view) { - [view->appLock unlock]; -} void innerNewPlatformWindow( char* utf8Title, @@ -402,5 +318,4 @@ void innerNewPlatformWindow( windowRet[0] = window; viewRet[0] = view; - } diff --git a/src/windy/platforms/macos/platform.nim b/src/windy/platforms/macos/platform.nim index e84ed38..065ce1d 100644 --- a/src/windy/platforms/macos/platform.nim +++ b/src/windy/platforms/macos/platform.nim @@ -21,8 +21,6 @@ proc innerNewPlatformWindow( proc innerPollEvents() {.importc.} proc innerMakeContextCurrent(viewPtr: pointer) {.importc.} proc innerSwapBuffers(viewPtr: pointer) {.importc.} -proc innerLock(viewPtr: pointer) {.importc.} -proc innerUnlock(viewPtr: pointer) {.importc.} proc platformInit*() = innerInit() @@ -55,9 +53,3 @@ proc swapBuffers*(window: PlatformWindow) = proc platformPollEvents*() = innerPollEvents() - -proc lock*(window: PlatformWindow) = - innerLock(window.viewPtr) - -proc unlock*(window: PlatformWindow) = - innerUnlock(window.viewPtr) From c960816403b63e8586da5e95d35b6bcc7ae6fba7 Mon Sep 17 00:00:00 2001 From: treeform Date: Fri, 15 Oct 2021 18:49:37 -0700 Subject: [PATCH 3/3] f --- examples/basic.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/basic.nim b/examples/basic.nim index d079329..9d31cf7 100644 --- a/examples/basic.nim +++ b/examples/basic.nim @@ -23,7 +23,6 @@ proc display() = bxy.drawRect(rect(vec2(0, 0), windowSize.vec2), color(1, 1, 1, 1)) bxy.drawRect(rect(vec2(100, 100), vec2(200, 200)), color(1, 0, 1, 1)) bxy.endFrame() - window.swapBuffers() while true: