void CreateContext(GraphicsMode mode, CarbonWindow wind, bool fullscreen) { int[] attribs = GetAttribs(mode, fullscreen); IntPtr pixelFormat; // Choose a pixel format with the attributes we specified. if (fullscreen) { IntPtr gdevice; IntPtr display = CG.CGMainDisplayID(); OSStatus status = API.DMGetGDeviceByDisplayID(display, out gdevice, false); if (status != OSStatus.NoError) { throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); } pixelFormat = Agl.aglChoosePixelFormat(ref gdevice, 1, attribs); int err = Agl.aglGetError(); if (err == Agl.AGL_BAD_PIXEL_FORMAT) { Debug.Print("Failed to create full screen pixel format."); Debug.Print("Trying again to create a non-fullscreen pixel format."); CreateContext(mode, wind, false); return; } } else { pixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attribs); Agl.CheckReturnValue(0, "aglChoosePixelFormat"); } Debug.Print("Creating AGL context."); // create the context and share it with the share reference. ContextHandle = Agl.aglCreateContext(pixelFormat, IntPtr.Zero); Agl.CheckReturnValue(0, "aglCreateContext"); // Free the pixel format from memory. Agl.aglDestroyPixelFormat(pixelFormat); Agl.CheckReturnValue(0, "aglDestroyPixelFormat"); SetDrawable(wind); Update(wind); MakeCurrent(wind); Debug.Print("context: {0}", ContextHandle); }
void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, bool fullscreen) { List <int> attribs = new List <int>(); attribs.Add((int)Agl.PixelFormatAttribute.AGL_RGBA); attribs.Add((int)Agl.PixelFormatAttribute.AGL_DOUBLEBUFFER); AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_RED_SIZE, mode.ColorFormat.Red); AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_GREEN_SIZE, mode.ColorFormat.Green); AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_BLUE_SIZE, mode.ColorFormat.Blue); AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_ALPHA_SIZE, mode.ColorFormat.Alpha); if (mode.Depth > 0) { AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_DEPTH_SIZE, mode.Depth); } if (mode.Stencil > 0) { AddPixelAttrib(attribs, Agl.PixelFormatAttribute.AGL_STENCIL_SIZE, mode.Stencil); } if (fullscreen) { attribs.Add((int)Agl.PixelFormatAttribute.AGL_FULLSCREEN); } attribs.Add((int)Agl.PixelFormatAttribute.AGL_NONE); Debug.Print("AGL Attribute array: "); for (int i = 0; i < attribs.Count; i++) { Debug.Print(attribs[i].ToString() + " "); } Debug.Print(""); AGLPixelFormat aglPixelFormat; // Choose a pixel format with the attributes we specified. if (fullscreen) { IntPtr gdevice; IntPtr cgdevice = GetQuartzDevice(carbonWindow); if (cgdevice == IntPtr.Zero) { cgdevice = QuartzDisplayDevice.MainDisplay; } OSStatus status = API.DMGetGDeviceByDisplayID(cgdevice, out gdevice, false); if (status != OSStatus.NoError) { throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); } aglPixelFormat = Agl.aglChoosePixelFormat(ref gdevice, 1, attribs.ToArray()); Agl.AglError err = Agl.aglGetError(); if (err == Agl.AglError.BadPixelFormat) { Debug.Print("Failed to create full screen pixel format."); Debug.Print("Trying again to create a non-fullscreen pixel format."); CreateContext(mode, carbonWindow, false); return; } } else { aglPixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attribs.ToArray()); Agl.CheckReturnValue(0, "aglChoosePixelFormat"); } Debug.Print("Creating AGL context."); // create the context and share it with the share reference. ContextHandle = Agl.aglCreateContext(aglPixelFormat, IntPtr.Zero); Agl.CheckReturnValue(0, "aglCreateContext"); // Free the pixel format from memory. Agl.aglDestroyPixelFormat(aglPixelFormat); Agl.CheckReturnValue(0, "aglDestroyPixelFormat"); SetDrawable(carbonWindow); Update(carbonWindow); MakeCurrent(carbonWindow); Debug.Print("context: {0}", ContextHandle); }