private PresentParameters CreateBasePresentParams(SDX_DisplayWindow displayWindow, int bpp)
        {
            PresentParameters present = new PresentParameters();

            present.BackBufferCount        = 1;
            present.AutoDepthStencilFormat = Format.Unknown;
            present.EnableAutoDepthStencil = false;
            present.DeviceWindowHandle     = displayWindow.RenderTarget.Handle;
            present.BackBufferWidth        = displayWindow.Width;
            present.BackBufferHeight       = displayWindow.Height;
            present.BackBufferFormat       = GetDisplayModeTrialPixelFormat(bpp);
            present.SwapEffect             = SwapEffect.Discard;
            present.Windowed = true;

            if (present.AutoDepthStencilFormat == Format.Unknown)
            {
                present.EnableAutoDepthStencil = false;
            }

            if (GetRenderState(RenderStateBool.WaitForVerticalBlank))
            {
                present.PresentationInterval = PresentInterval.Default;
            }
            else
            {
                present.PresentationInterval = PresentInterval.Immediate;
            }

            return(present);
        }
        private PresentParameters CreateWindowedPresentParameters(SDX_DisplayWindow displayWindow,
                                                                  int width, int height, int bpp)
        {
            PresentParameters present = CreateBasePresentParams(displayWindow, bpp);

            return(present);
        }
        private PresentParameters CreateFullScreenPresentParameters(SDX_DisplayWindow displayWindow,
                                                                    int width, int height, int bpp)
        {
            PresentParameters present = CreateBasePresentParams(displayWindow, bpp);

            present.SwapEffect = SwapEffect.Flip;
            present.Windowed   = false;

            SelectBestDisplayMode(present, bpp);

            return(present);
        }
        internal SwapChain CreateSwapChain(SDX_DisplayWindow displayWindow,
                                           int width, int height, int bpp, bool fullScreen)
        {
            if (fullScreen == true)
            {
                PresentParameters present =
                    CreateFullScreenPresentParameters(displayWindow, width, height, bpp);

                OnDeviceAboutToReset();

                System.Diagnostics.Debug.Print("{0} Going to full screen...", DateTime.Now);
                mDevice.Device.Reset(present);
                System.Diagnostics.Debug.Print("{0} Full screen success.", DateTime.Now);

                return(mDevice.Device.GetSwapChain(0));
            }
            else
            {
                PresentParameters present =
                    CreateWindowedPresentParameters(displayWindow, width, height, bpp);

                if (displayWindow.FrameBuffer != null && displayWindow.IsFullScreen == true)
                {
                    // if we are in full screen mode already, we must
                    // reset the device before creating the windowed swap chain.
                    present.BackBufferHeight   = 1;
                    present.BackBufferWidth    = 1;
                    present.DeviceWindowHandle = displayWindow.RenderTarget.TopLevelControl.Handle;

                    OnDeviceAboutToReset();

                    var result = mDevice.Device.TestCooperativeLevel();

                    System.Diagnostics.Debug.Print("TestCooperativeLevel result: {0}", result);
                    System.Diagnostics.Debug.Print("{0} Going to windowed mode...", DateTime.Now);
                    mDevice.Device.Reset(present);
                    System.Diagnostics.Debug.Print("{0} Windowed mode success.", DateTime.Now);


                    present = CreateWindowedPresentParameters(displayWindow, width, height, bpp);
                }

                return(new Direct3D.SwapChain(mDevice.Device, present));
            }
        }
        internal void Initialize(SDX_DisplayWindow window, CreateWindowParams windowParams)
        {
            if (mInitialized)
            {
                return;
            }

            if (window.RenderTarget.TopLevelControl == null)
            {
                throw new ArgumentException("The specified render target does not have a Form object yet.  " +
                                            "It's TopLevelControl property is null.  You may not create DisplayWindow objects before " +
                                            "the control to render to is added to the Form.");
            }

            mInitialized = true;

            // ok, create D3D device
            PresentParameters present = windowParams.IsFullScreen ?
                                        CreateFullScreenPresentParameters(window, windowParams.Width, windowParams.Height, windowParams.Bpp) :
                                        CreateWindowedPresentParameters(window, 0, 0, 32);

            DeviceType dtype = DeviceType.Hardware;

            int adapterOrdinal = mDirect3Dobject.Adapters.DefaultAdapter.Adapter;

            var caps  = mDirect3Dobject.GetDeviceCaps(adapterOrdinal, Direct3D.DeviceType.Hardware);
            var flags = Direct3D.CreateFlags.SoftwareVertexProcessing;

            // Is there support for hardware vertex processing? If so, replace
            // software vertex processing.
            if ((caps.DeviceCaps & DeviceCaps.HWTransformAndLight) == DeviceCaps.HWTransformAndLight)
            {
                flags = Direct3D.CreateFlags.HardwareVertexProcessing;
            }

            // Does the device support a pure device?
            if ((caps.DeviceCaps & DeviceCaps.PureDevice) == DeviceCaps.PureDevice)
            {
                flags |= Direct3D.CreateFlags.PureDevice;
            }

            Device device = new Device(mDirect3Dobject, adapterOrdinal, dtype,
                                       window.RenderTarget.TopLevelControl.Handle,
                                       flags, present);

            try
            {
                Format f = (Format)device.DepthStencilSurface.Description.Format;
                SetHaveDepthStencil(f);
            }
            catch
            {
                mHasDepth   = true;
                mHasStencil = false;

                SetHaveDepthStencil(Format.D16);
            }

            //device.DeviceLost += new EventHandler(mDevice_DeviceLost);
            //device.DeviceReset += new EventHandler(mDevice_DeviceReset);

            device.SetRenderState(RenderState.StencilEnable, false);
            device.SetRenderState(RenderState.ZEnable, true);

            mDevice = new D3DDevice(device);

            //InitializeShaders();

            mPosColorDecl = SDX_VertexBuffer.CreateVertexDeclaration(device, PositionColor.VertexLayout);
        }