SelectPixelFormat() публичный Метод

public SelectPixelFormat ( IntPtr hdc, int colorDepth, int multisample ) : bool
hdc System.IntPtr
colorDepth int
multisample int
Результат bool
Пример #1
0
        public override void Create(string name, int width, int height, bool isFullScreen, NamedParameterList miscParams)
        {
            if (_hWindow != IntPtr.Zero)
            {
                dispose(true);
            }

            _hWindow          = IntPtr.Zero;
            this.name         = name;
            this.IsFullScreen = isFullScreen;
            this._isClosed    = false;

            // load window defaults
            this.left              = this.top = -1; // centered
            this.width             = width;
            this.height            = height;
            this._displayFrequency = 0;
            this.isDepthBuffered   = true;
            this.colorDepth        = IsFullScreen ? 32 : 16;

            IntPtr parentHwnd = IntPtr.Zero;
            string title      = name;
            bool   vsync      = false;
            int    fsaa       = 0;
            string border     = "";
            bool   outerSize  = false;

            #region Parameter Handling
            if (miscParams != null)
            {
                foreach (KeyValuePair <string, object> entry in miscParams)
                {
                    switch (entry.Key)
                    {
                    case "title":
                        title = entry.Value.ToString();
                        break;

                    case "left":
                        left = Int32.Parse(entry.Value.ToString());
                        break;

                    case "top":
                        top = Int32.Parse(entry.Value.ToString());
                        break;

                    case "depthBuffer":
                        isDepthBuffered = bool.Parse(entry.Value.ToString());
                        break;

                    case "vsync":
                        vsync = entry.Value.ToString() == "Yes" ? true : false;
                        break;

                    case "fsaa":
                        fsaa = Int32.Parse(entry.Value.ToString());
                        break;

                    case "externalWindowHandle":
                        _hWindow = (IntPtr)entry.Value;
                        if (_hWindow != IntPtr.Zero)
                        {
                            _isExternal  = true;
                            IsFullScreen = false;
                        }
                        break;

                    case "externalGLControl":
                        break;

                    case "border":
                        border = ((string)miscParams["border"]).ToLower();
                        break;

                    case "outerDimensions":
                        break;

                    case "displayFrequency":
                        if (IsFullScreen)
                        {
                            _displayFrequency = Int32.Parse(entry.Value.ToString());
                        }
                        break;

                    case "colorDepth":
                        if (IsFullScreen)
                        {
                            colorDepth = Int32.Parse(entry.Value.ToString());
                        }
                        break;

                    case "parentWindowHandle":
                        if (!IsFullScreen)
                        {
                            parentHwnd = (IntPtr)entry.Value;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            #endregion Parameter Handling

            if (!_isExternal)
            {
                DefaultForm form = new DefaultForm();

                form.ClientSize    = new System.Drawing.Size(width, height);
                form.MaximizeBox   = false;
                form.MinimizeBox   = false;
                form.StartPosition = SWF.FormStartPosition.CenterScreen;

                if (IsFullScreen)
                {
                    // Set the display to the desired resolution
                    Gdi.DEVMODE screenSettings = new Gdi.DEVMODE();
                    screenSettings.dmSize       = (short)Marshal.SizeOf(screenSettings);
                    screenSettings.dmPelsWidth  = width;                                            // Selected Screen Width
                    screenSettings.dmPelsHeight = height;                                           // Selected Screen Height
                    screenSettings.dmBitsPerPel = ColorDepth;                                       // Selected Bits Per Pixel
                    screenSettings.dmFields     = Gdi.DM_BITSPERPEL | Gdi.DM_PELSWIDTH | Gdi.DM_PELSHEIGHT;

                    // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
                    int result = User.ChangeDisplaySettings(ref screenSettings, User.CDS_FULLSCREEN);

                    if (result != User.DISP_CHANGE_SUCCESSFUL)
                    {
                        throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to change user display settings.");
                    }

                    // Adjust form to size the screen
                    form.Top             = 0;
                    form.Left            = 0;
                    form.FormBorderStyle = SWF.FormBorderStyle.None;
                    form.WindowState     = SWF.FormWindowState.Maximized;
#if !DEBUG
                    form.TopMost  = true;
                    form.TopLevel = true;
#endif
                }
                else
                {
                    if (parentHwnd != IntPtr.Zero)
                    {
                        form.Owner = (SWF.Form)SWF.Control.FromHandle(parentHwnd);
                    }
                    else
                    {
                        if (border == "none")
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.None;
                        }
                        else if (border == "fixed")
                        {
                            form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
                            form.MaximizeBox     = false;
                        }
                    }

                    form.Top  = top;
                    form.Left = left;
                    //form.FormBorderStyle = SWF.FormBorderStyle.FixedSingle;
                    form.WindowState = SWF.FormWindowState.Normal;
                    form.Text        = title;
                }

                WindowEventMonitor.Instance.RegisterWindow(this);

                form.RenderWindow = this;
                _hWindow          = form.Handle;
                form.Show();
            }

            IntPtr old_hdc     = Wgl.wglGetCurrentDC();
            IntPtr old_context = Wgl.wglGetCurrentContext();

            SWF.Control ctrl = SWF.Form.FromHandle(_hWindow);
            //Form frm = (Form)ctrl.TopLevelControl;
            this.top    = ctrl.Top;
            this.left   = ctrl.Left;
            this.width  = ctrl.ClientRectangle.Width;
            this.height = ctrl.ClientRectangle.Height;

            _hDeviceContext = User.GetDC(_hWindow);

            // Do not change vsync if the external window has the OpenGL control
            if (!_isExternalGLControl)
            {
                if (!_glSupport.SelectPixelFormat(_hDeviceContext, ColorDepth, fsaa))
                {
                    if (fsaa == 0)
                    {
                        throw new Exception("selectPixelFormat failed");
                    }

                    LogManager.Instance.Write("FSAA level not supported, falling back");
                    if (!_glSupport.SelectPixelFormat(_hDeviceContext, ColorDepth, 0))
                    {
                        throw new Exception("selectPixelFormat failed");
                    }
                }
            }

            // attempt to get the rendering context
            _hRenderingContext = Wgl.wglCreateContext(_hDeviceContext);

            if (_hRenderingContext == IntPtr.Zero)
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to create a GL rendering context.");
            }

            if (!Wgl.wglMakeCurrent(_hDeviceContext, _hRenderingContext))
            {
                throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "Unable to activate the GL rendering context.");
            }

            // Do not change vsync if the external window has the OpenGL control
            if (!_isExternalGLControl)
            {
                // Don't use wglew as if this is the first window, we won't have initialised yet
                //IntPtr wglSwapIntervalEXT = Wgl.wglGetProcAddress( "wglSwapIntervalEXT" );
                //if ( wglSwapIntervalEXT != IntPtr.Zero )
                //Wgl.wglSwapIntervalEXT( wglSwapIntervalEXT, vsync );
                if (Wgl.IsExtensionSupported("wglSwapIntervalEXT"))
                {
                    Wgl.wglSwapIntervalEXT(vsync ? 1 : 0);                       // Tao 2.0
                }
            }

            if (old_context != IntPtr.Zero)
            {
                // Restore old context
                if (!Wgl.wglMakeCurrent(old_hdc, old_context))
                {
                    throw new Exception("wglMakeCurrent() failed");
                }

                // Share lists with old context
                if (!Wgl.wglShareLists(old_context, _hRenderingContext))
                {
                    throw new Exception("wglShareLists() failed");
                }
            }

            // Create RenderSystem context
            _glContext = new Win32Context(_hDeviceContext, _hRenderingContext);

            // make this window active
            this.IsActive = true;
        }