Exemplo n.º 1
0
        public Vector3 GetScreenScale(IUltravioletPlatform gfx, float width, float height)
        {
            if (gfx.Windows.GetPrimary() == null)
            {
                return(oldScreenScale);
            }
            var scaleX = gfx.Windows.GetPrimary().DrawableSize.Width / width;
            var scaleY = gfx.Windows.GetPrimary().DrawableSize.Height / height;

            oldScreenScale = new Vector3(scaleX, scaleY, 1.0f);
            return(oldScreenScale);
        }
        /// <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);
        }