示例#1
0
 static AL()
 {
     DynamicLibraryFactory.MapLibraryToType <AL>(
         DynamicLibraryFactory.CreateForLibrary(DllWindows, DllLinux, DllMac, DllAndroid));
 }
        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);
        }