ChoosePixelFormat() private method

private ChoosePixelFormat ( IntPtr dc, PixelFormatDescriptor &pfd ) : int
dc System.IntPtr
pfd PixelFormatDescriptor
return int
Ejemplo n.º 1
0
        GraphicsMode SelectGraphicsModePFD(ColorDepth color, int depth, int stencil, int samples, ColorDepth accum,
                                           int buffers, bool stereo)
        {
            using (Control native_window = new Control())
                using (WinWindowInfo window = new WinWindowInfo(native_window.Handle, null))
                {
                    IntPtr deviceContext = ((WinWindowInfo)window).DeviceContext;
                    Debug.WriteLine(String.Format("Device context: {0}", deviceContext));

                    Debug.Write("Selecting pixel format... ");
                    PixelFormatDescriptor pixelFormat = new PixelFormatDescriptor();
                    pixelFormat.Size    = API.PixelFormatDescriptorSize;
                    pixelFormat.Version = API.PixelFormatDescriptorVersion;
                    pixelFormat.Flags   =
                        PixelFormatDescriptorFlags.SUPPORT_OPENGL |
                        PixelFormatDescriptorFlags.DRAW_TO_WINDOW;
                    pixelFormat.ColorBits = (byte)(color.Red + color.Green + color.Blue);

                    pixelFormat.PixelType = color.IsIndexed ? PixelType.INDEXED : PixelType.RGBA;
                    pixelFormat.RedBits   = (byte)color.Red;
                    pixelFormat.GreenBits = (byte)color.Green;
                    pixelFormat.BlueBits  = (byte)color.Blue;
                    pixelFormat.AlphaBits = (byte)color.Alpha;

                    if (accum.BitsPerPixel > 0)
                    {
                        pixelFormat.AccumBits      = (byte)(accum.Red + accum.Green + accum.Blue);
                        pixelFormat.AccumRedBits   = (byte)accum.Red;
                        pixelFormat.AccumGreenBits = (byte)accum.Green;
                        pixelFormat.AccumBlueBits  = (byte)accum.Blue;
                        pixelFormat.AccumAlphaBits = (byte)accum.Alpha;
                    }

                    pixelFormat.DepthBits   = (byte)depth;
                    pixelFormat.StencilBits = (byte)stencil;

                    if (depth <= 0)
                    {
                        pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE;
                    }
                    if (stereo)
                    {
                        pixelFormat.Flags |= PixelFormatDescriptorFlags.STEREO;
                    }
                    if (buffers > 1)
                    {
                        pixelFormat.Flags |= PixelFormatDescriptorFlags.DOUBLEBUFFER;
                    }

                    int pixel = Functions.ChoosePixelFormat(deviceContext, ref pixelFormat);
                    if (pixel == 0)
                    {
                        throw new GraphicsModeException("The requested GraphicsMode is not available.");
                    }

                    // Find out what we really got as a format:
                    PixelFormatDescriptor pfd = new PixelFormatDescriptor();
                    pixelFormat.Size    = API.PixelFormatDescriptorSize;
                    pixelFormat.Version = API.PixelFormatDescriptorVersion;
                    Functions.DescribePixelFormat(deviceContext, pixel, API.PixelFormatDescriptorSize, ref pfd);
                    GraphicsMode fmt = new GraphicsMode((IntPtr)pixel,
                                                        new ColorDepth(pfd.RedBits, pfd.GreenBits, pfd.BlueBits, pfd.AlphaBits),
                                                        pfd.DepthBits,
                                                        pfd.StencilBits,
                                                        0,
                                                        new ColorDepth(pfd.AccumBits),
                                                        (pfd.Flags & PixelFormatDescriptorFlags.DOUBLEBUFFER) != 0 ? 2 : 1,
                                                        (pfd.Flags & PixelFormatDescriptorFlags.STEREO) != 0);

                    return(fmt);
                }
        }