public void Dispose() { lock (this.object_0) { if (!(this.intptr_2 != IntPtr.Zero)) { return; } if (WGL.wglDeleteContext(this.intptr_2) == 0) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception(lastWin32Error, string.Format("OpenGL DeleteContext call was not successful, error code: {0}", (object)lastWin32Error)); } this.intptr_2 = IntPtr.Zero; if (this.bool_0) { if (WGL.DeleteDC(this.intptr_1) == 0) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception(lastWin32Error, string.Format("OpenGL DeleteDC call was not successful, error code: {0}", (object)lastWin32Error)); } } else if (WGL.ReleaseDC(this.intptr_0, this.intptr_1) == 0) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception(lastWin32Error, string.Format("OpenGL ReleaseDC call was not successful, error code: {0}", (object)lastWin32Error)); } this.intptr_0 = IntPtr.Zero; this.intptr_1 = IntPtr.Zero; this.int_0 = 0; } }
private WinGlContext(IntPtr winHandle) { _hWnd = winHandle; RegisterClassOnce(); var width = 512; var height = 512; if (winHandle == IntPtr.Zero) { var style = WindowStyle.OverlappedWindow | WindowStyle.ClipChildren | WindowStyle.ClipSiblings; var exStyle = ParentStyleEx; var rect = new Rect { Left = 0, Top = 0, Right = width, Bottom = height }; AdjustWindowRectEx(ref rect, style, false, exStyle); var windowName = Marshal.StringToHGlobalAuto("Title"); _hWnd = CreateWindowEx( exStyle, ClassName, windowName, style, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, IntPtr.Zero, IntPtr.Zero, Instance, IntPtr.Zero ); if (_hWnd == IntPtr.Zero) { throw new Exception($"Failed to create window. Error: {Marshal.GetLastWin32Error()}"); } } _dc = GetDC(_hWnd); var pfd = new PixelFormatDescriptor(); pfd.Size = (short)sizeof(PixelFormatDescriptor); pfd.Version = 1; pfd.Flags = PixelFormatDescriptorFlags.DrawToWindow | PixelFormatDescriptorFlags.SupportOpengl | PixelFormatDescriptorFlags.Doublebuffer; pfd.LayerType = 0; pfd.PixelType = PixelType.RGBA; // PFD_TYPE_RGBA //pfd.ColorBits = 32; pfd.ColorBits = 24; pfd.DepthBits = 16; pfd.StencilBits = 8; var pf = ChoosePixelFormat(_dc, &pfd); //pf = 10; if (!SetPixelFormat(_dc, pf, &pfd)) { Console.WriteLine("Error SetPixelFormat failed."); } _context = Wgl.wglCreateContext(_dc); if (_sharedContext != IntPtr.Zero) { RetryShareLists: //Console.WriteLine("SharedContext!"); Console.ReadKey(); if (!Wgl.wglShareLists(_sharedContext, _context)) { var lastError = Platform.InternalWindows.GetLastError(); Console.WriteLine($"Can't share lists {lastError}"); if (lastError == 170) // BUSY { Console.WriteLine($"-- Due to Busy. RETRY."); Thread.Sleep(10); goto RetryShareLists; } Debugger.Break(); throw new InvalidOperationException($"Can't share lists {lastError}"); } } MakeCurrent(); Console.Out.WriteLineColored(ConsoleColor.Yellow, "Version:{0}.{1}", GL.glGetInteger(GL.GL_MAJOR_VERSION), GL.glGetInteger(GL.GL_MINOR_VERSION)); DynamicLibraryFactory.MapLibraryToType <Extension>(new DynamicLibraryGl()); GL.LoadAllOnce(); #if false if (Extension.wglCreateContextAttribsARB != null) { ReleaseCurrent(); WGL.wglDeleteContext(this.Context); fixed(int *AttribListPtr = new int[] { (int)ArbCreateContext.MajorVersion, 3, (int)ArbCreateContext.MinorVersion, 1, 0, 0 }) { this.Context = Extension.wglCreateContextAttribsARB(DC, SharedContext, AttribListPtr); } if (this.Context == IntPtr.Zero) { throw(new Exception("Error creating context")); } MakeCurrent(); Console.WriteLine("OpenGL Version: {0}", Marshal.PtrToStringAnsi(new IntPtr(GL.glGetString(GL.GL_VERSION)))); //Console.ReadKey(); } #endif if (_sharedContext == IntPtr.Zero) { _sharedContext = _context; } if (Extension.WglSwapIntervalExt != null) { Extension.WglSwapIntervalExt(0); } //RECT clientRect; //GetClientRect(hWnd, &clientRect); }