public CocoaContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, int majorVersion, int minorVersion) { Debug.Print("Context Type: {0}", shareContext); Debug.Print("Window info: {0}", window); cocoaWindow = (CocoaWindowInfo)window; if (shareContext is CocoaContext) shareContextRef = ((CocoaContext)shareContext).Handle.Handle; if (shareContext is GraphicsContext) { ContextHandle shareHandle = shareContext != null ? (shareContext as IGraphicsContextInternal).Context : (ContextHandle)IntPtr.Zero; shareContextRef = shareHandle.Handle; } if (shareContextRef == IntPtr.Zero) { Debug.Print("No context sharing will take place."); } CreateContext(mode, cocoaWindow, shareContextRef, majorVersion, minorVersion, true); }
private MouseCursor selectedCursor = MouseCursor.Default; // user-selected cursor public CocoaNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { // Create the window class Interlocked.Increment(ref UniqueId); windowClass = Class.AllocateClass("OpenTK_GameWindow" + UniqueId, "NSWindow"); Class.RegisterMethod(windowClass, new WindowKeyDownDelegate(WindowKeyDown), "keyDown:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidResizeDelegate(WindowDidResize), "windowDidResize:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidMoveDelegate(WindowDidMove), "windowDidMove:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidBecomeKeyDelegate(WindowDidBecomeKey), "windowDidBecomeKey:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidResignKeyDelegate(WindowDidResignKey), "windowDidResignKey:", "v@:@"); Class.RegisterMethod(windowClass, new WindowWillMiniaturizeDelegate(WindowWillMiniaturize), "windowWillMiniaturize:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidMiniaturizeDelegate(WindowDidMiniaturize), "windowDidMiniaturize:", "v@:@"); Class.RegisterMethod(windowClass, new WindowDidDeminiaturizeDelegate(WindowDidDeminiaturize), "windowDidDeminiaturize:", "v@:@"); Class.RegisterMethod(windowClass, new WindowShouldZoomToFrameDelegate(WindowShouldZoomToFrame), "windowShouldZoom:toFrame:", "b@:@{NSRect={NSPoint=ff}{NSSize=ff}}"); Class.RegisterMethod(windowClass, new WindowShouldCloseDelegate(WindowShouldClose), "windowShouldClose:", "b@:@"); Class.RegisterMethod(windowClass, new AcceptsFirstResponderDelegate(AcceptsFirstResponder), "acceptsFirstResponder", "b@:"); Class.RegisterMethod(windowClass, new CanBecomeKeyWindowDelegate(CanBecomeKeyWindow), "canBecomeKeyWindow", "b@:"); Class.RegisterMethod(windowClass, new CanBecomeMainWindowDelegate(CanBecomeMainWindow), "canBecomeMainWindow", "b@:"); Class.RegisterClass(windowClass); IntPtr viewClass = Class.AllocateClass("OpenTK_NSView" + UniqueId, "NSView"); Class.RegisterMethod(viewClass, new ResetCursorRectsDelegate(ResetCursorRects), "resetCursorRects", "v@:"); Class.RegisterClass(viewClass); // Create window instance // Note: The coordinate system of Cocoa places (0,0) at the bottom left. // We need to get the height of the main screen and flip that in order // to place the window at the correct position. // Note: NSWindows are laid out relative to the main screen. var screenRect = Cocoa.SendRect( Cocoa.SendIntPtr( Cocoa.SendIntPtr(Class.Get("NSScreen"), Selector.Get("screens")), Selector.Get("objectAtIndex:"), 0), Selector.Get("frame")); var contentRect = new System.Drawing.RectangleF(x, screenRect.Height - height - y, width, height); var style = GetStyleMask(windowBorder); var bufferingType = NSBackingStore.Buffered; IntPtr windowPtr; windowPtr = Cocoa.SendIntPtr(windowClass, Selector.Alloc); windowPtr = Cocoa.SendIntPtr(windowPtr, Selector.Get("initWithContentRect:styleMask:backing:defer:"), contentRect, (int)style, (int)bufferingType, false); // Replace view with our custom implementation // that overrides resetCursorRects (maybe there is // a better way to implement this override?) // Existing view: IntPtr viewPtr = Cocoa.SendIntPtr(windowPtr, Selector.Get("contentView")); // Our custom view with the same bounds: viewPtr = Cocoa.SendIntPtr( Cocoa.SendIntPtr(viewClass, Selector.Alloc), Selector.Get("initWithFrame:"), Cocoa.SendRect(viewPtr, selBounds)); if (viewPtr != IntPtr.Zero) { Cocoa.SendVoid(windowPtr, Selector.Get("setContentView:"), viewPtr); } windowInfo = new CocoaWindowInfo(windowPtr); // Set up behavior Cocoa.SendIntPtr(windowPtr, Selector.Get("setDelegate:"), windowPtr); // The window class acts as its own delegate Cocoa.SendVoid(windowPtr, Selector.Get("makeKeyWindow")); SetTitle(title, false); ResetTrackingArea(); exists = true; NSApplication.Quit += ApplicationQuit; }
public CocoaContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, int majorVersion, int minorVersion) { if (handle == ContextHandle.Zero) throw new ArgumentException("handle"); if (window == null) throw new ArgumentNullException("window"); Handle = handle; cocoaWindow = (CocoaWindowInfo)window; }
private void CreateContext(GraphicsMode mode, CocoaWindowInfo cocoaWindow, IntPtr shareContextRef, int majorVersion, int minorVersion, bool fullscreen) { // Prepare attributes IntPtr pixelFormat = SelectPixelFormat(mode, majorVersion, minorVersion); if (pixelFormat == IntPtr.Zero) { throw new GraphicsException(String.Format( "Failed to contruct NSOpenGLPixelFormat for GraphicsMode '{0}'", mode)); } // Create context var context = Cocoa.SendIntPtr(NSOpenGLContext, Selector.Alloc); context = Cocoa.SendIntPtr(context, Selector.Get("initWithFormat:shareContext:"), pixelFormat, shareContextRef); if (context == IntPtr.Zero) { throw new GraphicsException(String.Format( "Failed to construct NSOpenGLContext", mode)); } // Release pixel format Cocoa.SendVoid(pixelFormat, Selector.Release); pixelFormat = IntPtr.Zero; // Attach the view Cocoa.SendVoid(context, Selector.Get("setView:"), cocoaWindow.ViewHandle); Cocoa.SendVoid(cocoaWindow.ViewHandle, Selector.Get("setWantsBestResolutionOpenGLSurface:"), true); // Finalize Handle = new ContextHandle(context); Mode = GetGraphicsMode(context); Update(cocoaWindow); MakeCurrent(cocoaWindow); }
private MouseCursor selectedCursor = MouseCursor.Default; // user-selected cursor public CocoaNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { // Create callback methods. We need to store those, // otherwise the GC may collect them while they are // still active. WindowKeyDownHandler = WindowKeyDown; WindowDidResizeHandler = WindowDidResize; WindowDidMoveHandler = WindowDidMove; WindowDidBecomeKeyHandler = WindowDidBecomeKey; WindowDidResignKeyHandler = WindowDidResignKey; WindowWillMiniaturizeHandler = WindowWillMiniaturize; WindowDidMiniaturizeHandler = WindowDidMiniaturize; WindowDidDeminiaturizeHandler = WindowDidDeminiaturize; WindowShouldZoomToFrameHandler = WindowShouldZoomToFrame; WindowShouldCloseHandler = WindowShouldClose; AcceptsFirstResponderHandler = AcceptsFirstResponder; CanBecomeKeyWindowHandler = CanBecomeKeyWindow; CanBecomeMainWindowHandler = CanBecomeMainWindow; ResetCursorRectsHandler = ResetCursorRects; // Create the window class int unique_id = Interlocked.Increment(ref UniqueId); windowClass = Class.AllocateClass("OpenTK_GameWindow" + unique_id, "NSWindow"); Class.RegisterMethod(windowClass, WindowKeyDownHandler, "keyDown:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidResizeHandler, "windowDidResize:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidMoveHandler, "windowDidMove:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidBecomeKeyHandler, "windowDidBecomeKey:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidResignKeyHandler, "windowDidResignKey:", "v@:@"); Class.RegisterMethod(windowClass, WindowWillMiniaturizeHandler, "windowWillMiniaturize:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidMiniaturizeHandler, "windowDidMiniaturize:", "v@:@"); Class.RegisterMethod(windowClass, WindowDidDeminiaturizeHandler, "windowDidDeminiaturize:", "v@:@"); Class.RegisterMethod(windowClass, WindowShouldZoomToFrameHandler, "windowShouldZoom:toFrame:", "b@:@{NSRect={NSPoint=ff}{NSSize=ff}}"); Class.RegisterMethod(windowClass, WindowShouldCloseHandler, "windowShouldClose:", "b@:@"); Class.RegisterMethod(windowClass, AcceptsFirstResponderHandler, "acceptsFirstResponder", "b@:"); Class.RegisterMethod(windowClass, CanBecomeKeyWindowHandler, "canBecomeKeyWindow", "b@:"); Class.RegisterMethod(windowClass, CanBecomeMainWindowHandler, "canBecomeMainWindow", "b@:"); Class.RegisterClass(windowClass); IntPtr viewClass = Class.AllocateClass("OpenTK_NSView" + unique_id, "NSView"); Class.RegisterMethod(viewClass, ResetCursorRectsHandler, "resetCursorRects", "v@:"); Class.RegisterClass(viewClass); // Create window instance // Note: The coordinate system of Cocoa places (0,0) at the bottom left. // We need to get the height of the main screen and flip that in order // to place the window at the correct position. // Note: NSWindows are laid out relative to the main screen. var screenRect = Cocoa.SendRect( Cocoa.SendIntPtr( Cocoa.SendIntPtr(Class.Get("NSScreen"), Selector.Get("screens")), Selector.Get("objectAtIndex:"), 0), Selector.Get("frame")); var contentRect = new System.Drawing.RectangleF(x, screenRect.Height - height - y, width, height); var style = GetStyleMask(windowBorder); var bufferingType = NSBackingStore.Buffered; IntPtr classPtr; classPtr = Cocoa.SendIntPtr(windowClass, Selector.Alloc); if (classPtr == IntPtr.Zero) { Debug.Print("[Error] Failed to allocate window class."); throw new PlatformException(); } bool defer = false; IntPtr windowPtr = Cocoa.SendIntPtr(classPtr, Selector.Get("initWithContentRect:styleMask:backing:defer:"), contentRect, (int)style, (int)bufferingType, defer); if (windowPtr == IntPtr.Zero) { Debug.Print("[Error] Failed to initialize window with ({0}, {1}, {2}, {3}).", contentRect, style, bufferingType, defer); throw new PlatformException(); } // Replace view with our custom implementation // that overrides resetCursorRects (maybe there is // a better way to implement this override?) // Existing view: IntPtr viewPtr = Cocoa.SendIntPtr(windowPtr, Selector.Get("contentView")); if (viewPtr == IntPtr.Zero) { Debug.Print("[Error] Failed to retrieve content view for window {0}.", windowPtr); throw new PlatformException(); } // Our custom view with the same bounds: viewPtr = Cocoa.SendIntPtr( Cocoa.SendIntPtr(viewClass, Selector.Alloc), Selector.Get("initWithFrame:"), Cocoa.SendRect(viewPtr, selBounds)); if (viewPtr != IntPtr.Zero) { Cocoa.SendVoid(windowPtr, Selector.Get("setContentView:"), viewPtr); } else { Debug.Print("[Error] Failed to initialize content view with frame {0}.", selBounds); throw new PlatformException(); } windowInfo = new CocoaWindowInfo(windowPtr); // Set up behavior Cocoa.SendIntPtr(windowPtr, Selector.Get("setDelegate:"), windowPtr); // The window class acts as its own delegate Cocoa.SendVoid(windowPtr, Selector.Get("makeKeyWindow")); SetTitle(title, false); ResetTrackingArea(); exists = true; NSApplication.Quit += ApplicationQuit; }
private void CreateContext(GraphicsMode mode, CocoaWindowInfo cocoaWindow, IntPtr shareContextRef, int majorVersion, int minorVersion, bool fullscreen) { // Prepare attributes List<NSOpenGLPixelFormatAttribute> attributes = new List<NSOpenGLPixelFormatAttribute>(); var profile = NSOpenGLProfile.VersionLegacy; if (majorVersion > 3 || (majorVersion == 3 && minorVersion >= 2)) { profile = NSOpenGLProfile.Version3_2Core; Debug.Print("Running the OpenGL core profile."); } else { Debug.Print("Running the legacy OpenGL profile. Start with version major=3, minor=2 or later for the 3.2 profile."); } Debug.Print("NSGL pixel format attributes:"); Debug.Indent(); AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.OpenGLProfile, (int)profile); AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.DoubleBuffer); AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.Accelerated); AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.ColorSize, mode.ColorFormat.BitsPerPixel); if (mode.Depth > 0) AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.DepthSize, mode.Depth); if (mode.Stencil > 0) AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.StencilSize, mode.Stencil); if (mode.AccumulatorFormat.BitsPerPixel > 0) { AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.AccumSize, mode.AccumulatorFormat.BitsPerPixel); } if (mode.Samples > 1) { AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.SampleBuffers, 1); AddPixelAttrib(attributes, NSOpenGLPixelFormatAttribute.Samples, mode.Samples); } AddPixelAttrib(attributes, (NSOpenGLPixelFormatAttribute)0); Debug.Unindent(); Debug.Write("Attribute array: "); for (int i = 0; i < attributes.Count; i++) Debug.Write(attributes[i].ToString() + " "); Debug.WriteLine(""); // Create pixel format var pixelFormat = Cocoa.SendIntPtr(Class.Get("NSOpenGLPixelFormat"), Selector.Alloc); unsafe { fixed (NSOpenGLPixelFormatAttribute* ptr = attributes.ToArray()) { pixelFormat = Cocoa.SendIntPtr(pixelFormat, Selector.Get("initWithAttributes:"), (IntPtr)ptr); } } // Create context var context = Cocoa.SendIntPtr(NSOpenGLContext, Selector.Alloc); context = Cocoa.SendIntPtr(context, Selector.Get("initWithFormat:shareContext:"), pixelFormat, shareContextRef); // Release pixel format Cocoa.SendVoid(pixelFormat, Selector.Release); pixelFormat = IntPtr.Zero; // Attach the view Cocoa.SendVoid(context, Selector.Get("setView:"), cocoaWindow.ViewHandle); Cocoa.SendVoid(cocoaWindow.ViewHandle, Selector.Get("setWantsBestResolutionOpenGLSurface:"), true); // Finalize Handle = new ContextHandle(context); Mode = GetGraphicsMode(context); Update(cocoaWindow); MakeCurrent(cocoaWindow); }