예제 #1
0
        private void Initialize3DEnvironment()
        {
            DisplayAdapter adapterInfo = graphicsSettings.DisplayAdapter;
            DisplayDevice deviceInfo = graphicsSettings.DisplayDevice;

            isWindowed = graphicsSettings.IsWindowed;

            // Prepare window for possible windowed/fullscreen change
            // AdjustWindowForChange();

            // Set up the presentation parameters
            RefreshPresentParameters();

            if(deviceInfo.Caps.PrimitiveMiscCaps.IsNullReference)
            {
                // Warn user about null ref device that can't render anything
                throw new ApplicationException("null reference device");
            }

            CreateFlags createFlags = new CreateFlags();
            switch(graphicsSettings.VertexProcessingType)
            {
                case VertexProcessingType.Software:
                    createFlags = CreateFlags.SoftwareVertexProcessing;
                    break;
                case VertexProcessingType.Mixed:
                    createFlags = CreateFlags.MixedVertexProcessing;
                    break;
                case VertexProcessingType.Hardware:
                    createFlags = CreateFlags.HardwareVertexProcessing;
                    break;
                case VertexProcessingType.PureHardware:
                    createFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice;
                    break;
                default:
                    throw new ApplicationException("Unable to determine vertex processing method.");
            }

            // Create the device
            device = new Device(graphicsSettings.AdapterOrdinal,
                graphicsSettings.DisplayDevice.DeviceType,
                this.viewport,
                createFlags,
                this.presentParameters);

            if( device != null )
            {
                // Cache our local objects
                renderStates = device.RenderState;
                samplerStates = device.SamplerState;
                textureStates = device.TextureState;
                // When moving from fullscreen to windowed mode, it is important to
                // adjust the window size after recreating the device rather than
                // beforehand to ensure that you get the window size you want.  For
                // example, when switching from 640x480 fullscreen to windowed with
                // a 1000x600 window on a 1024x768 desktop, it is impossible to set
                // the window size to 1000x600 until after the display mode has
                // changed to 1024x768, because windows cannot be larger than the
                // desktop.
                if(graphicsSettings.IsWindowed && (this.viewport is System.Windows.Forms.Form))
                {
                    // Make sure main window isn't topmost, so error message is visible
                    this.viewport.Location = new System.Drawing.Point(rectWindowBounds.Left, rectWindowBounds.Top);
                    this.viewport.Size = new System.Drawing.Size(( rectWindowBounds.Right - rectWindowBounds.Left ), ( rectWindowBounds.Bottom - rectWindowBounds.Top));
                }

                // Store device Caps
                graphicsCaps = device.DeviceCaps;
                behavior = createFlags;

                // Store render target surface desc
                Surface BackBuffer = device.GetBackBuffer(0,0, BackBufferType.Mono);
                backBufferDesc = BackBuffer.Description;
                BackBuffer.Dispose();
                BackBuffer = null;

                // Set up the fullscreen cursor
                if(showFullScreenCursor && !graphicsSettings.IsWindowed)
                {
                    System.Windows.Forms.Cursor ourCursor = this.viewport.Cursor;
                    device.SetCursor(ourCursor, true);
                    device.ShowCursor(true);
                }

                // Confine cursor to fullscreen window
                if(clipFullScreenCursor)
                {
                    if (!isWindowed)
                    {
                        System.Drawing.Rectangle rcWindow = this.viewport.ClientRectangle;
                    }
                }

                // Setup the event handlers for our device
                device.DeviceLost += InvalidateDeviceObjects;
                device.DeviceReset += RestoreDeviceObjects;
                device.Disposing += DeleteDeviceObjects;
                device.DeviceResizing += new CancelEventHandler(EnvironmentResized);

                // Initialize the app's device-dependent objects
                try
                {
                    if(InitDeviceObjects != null)
                        InitDeviceObjects(null, null);

                    if(RestoreDeviceObjects != null)
                        RestoreDeviceObjects(null, null);

                    return;
                }
                catch
                {
                    // Cleanup before we try again
                    if(InvalidateDeviceObjects != null)
                        InvalidateDeviceObjects(null, null);

                    if(DeleteDeviceObjects != null)
                        DeleteDeviceObjects(null, null);

                    device.Dispose();
                    device = null;

                    if(this.viewport.Disposing)
                        return;
                }
            }

            //	HACK: removed fallback to reference rasterizer
            /*
            // If that failed, fall back to the reference rasterizer
            if( deviceInfo.DevType == Direct3D.DeviceType.Hardware )
            {
                if (FindBestWindowedMode(false, true))
                {
                    isWindowed = true;
                    if(viewport is System.Windows.Forms.Form)
                    {
                        // Make sure main window isn't topmost, so error message is visible
                        this.viewport.Location = new System.Drawing.Point(windowBoundsRect.Left, windowBoundsRect.Top);
                        this.viewport.Size = new System.Drawing.Size(( windowBoundsRect.Right - windowBoundsRect.Left ), ( windowBoundsRect.Bottom - windowBoundsRect.Top));
                        //AdjustWindowForChange();
                    }

                    // Let the user know we are switching from HAL to the reference rasterizer
                    //DisplayErrorMsg( null, AppMsgType.WarnSwitchToRef);

                    Initialize3DEnvironment();
                }
            }
            */
        }
예제 #2
0
        private void Initialize3DEnvironment()
        {
            DisplayAdapter adapterInfo = graphicsSettings.DisplayAdapter;
            DisplayDevice  deviceInfo  = graphicsSettings.DisplayDevice;

            isWindowed = graphicsSettings.IsWindowed;

            // Prepare window for possible windowed/fullscreen change
            // AdjustWindowForChange();

            // Set up the presentation parameters
            RefreshPresentParameters();

            if (deviceInfo.Caps.PrimitiveMiscCaps.IsNullReference)
            {
                // Warn user about null ref device that can't render anything
                throw new ApplicationException("null reference device");
            }

            CreateFlags createFlags = new CreateFlags();

            switch (graphicsSettings.VertexProcessingType)
            {
            case VertexProcessingType.Software:
                createFlags = CreateFlags.SoftwareVertexProcessing;
                break;

            case VertexProcessingType.Mixed:
                createFlags = CreateFlags.MixedVertexProcessing;
                break;

            case VertexProcessingType.Hardware:
                createFlags = CreateFlags.HardwareVertexProcessing;
                break;

            case VertexProcessingType.PureHardware:
                createFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice;
                break;

            default:
                throw new ApplicationException("Unable to determine vertex processing method.");
            }

            // Create the device
            device = new Device(graphicsSettings.AdapterOrdinal,
                                graphicsSettings.DisplayDevice.DeviceType,
                                this.viewport,
                                createFlags,
                                this.presentParameters);

            if (device != null)
            {
                // Cache our local objects
                renderStates  = device.RenderState;
                samplerStates = device.SamplerState;
                textureStates = device.TextureState;
                // When moving from fullscreen to windowed mode, it is important to
                // adjust the window size after recreating the device rather than
                // beforehand to ensure that you get the window size you want.  For
                // example, when switching from 640x480 fullscreen to windowed with
                // a 1000x600 window on a 1024x768 desktop, it is impossible to set
                // the window size to 1000x600 until after the display mode has
                // changed to 1024x768, because windows cannot be larger than the
                // desktop.
                if (graphicsSettings.IsWindowed && (this.viewport is System.Windows.Forms.Form))
                {
                    // Make sure main window isn't topmost, so error message is visible
                    this.viewport.Location = new System.Drawing.Point(rectWindowBounds.Left, rectWindowBounds.Top);
                    this.viewport.Size     = new System.Drawing.Size((rectWindowBounds.Right - rectWindowBounds.Left), (rectWindowBounds.Bottom - rectWindowBounds.Top));
                }

                // Store device Caps
                graphicsCaps = device.DeviceCaps;
                behavior     = createFlags;

                // Store render target surface desc
                Surface BackBuffer = device.GetBackBuffer(0, 0, BackBufferType.Mono);
                backBufferDesc = BackBuffer.Description;
                BackBuffer.Dispose();
                BackBuffer = null;

                // Set up the fullscreen cursor
                if (showFullScreenCursor && !graphicsSettings.IsWindowed)
                {
                    System.Windows.Forms.Cursor ourCursor = this.viewport.Cursor;
                    device.SetCursor(ourCursor, true);
                    device.ShowCursor(true);
                }

                // Confine cursor to fullscreen window
                if (clipFullScreenCursor)
                {
                    if (!isWindowed)
                    {
                        System.Drawing.Rectangle rcWindow = this.viewport.ClientRectangle;
                    }
                }

                // Setup the event handlers for our device
                device.DeviceLost     += InvalidateDeviceObjects;
                device.DeviceReset    += RestoreDeviceObjects;
                device.Disposing      += DeleteDeviceObjects;
                device.DeviceResizing += new CancelEventHandler(EnvironmentResized);

                // Initialize the app's device-dependent objects
                try
                {
                    if (InitDeviceObjects != null)
                    {
                        InitDeviceObjects(null, null);
                    }

                    if (RestoreDeviceObjects != null)
                    {
                        RestoreDeviceObjects(null, null);
                    }

                    return;
                }
                catch
                {
                    // Cleanup before we try again
                    if (InvalidateDeviceObjects != null)
                    {
                        InvalidateDeviceObjects(null, null);
                    }

                    if (DeleteDeviceObjects != null)
                    {
                        DeleteDeviceObjects(null, null);
                    }

                    device.Dispose();
                    device = null;

                    if (this.viewport.Disposing)
                    {
                        return;
                    }
                }
            }

            //	HACK: removed fallback to reference rasterizer

            /*
             * // If that failed, fall back to the reference rasterizer
             * if( deviceInfo.DevType == Direct3D.DeviceType.Hardware )
             * {
             *      if (FindBestWindowedMode(false, true))
             *      {
             *              isWindowed = true;
             *              if(viewport is System.Windows.Forms.Form)
             *              {
             *                      // Make sure main window isn't topmost, so error message is visible
             *                      this.viewport.Location = new System.Drawing.Point(windowBoundsRect.Left, windowBoundsRect.Top);
             *                      this.viewport.Size = new System.Drawing.Size(( windowBoundsRect.Right - windowBoundsRect.Left ), ( windowBoundsRect.Bottom - windowBoundsRect.Top));
             *                      //AdjustWindowForChange();
             *              }
             *
             *              // Let the user know we are switching from HAL to the reference rasterizer
             *              //DisplayErrorMsg( null, AppMsgType.WarnSwitchToRef);
             *
             *              Initialize3DEnvironment();
             *      }
             * }
             */
        }