Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the OpenGLUltravioletPlatform class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="configuration">The Ultraviolet Framework configuration settings for the current context.</param>
 public OpenGLUltravioletPlatform(UltravioletContext uv, OpenGLUltravioletConfiguration configuration)
     : base(uv)
 {
     this.clipboard = new OpenGLUltravioletClipboardInfo();
     this.windows   = new OpenGLUltravioletWindowInfo(uv, configuration);
     this.displays  = new OpenGLUltravioletDisplayInfo();
 }
Ejemplo n.º 2
0
        protected override UltravioletContext OnCreatingUltravioletContext()
        {
            var configuration = new OpenGLUltravioletConfiguration();
            PresentationFoundation.Configure(configuration);

            return new OpenGLUltravioletContext(this, configuration);
        }
 /// <summary>
 /// Initializes a new instance of the OpenGLUltravioletPlatform class.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="configuration">The Ultraviolet Framework configuration settings for the current context.</param>
 public OpenGLUltravioletPlatform(UltravioletContext uv, OpenGLUltravioletConfiguration configuration)
     : base(uv)
 {
     this.clipboard = new OpenGLUltravioletClipboardInfo();
     this.windows   = new OpenGLUltravioletWindowInfo(uv, configuration);
     this.displays  = new OpenGLUltravioletDisplayInfo();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to initialize the OpenGL context with the specified configuration.
        /// </summary>
        private Boolean TryInitializeGLContext(IntPtr masterptr, OpenGLUltravioletConfiguration configuration)
        {
            if (configuration.Debug)
            {
                SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_FLAGS, (int)SDL_GLcontextFlag.DEBUG);
            }

            if ((this.context = SDL.GL_CreateContext(masterptr)) == IntPtr.Zero)
            {
                if (configuration.Debug)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_FLAGS, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if ((this.context = SDL.GL_CreateContext(masterptr)) != IntPtr.Zero)
                    {
                        return(true);
                    }
                }
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Initializes the context's audio subsystem.
        /// </summary>
        /// <param name="configuration">The Ultraviolet Framework configuration settings for this context.</param>
        /// <returns>The audio subsystem.</returns>
        private IUltravioletAudio InitializeAudioSubsystem(OpenGLUltravioletConfiguration configuration)
        {
            if (String.IsNullOrEmpty(configuration.AudioSubsystemAssembly))
            {
                throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);
            }

            Assembly asm;

            try
            {
                asm = Assembly.Load(configuration.AudioSubsystemAssembly);
                InitializeFactoryMethodsInAssembly(asm);
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException ||
                    e is FileLoadException ||
                    e is BadImageFormatException)
                {
                    throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly, e);
                }
                throw;
            }

            var types = (from t in asm.GetTypes()
                         where
                         t.IsClass && !t.IsAbstract &&
                         t.GetInterfaces().Contains(typeof(IUltravioletAudio))
                         select t).ToList();

            if (!types.Any() || types.Count > 1)
            {
                throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);
            }

            var type = types.Single();

            var ctorWithConfig = type.GetConstructor(new[] { typeof(UltravioletContext), typeof(UltravioletConfiguration) });

            if (ctorWithConfig != null)
            {
                return((IUltravioletAudio)ctorWithConfig.Invoke(new object[] { this, configuration }));
            }

            var ctorWithoutConfig = type.GetConstructor(new[] { typeof(UltravioletContext) });

            if (ctorWithoutConfig != null)
            {
                return((IUltravioletAudio)ctorWithoutConfig.Invoke(new object[] { this }));
            }

            throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Called when the application is creating its Ultraviolet context.
        /// </summary>
        /// <returns>The Ultraviolet context.</returns>
        protected override UltravioletContext OnCreatingUltravioletContext()
        {
            var configuration = new OpenGLUltravioletConfiguration();
            PopulateConfiguration(configuration);

#if DEBUG
            configuration.Debug = true;
            configuration.DebugLevels = DebugLevels.Error | DebugLevels.Warning;
            configuration.DebugCallback = (uv, level, message) =>
            {
                System.Diagnostics.Debug.WriteLine(message);
            };
#endif
            return new OpenGLUltravioletContext(this, configuration);
        }
        /// <inheritdoc/>
        protected override UltravioletContext OnCreatingUltravioletContext()
        {
            var configuration = new OpenGLUltravioletConfiguration() { Headless = headless };
            configuration.IsHardwareInputDisabled = true;
            configuration.Debug = true;
            configuration.DebugLevels = DebugLevels.Error | DebugLevels.Warning;
            configuration.DebugCallback = (uv, level, message) =>
            {
                System.Diagnostics.Debug.WriteLine(message);
            };

            if (!String.IsNullOrEmpty(audioSubsystem))
                configuration.AudioSubsystemAssembly = audioSubsystem;

            if (configureUPF)
                PresentationFoundation.Configure(configuration);
            
            return new OpenGLUltravioletContext(this, configuration);
        }
 protected override UltravioletContext OnCreatingUltravioletContext()
 {
     var configuration = new OpenGLUltravioletConfiguration() { EnableServiceMode = true, LoadCompatibilityShim = false };
     return new OpenGLUltravioletContext(this, configuration);
 }
        /// <summary>
        /// Initializes a new instance of the OpenGLUltravioletContext class.
        /// </summary>
        /// <param name="host">The object that is hosting the Ultraviolet context.</param>
        /// <param name="configuration">The Ultraviolet Framework configuration settings for this context.</param>
        public OpenGLUltravioletContext(IUltravioletHost host, OpenGLUltravioletConfiguration configuration)
            : base(host, configuration)
        {
            Contract.Require(configuration, nameof(configuration));

            this.IsHardwareInputDisabled = configuration.IsHardwareInputDisabled;

            var sdlFlags = configuration.EnableServiceMode ?
                           SDL_Init.TIMER | SDL_Init.EVENTS :
                           SDL_Init.TIMER | SDL_Init.VIDEO | SDL_Init.JOYSTICK | SDL_Init.GAMECONTROLLER | SDL_Init.EVENTS;

            if (SDL.Init(sdlFlags) != 0)
            {
                throw new SDL2Exception();
            }

            var isGLES = (Platform == UltravioletPlatform.Android || Platform == UltravioletPlatform.iOS);

            var versionRequired  = isGLES ? new Version(2, 0) : new Version(3, 1);
            var versionRequested = isGLES ? configuration.MinimumOpenGLESVersion : configuration.MinimumOpenGLVersion;

            if (versionRequested == null || versionRequested < versionRequired)
            {
                if (isGLES)
                {
                    versionRequested = Platform == UltravioletPlatform.Android ?
                                       new Version(2, 0) : new Version(3, 0);
                }
                else
                {
                    versionRequested = versionRequired;
                }
            }

            if (!configuration.EnableServiceMode)
            {
                var profile = isGLES ? SDL_GLprofile.ES : SDL_GLprofile.CORE;

                if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_PROFILE_MASK, (Int32)profile) < 0)
                {
                    throw new SDL2Exception();
                }

                // NOTE: Asking for an ES 3.0 context in the emulator will return a valid
                // context pointer, but actually using it will cause segfaults. It seems like
                // the best thing to do on Android is just not ask for a specific version,
                // and trust the OS to give you the highest version it supports.
                if (Platform != UltravioletPlatform.Android)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, versionRequested.Major) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, versionRequested.Minor) < 0)
                    {
                        throw new SDL2Exception();
                    }
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.DEPTH_SIZE, configuration.BackBufferDepthSize) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.STENCIL_SIZE, configuration.BackBufferStencilSize) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.RETAINED_BACKING, 0) < 0)
                {
                    throw new SDL2Exception();
                }

                if (configuration.Use32BitFramebuffer)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.RED_SIZE, 8) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.GREEN_SIZE, 8) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.BLUE_SIZE, 8) < 0)
                    {
                        throw new SDL2Exception();
                    }
                }
                else
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.RED_SIZE, 5) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.GREEN_SIZE, 6) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.BLUE_SIZE, 5) < 0)
                    {
                        throw new SDL2Exception();
                    }
                }
            }

            this.platform = IsRunningInServiceMode ? (IUltravioletPlatform) new DummyUltravioletPlatform(this) : new OpenGLUltravioletPlatform(this, configuration);

            PumpEvents();

            this.graphics = IsRunningInServiceMode ? (IUltravioletGraphics) new DummyUltravioletGraphics(this) : new OpenGLUltravioletGraphics(this, configuration, versionRequested);
            if (!IsRunningInServiceMode)
            {
                ((OpenGLUltravioletGraphics)graphics).ResetDeviceStates();
            }

            this.audio   = IsRunningInServiceMode ? new DummyUltravioletAudio(this) : InitializeAudioSubsystem(configuration);
            this.input   = IsRunningInServiceMode ? (IUltravioletInput)(new DummyUltravioletInput(this)) : new SDL2UltravioletInput(this);
            this.content = new UltravioletContent(this);
            this.ui      = new UltravioletUI(this, configuration);

            this.content.RegisterImportersAndProcessors(new[]
            {
                typeof(SDL2.Native.SDL).Assembly,
                String.IsNullOrEmpty(configuration.AudioSubsystemAssembly) ? null : Assembly.Load(configuration.AudioSubsystemAssembly),
                String.IsNullOrEmpty(configuration.ViewProviderAssembly) ? null : Assembly.Load(configuration.ViewProviderAssembly)
            });
            this.content.Importers.RegisterImporter <XmlContentImporter>("prog");

            PumpEvents();

            unsafe
            {
                eventFilter    = new SDL.EventFilter(SDLEventFilter);
                eventFilterPtr = Marshal.GetFunctionPointerForDelegate(eventFilter);
                SDL.SetEventFilter(eventFilterPtr, IntPtr.Zero);
            }

            InitializeContext();
            InitializeViewProvider(configuration);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Attempts to initialize the OpenGL context with the specified configuration.
        /// </summary>
        private Boolean TryInitializeGLContext(IntPtr masterptr, OpenGLUltravioletConfiguration configuration)
        {
            if (configuration.Debug)
                SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_FLAGS, (int)SDL_GLcontextFlag.DEBUG);

            if ((this.context = SDL.GL_CreateContext(masterptr)) == IntPtr.Zero)
            {
                if (configuration.Debug)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_FLAGS, 0) < 0)
                        throw new SDL2Exception();

                    if ((this.context = SDL.GL_CreateContext(masterptr)) != IntPtr.Zero)
                        return true;
                }
                return false;
            }
            return true;
        }
Ejemplo n.º 11
0
        public unsafe OpenGLUltravioletGraphics(OpenGLUltravioletContext uv, OpenGLUltravioletConfiguration configuration, Version versionRequested)
            : base(uv)
        {
            var masterptr = ((OpenGLUltravioletWindowInfo)uv.GetPlatform().Windows).GetMasterPointer();
            if (!TryInitializeGLContext(masterptr, configuration))
            {
                var attemptedVersionMajor = 0;
                var attemptedVersionMinor = 0;

                if (SDL.GL_GetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, &attemptedVersionMajor) < 0)
                    throw new SDL2Exception();
                if (SDL.GL_GetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, &attemptedVersionMinor) < 0)
                    throw new SDL2Exception();
                
                var attemptedVersion = new Version(attemptedVersionMajor, attemptedVersionMinor, 0, 0);

                var isGLES = (uv.Platform == UltravioletPlatform.Android || uv.Platform == UltravioletPlatform.iOS);
                if (isGLES && attemptedVersion >= new Version(3, 0) && (configuration.MinimumOpenGLESVersion ?? new Version(2, 0)) <= new Version(2, 0))
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, 2) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, 0) < 0)
                        throw new SDL2Exception();

                    if (!TryInitializeGLContext(masterptr, configuration))
                        throw new SDL2Exception();
                }
                else throw new SDL2Exception();
            }

            if (SDL.GL_SetSwapInterval(1) < 0 && uv.Platform != UltravioletPlatform.iOS)
                throw new SDL2Exception();

            if (gl.Initialized)
            {
                gl.Uninitialize();
            }
            gl.Initialize(new OpenGLInitializer());
            
            if (!gl.IsVersionAtLeast(versionRequested))
                throw new InvalidOperationException(OpenGLStrings.DoesNotMeetMinimumVersionRequirement.Format(gl.MajorVersion, gl.MinorVersion, versionRequested.Major, versionRequested.Minor));
            
            OpenGLState.ResetCache();

            if (!VerifyCapabilities())
                throw new NotSupportedException(OpenGLStrings.UnsupportedGraphicsDevice);

            if (configuration.Debug && configuration.DebugCallback != null)
            {
                InitializeDebugOutput(configuration);
            }
            
            this.capabilities = new OpenGLGraphicsCapabilities();

            this.maxTextureStages = gl.GetInteger(gl.GL_MAX_TEXTURE_IMAGE_UNITS);
            this.textures = new Texture2D[maxTextureStages];
            this.samplerStates = new SamplerState[maxTextureStages];
            this.samplerObjects = capabilities.SupportsIndependentSamplerState ? new OpenGLSamplerObject[maxTextureStages] : null;
            this.backBufferRenderTargetUsage = configuration.BackBufferRenderTargetUsage;

            if (samplerObjects != null)
            {
                for (int i = 0; i < samplerObjects.Length; i++)
                {
                    samplerObjects[i] = new OpenGLSamplerObject(Ultraviolet);
                    samplerObjects[i].Bind((uint)i);
                }
            }            

            OpenGLState.VerifyCache();
        }
        /// <summary>
        /// Initializes a new instance of the OpenGLUltravioletContext class.
        /// </summary>
        /// <param name="host">The object that is hosting the Ultraviolet context.</param>
        /// <param name="configuration">The Ultraviolet Framework configuration settings for this context.</param>
        public OpenGLUltravioletContext(IUltravioletHost host, OpenGLUltravioletConfiguration configuration)
            : base(host, configuration)
        {
            Contract.Require(configuration, nameof(configuration));

            this.IsHardwareInputDisabled = configuration.IsHardwareInputDisabled;

            var sdlFlags = configuration.EnableServiceMode ?
                           SDL_Init.TIMER | SDL_Init.EVENTS :
                           SDL_Init.TIMER | SDL_Init.VIDEO | SDL_Init.JOYSTICK | SDL_Init.GAMECONTROLLER | SDL_Init.EVENTS;

            if (SDL.Init(sdlFlags) != 0)
            {
                throw new SDL2Exception();
            }

            var isGLES = (Platform == UltravioletPlatform.Android);

            var versionRequired  = isGLES ? new Version(2, 0) : new Version(3, 1);
            var versionRequested = isGLES ? configuration.MinimumOpenGLESVersion : configuration.MinimumOpenGLVersion;

            if (versionRequested == null || versionRequested < versionRequired)
            {
                versionRequested = versionRequired;
            }

            if (!configuration.EnableServiceMode)
            {
                var profile = isGLES ? SDL_GLprofile.ES : SDL_GLprofile.CORE;

                if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_PROFILE_MASK, (Int32)profile) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, versionRequested.Major) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, versionRequested.Minor) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.DEPTH_SIZE, configuration.BackBufferDepthSize) < 0)
                {
                    throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.STENCIL_SIZE, configuration.BackBufferStencilSize) < 0)
                {
                    throw new SDL2Exception();
                }
            }

            this.platform = IsRunningInServiceMode ? (IUltravioletPlatform)(new DummyUltravioletPlatform(this)) : new OpenGLUltravioletPlatform(this, configuration);

            PumpEvents();

            this.graphics = IsRunningInServiceMode ? (IUltravioletGraphics)(new DummyUltravioletGraphics(this)) : new OpenGLUltravioletGraphics(this, configuration);
            this.audio    = IsRunningInServiceMode ? new DummyUltravioletAudio(this) : InitializeAudioSubsystem(configuration);
            this.input    = IsRunningInServiceMode ? (IUltravioletInput)(new DummyUltravioletInput(this)) : new SDL2UltravioletInput(this);
            this.content  = new UltravioletContent(this);
            this.ui       = new UltravioletUI(this, configuration);

            this.content.RegisterImportersAndProcessors(new[]
            {
                typeof(SDL2.Native.SDL).Assembly,
                String.IsNullOrEmpty(configuration.AudioSubsystemAssembly) ? null : Assembly.Load(configuration.AudioSubsystemAssembly),
                String.IsNullOrEmpty(configuration.ViewProviderAssembly) ? null : Assembly.Load(configuration.ViewProviderAssembly)
            });
            this.content.Importers.RegisterImporter <XmlContentImporter>("prog");

            PumpEvents();

            InitializeContext();
            InitializeViewProvider(configuration);
        }
Ejemplo n.º 13
0
        public unsafe OpenGLUltravioletGraphics(OpenGLUltravioletContext uv, OpenGLUltravioletConfiguration configuration, Version versionRequested)
            : base(uv)
        {
            var masterptr = ((OpenGLUltravioletWindowInfo)uv.GetPlatform().Windows).GetMasterPointer();

            if (!TryInitializeGLContext(masterptr, configuration))
            {
                var attemptedVersionMajor = 0;
                var attemptedVersionMinor = 0;

                if (SDL.GL_GetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, &attemptedVersionMajor) < 0)
                {
                    throw new SDL2Exception();
                }
                if (SDL.GL_GetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, &attemptedVersionMinor) < 0)
                {
                    throw new SDL2Exception();
                }

                var attemptedVersion = new Version(attemptedVersionMajor, attemptedVersionMinor, 0, 0);

                var isGLES = (uv.Platform == UltravioletPlatform.Android || uv.Platform == UltravioletPlatform.iOS);
                if (isGLES && attemptedVersion >= new Version(3, 0) && (configuration.MinimumOpenGLESVersion ?? new Version(2, 0)) <= new Version(2, 0))
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, 2) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, 0) < 0)
                    {
                        throw new SDL2Exception();
                    }

                    if (!TryInitializeGLContext(masterptr, configuration))
                    {
                        throw new SDL2Exception();
                    }
                }
                else
                {
                    throw new SDL2Exception();
                }
            }

            if (SDL.GL_SetSwapInterval(1) < 0 && uv.Platform != UltravioletPlatform.iOS)
            {
                throw new SDL2Exception();
            }

            if (gl.Initialized)
            {
                gl.Uninitialize();
            }
            gl.Initialize(new OpenGLInitializer());

            if (!gl.IsVersionAtLeast(versionRequested))
            {
                throw new InvalidOperationException(OpenGLStrings.DoesNotMeetMinimumVersionRequirement.Format(gl.MajorVersion, gl.MinorVersion, versionRequested.Major, versionRequested.Minor));
            }

            OpenGLState.ResetCache();

            if (!VerifyCapabilities())
            {
                throw new NotSupportedException(OpenGLStrings.UnsupportedGraphicsDevice);
            }

            if (configuration.Debug && configuration.DebugCallback != null)
            {
                InitializeDebugOutput(configuration);
            }

            this.capabilities = new OpenGLGraphicsCapabilities(configuration);

            this.maxTextureStages            = gl.GetInteger(gl.GL_MAX_TEXTURE_IMAGE_UNITS);
            this.textures                    = new Texture2D[maxTextureStages];
            this.samplerStates               = new SamplerState[maxTextureStages];
            this.samplerObjects              = capabilities.SupportsIndependentSamplerState ? new OpenGLSamplerObject[maxTextureStages] : null;
            this.backBufferRenderTargetUsage = configuration.BackBufferRenderTargetUsage;

            if (samplerObjects != null)
            {
                for (int i = 0; i < samplerObjects.Length; i++)
                {
                    samplerObjects[i] = new OpenGLSamplerObject(Ultraviolet);
                    samplerObjects[i].Bind((uint)i);
                }
            }

            OpenGLState.VerifyCache();
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initializes a new instance of the OpenGLUltravioletContext class.
        /// </summary>
        /// <param name="host">The object that is hosting the Ultraviolet context.</param>
        /// <param name="configuration">The Ultraviolet Framework configuration settings for this context.</param>
        public OpenGLUltravioletContext(IUltravioletHost host, OpenGLUltravioletConfiguration configuration)
            : base(host, configuration)
        {
            Contract.Require(configuration, nameof(configuration));

            this.IsHardwareInputDisabled = configuration.IsHardwareInputDisabled;

            var sdlFlags = configuration.EnableServiceMode ?
                SDL_Init.TIMER | SDL_Init.EVENTS :
                SDL_Init.TIMER | SDL_Init.VIDEO | SDL_Init.JOYSTICK | SDL_Init.GAMECONTROLLER | SDL_Init.EVENTS;

            if (SDL.Init(sdlFlags) != 0)
                throw new SDL2Exception();

            var isGLES = (Platform == UltravioletPlatform.Android || Platform == UltravioletPlatform.iOS);

            var versionRequired = isGLES ? new Version(2, 0) : new Version(3, 1);
            var versionRequested = isGLES ? configuration.MinimumOpenGLESVersion : configuration.MinimumOpenGLVersion;
            if (versionRequested == null || versionRequested < versionRequired)
            {
                if (isGLES)
                {
                    versionRequested = Platform == UltravioletPlatform.Android ?
                        new Version(2, 0) : new Version(3, 0);
                }
                else
                {
                    versionRequested = versionRequired;
                }
            }

            if (!configuration.EnableServiceMode)
            {
                var profile = isGLES ? SDL_GLprofile.ES : SDL_GLprofile.CORE;

                if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_PROFILE_MASK, (Int32)profile) < 0)
                    throw new SDL2Exception();

                // NOTE: Asking for an ES 3.0 context in the emulator will return a valid
                // context pointer, but actually using it will cause segfaults. It seems like
                // the best thing to do on Android is just not ask for a specific version,
                // and trust the OS to give you the highest version it supports.
                if (Platform != UltravioletPlatform.Android)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MAJOR_VERSION, versionRequested.Major) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.CONTEXT_MINOR_VERSION, versionRequested.Minor) < 0)
                        throw new SDL2Exception();
                }

                if (SDL.GL_SetAttribute(SDL_GLattr.DEPTH_SIZE, configuration.BackBufferDepthSize) < 0)
                    throw new SDL2Exception();

                if (SDL.GL_SetAttribute(SDL_GLattr.STENCIL_SIZE, configuration.BackBufferStencilSize) < 0)
                    throw new SDL2Exception();

                if (SDL.GL_SetAttribute(SDL_GLattr.RETAINED_BACKING, 0) < 0)
                    throw new SDL2Exception();

                if (configuration.Use32BitFramebuffer)
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.RED_SIZE, 8) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.GREEN_SIZE, 8) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.BLUE_SIZE, 8) < 0)
                        throw new SDL2Exception();
                }
                else
                {
                    if (SDL.GL_SetAttribute(SDL_GLattr.RED_SIZE, 5) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.GREEN_SIZE, 6) < 0)
                        throw new SDL2Exception();

                    if (SDL.GL_SetAttribute(SDL_GLattr.BLUE_SIZE, 5) < 0)
                        throw new SDL2Exception();
                }
            }

            this.platform = IsRunningInServiceMode ? (IUltravioletPlatform)new DummyUltravioletPlatform(this) : new OpenGLUltravioletPlatform(this, configuration);

            PumpEvents();

            this.graphics = IsRunningInServiceMode ? (IUltravioletGraphics)new DummyUltravioletGraphics(this) : new OpenGLUltravioletGraphics(this, configuration, versionRequested);
            if (!IsRunningInServiceMode)
                ((OpenGLUltravioletGraphics)graphics).ResetDeviceStates();

            this.audio = IsRunningInServiceMode ? new DummyUltravioletAudio(this) : InitializeAudioSubsystem(configuration);
            this.input = IsRunningInServiceMode ? (IUltravioletInput)(new DummyUltravioletInput(this)) : new SDL2UltravioletInput(this);
            this.content = new UltravioletContent(this);
            this.ui = new UltravioletUI(this, configuration);

            this.content.RegisterImportersAndProcessors(new[]
            {
                typeof(SDL2.Native.SDL).Assembly,
                String.IsNullOrEmpty(configuration.AudioSubsystemAssembly) ? null : Assembly.Load(configuration.AudioSubsystemAssembly),
                String.IsNullOrEmpty(configuration.ViewProviderAssembly) ? null : Assembly.Load(configuration.ViewProviderAssembly)
            });
            this.content.Importers.RegisterImporter<XmlContentImporter>("prog");

            PumpEvents();

            unsafe
            {
                eventFilter = new SDL.EventFilter(SDLEventFilter);
                eventFilterPtr = Marshal.GetFunctionPointerForDelegate(eventFilter);
                SDL.SetEventFilter(eventFilterPtr, IntPtr.Zero);
            }

            InitializeContext();
            InitializeViewProvider(configuration);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Initializes the context's audio subsystem.
        /// </summary>
        /// <param name="configuration">The Ultraviolet Framework configuration settings for this context.</param>
        /// <returns>The audio subsystem.</returns>
        private IUltravioletAudio InitializeAudioSubsystem(OpenGLUltravioletConfiguration configuration)
        {
            if (String.IsNullOrEmpty(configuration.AudioSubsystemAssembly))
                throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);

            Assembly asm;
            try
            {
                asm = Assembly.Load(configuration.AudioSubsystemAssembly);
                InitializeFactoryMethodsInAssembly(asm);
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException ||
                    e is FileLoadException ||
                    e is BadImageFormatException)
                {
                    throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly, e);
                }
                throw;
            }

            var types = (from t in asm.GetTypes()
                         where
                          t.IsClass && !t.IsAbstract &&
                          t.GetInterfaces().Contains(typeof(IUltravioletAudio))
                         select t).ToList();

            if (!types.Any() || types.Count > 1)
                throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);

            var type = types.Single();

            var ctorWithConfig = type.GetConstructor(new[] { typeof(UltravioletContext), typeof(UltravioletConfiguration) });
            if (ctorWithConfig != null)
            {
                return (IUltravioletAudio)ctorWithConfig.Invoke(new object[] { this, configuration });
            }

            var ctorWithoutConfig = type.GetConstructor(new[] { typeof(UltravioletContext) });
            if (ctorWithoutConfig != null)
            {
                return (IUltravioletAudio)ctorWithoutConfig.Invoke(new object[] { this });
            }

            throw new InvalidOperationException(OpenGLStrings.InvalidAudioAssembly);
        }