/// <summary>
 /// Initializes the rendering API, potentially using lower settings in the event that
 /// the initial attempt to initialize the API failed.
 /// </summary>
 /// <param name="graphicsConfiguration">The platform configuration settings.</param>
 /// <param name="attempt">The number of attempts which have been made to find a working configuration.</param>
 /// <returns><see langword="true"/> if a configuration was provided; otherwise, <see langword="false"/>.</returns>
 protected virtual Boolean InitializeRenderingAPI(UltravioletGraphicsConfiguration graphicsConfiguration, Int32 attempt)
 {
     return(false);
 }
Example #2
0
        /// <inheritdoc/>
        protected override Boolean InitializeRenderingAPI(UltravioletGraphicsConfiguration graphicsConfig, Int32 attempt)
        {
            var glGraphicsConfig = graphicsConfig as OpenGLGraphicsConfiguration;

            if (glGraphicsConfig == null)
            {
                throw new InvalidOperationException(UltravioletStrings.MissingGraphicsConfiguration);
            }

            if (SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, glGraphicsConfig.SrgbBuffersEnabled ? 1 : 0) < 0)
            {
                throw new SDL2Exception();
            }

            if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, glGraphicsConfig.MultiSampleBuffers) < 0)
            {
                throw new SDL2Exception();
            }

            if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, glGraphicsConfig.MultiSampleSamples) < 0)
            {
                throw new SDL2Exception();
            }

            switch (attempt)
            {
            case 0:
                // Attempt #0: Try with specified configurations.
                return(true);

            case 1:
                // Attempt #1: Try turning off sRGB.
                if (glGraphicsConfig.SrgbBuffersEnabled)
                {
                    if (SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    return(true);
                }
                else
                {
                    goto case 2;
                }

            case 2:
                // Attempt #2: Try turning off multisampling.
                if (glGraphicsConfig.MultiSampleBuffers > 0 || glGraphicsConfig.MultiSampleSamples > 0)
                {
                    if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    return(true);
                }
                else
                {
                    goto case 3;
                }

            case 3:
                // Attempt #3: Try turning off sRGB and multisampling.
                if (glGraphicsConfig.SrgbBuffersEnabled)
                {
                    if (SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    return(true);
                }
                else
                {
                    goto default;
                }

            default:
                return(false);
            }
        }