Esempio n. 1
0
        /// <summary>
        /// Resets the device
        /// </summary>
        #endregion
        public void ResetDevice()
        {
            if (mIsInAReset)
            {
                return;
            }

            #region If the Renderer.Graphics is null that means the engine is not loaded yet
            if (!mSuspendDeviceReset && Renderer.Graphics == null)
            {
                throw new InvalidOperationException("Can't reset the device right now because the Renderer's Graphics are null. " +
                                                    "Are you attempting to change the GraphicsOption's properties prior to the creation of FlatRedBallServices? " +
                                                    "If so you must call SuspendDeviceReset before changing properties, then ResumeDeviceReset after the properties " +
                                                    "are set, but before calling FlatRedBallServices.InitializeFlatRedBall.  Otherwise, move the property-changing " +
                                                    "code to after FlatRedBall is initialized.");
            }
            #endregion

            #region Else, check to make sure device resetting is not suspended and the GraphicsOptions are not loading
            else if (!mSuspendDeviceReset && !GraphicsOptions.IsLoading)
            {
                mIsInAReset = true;

                // Reset the graphics device manager
                if (FlatRedBallServices.mGraphics != null)
                {
                    // Set window size
                    FlatRedBallServices.mGraphics.PreferredBackBufferWidth  = mResolutionWidth;
                    FlatRedBallServices.mGraphics.PreferredBackBufferHeight = mResolutionHeight;
                    FlatRedBallServices.mGraphics.PreferMultiSampling       = mUseMultiSampling;
                    FlatRedBallServices.mGraphics.IsFullScreen = mIsFullScreen;


                    try
                    {
                        // Victor Chelaru
                        // February 26, 2014
                        // Android doesn't have
                        // the ability to run on
                        // multiple resolutions, so
                        // we won't do any checks here.
#if DEBUG && !SILVERLIGHT && !ANDROID && !UWP
                        if (IsFullScreen)
                        {
                            bool foundResolution = false;

                            var supportedDisplay = GraphicsAdapter.DefaultAdapter.SupportedDisplayModes;

                            foreach (DisplayMode mode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
                            {
                                if (mode.Width == mResolutionWidth && mode.Height == mResolutionHeight)
                                {
                                    foundResolution = true;
                                }
                            }
                            if (!foundResolution)
                            {
                                string message = $"The resolution {mResolutionWidth} x {mResolutionHeight} is not supported in full screen mode.  Supported resolutions:\n";
                                message += "(width x height)\n";

                                foreach (var value in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
                                {
                                    message += value.Width + "x" + value.Height + "\n";
                                }


                                throw new NotImplementedException(message);
                            }
                        }
#endif
                        FlatRedBallServices.mGraphics.ApplyChanges();
                    }
                    // No longer needed since we are always going to use a sample quality of 0
                    //catch (Exception e)
                    //{
                    //    int qualityLevels = 0;
                    //    bool allowed = GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(DeviceType.Hardware,
                    //        SurfaceFormat.Color, false, mMultiSampleType, out qualityLevels);

                    //    throw e;
                    //}


                    finally
                    {
                        mIsInAReset = false;
                    }
                }
                // Prepare the presentation parameters
                PresentationParameters presParams = FlatRedBallServices.GraphicsDevice.PresentationParameters;

                // Sets the presentation parameters
                SetPresentationParameters(ref presParams);

                // Reset the device
                if (FlatRedBallServices.mGraphics != null)
                {
    #if !MONODROID
                    while (FlatRedBallServices.mGraphics.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost ||
                           FlatRedBallServices.mGraphics.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.NotReset)
                    {
                        int m = 3;
                        m += 32;
                        m /= 32;
                    }
    #endif
                }

    #if MONOGAME
                // Resetting crashes monogame currently, but we can still react as if a reset happened
                FlatRedBallServices.graphics_DeviceReset(null, null);
    #else
                FlatRedBallServices.GraphicsDevice.Reset(presParams);
    #endif

                // When the device resets the render states could get screwed up.  Force the
                // blend state changes in case they were changed but nothing later changes them
                // back.
                // Hm, this seems to cause a crash because the mCurrentEffect isn't set yet
                //Renderer.ForceSetColorOperation(Renderer.ColorOperation);
                Renderer.ForceSetBlendOperation();
                mIsInAReset = false;
            }
            #endregion
        }
Esempio n. 2
0
        /// <summary>
        /// Resets the device if the device is not currently rendering, if the graphics device is not null, and if
        /// device resetting is not suspended.
        /// </summary>
        public void ResetDevice()
        {
            if (mIsInAReset)
            {
                return;
            }

            #region If the Renderer.Graphics is null that means the engine is not loaded yet
            if (!mSuspendDeviceReset && Renderer.Graphics == null)
            {
                throw new InvalidOperationException("Can't reset the device right now because the Renderer's Graphics are null. " +
                                                    "Are you attempting to change the GraphicsOption's properties prior to the creation of FlatRedBallServices? " +
                                                    "If so you must call SuspendDeviceReset before changing properties, then ResumeDeviceReset after the properties " +
                                                    "are set, but before calling FlatRedBallServices.InitializeFlatRedBall.  Otherwise, move the property-changing " +
                                                    "code to after FlatRedBall is initialized.");
            }
            #endregion

            #region Else, check to make sure device resetting is not suspended and the GraphicsOptions are not loading
            else if (!mSuspendDeviceReset && !GraphicsOptions.IsLoading)
            {
                mIsInAReset = true;

                // Reset the graphics device manager
                if (FlatRedBallServices.mGraphics != null)
                {
                    if (windowedFullscreenMode == WindowedFullscreenMode.Fullscreen)
                    {
                        ThrowExceptionIfFullScreenResolutionNotSupported(mResolutionWidth, mResolutionHeight);
                    }


                    // Set window size
                    FlatRedBallServices.mGraphics.PreferredBackBufferWidth  = mResolutionWidth;
                    FlatRedBallServices.mGraphics.PreferredBackBufferHeight = mResolutionHeight;
                    FlatRedBallServices.mGraphics.PreferMultiSampling       = mUseMultiSampling;
                    FlatRedBallServices.mGraphics.IsFullScreen = windowedFullscreenMode == WindowedFullscreenMode.Fullscreen;

#if DESKTOP_GL
                    // for borderless the user must set the GraphicsDeviceManager.HardwareModeSwitch to false
http:               //community.monogame.net/t/how-to-implement-borderless-fullscreen-on-desktopgl-project/8359
                    FlatRedBallServices.mGraphics.IsFullScreen =
                        windowedFullscreenMode == WindowedFullscreenMode.Fullscreen ||
                        windowedFullscreenMode == WindowedFullscreenMode.FullscreenBorderless


                    ;
#else
                    FlatRedBallServices.mGraphics.IsFullScreen = windowedFullscreenMode == WindowedFullscreenMode.Fullscreen;
#endif



                    try
                    {
                        FlatRedBallServices.mGraphics.ApplyChanges();
                    }
                    // No longer needed since we are always going to use a sample quality of 0
                    //catch (Exception e)
                    //{
                    //    int qualityLevels = 0;
                    //    bool allowed = GraphicsAdapter.DefaultAdapter.CheckDeviceMultiSampleType(DeviceType.Hardware,
                    //        SurfaceFormat.Color, false, mMultiSampleType, out qualityLevels);

                    //    throw e;
                    //}


                    finally
                    {
                        mIsInAReset = false;
                    }
                }
                // Prepare the presentation parameters
                PresentationParameters presParams = FlatRedBallServices.GraphicsDevice.PresentationParameters;

                // Sets the presentation parameters
                SetPresentationParameters(ref presParams);

                // Reset the device
                if (FlatRedBallServices.mGraphics != null)
                {
    #if !MONODROID
                    while (FlatRedBallServices.mGraphics.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost ||
                           FlatRedBallServices.mGraphics.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.NotReset)
                    {
                        int m = 3;
                        m += 32;
                        m /= 32;
                    }
    #endif
                }

    #if MONOGAME
                // Resetting crashes monogame currently, but we can still react as if a reset happened
                FlatRedBallServices.graphics_DeviceReset(null, null);
    #else
                FlatRedBallServices.GraphicsDevice.Reset(presParams);
    #endif

                // When the device resets the render states could get screwed up.  Force the
                // blend state changes in case they were changed but nothing later changes them
                // back.
                // Hm, this seems to cause a crash because the mCurrentEffect isn't set yet
                //Renderer.ForceSetColorOperation(Renderer.ColorOperation);
                Renderer.ForceSetBlendOperation();
                mIsInAReset = false;
            }
            #endregion
        }